Branch data Line data Source code
1 : : /* C++ modules. Experimental!
2 : : Copyright (C) 2017-2025 Free Software Foundation, Inc.
3 : : Written by Nathan Sidwell <nathan@acm.org> while at FaceBook
4 : :
5 : : This file is part of GCC.
6 : :
7 : : GCC is free software; you can redistribute it and/or modify it
8 : : under the terms of the GNU General Public License as published by
9 : : the Free Software Foundation; either version 3, or (at your option)
10 : : any later version.
11 : :
12 : : GCC is distributed in the hope that it will be useful, but
13 : : WITHOUT ANY WARRANTY; without even the implied warranty of
14 : : MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 : : General Public License for more details.
16 : :
17 : : You should have received a copy of the GNU General Public License
18 : : along with GCC; see the file COPYING3. If not see
19 : : <http://www.gnu.org/licenses/>. */
20 : :
21 : : /* Comments in this file have a non-negligible chance of being wrong
22 : : or at least inaccurate. Due to (a) my misunderstanding, (b)
23 : : ambiguities that I have interpretted differently to original intent
24 : : (c) changes in the specification, (d) my poor wording, (e) source
25 : : changes. */
26 : :
27 : : /* (Incomplete) Design Notes
28 : :
29 : : A hash table contains all module names. Imported modules are
30 : : present in a modules array, which by construction places an
31 : : import's dependencies before the import itself. The single
32 : : exception is the current TU, which always occupies slot zero (even
33 : : when it is not a module).
34 : :
35 : : Imported decls occupy an entity_ary, an array of binding_slots, indexed
36 : : by importing module and index within that module. A flat index is
37 : : used, as each module reserves a contiguous range of indices.
38 : : Initially each slot indicates the CMI section containing the
39 : : streamed decl. When the decl is imported it will point to the decl
40 : : itself.
41 : :
42 : : Additionally each imported decl is mapped in the entity_map via its
43 : : DECL_UID to the flat index in the entity_ary. Thus we can locate
44 : : the index for any imported decl by using this map and then
45 : : de-flattening the index via a binary seach of the module vector.
46 : : Cross-module references are by (remapped) module number and
47 : : module-local index.
48 : :
49 : : Each importable DECL contains several flags. The simple set are
50 : : DECL_MODULE_EXPORT_P, DECL_MODULE_PURVIEW_P, DECL_MODULE_ATTACH_P
51 : : and DECL_MODULE_IMPORT_P. The first indicates whether it is
52 : : exported, the second whether it is in module or header-unit
53 : : purview. The third indicates it is attached to the named module in
54 : : whose purview it resides and the fourth indicates whether it was an
55 : : import into this TU or not. DECL_MODULE_ATTACH_P will be false for
56 : : all decls in a header-unit, and for those in a named module inside
57 : : a linkage declaration.
58 : :
59 : : The more detailed flags are DECL_MODULE_PARTITION_P,
60 : : DECL_MODULE_ENTITY_P. The first is set in a primary interface unit
61 : : on decls that were read from module partitions (these will have
62 : : DECL_MODULE_IMPORT_P set too). Such decls will be streamed out to
63 : : the primary's CMI. DECL_MODULE_ENTITY_P is set when an entity is
64 : : imported, even if it matched a non-imported entity. Such a decl
65 : : will not have DECL_MODULE_IMPORT_P set, even though it has an entry
66 : : in the entity map and array.
67 : :
68 : : Header units are module-like.
69 : :
70 : : For namespace-scope lookup, the decls for a particular module are
71 : : held located in a sparse array hanging off the binding of the name.
72 : : This is partitioned into two: a few fixed slots at the start
73 : : followed by the sparse slots afterwards. By construction we only
74 : : need to append new slots to the end -- there is never a need to
75 : : insert in the middle. The fixed slots are MODULE_SLOT_CURRENT for
76 : : the current TU (regardless of whether it is a module or not),
77 : : MODULE_SLOT_GLOBAL and MODULE_SLOT_PARTITION. These latter two
78 : : slots are used for merging entities across the global module and
79 : : module partitions respectively. MODULE_SLOT_PARTITION is only
80 : : present in a module. Neither of those two slots is searched during
81 : : name lookup -- they are internal use only. This vector is created
82 : : lazily once we require it, if there is only a declaration from the
83 : : current TU, a regular binding is present. It is converted on
84 : : demand.
85 : :
86 : : OPTIMIZATION: Outside of the current TU, we only need ADL to work.
87 : : We could optimize regular lookup for the current TU by glomming all
88 : : the visible decls on its slot. Perhaps wait until design is a
89 : : little more settled though.
90 : :
91 : : There is only one instance of each extern-linkage namespace. It
92 : : appears in every module slot that makes it visible. It also
93 : : appears in MODULE_SLOT_GLOBAL. (It is an ODR violation if they
94 : : collide with some other global module entity.) We also have an
95 : : optimization that shares the slot for adjacent modules that declare
96 : : the same such namespace.
97 : :
98 : : A module interface compilation produces a Compiled Module Interface
99 : : (CMI). The format used is Encapsulated Lazy Records Of Numbered
100 : : Declarations, which is essentially ELF's section encapsulation. (As
101 : : all good nerds are aware, Elrond is half Elf.) Some sections are
102 : : named, and contain information about the module as a whole (indices
103 : : etc), and other sections are referenced by number. Although I
104 : : don't defend against actively hostile CMIs, there is some
105 : : checksumming involved to verify data integrity. When dumping out
106 : : an interface, we generate a graph of all the
107 : : independently-redeclarable DECLS that are needed, and the decls
108 : : they reference. From that we determine the strongly connected
109 : : components (SCC) within this TU. Each SCC is dumped to a separate
110 : : numbered section of the CMI. We generate a binding table section,
111 : : mapping each namespace&name to a defining section. This allows
112 : : lazy loading.
113 : :
114 : : Lazy loading employs mmap to map a read-only image of the CMI.
115 : : It thus only occupies address space and is paged in on demand,
116 : : backed by the CMI file itself. If mmap is unavailable, regular
117 : : FILEIO is used. Also, there's a bespoke ELF reader/writer here,
118 : : which implements just the section table and sections (including
119 : : string sections) of a 32-bit ELF in host byte-order. You can of
120 : : course inspect it with readelf. I figured 32-bit is sufficient,
121 : : for a single module. I detect running out of section numbers, but
122 : : do not implement the ELF overflow mechanism. At least you'll get
123 : : an error if that happens.
124 : :
125 : : We do not separate declarations and definitions. My guess is that
126 : : if you refer to the declaration, you'll also need the definition
127 : : (template body, inline function, class definition etc). But this
128 : : does mean we can get larger SCCs than if we separated them. It is
129 : : unclear whether this is a win or not.
130 : :
131 : : Notice that we embed section indices into the contents of other
132 : : sections. Thus random manipulation of the CMI file by ELF tools
133 : : may well break it. The kosher way would probably be to introduce
134 : : indirection via section symbols, but that would require defining a
135 : : relocation type.
136 : :
137 : : Notice that lazy loading of one module's decls can cause lazy
138 : : loading of other decls in the same or another module. Clearly we
139 : : want to avoid loops. In a correct program there can be no loops in
140 : : the module dependency graph, and the above-mentioned SCC algorithm
141 : : places all intra-module circular dependencies in the same SCC. It
142 : : also orders the SCCs wrt each other, so dependent SCCs come first.
143 : : As we load dependent modules first, we know there can be no
144 : : reference to a higher-numbered module, and because we write out
145 : : dependent SCCs first, likewise for SCCs within the module. This
146 : : allows us to immediately detect broken references. When loading,
147 : : we must ensure the rest of the compiler doesn't cause some
148 : : unconnected load to occur (for instance, instantiate a template).
149 : :
150 : : Classes used:
151 : :
152 : : dumper - logger
153 : :
154 : : data - buffer
155 : :
156 : : bytes_in : data - scalar reader
157 : : bytes_out : data - scalar writer
158 : :
159 : : bytes_in::bits_in - bit stream reader
160 : : bytes_out::bits_out - bit stream writer
161 : :
162 : : elf - ELROND format
163 : : elf_in : elf - ELROND reader
164 : : elf_out : elf - ELROND writer
165 : :
166 : : trees_in : bytes_in - tree reader
167 : : trees_out : bytes_out - tree writer
168 : :
169 : : depset - dependency set
170 : : depset::hash - hash table of depsets
171 : : depset::tarjan - SCC determinator
172 : :
173 : : uidset<T> - set T's related to a UID
174 : : uidset<T>::hash hash table of uidset<T>
175 : :
176 : : loc_spans - location map data
177 : :
178 : : module_state - module object
179 : :
180 : : slurping - data needed during loading
181 : :
182 : : macro_import - imported macro data
183 : : macro_export - exported macro data
184 : :
185 : : The ELROND objects use mmap, for both reading and writing. If mmap
186 : : is unavailable, fileno IO is used to read and write blocks of data.
187 : :
188 : : The mapper object uses fileno IO to communicate with the server or
189 : : program. */
190 : :
191 : : /* In expermental (trunk) sources, MODULE_VERSION is a #define passed
192 : : in from the Makefile. It records the modification date of the
193 : : source directory -- that's the only way to stay sane. In release
194 : : sources, we (plan to) use the compiler's major.minor versioning.
195 : : While the format might not change between at minor versions, it
196 : : seems simplest to tie the two together. There's no concept of
197 : : inter-version compatibility. */
198 : : #define IS_EXPERIMENTAL(V) ((V) >= (1U << 20))
199 : : #define MODULE_MAJOR(V) ((V) / 10000)
200 : : #define MODULE_MINOR(V) ((V) % 10000)
201 : : #define EXPERIMENT(A,B) (IS_EXPERIMENTAL (MODULE_VERSION) ? (A) : (B))
202 : : #ifndef MODULE_VERSION
203 : : #include "bversion.h"
204 : : #define MODULE_VERSION (BUILDING_GCC_MAJOR * 10000U + BUILDING_GCC_MINOR)
205 : : #elif !IS_EXPERIMENTAL (MODULE_VERSION)
206 : : #error "This is not the version I was looking for."
207 : : #endif
208 : :
209 : : #define _DEFAULT_SOURCE 1 /* To get TZ field of struct tm, if available. */
210 : : #include "config.h"
211 : : #define INCLUDE_STRING
212 : : #define INCLUDE_VECTOR
213 : : #include "system.h"
214 : : #include "coretypes.h"
215 : : #include "cp-tree.h"
216 : : #include "timevar.h"
217 : : #include "stringpool.h"
218 : : #include "dumpfile.h"
219 : : #include "bitmap.h"
220 : : #include "cgraph.h"
221 : : #include "varasm.h"
222 : : #include "tree-iterator.h"
223 : : #include "cpplib.h"
224 : : #include "mkdeps.h"
225 : : #include "incpath.h"
226 : : #include "libiberty.h"
227 : : #include "stor-layout.h"
228 : : #include "version.h"
229 : : #include "tree-diagnostic.h"
230 : : #include "toplev.h"
231 : : #include "opts.h"
232 : : #include "attribs.h"
233 : : #include "intl.h"
234 : : #include "langhooks.h"
235 : : /* This TU doesn't need or want to see the networking. */
236 : : #define CODY_NETWORKING 0
237 : : #include "mapper-client.h"
238 : : #include <zlib.h> // for crc32, crc32_combine
239 : :
240 : : #if 0 // 1 for testing no mmap
241 : : #define MAPPED_READING 0
242 : : #define MAPPED_WRITING 0
243 : : #else
244 : : #if HAVE_MMAP_FILE && HAVE_MUNMAP && HAVE_MSYNC
245 : : /* mmap, munmap, msync. */
246 : : #define MAPPED_READING 1
247 : : #if HAVE_SYSCONF && defined (_SC_PAGE_SIZE)
248 : : /* sysconf (_SC_PAGE_SIZE), ftruncate */
249 : : /* posix_fallocate used if available. */
250 : : #define MAPPED_WRITING 1
251 : : #else
252 : : #define MAPPED_WRITING 0
253 : : #endif
254 : : #else
255 : : #define MAPPED_READING 0
256 : : #define MAPPED_WRITING 0
257 : : #endif
258 : : #endif
259 : :
260 : : /* Some open(2) flag differences, what a colourful world it is! */
261 : : #if defined (O_CLOEXEC)
262 : : // OK
263 : : #elif defined (_O_NOINHERIT)
264 : : /* Windows' _O_NOINHERIT matches O_CLOEXEC flag */
265 : : #define O_CLOEXEC _O_NOINHERIT
266 : : #else
267 : : #define O_CLOEXEC 0
268 : : #endif
269 : : #if defined (O_BINARY)
270 : : // Ok?
271 : : #elif defined (_O_BINARY)
272 : : /* Windows' open(2) call defaults to text! */
273 : : #define O_BINARY _O_BINARY
274 : : #else
275 : : #define O_BINARY 0
276 : : #endif
277 : :
278 : 213249 : static inline cpp_hashnode *cpp_node (tree id)
279 : : {
280 : 213249 : return CPP_HASHNODE (GCC_IDENT_TO_HT_IDENT (id));
281 : : }
282 : :
283 : 132284 : static inline tree identifier (const cpp_hashnode *node)
284 : : {
285 : : /* HT_NODE() expands to node->ident that HT_IDENT_TO_GCC_IDENT()
286 : : then subtracts a nonzero constant, deriving a pointer to
287 : : a different member than ident. That's strictly undefined
288 : : and detected by -Warray-bounds. Suppress it. See PR 101372. */
289 : 132284 : #pragma GCC diagnostic push
290 : 132284 : #pragma GCC diagnostic ignored "-Warray-bounds"
291 : 132284 : return HT_IDENT_TO_GCC_IDENT (HT_NODE (const_cast<cpp_hashnode *> (node)));
292 : 132284 : #pragma GCC diagnostic pop
293 : : }
294 : :
295 : : /* Id for dumping module information. */
296 : : int module_dump_id;
297 : :
298 : : /* We have a special module owner. */
299 : : #define MODULE_UNKNOWN (~0U) /* Not yet known. */
300 : :
301 : : /* Prefix for section names. */
302 : : #define MOD_SNAME_PFX ".gnu.c++"
303 : :
304 : : /* Format a version for user consumption. */
305 : :
306 : : typedef char verstr_t[32];
307 : : static void
308 : 3618 : version2string (unsigned version, verstr_t &out)
309 : : {
310 : 3618 : unsigned major = MODULE_MAJOR (version);
311 : 3618 : unsigned minor = MODULE_MINOR (version);
312 : :
313 : 3618 : if (IS_EXPERIMENTAL (version))
314 : 3618 : sprintf (out, "%04u/%02u/%02u-%02u:%02u%s",
315 : 3618 : 2000 + major / 10000, (major / 100) % 100, (major % 100),
316 : : minor / 100, minor % 100,
317 : : EXPERIMENT ("", " (experimental)"));
318 : : else
319 : 0 : sprintf (out, "%u.%u", major, minor);
320 : 3618 : }
321 : :
322 : : /* Include files to note translation for. */
323 : : static vec<const char *, va_heap, vl_embed> *note_includes;
324 : :
325 : : /* Modules to note CMI pathames. */
326 : : static vec<const char *, va_heap, vl_embed> *note_cmis;
327 : :
328 : : /* Traits to hash an arbitrary pointer. Entries are not deletable,
329 : : and removal is a noop (removal needed upon destruction). */
330 : : template <typename T>
331 : : struct nodel_ptr_hash : pointer_hash<T>, typed_noop_remove <T *> {
332 : : /* Nothing is deletable. Everything is insertable. */
333 : : static bool is_deleted (T *) { return false; }
334 : : static void mark_deleted (T *) { gcc_unreachable (); }
335 : : };
336 : :
337 : : /* Map from pointer to signed integer. */
338 : : typedef simple_hashmap_traits<nodel_ptr_hash<void>, int> ptr_int_traits;
339 : : typedef hash_map<void *,signed,ptr_int_traits> ptr_int_hash_map;
340 : :
341 : : /********************************************************************/
342 : : /* Basic streaming & ELF. Serialization is usually via mmap. For
343 : : writing we slide a buffer over the output file, syncing it
344 : : approproiately. For reading we simply map the whole file (as a
345 : : file-backed read-only map -- it's just address space, leaving the
346 : : OS pager to deal with getting the data to us). Some buffers need
347 : : to be more conventional malloc'd contents. */
348 : :
349 : : /* Variable length buffer. */
350 : :
351 : : namespace {
352 : :
353 : : constexpr line_map_uint_t loc_one = 1;
354 : :
355 : : class data {
356 : : public:
357 : 2518 : class allocator {
358 : : public:
359 : : /* Tools tend to moan if the dtor's not virtual. */
360 : 97637 : virtual ~allocator () {}
361 : :
362 : : public:
363 : : void grow (data &obj, unsigned needed, bool exact);
364 : : void shrink (data &obj);
365 : :
366 : : public:
367 : : virtual char *grow (char *ptr, unsigned needed);
368 : : virtual void shrink (char *ptr);
369 : : };
370 : :
371 : : public:
372 : : char *buffer; /* Buffer being transferred. */
373 : : /* Although size_t would be the usual size, we know we never get
374 : : more than 4GB of buffer -- because that's the limit of the
375 : : encapsulation format. And if you need bigger imports, you're
376 : : doing it wrong. */
377 : : unsigned size; /* Allocated size of buffer. */
378 : : unsigned pos; /* Position in buffer. */
379 : :
380 : : public:
381 : 662098 : data ()
382 : 662098 : :buffer (NULL), size (0), pos (0)
383 : : {
384 : : }
385 : 674644 : ~data ()
386 : : {
387 : : /* Make sure the derived and/or using class know what they're
388 : : doing. */
389 : 674644 : gcc_checking_assert (!buffer);
390 : 674644 : }
391 : :
392 : : protected:
393 : 424891268 : char *use (unsigned count)
394 : : {
395 : 424891268 : if (size < pos + count)
396 : : return NULL;
397 : 424891268 : char *res = &buffer[pos];
398 : 424891268 : pos += count;
399 : 227973312 : return res;
400 : : }
401 : :
402 : : unsigned calc_crc (unsigned) const;
403 : :
404 : : public:
405 : 32243183 : void unuse (unsigned count)
406 : : {
407 : 32243183 : pos -= count;
408 : 24365 : }
409 : :
410 : : public:
411 : : static allocator simple_memory;
412 : : };
413 : : } // anon namespace
414 : :
415 : : /* The simple data allocator. */
416 : : data::allocator data::simple_memory;
417 : :
418 : : /* Grow buffer to at least size NEEDED. */
419 : :
420 : : void
421 : 580389 : data::allocator::grow (data &obj, unsigned needed, bool exact)
422 : : {
423 : 580389 : gcc_checking_assert (needed ? needed > obj.size : !obj.size);
424 : 580389 : if (!needed)
425 : : /* Pick a default size. */
426 : 246870 : needed = EXPERIMENT (100, 1000);
427 : :
428 : 580389 : if (!exact)
429 : 573150 : needed *= 2;
430 : 580389 : obj.buffer = grow (obj.buffer, needed);
431 : 580389 : if (obj.buffer)
432 : 580389 : obj.size = needed;
433 : : else
434 : 0 : obj.pos = obj.size = 0;
435 : 580389 : }
436 : :
437 : : /* Free a buffer. */
438 : :
439 : : void
440 : 259472 : data::allocator::shrink (data &obj)
441 : : {
442 : 0 : shrink (obj.buffer);
443 : 259472 : obj.buffer = NULL;
444 : 259472 : obj.size = 0;
445 : 0 : }
446 : :
447 : : char *
448 : 9978 : data::allocator::grow (char *ptr, unsigned needed)
449 : : {
450 : 9978 : return XRESIZEVAR (char, ptr, needed);
451 : : }
452 : :
453 : : void
454 : 12590 : data::allocator::shrink (char *ptr)
455 : : {
456 : 12590 : XDELETEVEC (ptr);
457 : 12590 : }
458 : :
459 : : /* Calculate the crc32 of the buffer. Note the CRC is stored in the
460 : : first 4 bytes, so don't include them. */
461 : :
462 : : unsigned
463 : 422053 : data::calc_crc (unsigned l) const
464 : : {
465 : 422053 : return crc32 (0, (unsigned char *)buffer + 4, l - 4);
466 : : }
467 : :
468 : : class elf_in;
469 : :
470 : : /* Byte stream reader. */
471 : :
472 : : namespace {
473 : : class bytes_in : public data {
474 : : typedef data parent;
475 : :
476 : : protected:
477 : : bool overrun; /* Sticky read-too-much flag. */
478 : :
479 : : public:
480 : 183952 : bytes_in ()
481 : 183952 : : parent (), overrun (false)
482 : : {
483 : : }
484 : 186430 : ~bytes_in ()
485 : : {
486 : 13722 : }
487 : :
488 : : public:
489 : : /* Begin reading a named section. */
490 : : bool begin (location_t loc, elf_in *src, const char *name);
491 : : /* Begin reading a numbered section with optional name. */
492 : : bool begin (location_t loc, elf_in *src, unsigned, const char * = NULL);
493 : : /* Complete reading a buffer. Propagate errors and return true on
494 : : success. */
495 : : bool end (elf_in *src);
496 : : /* Return true if there is unread data. */
497 : 1286202 : bool more_p () const
498 : : {
499 : 1286202 : return pos != size;
500 : : }
501 : :
502 : : public:
503 : : /* Start reading at OFFSET. */
504 : 322 : void random_access (unsigned offset)
505 : : {
506 : 322 : if (offset > size)
507 : 0 : set_overrun ();
508 : 322 : pos = offset;
509 : : }
510 : :
511 : : public:
512 : 1188320 : void align (unsigned boundary)
513 : : {
514 : 1188320 : if (unsigned pad = pos & (boundary - 1))
515 : 2301359 : read (boundary - pad);
516 : : }
517 : :
518 : : public:
519 : 196917956 : const char *read (unsigned count)
520 : : {
521 : 1113039 : char *ptr = use (count);
522 : 196917956 : if (!ptr)
523 : 0 : set_overrun ();
524 : 160606081 : return ptr;
525 : : }
526 : :
527 : : public:
528 : : bool check_crc () const;
529 : : /* We store the CRC in the first 4 bytes, using host endianness. */
530 : 184994 : unsigned get_crc () const
531 : : {
532 : 184994 : return *(const unsigned *)&buffer[0];
533 : : }
534 : :
535 : : public:
536 : : /* Manipulate the overrun flag. */
537 : 115308505 : bool get_overrun () const
538 : : {
539 : 115308505 : return overrun;
540 : : }
541 : 26 : void set_overrun ()
542 : : {
543 : 26 : overrun = true;
544 : 0 : }
545 : :
546 : : public:
547 : : unsigned u32 (); /* Read uncompressed integer. */
548 : :
549 : : public:
550 : : int c () ATTRIBUTE_UNUSED; /* Read a char. */
551 : : int i (); /* Read a signed int. */
552 : : unsigned u (); /* Read an unsigned int. */
553 : : size_t z (); /* Read a size_t. */
554 : : location_t loc (); /* Read a location_t. */
555 : : HOST_WIDE_INT wi (); /* Read a HOST_WIDE_INT. */
556 : : unsigned HOST_WIDE_INT wu (); /* Read an unsigned HOST_WIDE_INT. */
557 : : const char *str (size_t * = NULL); /* Read a string. */
558 : : const void *buf (size_t); /* Read a fixed-length buffer. */
559 : : cpp_hashnode *cpp_node (); /* Read a cpp node. */
560 : :
561 : : struct bits_in;
562 : : bits_in stream_bits ();
563 : : };
564 : : } // anon namespace
565 : :
566 : : /* Verify the buffer's CRC is correct. */
567 : :
568 : : bool
569 : 182382 : bytes_in::check_crc () const
570 : : {
571 : 182382 : if (size < 4)
572 : : return false;
573 : :
574 : 182382 : unsigned c_crc = calc_crc (size);
575 : 182382 : if (c_crc != get_crc ())
576 : : return false;
577 : :
578 : : return true;
579 : : }
580 : :
581 : : class elf_out;
582 : :
583 : : /* Byte stream writer. */
584 : :
585 : : namespace {
586 : : class bytes_out : public data {
587 : : typedef data parent;
588 : :
589 : : public:
590 : : allocator *memory; /* Obtainer of memory. */
591 : :
592 : : public:
593 : 472998 : bytes_out (allocator *memory)
594 : 472998 : : parent (), memory (memory)
595 : : {
596 : : }
597 : 472998 : ~bytes_out ()
598 : : {
599 : 498509 : }
600 : :
601 : : public:
602 : 548175612 : bool streaming_p () const
603 : : {
604 : 548175612 : return memory != NULL;
605 : : }
606 : :
607 : : public:
608 : : void set_crc (unsigned *crc_ptr);
609 : :
610 : : public:
611 : : /* Begin writing, maybe reserve space for CRC. */
612 : : void begin (bool need_crc = true);
613 : : /* Finish writing. Spill to section by number. */
614 : : unsigned end (elf_out *, unsigned, unsigned *crc_ptr = NULL);
615 : :
616 : : public:
617 : 1495122 : void align (unsigned boundary)
618 : : {
619 : 1495122 : if (unsigned pad = pos & (boundary - 1))
620 : 1398763 : write (boundary - pad);
621 : 1495122 : }
622 : :
623 : : public:
624 : 227973312 : char *write (unsigned count, bool exact = false)
625 : : {
626 : 227973312 : if (size < pos + count)
627 : 321120 : memory->grow (*this, pos + count, exact);
628 : 227973312 : return use (count);
629 : : }
630 : :
631 : : public:
632 : : void u32 (unsigned); /* Write uncompressed integer. */
633 : :
634 : : public:
635 : : void c (unsigned char) ATTRIBUTE_UNUSED; /* Write unsigned char. */
636 : : void i (int); /* Write signed int. */
637 : : void u (unsigned); /* Write unsigned int. */
638 : : void z (size_t s); /* Write size_t. */
639 : : void loc (location_t); /* Write location_t. */
640 : : void wi (HOST_WIDE_INT); /* Write HOST_WIDE_INT. */
641 : : void wu (unsigned HOST_WIDE_INT); /* Write unsigned HOST_WIDE_INT. */
642 : 15334 : void str (const char *ptr)
643 : : {
644 : 15334 : str (ptr, strlen (ptr));
645 : 15334 : }
646 : 234561 : void cpp_node (const cpp_hashnode *node)
647 : : {
648 : 234561 : str ((const char *)NODE_NAME (node), NODE_LEN (node));
649 : 11699 : }
650 : : void str (const char *, size_t); /* Write string of known length. */
651 : : void buf (const void *, size_t); /* Write fixed length buffer. */
652 : : void *buf (size_t); /* Create a writable buffer */
653 : :
654 : : struct bits_out;
655 : : bits_out stream_bits ();
656 : :
657 : : public:
658 : : /* Format a NUL-terminated raw string. */
659 : : void printf (const char *, ...) ATTRIBUTE_PRINTF_2;
660 : : void print_time (const char *, const tm *, const char *);
661 : :
662 : : public:
663 : : /* Dump instrumentation. */
664 : : static void instrument ();
665 : :
666 : : protected:
667 : : /* Instrumentation. */
668 : : static unsigned spans[4];
669 : : static unsigned lengths[4];
670 : : };
671 : : } // anon namespace
672 : :
673 : : /* Finish bit packet. Rewind the bytes not used. */
674 : :
675 : : static unsigned
676 : 32194755 : bit_flush (data& bits, uint32_t& bit_val, unsigned& bit_pos)
677 : : {
678 : 32194755 : gcc_assert (bit_pos);
679 : 32194755 : unsigned bytes = (bit_pos + 7) / 8;
680 : 32194755 : bits.unuse (4 - bytes);
681 : 32194755 : bit_pos = 0;
682 : 32194755 : bit_val = 0;
683 : 32194755 : return bytes;
684 : : }
685 : :
686 : : /* Bit stream reader (RAII-enabled). Bools are packed into bytes. You
687 : : cannot mix bools and non-bools. Use bflush to flush the current stream
688 : : of bools on demand. Upon destruction bflush is called.
689 : :
690 : : When reading, we don't know how many bools we'll read in. So read
691 : : 4 bytes-worth, and then rewind when flushing if we didn't need them
692 : : all. You can't have a block of bools closer than 4 bytes to the
693 : : end of the buffer.
694 : :
695 : : Both bits_in and bits_out maintain the necessary state for bit packing,
696 : : and since these objects are locally constructed the compiler can more
697 : : easily track their state across consecutive reads/writes and optimize
698 : : away redundant buffering checks. */
699 : :
700 : : struct bytes_in::bits_in {
701 : : bytes_in& in;
702 : : uint32_t bit_val = 0;
703 : : unsigned bit_pos = 0;
704 : :
705 : 11046374 : bits_in (bytes_in& in)
706 : 11046374 : : in (in)
707 : : { }
708 : :
709 : 11046374 : ~bits_in ()
710 : : {
711 : 10336396 : bflush ();
712 : 11046374 : }
713 : :
714 : : bits_in(bits_in&&) = default;
715 : : bits_in(const bits_in&) = delete;
716 : : bits_in& operator=(const bits_in&) = delete;
717 : :
718 : : /* Completed a block of bools. */
719 : 22937196 : void bflush ()
720 : : {
721 : 22937196 : if (bit_pos)
722 : 12600800 : bit_flush (in, bit_val, bit_pos);
723 : 22937196 : }
724 : :
725 : : /* Read one bit. */
726 : 377287708 : bool b ()
727 : : {
728 : 377287708 : if (!bit_pos)
729 : 16437347 : bit_val = in.u32 ();
730 : 377287708 : bool x = (bit_val >> bit_pos) & 1;
731 : 377287708 : bit_pos = (bit_pos + 1) % 32;
732 : 377287708 : return x;
733 : : }
734 : : };
735 : :
736 : : /* Factory function for bits_in. */
737 : :
738 : : bytes_in::bits_in
739 : 11046374 : bytes_in::stream_bits ()
740 : : {
741 : 11046374 : return bits_in (*this);
742 : : }
743 : :
744 : : /* Bit stream writer (RAII-enabled), counterpart to bits_in. */
745 : :
746 : : struct bytes_out::bits_out {
747 : : bytes_out& out;
748 : : uint32_t bit_val = 0;
749 : : unsigned bit_pos = 0;
750 : : char is_set = -1;
751 : :
752 : 13051987 : bits_out (bytes_out& out)
753 : 13051987 : : out (out)
754 : : { }
755 : :
756 : 13051987 : ~bits_out ()
757 : : {
758 : 66099 : bflush ();
759 : : }
760 : :
761 : : bits_out(bits_out&&) = default;
762 : : bits_out(const bits_out&) = delete;
763 : : bits_out& operator=(const bits_out&) = delete;
764 : :
765 : : /* Completed a block of bools. */
766 : 27110479 : void bflush ()
767 : : {
768 : 27110479 : if (bit_pos)
769 : : {
770 : 14932611 : out.u32 (bit_val);
771 : 14932611 : out.lengths[2] += bit_flush (out, bit_val, bit_pos);
772 : : }
773 : 27110479 : out.spans[2]++;
774 : 27110479 : is_set = -1;
775 : 27110479 : }
776 : :
777 : : /* Write one bit.
778 : :
779 : : It may be worth optimizing for most bools being zero. Some kind of
780 : : run-length encoding? */
781 : 446070466 : void b (bool x)
782 : : {
783 : 446070466 : if (is_set != x)
784 : : {
785 : 47399213 : is_set = x;
786 : 47399213 : out.spans[x]++;
787 : : }
788 : 446070466 : out.lengths[x]++;
789 : 446070466 : bit_val |= unsigned (x) << bit_pos++;
790 : 446070466 : if (bit_pos == 32)
791 : : {
792 : 4661344 : out.u32 (bit_val);
793 : 4661344 : out.lengths[2] += bit_flush (out, bit_val, bit_pos);
794 : : }
795 : 446070466 : }
796 : : };
797 : :
798 : : /* Factory function for bits_out. */
799 : :
800 : : bytes_out::bits_out
801 : 13051987 : bytes_out::stream_bits ()
802 : : {
803 : 13051987 : return bits_out (*this);
804 : : }
805 : :
806 : : /* Instrumentation. */
807 : : unsigned bytes_out::spans[4];
808 : : unsigned bytes_out::lengths[4];
809 : :
810 : : /* If CRC_PTR non-null, set the CRC of the buffer. Mix the CRC into
811 : : that pointed to by CRC_PTR. */
812 : :
813 : : void
814 : 242052 : bytes_out::set_crc (unsigned *crc_ptr)
815 : : {
816 : 242052 : if (crc_ptr)
817 : : {
818 : 239671 : gcc_checking_assert (pos >= 4);
819 : :
820 : 239671 : unsigned crc = calc_crc (pos);
821 : 239671 : unsigned accum = *crc_ptr;
822 : : /* Only mix the existing *CRC_PTR if it is non-zero. */
823 : 239671 : accum = accum ? crc32_combine (accum, crc, pos - 4) : crc;
824 : 239671 : *crc_ptr = accum;
825 : :
826 : : /* Buffer will be sufficiently aligned. */
827 : 239671 : *(unsigned *)buffer = crc;
828 : : }
829 : 242052 : }
830 : :
831 : : /* Exactly 4 bytes. Used internally for bool packing and a few other
832 : : places. We can't simply use uint32_t because (a) alignment and
833 : : (b) we need little-endian for the bool streaming rewinding to make
834 : : sense. */
835 : :
836 : : void
837 : 19601658 : bytes_out::u32 (unsigned val)
838 : : {
839 : 19601658 : if (char *ptr = write (4))
840 : : {
841 : 19601658 : ptr[0] = val;
842 : 19601658 : ptr[1] = val >> 8;
843 : 19601658 : ptr[2] = val >> 16;
844 : 19601658 : ptr[3] = val >> 24;
845 : : }
846 : 19601658 : }
847 : :
848 : : unsigned
849 : 16445530 : bytes_in::u32 ()
850 : : {
851 : 16445530 : unsigned val = 0;
852 : 16445530 : if (const char *ptr = read (4))
853 : : {
854 : 16445530 : val |= (unsigned char)ptr[0];
855 : 16445530 : val |= (unsigned char)ptr[1] << 8;
856 : 16445530 : val |= (unsigned char)ptr[2] << 16;
857 : 16445530 : val |= (unsigned char)ptr[3] << 24;
858 : : }
859 : :
860 : 16445530 : return val;
861 : : }
862 : :
863 : : /* Chars are unsigned and written as single bytes. */
864 : :
865 : : void
866 : 0 : bytes_out::c (unsigned char v)
867 : : {
868 : 0 : if (char *ptr = write (1))
869 : 0 : *ptr = v;
870 : 0 : }
871 : :
872 : : int
873 : 0 : bytes_in::c ()
874 : : {
875 : 0 : int v = 0;
876 : 0 : if (const char *ptr = read (1))
877 : 0 : v = (unsigned char)ptr[0];
878 : 0 : return v;
879 : : }
880 : :
881 : : /* Ints 7-bit as a byte. Otherwise a 3bit count of following bytes in
882 : : big-endian form. 4 bits are in the first byte. */
883 : :
884 : : void
885 : 81207855 : bytes_out::i (int v)
886 : : {
887 : 81207855 : if (char *ptr = write (1))
888 : : {
889 : 81207855 : if (v <= 0x3f && v >= -0x40)
890 : 64359262 : *ptr = v & 0x7f;
891 : : else
892 : : {
893 : 16848593 : unsigned bytes = 0;
894 : 16848593 : int probe;
895 : 16848593 : if (v >= 0)
896 : 0 : for (probe = v >> 8; probe > 0x7; probe >>= 8)
897 : 0 : bytes++;
898 : : else
899 : 26108487 : for (probe = v >> 8; probe < -0x8; probe >>= 8)
900 : 9259894 : bytes++;
901 : 16848593 : *ptr = 0x80 | bytes << 4 | (probe & 0xf);
902 : 16848593 : if ((ptr = write (++bytes)))
903 : 42957080 : for (; bytes--; v >>= 8)
904 : 26108487 : ptr[bytes] = v & 0xff;
905 : : }
906 : : }
907 : 81207855 : }
908 : :
909 : : int
910 : 69415902 : bytes_in::i ()
911 : : {
912 : 69415902 : int v = 0;
913 : 69415902 : if (const char *ptr = read (1))
914 : : {
915 : 69415902 : v = *ptr & 0xff;
916 : 69415902 : if (v & 0x80)
917 : : {
918 : 15125218 : unsigned bytes = (v >> 4) & 0x7;
919 : 15125218 : v &= 0xf;
920 : 15125218 : if (v & 0x8)
921 : 15125218 : v |= -1 ^ 0x7;
922 : : /* unsigned necessary due to left shifts of -ve values. */
923 : 15125218 : unsigned uv = unsigned (v);
924 : 15125218 : if ((ptr = read (++bytes)))
925 : 39165640 : while (bytes--)
926 : 24040422 : uv = (uv << 8) | (*ptr++ & 0xff);
927 : 15125218 : v = int (uv);
928 : : }
929 : 54290684 : else if (v & 0x40)
930 : 7299514 : v |= -1 ^ 0x3f;
931 : : }
932 : :
933 : 69415902 : return v;
934 : : }
935 : :
936 : : void
937 : 69016626 : bytes_out::u (unsigned v)
938 : : {
939 : 69016626 : if (char *ptr = write (1))
940 : : {
941 : 69016626 : if (v <= 0x7f)
942 : 60144458 : *ptr = v;
943 : : else
944 : : {
945 : 8872168 : unsigned bytes = 0;
946 : 8872168 : unsigned probe;
947 : 10408663 : for (probe = v >> 8; probe > 0xf; probe >>= 8)
948 : 1536495 : bytes++;
949 : 8872168 : *ptr = 0x80 | bytes << 4 | probe;
950 : 8872168 : if ((ptr = write (++bytes)))
951 : 19280831 : for (; bytes--; v >>= 8)
952 : 10408663 : ptr[bytes] = v & 0xff;
953 : : }
954 : : }
955 : 69016626 : }
956 : :
957 : : unsigned
958 : 59970596 : bytes_in::u ()
959 : : {
960 : 59970596 : unsigned v = 0;
961 : :
962 : 59970596 : if (const char *ptr = read (1))
963 : : {
964 : 59970596 : v = *ptr & 0xff;
965 : 59970596 : if (v & 0x80)
966 : : {
967 : 7928459 : unsigned bytes = (v >> 4) & 0x7;
968 : 7928459 : v &= 0xf;
969 : 7928459 : if ((ptr = read (++bytes)))
970 : 17335165 : while (bytes--)
971 : 9406706 : v = (v << 8) | (*ptr++ & 0xff);
972 : : }
973 : : }
974 : :
975 : 59970596 : return v;
976 : : }
977 : :
978 : : void
979 : 16390032 : bytes_out::wi (HOST_WIDE_INT v)
980 : : {
981 : 16390032 : if (char *ptr = write (1))
982 : : {
983 : 16390032 : if (v <= 0x3f && v >= -0x40)
984 : 3295965 : *ptr = v & 0x7f;
985 : : else
986 : : {
987 : 13094067 : unsigned bytes = 0;
988 : 13094067 : HOST_WIDE_INT probe;
989 : 13094067 : if (v >= 0)
990 : 46987078 : for (probe = v >> 8; probe > 0x7; probe >>= 8)
991 : 33894752 : bytes++;
992 : : else
993 : 5786 : for (probe = v >> 8; probe < -0x8; probe >>= 8)
994 : 4045 : bytes++;
995 : 13094067 : *ptr = 0x80 | bytes << 4 | (probe & 0xf);
996 : 13094067 : if ((ptr = write (++bytes)))
997 : 60086931 : for (; bytes--; v >>= 8)
998 : 46992864 : ptr[bytes] = v & 0xff;
999 : : }
1000 : : }
1001 : 16390032 : }
1002 : :
1003 : : HOST_WIDE_INT
1004 : 13585733 : bytes_in::wi ()
1005 : : {
1006 : 13585733 : HOST_WIDE_INT v = 0;
1007 : 13585733 : if (const char *ptr = read (1))
1008 : : {
1009 : 13585733 : v = *ptr & 0xff;
1010 : 13585733 : if (v & 0x80)
1011 : : {
1012 : 12145159 : unsigned bytes = (v >> 4) & 0x7;
1013 : 12145159 : v &= 0xf;
1014 : 12145159 : if (v & 0x8)
1015 : 1452 : v |= -1 ^ 0x7;
1016 : : /* unsigned necessary due to left shifts of -ve values. */
1017 : 12145159 : unsigned HOST_WIDE_INT uv = (unsigned HOST_WIDE_INT) v;
1018 : 12145159 : if ((ptr = read (++bytes)))
1019 : 55559864 : while (bytes--)
1020 : 43414705 : uv = (uv << 8) | (*ptr++ & 0xff);
1021 : 12145159 : v = (HOST_WIDE_INT) uv;
1022 : : }
1023 : 1440574 : else if (v & 0x40)
1024 : 6749 : v |= -1 ^ 0x3f;
1025 : : }
1026 : :
1027 : 13585733 : return v;
1028 : : }
1029 : :
1030 : : /* unsigned wide ints are just written as signed wide ints. */
1031 : :
1032 : : inline void
1033 : 16389468 : bytes_out::wu (unsigned HOST_WIDE_INT v)
1034 : : {
1035 : 16389468 : wi ((HOST_WIDE_INT) v);
1036 : : }
1037 : :
1038 : : inline unsigned HOST_WIDE_INT
1039 : 13585243 : bytes_in::wu ()
1040 : : {
1041 : 26705002 : return (unsigned HOST_WIDE_INT) wi ();
1042 : : }
1043 : :
1044 : : /* size_t written as unsigned or unsigned wide int. */
1045 : :
1046 : : inline void
1047 : 1468931 : bytes_out::z (size_t s)
1048 : : {
1049 : 1468931 : if (sizeof (s) == sizeof (unsigned))
1050 : : u (s);
1051 : : else
1052 : 2908068 : wu (s);
1053 : 12 : }
1054 : :
1055 : : inline size_t
1056 : 1174915 : bytes_in::z ()
1057 : : {
1058 : 1174915 : if (sizeof (size_t) == sizeof (unsigned))
1059 : : return u ();
1060 : : else
1061 : 2349830 : return wu ();
1062 : : }
1063 : :
1064 : : /* location_t written as 32- or 64-bit as needed. */
1065 : :
1066 : 14370076 : inline void bytes_out::loc (location_t l)
1067 : : {
1068 : 14370076 : if (sizeof (location_t) > sizeof (unsigned))
1069 : 27241955 : wu (l);
1070 : : else
1071 : : u (l);
1072 : 1495816 : }
1073 : :
1074 : 11947413 : inline location_t bytes_in::loc ()
1075 : : {
1076 : 11947413 : if (sizeof (location_t) > sizeof (unsigned))
1077 : 23892227 : return wu ();
1078 : : else
1079 : : return u ();
1080 : : }
1081 : :
1082 : : /* Buffer simply memcpied. */
1083 : : void *
1084 : 1495122 : bytes_out::buf (size_t len)
1085 : : {
1086 : 1495122 : align (sizeof (void *) * 2);
1087 : 1495122 : return write (len);
1088 : : }
1089 : :
1090 : : void
1091 : 1451491 : bytes_out::buf (const void *src, size_t len)
1092 : : {
1093 : 1451491 : if (void *ptr = buf (len))
1094 : 1451491 : memcpy (ptr, src, len);
1095 : 1451491 : }
1096 : :
1097 : : const void *
1098 : 1188320 : bytes_in::buf (size_t len)
1099 : : {
1100 : 1188320 : align (sizeof (void *) * 2);
1101 : 1188320 : const char *ptr = read (len);
1102 : :
1103 : 1188320 : return ptr;
1104 : : }
1105 : :
1106 : : /* strings as an size_t length, followed by the buffer. Make sure
1107 : : there's a NUL terminator on read. */
1108 : :
1109 : : void
1110 : 1468913 : bytes_out::str (const char *string, size_t len)
1111 : : {
1112 : 1439131 : z (len);
1113 : 1439131 : if (len)
1114 : : {
1115 : 1439131 : gcc_checking_assert (!string[len]);
1116 : 1439131 : buf (string, len + 1);
1117 : : }
1118 : 29782 : }
1119 : :
1120 : : const char *
1121 : 1174909 : bytes_in::str (size_t *len_p)
1122 : : {
1123 : 1174909 : size_t len = z ();
1124 : :
1125 : : /* We're about to trust some user data. */
1126 : 1174909 : if (overrun)
1127 : 0 : len = 0;
1128 : 1174909 : if (len_p)
1129 : 1171580 : *len_p = len;
1130 : 1174909 : const char *str = NULL;
1131 : 1174909 : if (len)
1132 : : {
1133 : 1174701 : str = reinterpret_cast<const char *> (buf (len + 1));
1134 : 1174701 : if (!str || str[len])
1135 : : {
1136 : 0 : set_overrun ();
1137 : 0 : str = NULL;
1138 : : }
1139 : : }
1140 : 0 : return str ? str : "";
1141 : : }
1142 : :
1143 : : cpp_hashnode *
1144 : 213457 : bytes_in::cpp_node ()
1145 : : {
1146 : 213457 : size_t len;
1147 : 213457 : const char *s = str (&len);
1148 : 213457 : if (!len)
1149 : : return NULL;
1150 : 213249 : return ::cpp_node (get_identifier_with_length (s, len));
1151 : : }
1152 : :
1153 : : /* Format a string directly to the buffer, including a terminating
1154 : : NUL. Intended for human consumption. */
1155 : :
1156 : : void
1157 : 24365 : bytes_out::printf (const char *format, ...)
1158 : : {
1159 : 24365 : va_list args;
1160 : : /* Exercise buffer expansion. */
1161 : 24365 : size_t len = EXPERIMENT (10, 500);
1162 : :
1163 : 48428 : while (char *ptr = write (len))
1164 : : {
1165 : 48428 : va_start (args, format);
1166 : 48428 : size_t actual = vsnprintf (ptr, len, format, args) + 1;
1167 : 48428 : va_end (args);
1168 : 48428 : if (actual <= len)
1169 : : {
1170 : 24365 : unuse (len - actual);
1171 : 24365 : break;
1172 : : }
1173 : 24063 : unuse (len);
1174 : 24063 : len = actual;
1175 : 24063 : }
1176 : 24365 : }
1177 : :
1178 : : void
1179 : 4762 : bytes_out::print_time (const char *kind, const tm *time, const char *tz)
1180 : : {
1181 : 4762 : printf ("%stime: %4u/%02u/%02u %02u:%02u:%02u %s",
1182 : 4762 : kind, time->tm_year + 1900, time->tm_mon + 1, time->tm_mday,
1183 : 4762 : time->tm_hour, time->tm_min, time->tm_sec, tz);
1184 : 4762 : }
1185 : :
1186 : : /* Encapsulated Lazy Records Of Named Declarations.
1187 : : Header: Stunningly Elf32_Ehdr-like
1188 : : Sections: Sectional data
1189 : : [1-N) : User data sections
1190 : : N .strtab : strings, stunningly ELF STRTAB-like
1191 : : Index: Section table, stunningly ELF32_Shdr-like. */
1192 : :
1193 : : class elf {
1194 : : protected:
1195 : : /* Constants used within the format. */
1196 : : enum private_constants {
1197 : : /* File kind. */
1198 : : ET_NONE = 0,
1199 : : EM_NONE = 0,
1200 : : OSABI_NONE = 0,
1201 : :
1202 : : /* File format. */
1203 : : EV_CURRENT = 1,
1204 : : CLASS32 = 1,
1205 : : DATA2LSB = 1,
1206 : : DATA2MSB = 2,
1207 : :
1208 : : /* Section numbering. */
1209 : : SHN_UNDEF = 0,
1210 : : SHN_LORESERVE = 0xff00,
1211 : : SHN_XINDEX = 0xffff,
1212 : :
1213 : : /* Section types. */
1214 : : SHT_NONE = 0, /* No contents. */
1215 : : SHT_PROGBITS = 1, /* Random bytes. */
1216 : : SHT_STRTAB = 3, /* A string table. */
1217 : :
1218 : : /* Section flags. */
1219 : : SHF_NONE = 0x00, /* Nothing. */
1220 : : SHF_STRINGS = 0x20, /* NUL-Terminated strings. */
1221 : :
1222 : : /* I really hope we do not get CMI files larger than 4GB. */
1223 : : MY_CLASS = CLASS32,
1224 : : /* It is host endianness that is relevant. */
1225 : : MY_ENDIAN = DATA2LSB
1226 : : #ifdef WORDS_BIGENDIAN
1227 : : ^ DATA2LSB ^ DATA2MSB
1228 : : #endif
1229 : : };
1230 : :
1231 : : public:
1232 : : /* Constants visible to users. */
1233 : : enum public_constants {
1234 : : /* Special error codes. Breaking layering a bit. */
1235 : : E_BAD_DATA = -1, /* Random unexpected data errors. */
1236 : : E_BAD_LAZY = -2, /* Badly ordered laziness. */
1237 : : E_BAD_IMPORT = -3 /* A nested import failed. */
1238 : : };
1239 : :
1240 : : protected:
1241 : : /* File identification. On-disk representation. */
1242 : : struct ident {
1243 : : uint8_t magic[4]; /* 0x7f, 'E', 'L', 'F' */
1244 : : uint8_t klass; /* 4:CLASS32 */
1245 : : uint8_t data; /* 5:DATA2[LM]SB */
1246 : : uint8_t version; /* 6:EV_CURRENT */
1247 : : uint8_t osabi; /* 7:OSABI_NONE */
1248 : : uint8_t abiver; /* 8: 0 */
1249 : : uint8_t pad[7]; /* 9-15 */
1250 : : };
1251 : : /* File header. On-disk representation. */
1252 : : struct header {
1253 : : struct ident ident;
1254 : : uint16_t type; /* ET_NONE */
1255 : : uint16_t machine; /* EM_NONE */
1256 : : uint32_t version; /* EV_CURRENT */
1257 : : uint32_t entry; /* 0 */
1258 : : uint32_t phoff; /* 0 */
1259 : : uint32_t shoff; /* Section Header Offset in file */
1260 : : uint32_t flags;
1261 : : uint16_t ehsize; /* ELROND Header SIZE -- sizeof (header) */
1262 : : uint16_t phentsize; /* 0 */
1263 : : uint16_t phnum; /* 0 */
1264 : : uint16_t shentsize; /* Section Header SIZE -- sizeof (section) */
1265 : : uint16_t shnum; /* Section Header NUM */
1266 : : uint16_t shstrndx; /* Section Header STRing iNDeX */
1267 : : };
1268 : : /* File section. On-disk representation. */
1269 : : struct section {
1270 : : uint32_t name; /* String table offset. */
1271 : : uint32_t type; /* SHT_* */
1272 : : uint32_t flags; /* SHF_* */
1273 : : uint32_t addr; /* 0 */
1274 : : uint32_t offset; /* OFFSET in file */
1275 : : uint32_t size; /* SIZE of section */
1276 : : uint32_t link; /* 0 */
1277 : : uint32_t info; /* 0 */
1278 : : uint32_t addralign; /* 0 */
1279 : : uint32_t entsize; /* ENTry SIZE, usually 0 */
1280 : : };
1281 : :
1282 : : protected:
1283 : : data hdr; /* The header. */
1284 : : data sectab; /* The section table. */
1285 : : data strtab; /* String table. */
1286 : : int fd; /* File descriptor we're reading or writing. */
1287 : : int err; /* Sticky error code. */
1288 : :
1289 : : public:
1290 : : /* Construct from STREAM. E is errno if STREAM NULL. */
1291 : 5148 : elf (int fd, int e)
1292 : 10296 : :hdr (), sectab (), strtab (), fd (fd), err (fd >= 0 ? 0 : e)
1293 : : {}
1294 : 5072 : ~elf ()
1295 : : {
1296 : 5072 : gcc_checking_assert (fd < 0 && !hdr.buffer
1297 : : && !sectab.buffer && !strtab.buffer);
1298 : 5072 : }
1299 : :
1300 : : public:
1301 : : /* Return the error, if we have an error. */
1302 : 356039 : int get_error () const
1303 : : {
1304 : 356039 : return err;
1305 : : }
1306 : : /* Set the error, unless it's already been set. */
1307 : 32 : void set_error (int e = E_BAD_DATA)
1308 : : {
1309 : 32 : if (!err)
1310 : 19 : err = e;
1311 : 0 : }
1312 : : /* Get an error string. */
1313 : : const char *get_error (const char *) const;
1314 : :
1315 : : public:
1316 : : /* Begin reading/writing file. Return false on error. */
1317 : 5021 : bool begin () const
1318 : : {
1319 : 5021 : return !get_error ();
1320 : : }
1321 : : /* Finish reading/writing file. Return false on error. */
1322 : : bool end ();
1323 : : };
1324 : :
1325 : : /* Return error string. */
1326 : :
1327 : : const char *
1328 : 37 : elf::get_error (const char *name) const
1329 : : {
1330 : 37 : if (!name)
1331 : : return "Unknown CMI mapping";
1332 : :
1333 : 37 : switch (err)
1334 : : {
1335 : 0 : case 0:
1336 : 0 : gcc_unreachable ();
1337 : : case E_BAD_DATA:
1338 : : return "Bad file data";
1339 : 6 : case E_BAD_IMPORT:
1340 : 6 : return "Bad import dependency";
1341 : 0 : case E_BAD_LAZY:
1342 : 0 : return "Bad lazy ordering";
1343 : 18 : default:
1344 : 18 : return xstrerror (err);
1345 : : }
1346 : : }
1347 : :
1348 : : /* Finish file, return true if there's an error. */
1349 : :
1350 : : bool
1351 : 7190 : elf::end ()
1352 : : {
1353 : : /* Close the stream and free the section table. */
1354 : 7190 : if (fd >= 0 && close (fd))
1355 : 0 : set_error (errno);
1356 : 7190 : fd = -1;
1357 : :
1358 : 7190 : return !get_error ();
1359 : : }
1360 : :
1361 : : /* ELROND reader. */
1362 : :
1363 : : class elf_in : public elf {
1364 : : typedef elf parent;
1365 : :
1366 : : private:
1367 : : /* For freezing & defrosting. */
1368 : : #if !defined (HOST_LACKS_INODE_NUMBERS)
1369 : : dev_t device;
1370 : : ino_t inode;
1371 : : #endif
1372 : :
1373 : : public:
1374 : 2630 : elf_in (int fd, int e)
1375 : 5260 : :parent (fd, e)
1376 : : {
1377 : : }
1378 : 2554 : ~elf_in ()
1379 : : {
1380 : 2554 : }
1381 : :
1382 : : public:
1383 : 166499 : bool is_frozen () const
1384 : : {
1385 : 18 : return fd < 0 && hdr.pos;
1386 : : }
1387 : 18 : bool is_freezable () const
1388 : : {
1389 : 9 : return fd >= 0 && hdr.pos;
1390 : : }
1391 : : void freeze ();
1392 : : bool defrost (const char *);
1393 : :
1394 : : /* If BYTES is in the mmapped area, allocate a new buffer for it. */
1395 : 0 : void preserve (bytes_in &bytes ATTRIBUTE_UNUSED)
1396 : : {
1397 : : #if MAPPED_READING
1398 : 0 : if (hdr.buffer && bytes.buffer >= hdr.buffer
1399 : 0 : && bytes.buffer < hdr.buffer + hdr.pos)
1400 : : {
1401 : 0 : char *buf = bytes.buffer;
1402 : 0 : bytes.buffer = data::simple_memory.grow (NULL, bytes.size);
1403 : 0 : memcpy (bytes.buffer, buf, bytes.size);
1404 : : }
1405 : : #endif
1406 : 0 : }
1407 : : /* If BYTES is not in SELF's mmapped area, free it. SELF might be
1408 : : NULL. */
1409 : 1021 : static void release (elf_in *self ATTRIBUTE_UNUSED, bytes_in &bytes)
1410 : : {
1411 : : #if MAPPED_READING
1412 : 1021 : if (!(self && self->hdr.buffer && bytes.buffer >= self->hdr.buffer
1413 : 1021 : && bytes.buffer < self->hdr.buffer + self->hdr.pos))
1414 : : #endif
1415 : 0 : data::simple_memory.shrink (bytes.buffer);
1416 : 1021 : bytes.buffer = NULL;
1417 : 1021 : bytes.size = 0;
1418 : 1021 : }
1419 : :
1420 : : public:
1421 : 187606 : static void grow (data &data, unsigned needed)
1422 : : {
1423 : 187606 : gcc_checking_assert (!data.buffer);
1424 : : #if !MAPPED_READING
1425 : : data.buffer = XNEWVEC (char, needed);
1426 : : #endif
1427 : 187606 : data.size = needed;
1428 : 187606 : }
1429 : 193253 : static void shrink (data &data)
1430 : : {
1431 : : #if !MAPPED_READING
1432 : : XDELETEVEC (data.buffer);
1433 : : #endif
1434 : 193253 : data.buffer = NULL;
1435 : 193253 : data.size = 0;
1436 : 0 : }
1437 : :
1438 : : public:
1439 : 184994 : const section *get_section (unsigned s) const
1440 : : {
1441 : 184994 : if (s * sizeof (section) < sectab.size)
1442 : 184994 : return reinterpret_cast<const section *>
1443 : 184994 : (§ab.buffer[s * sizeof (section)]);
1444 : : else
1445 : : return NULL;
1446 : : }
1447 : : unsigned get_section_limit () const
1448 : : {
1449 : : return sectab.size / sizeof (section);
1450 : : }
1451 : :
1452 : : protected:
1453 : : const char *read (data *, unsigned, unsigned);
1454 : :
1455 : : public:
1456 : : /* Read section by number. */
1457 : 184994 : bool read (data *d, const section *s)
1458 : : {
1459 : 184994 : return s && read (d, s->offset, s->size);
1460 : : }
1461 : :
1462 : : /* Find section by name. */
1463 : : unsigned find (const char *name);
1464 : : /* Find section by index. */
1465 : : const section *find (unsigned snum, unsigned type = SHT_PROGBITS);
1466 : :
1467 : : public:
1468 : : /* Release the string table, when we're done with it. */
1469 : 7259 : void release ()
1470 : : {
1471 : 7259 : shrink (strtab);
1472 : 39 : }
1473 : :
1474 : : public:
1475 : : bool begin (location_t);
1476 : 4672 : bool end ()
1477 : : {
1478 : 4672 : release ();
1479 : : #if MAPPED_READING
1480 : 4672 : if (hdr.buffer)
1481 : 2554 : munmap (hdr.buffer, hdr.pos);
1482 : 4672 : hdr.buffer = NULL;
1483 : : #endif
1484 : 4672 : shrink (sectab);
1485 : :
1486 : 4672 : return parent::end ();
1487 : : }
1488 : :
1489 : : public:
1490 : : /* Return string name at OFFSET. Checks OFFSET range. Always
1491 : : returns non-NULL. We know offset 0 is an empty string. */
1492 : 267117 : const char *name (unsigned offset)
1493 : : {
1494 : 534234 : return &strtab.buffer[offset < strtab.size ? offset : 0];
1495 : : }
1496 : : };
1497 : :
1498 : : /* ELROND writer. */
1499 : :
1500 : : class elf_out : public elf, public data::allocator {
1501 : : typedef elf parent;
1502 : : /* Desired section alignment on disk. */
1503 : : static const int SECTION_ALIGN = 16;
1504 : :
1505 : : private:
1506 : : ptr_int_hash_map identtab; /* Map of IDENTIFIERS to strtab offsets. */
1507 : : unsigned pos; /* Write position in file. */
1508 : : #if MAPPED_WRITING
1509 : : unsigned offset; /* Offset of the mapping. */
1510 : : unsigned extent; /* Length of mapping. */
1511 : : unsigned page_size; /* System page size. */
1512 : : #endif
1513 : :
1514 : : public:
1515 : 2518 : elf_out (int fd, int e)
1516 : 4933 : :parent (fd, e), identtab (500), pos (0)
1517 : : {
1518 : : #if MAPPED_WRITING
1519 : 2518 : offset = extent = 0;
1520 : 2518 : page_size = sysconf (_SC_PAGE_SIZE);
1521 : 2518 : if (page_size < SECTION_ALIGN)
1522 : : /* Something really strange. */
1523 : 0 : set_error (EINVAL);
1524 : : #endif
1525 : 2518 : }
1526 : 2518 : ~elf_out ()
1527 : 2518 : {
1528 : 2518 : data::simple_memory.shrink (hdr);
1529 : 2518 : data::simple_memory.shrink (sectab);
1530 : 2518 : data::simple_memory.shrink (strtab);
1531 : 2518 : }
1532 : :
1533 : : #if MAPPED_WRITING
1534 : : private:
1535 : : void create_mapping (unsigned ext, bool extending = true);
1536 : : void remove_mapping ();
1537 : : #endif
1538 : :
1539 : : protected:
1540 : : using allocator::grow;
1541 : : char *grow (char *, unsigned needed) final override;
1542 : : #if MAPPED_WRITING
1543 : : using allocator::shrink;
1544 : : void shrink (char *) final override;
1545 : : #endif
1546 : :
1547 : : public:
1548 : 5013 : unsigned get_section_limit () const
1549 : : {
1550 : 5013 : return sectab.pos / sizeof (section);
1551 : : }
1552 : :
1553 : : protected:
1554 : : unsigned add (unsigned type, unsigned name = 0,
1555 : : unsigned off = 0, unsigned size = 0, unsigned flags = SHF_NONE);
1556 : : unsigned write (const data &);
1557 : : #if MAPPED_WRITING
1558 : : unsigned write (const bytes_out &);
1559 : : #endif
1560 : :
1561 : : public:
1562 : : /* IDENTIFIER to strtab offset. */
1563 : : unsigned name (tree ident);
1564 : : /* String literal to strtab offset. */
1565 : : unsigned name (const char *n);
1566 : : /* Qualified name of DECL to strtab offset. */
1567 : : unsigned qualified_name (tree decl, bool is_defn);
1568 : :
1569 : : private:
1570 : : unsigned strtab_write (const char *s, unsigned l);
1571 : : void strtab_write (tree decl, int);
1572 : :
1573 : : public:
1574 : : /* Add a section with contents or strings. */
1575 : : unsigned add (const bytes_out &, bool string_p, unsigned name);
1576 : :
1577 : : public:
1578 : : /* Begin and end writing. */
1579 : : bool begin ();
1580 : : bool end ();
1581 : : };
1582 : :
1583 : : /* Begin reading section NAME (of type PROGBITS) from SOURCE.
1584 : : Data always checked for CRC. */
1585 : :
1586 : : bool
1587 : 18468 : bytes_in::begin (location_t loc, elf_in *source, const char *name)
1588 : : {
1589 : 18468 : unsigned snum = source->find (name);
1590 : :
1591 : 18468 : return begin (loc, source, snum, name);
1592 : : }
1593 : :
1594 : : /* Begin reading section numbered SNUM with NAME (may be NULL). */
1595 : :
1596 : : bool
1597 : 182382 : bytes_in::begin (location_t loc, elf_in *source, unsigned snum, const char *name)
1598 : : {
1599 : 182382 : if (!source->read (this, source->find (snum))
1600 : 182382 : || !size || !check_crc ())
1601 : : {
1602 : 0 : source->set_error (elf::E_BAD_DATA);
1603 : 0 : source->shrink (*this);
1604 : 0 : if (name)
1605 : 0 : error_at (loc, "section %qs is missing or corrupted", name);
1606 : : else
1607 : 0 : error_at (loc, "section #%u is missing or corrupted", snum);
1608 : 0 : return false;
1609 : : }
1610 : 182382 : pos = 4;
1611 : 182382 : return true;
1612 : : }
1613 : :
1614 : : /* Finish reading a section. */
1615 : :
1616 : : bool
1617 : 181322 : bytes_in::end (elf_in *src)
1618 : : {
1619 : 181322 : if (more_p ())
1620 : 13 : set_overrun ();
1621 : 181322 : if (overrun)
1622 : 13 : src->set_error ();
1623 : :
1624 : 181322 : src->shrink (*this);
1625 : :
1626 : 181322 : return !overrun;
1627 : : }
1628 : :
1629 : : /* Begin writing buffer. */
1630 : :
1631 : : void
1632 : 242052 : bytes_out::begin (bool need_crc)
1633 : : {
1634 : 0 : if (need_crc)
1635 : 0 : pos = 4;
1636 : 0 : memory->grow (*this, 0, false);
1637 : 223752 : }
1638 : :
1639 : : /* Finish writing buffer. Stream out to SINK as named section NAME.
1640 : : Return section number or 0 on failure. If CRC_PTR is true, crc
1641 : : the data. Otherwise it is a string section. */
1642 : :
1643 : : unsigned
1644 : 242052 : bytes_out::end (elf_out *sink, unsigned name, unsigned *crc_ptr)
1645 : : {
1646 : 242052 : lengths[3] += pos;
1647 : 242052 : spans[3]++;
1648 : :
1649 : 242052 : set_crc (crc_ptr);
1650 : 242052 : unsigned sec_num = sink->add (*this, !crc_ptr, name);
1651 : 242052 : memory->shrink (*this);
1652 : :
1653 : 242052 : return sec_num;
1654 : : }
1655 : :
1656 : : /* Close and open the file, without destroying it. */
1657 : :
1658 : : void
1659 : 9 : elf_in::freeze ()
1660 : : {
1661 : 9 : gcc_checking_assert (!is_frozen ());
1662 : : #if MAPPED_READING
1663 : 9 : if (munmap (hdr.buffer, hdr.pos) < 0)
1664 : 0 : set_error (errno);
1665 : : #endif
1666 : 9 : if (close (fd) < 0)
1667 : 0 : set_error (errno);
1668 : 9 : fd = -1;
1669 : 9 : }
1670 : :
1671 : : bool
1672 : 9 : elf_in::defrost (const char *name)
1673 : : {
1674 : 9 : gcc_checking_assert (is_frozen ());
1675 : 9 : struct stat stat;
1676 : :
1677 : 9 : fd = open (name, O_RDONLY | O_CLOEXEC | O_BINARY);
1678 : 9 : if (fd < 0 || fstat (fd, &stat) < 0)
1679 : 0 : set_error (errno);
1680 : : else
1681 : : {
1682 : 9 : bool ok = hdr.pos == unsigned (stat.st_size);
1683 : : #ifndef HOST_LACKS_INODE_NUMBERS
1684 : 9 : if (device != stat.st_dev
1685 : 9 : || inode != stat.st_ino)
1686 : : ok = false;
1687 : : #endif
1688 : 9 : if (!ok)
1689 : 0 : set_error (EMFILE);
1690 : : #if MAPPED_READING
1691 : 0 : if (ok)
1692 : : {
1693 : 9 : char *mapping = reinterpret_cast<char *>
1694 : 9 : (mmap (NULL, hdr.pos, PROT_READ, MAP_SHARED, fd, 0));
1695 : 9 : if (mapping == MAP_FAILED)
1696 : 0 : fail:
1697 : 0 : set_error (errno);
1698 : : else
1699 : : {
1700 : 9 : if (madvise (mapping, hdr.pos, MADV_RANDOM))
1701 : 0 : goto fail;
1702 : :
1703 : : /* These buffers are never NULL in this case. */
1704 : 9 : strtab.buffer = mapping + strtab.pos;
1705 : 9 : sectab.buffer = mapping + sectab.pos;
1706 : 9 : hdr.buffer = mapping;
1707 : : }
1708 : : }
1709 : : #endif
1710 : : }
1711 : :
1712 : 9 : return !get_error ();
1713 : : }
1714 : :
1715 : : /* Read at current position into BUFFER. Return true on success. */
1716 : :
1717 : : const char *
1718 : 187606 : elf_in::read (data *data, unsigned pos, unsigned length)
1719 : : {
1720 : : #if MAPPED_READING
1721 : 187606 : if (pos + length > hdr.pos)
1722 : : {
1723 : 0 : set_error (EINVAL);
1724 : 0 : return NULL;
1725 : : }
1726 : : #else
1727 : : if (pos != ~0u && lseek (fd, pos, SEEK_SET) < 0)
1728 : : {
1729 : : set_error (errno);
1730 : : return NULL;
1731 : : }
1732 : : #endif
1733 : 187606 : grow (*data, length);
1734 : : #if MAPPED_READING
1735 : 187606 : data->buffer = hdr.buffer + pos;
1736 : : #else
1737 : : if (::read (fd, data->buffer, data->size) != ssize_t (length))
1738 : : {
1739 : : set_error (errno);
1740 : : shrink (*data);
1741 : : return NULL;
1742 : : }
1743 : : #endif
1744 : :
1745 : 187606 : return data->buffer;
1746 : : }
1747 : :
1748 : : /* Read section SNUM of TYPE. Return section pointer or NULL on error. */
1749 : :
1750 : : const elf::section *
1751 : 184994 : elf_in::find (unsigned snum, unsigned type)
1752 : : {
1753 : 184994 : const section *sec = get_section (snum);
1754 : 184994 : if (!snum || !sec || sec->type != type)
1755 : 0 : return NULL;
1756 : : return sec;
1757 : : }
1758 : :
1759 : : /* Find a section NAME and TYPE. Return section number, or zero on
1760 : : failure. */
1761 : :
1762 : : unsigned
1763 : 18513 : elf_in::find (const char *sname)
1764 : : {
1765 : 117223 : for (unsigned pos = sectab.size; pos -= sizeof (section); )
1766 : : {
1767 : 117223 : const section *sec
1768 : 117223 : = reinterpret_cast<const section *> (§ab.buffer[pos]);
1769 : :
1770 : 234446 : if (0 == strcmp (sname, name (sec->name)))
1771 : 18513 : return pos / sizeof (section);
1772 : : }
1773 : :
1774 : : return 0;
1775 : : }
1776 : :
1777 : : /* Begin reading file. Verify header. Pull in section and string
1778 : : tables. Return true on success. */
1779 : :
1780 : : bool
1781 : 2612 : elf_in::begin (location_t loc)
1782 : : {
1783 : 2612 : if (!parent::begin ())
1784 : : return false;
1785 : :
1786 : 2612 : struct stat stat;
1787 : 2612 : unsigned size = 0;
1788 : 2612 : if (!fstat (fd, &stat))
1789 : : {
1790 : : #if !defined (HOST_LACKS_INODE_NUMBERS)
1791 : 2612 : device = stat.st_dev;
1792 : 2612 : inode = stat.st_ino;
1793 : : #endif
1794 : : /* Never generate files > 4GB, check we've not been given one. */
1795 : 2612 : if (stat.st_size == unsigned (stat.st_size))
1796 : 2612 : size = unsigned (stat.st_size);
1797 : : }
1798 : :
1799 : : #if MAPPED_READING
1800 : : /* MAP_SHARED so that the file is backing store. If someone else
1801 : : concurrently writes it, they're wrong. */
1802 : 2612 : void *mapping = mmap (NULL, size, PROT_READ, MAP_SHARED, fd, 0);
1803 : 2612 : if (mapping == MAP_FAILED)
1804 : : {
1805 : 0 : fail:
1806 : 0 : set_error (errno);
1807 : 0 : return false;
1808 : : }
1809 : : /* We'll be hopping over this randomly. Some systems declare the
1810 : : first parm as char *, and other declare it as void *. */
1811 : 2612 : if (madvise (reinterpret_cast <char *> (mapping), size, MADV_RANDOM))
1812 : 0 : goto fail;
1813 : :
1814 : 2612 : hdr.buffer = (char *)mapping;
1815 : : #else
1816 : : read (&hdr, 0, sizeof (header));
1817 : : #endif
1818 : 2612 : hdr.pos = size; /* Record size of the file. */
1819 : :
1820 : 2612 : const header *h = reinterpret_cast<const header *> (hdr.buffer);
1821 : 2612 : if (!h)
1822 : : return false;
1823 : :
1824 : 2612 : if (h->ident.magic[0] != 0x7f
1825 : 2612 : || h->ident.magic[1] != 'E'
1826 : 2612 : || h->ident.magic[2] != 'L'
1827 : 2612 : || h->ident.magic[3] != 'F')
1828 : : {
1829 : 0 : error_at (loc, "not Encapsulated Lazy Records of Named Declarations");
1830 : 0 : failed:
1831 : 0 : shrink (hdr);
1832 : 0 : return false;
1833 : : }
1834 : :
1835 : : /* We expect a particular format -- the ELF is not intended to be
1836 : : distributable. */
1837 : 2612 : if (h->ident.klass != MY_CLASS
1838 : 2612 : || h->ident.data != MY_ENDIAN
1839 : 2612 : || h->ident.version != EV_CURRENT
1840 : 2612 : || h->type != ET_NONE
1841 : 2612 : || h->machine != EM_NONE
1842 : 2612 : || h->ident.osabi != OSABI_NONE)
1843 : : {
1844 : 0 : error_at (loc, "unexpected encapsulation format or type");
1845 : 0 : goto failed;
1846 : : }
1847 : :
1848 : 2612 : int e = -1;
1849 : 2612 : if (!h->shoff || h->shentsize != sizeof (section))
1850 : : {
1851 : 0 : malformed:
1852 : 0 : set_error (e);
1853 : 0 : error_at (loc, "encapsulation is malformed");
1854 : 0 : goto failed;
1855 : : }
1856 : :
1857 : 2612 : unsigned strndx = h->shstrndx;
1858 : 2612 : unsigned shnum = h->shnum;
1859 : 2612 : if (shnum == SHN_XINDEX)
1860 : : {
1861 : 0 : if (!read (§ab, h->shoff, sizeof (section)))
1862 : : {
1863 : 0 : section_table_fail:
1864 : 0 : e = errno;
1865 : 0 : goto malformed;
1866 : : }
1867 : 0 : shnum = get_section (0)->size;
1868 : : /* Freeing does mean we'll re-read it in the case we're not
1869 : : mapping, but this is going to be rare. */
1870 : 0 : shrink (sectab);
1871 : : }
1872 : :
1873 : 2612 : if (!shnum)
1874 : 0 : goto malformed;
1875 : :
1876 : 2612 : if (!read (§ab, h->shoff, shnum * sizeof (section)))
1877 : 0 : goto section_table_fail;
1878 : :
1879 : 2612 : if (strndx == SHN_XINDEX)
1880 : 0 : strndx = get_section (0)->link;
1881 : :
1882 : 2612 : if (!read (&strtab, find (strndx, SHT_STRTAB)))
1883 : 0 : goto malformed;
1884 : :
1885 : : /* The string table should be at least one byte, with NUL chars
1886 : : at either end. */
1887 : 2612 : if (!(strtab.size && !strtab.buffer[0]
1888 : 2612 : && !strtab.buffer[strtab.size - 1]))
1889 : 0 : goto malformed;
1890 : :
1891 : : #if MAPPED_READING
1892 : : /* Record the offsets of the section and string tables. */
1893 : 2612 : sectab.pos = h->shoff;
1894 : 2612 : strtab.pos = shnum * sizeof (section);
1895 : : #else
1896 : : shrink (hdr);
1897 : : #endif
1898 : :
1899 : 2612 : return true;
1900 : : }
1901 : :
1902 : : /* Create a new mapping. */
1903 : :
1904 : : #if MAPPED_WRITING
1905 : : void
1906 : 3132 : elf_out::create_mapping (unsigned ext, bool extending)
1907 : : {
1908 : : /* A wrapper around posix_fallocate, falling back to ftruncate
1909 : : if the underlying filesystem does not support the operation. */
1910 : 6140 : auto allocate = [](int fd, off_t offset, off_t length)
1911 : : {
1912 : : #ifdef HAVE_POSIX_FALLOCATE
1913 : 3008 : int result = posix_fallocate (fd, offset, length);
1914 : 3008 : if (result != EINVAL)
1915 : 3008 : return result == 0;
1916 : : /* Not supported by the underlying filesystem, fallback to ftruncate. */
1917 : : #endif
1918 : 0 : return ftruncate (fd, offset + length) == 0;
1919 : : };
1920 : :
1921 : 3132 : void *mapping = MAP_FAILED;
1922 : 3132 : if (extending && ext < 1024 * 1024)
1923 : : {
1924 : 2896 : if (allocate (fd, offset, ext * 2))
1925 : 2896 : mapping = mmap (NULL, ext * 2, PROT_READ | PROT_WRITE,
1926 : 2896 : MAP_SHARED, fd, offset);
1927 : 2896 : if (mapping != MAP_FAILED)
1928 : : ext *= 2;
1929 : : }
1930 : : if (mapping == MAP_FAILED)
1931 : : {
1932 : 236 : if (!extending || allocate (fd, offset, ext))
1933 : 236 : mapping = mmap (NULL, ext, PROT_READ | PROT_WRITE,
1934 : 236 : MAP_SHARED, fd, offset);
1935 : 236 : if (mapping == MAP_FAILED)
1936 : : {
1937 : 0 : set_error (errno);
1938 : : mapping = NULL;
1939 : : ext = 0;
1940 : : }
1941 : : }
1942 : 3132 : hdr.buffer = (char *)mapping;
1943 : 3132 : extent = ext;
1944 : 3132 : }
1945 : : #endif
1946 : :
1947 : : /* Flush out the current mapping. */
1948 : :
1949 : : #if MAPPED_WRITING
1950 : : void
1951 : 3138 : elf_out::remove_mapping ()
1952 : : {
1953 : 3138 : if (hdr.buffer)
1954 : : {
1955 : : /* MS_ASYNC dtrt with the removed mapping, including a
1956 : : subsequent overlapping remap. */
1957 : 3132 : if (msync (hdr.buffer, extent, MS_ASYNC)
1958 : 3132 : || munmap (hdr.buffer, extent))
1959 : : /* We're somewhat screwed at this point. */
1960 : 0 : set_error (errno);
1961 : : }
1962 : :
1963 : 3138 : hdr.buffer = NULL;
1964 : 3138 : }
1965 : : #endif
1966 : :
1967 : : /* Grow a mapping of PTR to be NEEDED bytes long. This gets
1968 : : interesting if the new size grows the EXTENT. */
1969 : :
1970 : : char *
1971 : 570411 : elf_out::grow (char *data, unsigned needed)
1972 : : {
1973 : 570411 : if (!data)
1974 : : {
1975 : : /* First allocation, check we're aligned. */
1976 : 246882 : gcc_checking_assert (!(pos & (SECTION_ALIGN - 1)));
1977 : : #if MAPPED_WRITING
1978 : 246882 : data = hdr.buffer + (pos - offset);
1979 : : #endif
1980 : : }
1981 : :
1982 : : #if MAPPED_WRITING
1983 : 570411 : unsigned off = data - hdr.buffer;
1984 : 570411 : if (off + needed > extent)
1985 : : {
1986 : : /* We need to grow the mapping. */
1987 : 599 : unsigned lwm = off & ~(page_size - 1);
1988 : 599 : unsigned hwm = (off + needed + page_size - 1) & ~(page_size - 1);
1989 : :
1990 : 599 : gcc_checking_assert (hwm > extent);
1991 : :
1992 : 599 : remove_mapping ();
1993 : :
1994 : 599 : offset += lwm;
1995 : 599 : create_mapping (extent < hwm - lwm ? hwm - lwm : extent);
1996 : :
1997 : 599 : data = hdr.buffer + (off - lwm);
1998 : : }
1999 : : #else
2000 : : data = allocator::grow (data, needed);
2001 : : #endif
2002 : :
2003 : 570411 : return data;
2004 : : }
2005 : :
2006 : : #if MAPPED_WRITING
2007 : : /* Shrinking is a NOP. */
2008 : : void
2009 : 246882 : elf_out::shrink (char *)
2010 : : {
2011 : 246882 : }
2012 : : #endif
2013 : :
2014 : : /* Write S of length L to the strtab buffer. L must include the ending
2015 : : NUL, if that's what you want. */
2016 : :
2017 : : unsigned
2018 : 1067648 : elf_out::strtab_write (const char *s, unsigned l)
2019 : : {
2020 : 1067648 : if (strtab.pos + l > strtab.size)
2021 : 1053 : data::simple_memory.grow (strtab, strtab.pos + l, false);
2022 : 1067648 : memcpy (strtab.buffer + strtab.pos, s, l);
2023 : 1067648 : unsigned res = strtab.pos;
2024 : 1067648 : strtab.pos += l;
2025 : 1067648 : return res;
2026 : : }
2027 : :
2028 : : /* Write qualified name of decl. INNER >0 if this is a definition, <0
2029 : : if this is a qualifier of an outer name. */
2030 : :
2031 : : void
2032 : 420825 : elf_out::strtab_write (tree decl, int inner)
2033 : : {
2034 : 420825 : tree ctx = CP_DECL_CONTEXT (decl);
2035 : 420825 : if (TYPE_P (ctx))
2036 : 4483 : ctx = TYPE_NAME (ctx);
2037 : 420825 : if (ctx != global_namespace)
2038 : 207260 : strtab_write (ctx, -1);
2039 : :
2040 : 420825 : tree name = DECL_NAME (decl);
2041 : 420825 : if (!name)
2042 : 348 : name = DECL_ASSEMBLER_NAME_RAW (decl);
2043 : 420825 : strtab_write (IDENTIFIER_POINTER (name), IDENTIFIER_LENGTH (name));
2044 : :
2045 : 420825 : if (inner)
2046 : 297054 : strtab_write (&"::{}"[inner+1], 2);
2047 : 420825 : }
2048 : :
2049 : : /* Map IDENTIFIER IDENT to strtab offset. Inserts into strtab if not
2050 : : already there. */
2051 : :
2052 : : unsigned
2053 : 117933 : elf_out::name (tree ident)
2054 : : {
2055 : 117933 : unsigned res = 0;
2056 : 117933 : if (ident)
2057 : : {
2058 : 117893 : bool existed;
2059 : 117893 : int *slot = &identtab.get_or_insert (ident, &existed);
2060 : 117893 : if (!existed)
2061 : 211784 : *slot = strtab_write (IDENTIFIER_POINTER (ident),
2062 : 105892 : IDENTIFIER_LENGTH (ident) + 1);
2063 : 117893 : res = *slot;
2064 : : }
2065 : 117933 : return res;
2066 : : }
2067 : :
2068 : : /* Map LITERAL to strtab offset. Does not detect duplicates and
2069 : : expects LITERAL to remain live until strtab is written out. */
2070 : :
2071 : : unsigned
2072 : 30312 : elf_out::name (const char *literal)
2073 : : {
2074 : 30312 : return strtab_write (literal, strlen (literal) + 1);
2075 : : }
2076 : :
2077 : : /* Map a DECL's qualified name to strtab offset. Does not detect
2078 : : duplicates. */
2079 : :
2080 : : unsigned
2081 : 213565 : elf_out::qualified_name (tree decl, bool is_defn)
2082 : : {
2083 : 213565 : gcc_checking_assert (DECL_P (decl) && decl != global_namespace);
2084 : 213565 : unsigned result = strtab.pos;
2085 : :
2086 : 213565 : strtab_write (decl, is_defn);
2087 : 213565 : strtab_write ("", 1);
2088 : :
2089 : 213565 : return result;
2090 : : }
2091 : :
2092 : : /* Add section to file. Return section number. TYPE & NAME identify
2093 : : the section. OFF and SIZE identify the file location of its
2094 : : data. FLAGS contains additional info. */
2095 : :
2096 : : unsigned
2097 : 246876 : elf_out::add (unsigned type, unsigned name, unsigned off, unsigned size,
2098 : : unsigned flags)
2099 : : {
2100 : 246876 : gcc_checking_assert (!(off & (SECTION_ALIGN - 1)));
2101 : 246876 : if (sectab.pos + sizeof (section) > sectab.size)
2102 : 4107 : data::simple_memory.grow (sectab, sectab.pos + sizeof (section), false);
2103 : 246876 : section *sec = reinterpret_cast<section *> (sectab.buffer + sectab.pos);
2104 : 246876 : memset (sec, 0, sizeof (section));
2105 : 246876 : sec->type = type;
2106 : 246876 : sec->flags = flags;
2107 : 246876 : sec->name = name;
2108 : 246876 : sec->offset = off;
2109 : 246876 : sec->size = size;
2110 : 246876 : if (flags & SHF_STRINGS)
2111 : 4796 : sec->entsize = 1;
2112 : :
2113 : 246876 : unsigned res = sectab.pos;
2114 : 246876 : sectab.pos += sizeof (section);
2115 : 246876 : return res / sizeof (section);
2116 : : }
2117 : :
2118 : : /* Pad to the next alignment boundary, then write BUFFER to disk.
2119 : : Return the position of the start of the write, or zero on failure. */
2120 : :
2121 : : unsigned
2122 : 9654 : elf_out::write (const data &buffer)
2123 : : {
2124 : : #if MAPPED_WRITING
2125 : : /* HDR is always mapped. */
2126 : 9654 : if (&buffer != &hdr)
2127 : : {
2128 : 4830 : bytes_out out (this);
2129 : 4830 : grow (out, buffer.pos, true);
2130 : 4830 : if (out.buffer)
2131 : 4830 : memcpy (out.buffer, buffer.buffer, buffer.pos);
2132 : 4830 : shrink (out);
2133 : 4830 : }
2134 : : else
2135 : : /* We should have been aligned during the first allocation. */
2136 : 4824 : gcc_checking_assert (!(pos & (SECTION_ALIGN - 1)));
2137 : : #else
2138 : : if (::write (fd, buffer.buffer, buffer.pos) != ssize_t (buffer.pos))
2139 : : {
2140 : : set_error (errno);
2141 : : return 0;
2142 : : }
2143 : : #endif
2144 : 9654 : unsigned res = pos;
2145 : 9654 : pos += buffer.pos;
2146 : :
2147 : 9654 : if (unsigned padding = -pos & (SECTION_ALIGN - 1))
2148 : : {
2149 : : #if !MAPPED_WRITING
2150 : : /* Align the section on disk, should help the necessary copies.
2151 : : fseeking to extend is non-portable. */
2152 : : static char zero[SECTION_ALIGN];
2153 : : if (::write (fd, &zero, padding) != ssize_t (padding))
2154 : : set_error (errno);
2155 : : #endif
2156 : 8213 : pos += padding;
2157 : : }
2158 : 9654 : return res;
2159 : : }
2160 : :
2161 : : /* Write a streaming buffer. It must be using us as an allocator. */
2162 : :
2163 : : #if MAPPED_WRITING
2164 : : unsigned
2165 : 242052 : elf_out::write (const bytes_out &buf)
2166 : : {
2167 : 242052 : gcc_checking_assert (buf.memory == this);
2168 : : /* A directly mapped buffer. */
2169 : 242052 : gcc_checking_assert (buf.buffer - hdr.buffer >= 0
2170 : : && buf.buffer - hdr.buffer + buf.size <= extent);
2171 : 242052 : unsigned res = pos;
2172 : 242052 : pos += buf.pos;
2173 : :
2174 : : /* Align up. We're not going to advance into the next page. */
2175 : 242052 : pos += -pos & (SECTION_ALIGN - 1);
2176 : :
2177 : 242052 : return res;
2178 : : }
2179 : : #endif
2180 : :
2181 : : /* Write data and add section. STRING_P is true for a string
2182 : : section, false for PROGBITS. NAME identifies the section (0 is the
2183 : : empty name). DATA is the contents. Return section number or 0 on
2184 : : failure (0 is the undef section). */
2185 : :
2186 : : unsigned
2187 : 242052 : elf_out::add (const bytes_out &data, bool string_p, unsigned name)
2188 : : {
2189 : 242052 : unsigned off = write (data);
2190 : :
2191 : 484104 : return add (string_p ? SHT_STRTAB : SHT_PROGBITS, name,
2192 : 242052 : off, data.pos, string_p ? SHF_STRINGS : SHF_NONE);
2193 : : }
2194 : :
2195 : : /* Begin writing the file. Initialize the section table and write an
2196 : : empty header. Return false on failure. */
2197 : :
2198 : : bool
2199 : 2409 : elf_out::begin ()
2200 : : {
2201 : 2409 : if (!parent::begin ())
2202 : : return false;
2203 : :
2204 : : /* Let the allocators pick a default. */
2205 : 2409 : data::simple_memory.grow (strtab, 0, false);
2206 : 2409 : data::simple_memory.grow (sectab, 0, false);
2207 : :
2208 : : /* The string table starts with an empty string. */
2209 : 2409 : name ("");
2210 : :
2211 : : /* Create the UNDEF section. */
2212 : 2409 : add (SHT_NONE);
2213 : :
2214 : : #if MAPPED_WRITING
2215 : : /* Start a mapping. */
2216 : 2409 : create_mapping (EXPERIMENT (page_size,
2217 : : (32767 + page_size) & ~(page_size - 1)));
2218 : 2409 : if (!hdr.buffer)
2219 : : return false;
2220 : : #endif
2221 : :
2222 : : /* Write an empty header. */
2223 : 2409 : grow (hdr, sizeof (header), true);
2224 : 2409 : header *h = reinterpret_cast<header *> (hdr.buffer);
2225 : 2409 : memset (h, 0, sizeof (header));
2226 : 2409 : hdr.pos = hdr.size;
2227 : 2409 : write (hdr);
2228 : 2409 : return !get_error ();
2229 : : }
2230 : :
2231 : : /* Finish writing the file. Write out the string & section tables.
2232 : : Fill in the header. Return true on error. */
2233 : :
2234 : : bool
2235 : 2518 : elf_out::end ()
2236 : : {
2237 : 2518 : if (fd >= 0)
2238 : : {
2239 : : /* Write the string table. */
2240 : 2415 : unsigned strnam = name (".strtab");
2241 : 2415 : unsigned stroff = write (strtab);
2242 : 2415 : unsigned strndx = add (SHT_STRTAB, strnam, stroff, strtab.pos,
2243 : : SHF_STRINGS);
2244 : :
2245 : : /* Store escape values in section[0]. */
2246 : 2415 : if (strndx >= SHN_LORESERVE)
2247 : : {
2248 : 0 : reinterpret_cast<section *> (sectab.buffer)->link = strndx;
2249 : 0 : strndx = SHN_XINDEX;
2250 : : }
2251 : 2415 : unsigned shnum = sectab.pos / sizeof (section);
2252 : 2415 : if (shnum >= SHN_LORESERVE)
2253 : : {
2254 : 0 : reinterpret_cast<section *> (sectab.buffer)->size = shnum;
2255 : 0 : shnum = SHN_XINDEX;
2256 : : }
2257 : :
2258 : 2415 : unsigned shoff = write (sectab);
2259 : :
2260 : : #if MAPPED_WRITING
2261 : 2415 : if (offset)
2262 : : {
2263 : 124 : remove_mapping ();
2264 : 124 : offset = 0;
2265 : 124 : create_mapping ((sizeof (header) + page_size - 1) & ~(page_size - 1),
2266 : : false);
2267 : : }
2268 : 2415 : unsigned length = pos;
2269 : : #else
2270 : : if (lseek (fd, 0, SEEK_SET) < 0)
2271 : : set_error (errno);
2272 : : #endif
2273 : : /* Write header. */
2274 : 2415 : if (!get_error ())
2275 : : {
2276 : : /* Write the correct header now. */
2277 : 2415 : header *h = reinterpret_cast<header *> (hdr.buffer);
2278 : 2415 : h->ident.magic[0] = 0x7f;
2279 : 2415 : h->ident.magic[1] = 'E'; /* Elrond */
2280 : 2415 : h->ident.magic[2] = 'L'; /* is an */
2281 : 2415 : h->ident.magic[3] = 'F'; /* elf. */
2282 : 2415 : h->ident.klass = MY_CLASS;
2283 : 2415 : h->ident.data = MY_ENDIAN;
2284 : 2415 : h->ident.version = EV_CURRENT;
2285 : 2415 : h->ident.osabi = OSABI_NONE;
2286 : 2415 : h->type = ET_NONE;
2287 : 2415 : h->machine = EM_NONE;
2288 : 2415 : h->version = EV_CURRENT;
2289 : 2415 : h->shoff = shoff;
2290 : 2415 : h->ehsize = sizeof (header);
2291 : 2415 : h->shentsize = sizeof (section);
2292 : 2415 : h->shnum = shnum;
2293 : 2415 : h->shstrndx = strndx;
2294 : :
2295 : 2415 : pos = 0;
2296 : 2415 : write (hdr);
2297 : : }
2298 : :
2299 : : #if MAPPED_WRITING
2300 : 2415 : remove_mapping ();
2301 : 2415 : if (ftruncate (fd, length))
2302 : 0 : set_error (errno);
2303 : : #endif
2304 : : }
2305 : :
2306 : 2518 : data::simple_memory.shrink (sectab);
2307 : 2518 : data::simple_memory.shrink (strtab);
2308 : :
2309 : 2518 : return parent::end ();
2310 : : }
2311 : :
2312 : : /********************************************************************/
2313 : :
2314 : : /* A dependency set. This is used during stream out to determine the
2315 : : connectivity of the graph. Every namespace-scope declaration that
2316 : : needs writing has a depset. The depset is filled with the (depsets
2317 : : of) declarations within this module that it references. For a
2318 : : declaration that'll generally be named types. For definitions
2319 : : it'll also be declarations in the body.
2320 : :
2321 : : From that we can convert the graph to a DAG, via determining the
2322 : : Strongly Connected Clusters. Each cluster is streamed
2323 : : independently, and thus we achieve lazy loading.
2324 : :
2325 : : Other decls that get a depset are namespaces themselves and
2326 : : unnameable declarations. */
2327 : :
2328 : : class depset {
2329 : : private:
2330 : : tree entity; /* Entity, or containing namespace. */
2331 : : uintptr_t discriminator; /* Flags or identifier. */
2332 : :
2333 : : public:
2334 : : /* The kinds of entity the depset could describe. The ordering is
2335 : : significant, see entity_kind_name. */
2336 : : enum entity_kind
2337 : : {
2338 : : EK_DECL, /* A decl. */
2339 : : EK_SPECIALIZATION, /* A specialization. */
2340 : : EK_PARTIAL, /* A partial specialization. */
2341 : : EK_USING, /* A using declaration (at namespace scope). */
2342 : : EK_NAMESPACE, /* A namespace. */
2343 : : EK_REDIRECT, /* Redirect to a template_decl. */
2344 : : EK_EXPLICIT_HWM,
2345 : : EK_BINDING = EK_EXPLICIT_HWM, /* Implicitly encoded. */
2346 : : EK_FOR_BINDING, /* A decl being inserted for a binding. */
2347 : : EK_INNER_DECL, /* A decl defined outside of its imported
2348 : : context. */
2349 : : EK_DIRECT_HWM = EK_PARTIAL + 1,
2350 : :
2351 : : EK_BITS = 3 /* Only need to encode below EK_EXPLICIT_HWM. */
2352 : : };
2353 : :
2354 : : private:
2355 : : /* Placement of bit fields in discriminator. */
2356 : : enum disc_bits
2357 : : {
2358 : : DB_ZERO_BIT, /* Set to disambiguate identifier from flags */
2359 : : DB_SPECIAL_BIT, /* First dep slot is special. */
2360 : : DB_KIND_BIT, /* Kind of the entity. */
2361 : : DB_KIND_BITS = EK_BITS,
2362 : : DB_DEFN_BIT = DB_KIND_BIT + DB_KIND_BITS,
2363 : : DB_IS_PENDING_BIT, /* Is a maybe-pending entity. */
2364 : : DB_TU_LOCAL_BIT, /* It is a TU-local entity. */
2365 : : DB_REFS_TU_LOCAL_BIT, /* Refers to a TU-local entity (but is not
2366 : : necessarily an exposure.) */
2367 : : DB_EXPOSURE_BIT, /* Exposes a TU-local entity. */
2368 : : DB_IMPORTED_BIT, /* An imported entity. */
2369 : : DB_UNREACHED_BIT, /* A yet-to-be reached entity. */
2370 : : DB_MAYBE_RECURSIVE_BIT, /* An entity maybe in a recursive cluster. */
2371 : : DB_ENTRY_BIT, /* The first reached recursive dep. */
2372 : : DB_HIDDEN_BIT, /* A hidden binding. */
2373 : : /* The following bits are not independent, but enumerating them is
2374 : : awkward. */
2375 : : DB_TYPE_SPEC_BIT, /* Specialization in the type table. */
2376 : : DB_FRIEND_SPEC_BIT, /* An instantiated template friend. */
2377 : : };
2378 : :
2379 : : public:
2380 : : /* The first slot is special for EK_SPECIALIZATIONS it is a
2381 : : spec_entry pointer. It is not relevant for the SCC
2382 : : determination. */
2383 : : vec<depset *> deps; /* Depsets we reference. */
2384 : :
2385 : : public:
2386 : : unsigned cluster; /* Strongly connected cluster, later entity number */
2387 : : unsigned section; /* Section written to. */
2388 : : /* During SCC construction, section is lowlink, until the depset is
2389 : : removed from the stack. See Tarjan algorithm for details. */
2390 : :
2391 : : private:
2392 : : /* Construction via factories. Destruction via hash traits. */
2393 : : depset (tree entity);
2394 : : ~depset ();
2395 : :
2396 : : public:
2397 : : static depset *make_binding (tree, tree);
2398 : : static depset *make_entity (tree, entity_kind, bool = false);
2399 : : /* Late setting a binding name -- /then/ insert into hash! */
2400 : : inline void set_binding_name (tree name)
2401 : : {
2402 : : gcc_checking_assert (!get_name ());
2403 : : discriminator = reinterpret_cast<uintptr_t> (name);
2404 : : }
2405 : :
2406 : : private:
2407 : 3295238 : template<unsigned I> void set_flag_bit ()
2408 : : {
2409 : 0 : gcc_checking_assert (I < 2 || !is_binding ());
2410 : 3295238 : discriminator |= 1u << I;
2411 : 1986085 : }
2412 : 284779 : template<unsigned I> void clear_flag_bit ()
2413 : : {
2414 : 0 : gcc_checking_assert (I < 2 || !is_binding ());
2415 : 284779 : discriminator &= ~(1u << I);
2416 : 284779 : }
2417 : 351037776 : template<unsigned I> bool get_flag_bit () const
2418 : : {
2419 : 0 : gcc_checking_assert (I < 2 || !is_binding ());
2420 : 421871433 : return bool ((discriminator >> I) & 1);
2421 : : }
2422 : :
2423 : : public:
2424 : 343760470 : bool is_binding () const
2425 : : {
2426 : 73104521 : return !get_flag_bit<DB_ZERO_BIT> ();
2427 : : }
2428 : 187512470 : entity_kind get_entity_kind () const
2429 : : {
2430 : 13447 : if (is_binding ())
2431 : : return EK_BINDING;
2432 : 142030659 : return entity_kind ((discriminator >> DB_KIND_BIT) & ((1u << EK_BITS) - 1));
2433 : : }
2434 : : const char *entity_kind_name () const;
2435 : :
2436 : : public:
2437 : 6289121 : bool has_defn () const
2438 : : {
2439 : : /* Never consider TU-local entities as having definitions, since
2440 : : we will never be accessing them from importers anyway. */
2441 : 8296458 : return get_flag_bit<DB_DEFN_BIT> () && !is_tu_local ();
2442 : : }
2443 : :
2444 : : public:
2445 : : /* This entity might be found other than by namespace-scope lookup;
2446 : : see module_state::write_pendings for more details. */
2447 : 1546827 : bool is_pending_entity () const
2448 : : {
2449 : 2472005 : return (get_entity_kind () == EK_SPECIALIZATION
2450 : 925178 : || get_entity_kind () == EK_PARTIAL
2451 : 2441799 : || (get_entity_kind () == EK_DECL
2452 : 877460 : && get_flag_bit<DB_IS_PENDING_BIT> ()));
2453 : : }
2454 : : public:
2455 : 28352026 : bool is_tu_local () const
2456 : : {
2457 : 16481051 : return get_flag_bit<DB_TU_LOCAL_BIT> ();
2458 : : }
2459 : 648045 : bool refs_tu_local () const
2460 : : {
2461 : 80590 : return get_flag_bit<DB_REFS_TU_LOCAL_BIT> ();
2462 : : }
2463 : 1040202 : bool is_exposure () const
2464 : : {
2465 : 1040202 : return get_flag_bit<DB_EXPOSURE_BIT> ();
2466 : : }
2467 : 18758744 : bool is_import () const
2468 : : {
2469 : 5840282 : return get_flag_bit<DB_IMPORTED_BIT> ();
2470 : : }
2471 : 11423823 : bool is_unreached () const
2472 : : {
2473 : 777820 : return get_flag_bit<DB_UNREACHED_BIT> ();
2474 : : }
2475 : 939375 : bool is_hidden () const
2476 : : {
2477 : 939375 : return get_flag_bit<DB_HIDDEN_BIT> ();
2478 : : }
2479 : 639227 : bool is_maybe_recursive () const
2480 : : {
2481 : 639227 : return get_flag_bit<DB_MAYBE_RECURSIVE_BIT> ();
2482 : : }
2483 : 822 : bool is_entry () const
2484 : : {
2485 : 822 : return get_flag_bit<DB_ENTRY_BIT> ();
2486 : : }
2487 : 932406 : bool is_type_spec () const
2488 : : {
2489 : 932406 : return get_flag_bit<DB_TYPE_SPEC_BIT> ();
2490 : : }
2491 : 932406 : bool is_friend_spec () const
2492 : : {
2493 : 932406 : return get_flag_bit<DB_FRIEND_SPEC_BIT> ();
2494 : : }
2495 : :
2496 : : public:
2497 : : /* We set these bit outside of depset. */
2498 : 3754 : void set_hidden_binding ()
2499 : : {
2500 : 3754 : set_flag_bit<DB_HIDDEN_BIT> ();
2501 : 3754 : }
2502 : 30 : void clear_hidden_binding ()
2503 : : {
2504 : 30 : clear_flag_bit<DB_HIDDEN_BIT> ();
2505 : 30 : }
2506 : :
2507 : : public:
2508 : 7277306 : bool is_special () const
2509 : : {
2510 : 7277306 : return get_flag_bit<DB_SPECIAL_BIT> ();
2511 : : }
2512 : 1309153 : void set_special ()
2513 : : {
2514 : 1309153 : set_flag_bit<DB_SPECIAL_BIT> ();
2515 : 0 : }
2516 : :
2517 : : public:
2518 : 85486778 : tree get_entity () const
2519 : : {
2520 : 85486778 : return entity;
2521 : : }
2522 : 15802988 : tree get_name () const
2523 : : {
2524 : 15802988 : gcc_checking_assert (is_binding ());
2525 : 15802988 : return reinterpret_cast <tree> (discriminator);
2526 : : }
2527 : :
2528 : : public:
2529 : : /* Traits for a hash table of pointers to bindings. */
2530 : : struct traits {
2531 : : /* Each entry is a pointer to a depset. */
2532 : : typedef depset *value_type;
2533 : : /* We lookup by container:maybe-identifier pair. */
2534 : : typedef std::pair<tree,tree> compare_type;
2535 : :
2536 : : static const bool empty_zero_p = true;
2537 : :
2538 : : /* hash and equality for compare_type. */
2539 : 11807014 : inline static hashval_t hash (const compare_type &p)
2540 : : {
2541 : 11807014 : hashval_t h = pointer_hash<tree_node>::hash (p.first);
2542 : 11807014 : if (p.second)
2543 : : {
2544 : 149413 : hashval_t nh = IDENTIFIER_HASH_VALUE (p.second);
2545 : 149413 : h = iterative_hash_hashval_t (h, nh);
2546 : : }
2547 : 11807014 : return h;
2548 : : }
2549 : 56638579 : inline static bool equal (const value_type b, const compare_type &p)
2550 : : {
2551 : 56638579 : if (b->entity != p.first)
2552 : : return false;
2553 : :
2554 : 8561578 : if (p.second)
2555 : 22889 : return b->discriminator == reinterpret_cast<uintptr_t> (p.second);
2556 : : else
2557 : 8538689 : return !b->is_binding ();
2558 : : }
2559 : :
2560 : : /* (re)hasher for a binding itself. */
2561 : 43556764 : inline static hashval_t hash (const value_type b)
2562 : : {
2563 : 43556764 : hashval_t h = pointer_hash<tree_node>::hash (b->entity);
2564 : 43556764 : if (b->is_binding ())
2565 : : {
2566 : 4167076 : hashval_t nh = IDENTIFIER_HASH_VALUE (b->get_name ());
2567 : 4167076 : h = iterative_hash_hashval_t (h, nh);
2568 : : }
2569 : 43556764 : return h;
2570 : : }
2571 : :
2572 : : /* Empty via NULL. */
2573 : 0 : static inline void mark_empty (value_type &p) {p = NULL;}
2574 : : static inline bool is_empty (value_type p) {return !p;}
2575 : :
2576 : : /* Nothing is deletable. Everything is insertable. */
2577 : : static bool is_deleted (value_type) { return false; }
2578 : : static void mark_deleted (value_type) { gcc_unreachable (); }
2579 : :
2580 : : /* We own the entities in the hash table. */
2581 : 1920783 : static void remove (value_type p)
2582 : : {
2583 : 1920783 : delete (p);
2584 : 1920783 : }
2585 : : };
2586 : :
2587 : : public:
2588 : : class hash : public hash_table<traits> {
2589 : : typedef traits::compare_type key_t;
2590 : : typedef hash_table<traits> parent;
2591 : :
2592 : : public:
2593 : : vec<depset *> worklist; /* Worklist of decls to walk. */
2594 : : hash *chain; /* Original table. */
2595 : : depset *current; /* Current depset being depended. */
2596 : : unsigned section; /* When writing out, the section. */
2597 : : bool reached_unreached; /* We reached an unreached entity. */
2598 : : bool writing_merge_key; /* We're writing merge key information. */
2599 : : bool ignore_tu_local; /* In a context where referencing a TU-local
2600 : : entity is not an exposure. */
2601 : :
2602 : : public:
2603 : 223735 : hash (size_t size, hash *c = NULL)
2604 : 447470 : : parent (size), chain (c), current (NULL), section (0),
2605 : 223735 : reached_unreached (false), writing_merge_key (false),
2606 : 223735 : ignore_tu_local (false)
2607 : : {
2608 : 223735 : worklist.create (size);
2609 : 223735 : }
2610 : 223735 : ~hash ()
2611 : : {
2612 : 2409 : worklist.release ();
2613 : 223735 : }
2614 : :
2615 : : public:
2616 : 66214528 : bool is_key_order () const
2617 : : {
2618 : 66214528 : return chain != NULL;
2619 : : }
2620 : :
2621 : : private:
2622 : : depset **entity_slot (tree entity, bool = true);
2623 : : depset **binding_slot (tree ctx, tree name, bool = true);
2624 : : depset *maybe_add_declaration (tree decl);
2625 : :
2626 : : public:
2627 : : depset *find_dependency (tree entity);
2628 : : depset *find_binding (tree ctx, tree name);
2629 : : depset *make_dependency (tree decl, entity_kind);
2630 : : void add_dependency (depset *);
2631 : :
2632 : : public:
2633 : : void add_mergeable (depset *);
2634 : : depset *add_dependency (tree decl, entity_kind);
2635 : : void add_namespace_context (depset *, tree ns);
2636 : :
2637 : : private:
2638 : : bool has_tu_local_tmpl_arg (tree decl, tree args, bool explain);
2639 : : bool is_tu_local_entity (tree decl, bool explain = false);
2640 : : bool is_tu_local_value (tree decl, tree expr, bool explain = false);
2641 : :
2642 : : private:
2643 : : static bool add_binding_entity (tree, WMB_Flags, void *);
2644 : :
2645 : : public:
2646 : : bool add_namespace_entities (tree ns, bitmap partitions);
2647 : : void add_specializations (bool decl_p);
2648 : : void add_partial_entities (vec<tree, va_gc> *);
2649 : : void add_class_entities (vec<tree, va_gc> *);
2650 : :
2651 : : private:
2652 : : void add_deduction_guides (tree decl);
2653 : :
2654 : : public:
2655 : : void find_dependencies (module_state *);
2656 : : bool finalize_dependencies ();
2657 : : vec<depset *> connect ();
2658 : : };
2659 : :
2660 : : public:
2661 : : struct tarjan {
2662 : : vec<depset *> result;
2663 : : vec<depset *> stack;
2664 : : unsigned index;
2665 : :
2666 : 223707 : tarjan (unsigned size)
2667 : 223707 : : index (0)
2668 : : {
2669 : 223707 : result.create (size);
2670 : 223707 : stack.create (50);
2671 : 223707 : }
2672 : 223707 : ~tarjan ()
2673 : : {
2674 : 223707 : gcc_assert (!stack.length ());
2675 : 223707 : stack.release ();
2676 : 223707 : }
2677 : :
2678 : : public:
2679 : : void connect (depset *);
2680 : : };
2681 : : };
2682 : :
2683 : : inline
2684 : 1920783 : depset::depset (tree entity)
2685 : 1920783 : :entity (entity), discriminator (0), cluster (0), section (0)
2686 : : {
2687 : 1920783 : deps.create (0);
2688 : : }
2689 : :
2690 : : inline
2691 : 1920783 : depset::~depset ()
2692 : : {
2693 : 1920783 : deps.release ();
2694 : 1920783 : }
2695 : :
2696 : : const char *
2697 : 39722 : depset::entity_kind_name () const
2698 : : {
2699 : : /* Same order as entity_kind. */
2700 : 39722 : static const char *const names[] =
2701 : : {"decl", "specialization", "partial", "using",
2702 : : "namespace", "redirect", "binding"};
2703 : 39722 : entity_kind kind = get_entity_kind ();
2704 : 39527 : gcc_checking_assert (kind < ARRAY_SIZE (names));
2705 : 39722 : return names[kind];
2706 : : }
2707 : :
2708 : : /* Create a depset for a namespace binding NS::NAME. */
2709 : :
2710 : 116208 : depset *depset::make_binding (tree ns, tree name)
2711 : : {
2712 : 116208 : depset *binding = new depset (ns);
2713 : :
2714 : 116208 : binding->discriminator = reinterpret_cast <uintptr_t> (name);
2715 : :
2716 : 116208 : return binding;
2717 : : }
2718 : :
2719 : 1804575 : depset *depset::make_entity (tree entity, entity_kind ek, bool is_defn)
2720 : : {
2721 : 1804575 : depset *r = new depset (entity);
2722 : :
2723 : 1804575 : r->discriminator = ((1 << DB_ZERO_BIT)
2724 : 1804575 : | (ek << DB_KIND_BIT)
2725 : 1804575 : | is_defn << DB_DEFN_BIT);
2726 : :
2727 : 1804575 : return r;
2728 : : }
2729 : :
2730 : : class pending_key
2731 : : {
2732 : : public:
2733 : : tree ns;
2734 : : tree id;
2735 : : };
2736 : :
2737 : : template<>
2738 : : struct default_hash_traits<pending_key>
2739 : : {
2740 : : using value_type = pending_key;
2741 : :
2742 : : static const bool empty_zero_p = false;
2743 : 32693784 : static hashval_t hash (const value_type &k)
2744 : : {
2745 : 32693784 : hashval_t h = IDENTIFIER_HASH_VALUE (k.id);
2746 : 32693784 : h = iterative_hash_hashval_t (DECL_UID (k.ns), h);
2747 : :
2748 : 32693784 : return h;
2749 : : }
2750 : 10849837 : static bool equal (const value_type &k, const value_type &l)
2751 : : {
2752 : 10849837 : return k.ns == l.ns && k.id == l.id;
2753 : : }
2754 : 174307 : static void mark_empty (value_type &k)
2755 : : {
2756 : 174307 : k.ns = k.id = NULL_TREE;
2757 : : }
2758 : 4742 : static void mark_deleted (value_type &k)
2759 : : {
2760 : 4742 : k.ns = NULL_TREE;
2761 : 4742 : gcc_checking_assert (k.id);
2762 : 4742 : }
2763 : 242357590 : static bool is_empty (const value_type &k)
2764 : : {
2765 : 242317686 : return k.ns == NULL_TREE && k.id == NULL_TREE;
2766 : : }
2767 : 15498579 : static bool is_deleted (const value_type &k)
2768 : : {
2769 : 15498579 : return k.ns == NULL_TREE && k.id != NULL_TREE;
2770 : : }
2771 : : static void remove (value_type &)
2772 : : {
2773 : : }
2774 : : };
2775 : :
2776 : : typedef hash_map<pending_key, auto_vec<unsigned>> pending_map_t;
2777 : :
2778 : : /* Not-loaded entities that are keyed to a namespace-scope
2779 : : identifier. See module_state::write_pendings for details. */
2780 : : pending_map_t *pending_table;
2781 : :
2782 : : /* Decls that need some post processing once a batch of lazy loads has
2783 : : completed. */
2784 : : vec<tree, va_heap, vl_embed> *post_load_decls;
2785 : :
2786 : : /* Some entities are keyed to another entitity for ODR purposes.
2787 : : For example, at namespace scope, 'inline auto var = []{};', that
2788 : : lambda is keyed to 'var', and follows its ODRness. */
2789 : : typedef hash_map<tree, auto_vec<tree>> keyed_map_t;
2790 : : static keyed_map_t *keyed_table;
2791 : :
2792 : : /* Instantiations of temploid friends imported from another module
2793 : : need to be attached to the same module as the temploid. This maps
2794 : : these decls to the temploid they are instantiated from, as there is
2795 : : no other easy way to get this information. */
2796 : : static GTY((cache)) decl_tree_cache_map *imported_temploid_friends;
2797 : :
2798 : : /********************************************************************/
2799 : : /* Tree streaming. The tree streaming is very specific to the tree
2800 : : structures themselves. A tag indicates the kind of tree being
2801 : : streamed. -ve tags indicate backreferences to already-streamed
2802 : : trees. Backreferences are auto-numbered. */
2803 : :
2804 : : /* Tree tags. */
2805 : : enum tree_tag {
2806 : : tt_null, /* NULL_TREE. */
2807 : : tt_tu_local, /* A TU-local entity. */
2808 : : tt_fixed, /* Fixed vector index. */
2809 : :
2810 : : tt_node, /* By-value node. */
2811 : : tt_decl, /* By-value mergeable decl. */
2812 : : tt_tpl_parm, /* Template parm. */
2813 : :
2814 : : /* The ordering of the following 4 is relied upon in
2815 : : trees_out::tree_node. */
2816 : : tt_id, /* Identifier node. */
2817 : : tt_conv_id, /* Conversion operator name. */
2818 : : tt_anon_id, /* Anonymous name. */
2819 : : tt_lambda_id, /* Lambda name. */
2820 : :
2821 : : tt_typedef_type, /* A (possibly implicit) typedefed type. */
2822 : : tt_derived_type, /* A type derived from another type. */
2823 : : tt_variant_type, /* A variant of another type. */
2824 : :
2825 : : tt_tinfo_var, /* Typeinfo object. */
2826 : : tt_tinfo_typedef, /* Typeinfo typedef. */
2827 : : tt_ptrmem_type, /* Pointer to member type. */
2828 : : tt_nttp_var, /* NTTP_OBJECT VAR_DECL. */
2829 : :
2830 : : tt_parm, /* Function parameter or result. */
2831 : : tt_enum_value, /* An enum value. */
2832 : : tt_enum_decl, /* An enum decl. */
2833 : : tt_data_member, /* Data member/using-decl. */
2834 : :
2835 : : tt_binfo, /* A BINFO. */
2836 : : tt_vtable, /* A vtable. */
2837 : : tt_thunk, /* A thunk. */
2838 : : tt_clone_ref,
2839 : :
2840 : : tt_entity, /* A extra-cluster entity. */
2841 : :
2842 : : tt_template, /* The TEMPLATE_RESULT of a template. */
2843 : : };
2844 : :
2845 : : enum walk_kind {
2846 : : WK_none, /* No walk to do (a back- or fixed-ref happened). */
2847 : : WK_normal, /* Normal walk (by-name if possible). */
2848 : :
2849 : : WK_value, /* By-value walk. */
2850 : : };
2851 : :
2852 : : enum merge_kind
2853 : : {
2854 : : MK_unique, /* Known unique. */
2855 : : MK_named, /* Found by CTX, NAME + maybe_arg types etc. */
2856 : : MK_field, /* Found by CTX and index on TYPE_FIELDS */
2857 : : MK_vtable, /* Found by CTX and index on TYPE_VTABLES */
2858 : : MK_as_base, /* Found by CTX. */
2859 : :
2860 : : MK_partial,
2861 : :
2862 : : MK_enum, /* Found by CTX, & 1stMemberNAME. */
2863 : : MK_keyed, /* Found by key & index. */
2864 : : MK_local_type, /* Found by CTX, index. */
2865 : :
2866 : : MK_friend_spec, /* Like named, but has a tmpl & args too. */
2867 : : MK_local_friend, /* Found by CTX, index. */
2868 : :
2869 : : MK_indirect_lwm = MK_enum,
2870 : :
2871 : : /* Template specialization kinds below. These are all found via
2872 : : primary template and specialization args. */
2873 : : MK_template_mask = 0x10, /* A template specialization. */
2874 : :
2875 : : MK_tmpl_decl_mask = 0x4, /* In decl table. */
2876 : :
2877 : : MK_tmpl_tmpl_mask = 0x1, /* We want TEMPLATE_DECL. */
2878 : :
2879 : : MK_type_spec = MK_template_mask,
2880 : : MK_decl_spec = MK_template_mask | MK_tmpl_decl_mask,
2881 : :
2882 : : MK_hwm = 0x20
2883 : : };
2884 : : /* This is more than a debugging array. NULLs are used to determine
2885 : : an invalid merge_kind number. */
2886 : : static char const *const merge_kind_name[MK_hwm] =
2887 : : {
2888 : : "unique", "named", "field", "vtable", /* 0...3 */
2889 : : "asbase", "partial", "enum", "attached", /* 4...7 */
2890 : :
2891 : : "local type", "friend spec", "local friend", NULL, /* 8...11 */
2892 : : NULL, NULL, NULL, NULL,
2893 : :
2894 : : "type spec", "type tmpl spec", /* 16,17 type (template). */
2895 : : NULL, NULL,
2896 : :
2897 : : "decl spec", "decl tmpl spec", /* 20,21 decl (template). */
2898 : : NULL, NULL,
2899 : : NULL, NULL, NULL, NULL,
2900 : : NULL, NULL, NULL, NULL,
2901 : : };
2902 : :
2903 : : /* Mergeable entity location data. */
2904 : : struct merge_key {
2905 : : cp_ref_qualifier ref_q : 2;
2906 : : unsigned index;
2907 : :
2908 : : tree ret; /* Return type, if appropriate. */
2909 : : tree args; /* Arg types, if appropriate. */
2910 : :
2911 : : tree constraints; /* Constraints. */
2912 : :
2913 : 2031685 : merge_key ()
2914 : 2031685 : :ref_q (REF_QUAL_NONE), index (0),
2915 : 2031685 : ret (NULL_TREE), args (NULL_TREE),
2916 : 2031685 : constraints (NULL_TREE)
2917 : : {
2918 : : }
2919 : : };
2920 : :
2921 : : /* Hashmap of merged duplicates. Usually decls, but can contain
2922 : : BINFOs. */
2923 : : typedef hash_map<tree,uintptr_t,
2924 : : simple_hashmap_traits<nodel_ptr_hash<tree_node>,uintptr_t> >
2925 : : duplicate_hash_map;
2926 : :
2927 : : /* Data needed for post-processing. */
2928 : : struct post_process_data {
2929 : : tree decl;
2930 : : location_t start_locus;
2931 : : location_t end_locus;
2932 : : bool returns_value;
2933 : : bool returns_null;
2934 : : bool returns_abnormally;
2935 : : bool infinite_loop;
2936 : : };
2937 : :
2938 : : /* Tree stream reader. Note that reading a stream doesn't mark the
2939 : : read trees with TREE_VISITED. Thus it's quite safe to have
2940 : : multiple concurrent readers. Which is good, because lazy
2941 : : loading.
2942 : :
2943 : : It's important that trees_in/out have internal linkage so that the
2944 : : compiler knows core_bools, lang_type_bools and lang_decl_bools have
2945 : : only a single caller (tree_node_bools) and inlines them appropriately. */
2946 : : namespace {
2947 : : class trees_in : public bytes_in {
2948 : : typedef bytes_in parent;
2949 : :
2950 : : private:
2951 : : module_state *state; /* Module being imported. */
2952 : : vec<tree> back_refs; /* Back references. */
2953 : : duplicate_hash_map *duplicates; /* Map from existings to duplicate. */
2954 : : vec<post_process_data> post_decls; /* Decls to post process. */
2955 : : unsigned unused; /* Inhibit any interior TREE_USED
2956 : : marking. */
2957 : :
2958 : : public:
2959 : : trees_in (module_state *);
2960 : : ~trees_in ();
2961 : :
2962 : : public:
2963 : : int insert (tree);
2964 : : tree back_ref (int);
2965 : :
2966 : : private:
2967 : : tree start (unsigned = 0);
2968 : :
2969 : : public:
2970 : : /* Needed for binfo writing */
2971 : : bool core_bools (tree, bits_in&);
2972 : :
2973 : : private:
2974 : : /* Stream tree_core, lang_decl_specific and lang_type_specific
2975 : : bits. */
2976 : : bool core_vals (tree);
2977 : : bool lang_type_bools (tree, bits_in&);
2978 : : bool lang_type_vals (tree);
2979 : : bool lang_decl_bools (tree, bits_in&);
2980 : : bool lang_decl_vals (tree);
2981 : : bool lang_vals (tree);
2982 : : bool tree_node_bools (tree);
2983 : : bool tree_node_vals (tree);
2984 : : tree tree_value ();
2985 : : tree decl_value ();
2986 : : tree tpl_parm_value ();
2987 : :
2988 : : private:
2989 : : tree chained_decls (); /* Follow DECL_CHAIN. */
2990 : : vec<tree, va_heap> *vec_chained_decls ();
2991 : : vec<tree, va_gc> *tree_vec (); /* vec of tree. */
2992 : : vec<tree_pair_s, va_gc> *tree_pair_vec (); /* vec of tree_pair. */
2993 : : tree tree_list (bool has_purpose);
2994 : :
2995 : : public:
2996 : : /* Read a tree node. */
2997 : : tree tree_node (bool is_use = false);
2998 : :
2999 : : private:
3000 : : bool install_entity (tree decl);
3001 : : tree tpl_parms (unsigned &tpl_levels);
3002 : : bool tpl_parms_fini (tree decl, unsigned tpl_levels);
3003 : : bool tpl_header (tree decl, unsigned *tpl_levels);
3004 : : int fn_parms_init (tree);
3005 : : void fn_parms_fini (int tag, tree fn, tree existing, bool has_defn);
3006 : : unsigned add_indirect_tpl_parms (tree);
3007 : : public:
3008 : : bool add_indirects (tree);
3009 : :
3010 : : public:
3011 : : /* Serialize various definitions. */
3012 : : bool read_definition (tree decl);
3013 : :
3014 : : private:
3015 : : void check_abi_tags (tree existing, tree decl, tree &eattr, tree &dattr);
3016 : : bool is_matching_decl (tree existing, tree decl, bool is_typedef);
3017 : : static bool install_implicit_member (tree decl);
3018 : : bool read_function_def (tree decl, tree maybe_template);
3019 : : bool read_var_def (tree decl, tree maybe_template);
3020 : : bool read_class_def (tree decl, tree maybe_template);
3021 : : bool read_enum_def (tree decl, tree maybe_template);
3022 : :
3023 : : public:
3024 : : tree decl_container ();
3025 : : tree key_mergeable (int tag, merge_kind, tree decl, tree inner, tree type,
3026 : : tree container, bool is_attached,
3027 : : bool is_imported_temploid_friend);
3028 : : unsigned binfo_mergeable (tree *);
3029 : :
3030 : : private:
3031 : : tree key_local_type (const merge_key&, tree, tree);
3032 : : uintptr_t *find_duplicate (tree existing);
3033 : : void register_duplicate (tree decl, tree existing);
3034 : : /* Mark as an already diagnosed bad duplicate. */
3035 : 48 : void unmatched_duplicate (tree existing)
3036 : : {
3037 : 96 : *find_duplicate (existing) |= 1;
3038 : 48 : }
3039 : :
3040 : : public:
3041 : 123746 : bool is_duplicate (tree decl)
3042 : : {
3043 : 247492 : return find_duplicate (decl) != NULL;
3044 : : }
3045 : 111994 : tree maybe_duplicate (tree decl)
3046 : : {
3047 : 111994 : if (uintptr_t *dup = find_duplicate (decl))
3048 : 8435 : return reinterpret_cast<tree> (*dup & ~uintptr_t (1));
3049 : : return decl;
3050 : : }
3051 : : tree odr_duplicate (tree decl, bool has_defn);
3052 : :
3053 : : public:
3054 : : /* Return the decls to postprocess. */
3055 : : const vec<post_process_data>& post_process ()
3056 : : {
3057 : : return post_decls;
3058 : : }
3059 : : private:
3060 : : /* Register DATA for postprocessing. */
3061 : 70804 : void post_process (post_process_data data)
3062 : : {
3063 : 70804 : post_decls.safe_push (data);
3064 : : }
3065 : :
3066 : : private:
3067 : : void assert_definition (tree, bool installing);
3068 : : };
3069 : : } // anon namespace
3070 : :
3071 : 167600 : trees_in::trees_in (module_state *state)
3072 : 167600 : :parent (), state (state), unused (0)
3073 : : {
3074 : 167600 : duplicates = NULL;
3075 : 167600 : back_refs.create (500);
3076 : 167600 : post_decls.create (0);
3077 : 167600 : }
3078 : :
3079 : 167600 : trees_in::~trees_in ()
3080 : : {
3081 : 261800 : delete (duplicates);
3082 : 167600 : back_refs.release ();
3083 : 167600 : post_decls.release ();
3084 : 167600 : }
3085 : :
3086 : : /* Tree stream writer. */
3087 : : namespace {
3088 : : class trees_out : public bytes_out {
3089 : : typedef bytes_out parent;
3090 : :
3091 : : private:
3092 : : module_state *state; /* The module we are writing. */
3093 : : ptr_int_hash_map tree_map; /* Trees to references */
3094 : : depset::hash *dep_hash; /* Dependency table. */
3095 : : int ref_num; /* Back reference number. */
3096 : : unsigned section;
3097 : : bool writing_local_entities; /* Whether we might walk into a TU-local
3098 : : entity we need to emit placeholders for. */
3099 : : #if CHECKING_P
3100 : : int importedness; /* Checker that imports not occurring
3101 : : inappropriately. +ve imports ok,
3102 : : -ve imports not ok. */
3103 : : #endif
3104 : :
3105 : : public:
3106 : : trees_out (allocator *, module_state *, depset::hash &deps, unsigned sec = 0);
3107 : : ~trees_out ();
3108 : :
3109 : : private:
3110 : : void mark_trees ();
3111 : : void unmark_trees ();
3112 : :
3113 : : public:
3114 : : /* Hey, let's ignore the well known STL iterator idiom. */
3115 : : void begin ();
3116 : : unsigned end (elf_out *sink, unsigned name, unsigned *crc_ptr);
3117 : : void end ();
3118 : :
3119 : : public:
3120 : : enum tags
3121 : : {
3122 : : tag_backref = -1, /* Upper bound on the backrefs. */
3123 : : tag_value = 0, /* Write by value. */
3124 : : tag_fixed /* Lower bound on the fixed trees. */
3125 : : };
3126 : :
3127 : : public:
3128 : : /* The walk is used for three similar purposes:
3129 : :
3130 : : 1. The initial scan for dependencies.
3131 : : 2. Once dependencies have been found, ordering them.
3132 : : 3. Writing dependencies to file (streaming_p).
3133 : :
3134 : : For cases where it matters, these accessers can be used to determine
3135 : : which state we're in. */
3136 : 46858367 : bool is_initial_scan () const
3137 : : {
3138 : 79152830 : return !streaming_p () && !is_key_order ();
3139 : : }
3140 : 43862356 : bool is_key_order () const
3141 : : {
3142 : 32294463 : return dep_hash->is_key_order ();
3143 : : }
3144 : :
3145 : : public:
3146 : : int insert (tree, walk_kind = WK_normal);
3147 : :
3148 : : private:
3149 : : void start (tree, bool = false);
3150 : :
3151 : : private:
3152 : : walk_kind ref_node (tree);
3153 : : public:
3154 : : int get_tag (tree);
3155 : 442652 : void set_importing (int i ATTRIBUTE_UNUSED)
3156 : : {
3157 : : #if CHECKING_P
3158 : 442652 : importedness = i;
3159 : : #endif
3160 : : }
3161 : :
3162 : : private:
3163 : : void core_bools (tree, bits_out&);
3164 : : void core_vals (tree);
3165 : : void lang_type_bools (tree, bits_out&);
3166 : : void lang_type_vals (tree);
3167 : : void lang_decl_bools (tree, bits_out&);
3168 : : void lang_decl_vals (tree);
3169 : : void lang_vals (tree);
3170 : : void tree_node_bools (tree);
3171 : : void tree_node_vals (tree);
3172 : :
3173 : : private:
3174 : : void chained_decls (tree);
3175 : : void vec_chained_decls (tree);
3176 : : void tree_vec (vec<tree, va_gc> *);
3177 : : void tree_pair_vec (vec<tree_pair_s, va_gc> *);
3178 : : void tree_list (tree, bool has_purpose);
3179 : :
3180 : : private:
3181 : : bool has_tu_local_dep (tree) const;
3182 : : tree find_tu_local_decl (tree);
3183 : :
3184 : : public:
3185 : : /* Mark a node for by-value walking. */
3186 : : void mark_by_value (tree);
3187 : :
3188 : : public:
3189 : : void tree_node (tree);
3190 : :
3191 : : private:
3192 : : void install_entity (tree decl, depset *);
3193 : : void tpl_parms (tree parms, unsigned &tpl_levels);
3194 : : void tpl_parms_fini (tree decl, unsigned tpl_levels);
3195 : : void fn_parms_fini (tree) {}
3196 : : unsigned add_indirect_tpl_parms (tree);
3197 : : public:
3198 : : void add_indirects (tree);
3199 : : void fn_parms_init (tree);
3200 : : void tpl_header (tree decl, unsigned *tpl_levels);
3201 : :
3202 : : public:
3203 : : merge_kind get_merge_kind (tree decl, depset *maybe_dep);
3204 : : tree decl_container (tree decl);
3205 : : void key_mergeable (int tag, merge_kind, tree decl, tree inner,
3206 : : tree container, depset *maybe_dep);
3207 : : void binfo_mergeable (tree binfo);
3208 : :
3209 : : private:
3210 : : void key_local_type (merge_key&, tree, tree);
3211 : : bool decl_node (tree, walk_kind ref);
3212 : : void type_node (tree);
3213 : : void tree_value (tree);
3214 : : void tpl_parm_value (tree);
3215 : :
3216 : : public:
3217 : : void decl_value (tree, depset *);
3218 : :
3219 : : public:
3220 : : /* Serialize various definitions. */
3221 : : void write_definition (tree decl, bool refs_tu_local = false);
3222 : : void mark_declaration (tree decl, bool do_defn);
3223 : :
3224 : : private:
3225 : : void mark_function_def (tree decl);
3226 : : void mark_var_def (tree decl);
3227 : : void mark_class_def (tree decl);
3228 : : void mark_enum_def (tree decl);
3229 : : void mark_class_member (tree decl, bool do_defn = true);
3230 : : void mark_binfos (tree type);
3231 : :
3232 : : private:
3233 : : void write_var_def (tree decl);
3234 : : void write_function_def (tree decl);
3235 : : void write_class_def (tree decl);
3236 : : void write_enum_def (tree decl);
3237 : :
3238 : : private:
3239 : : static void assert_definition (tree);
3240 : :
3241 : : public:
3242 : : static void instrument ();
3243 : :
3244 : : private:
3245 : : /* Tree instrumentation. */
3246 : : static unsigned tree_val_count;
3247 : : static unsigned decl_val_count;
3248 : : static unsigned back_ref_count;
3249 : : static unsigned tu_local_count;
3250 : : static unsigned null_count;
3251 : : };
3252 : : } // anon namespace
3253 : :
3254 : : /* Instrumentation counters. */
3255 : : unsigned trees_out::tree_val_count;
3256 : : unsigned trees_out::decl_val_count;
3257 : : unsigned trees_out::back_ref_count;
3258 : : unsigned trees_out::tu_local_count;
3259 : : unsigned trees_out::null_count;
3260 : :
3261 : 447487 : trees_out::trees_out (allocator *mem, module_state *state, depset::hash &deps,
3262 : 447487 : unsigned section)
3263 : 894974 : :parent (mem), state (state), tree_map (500),
3264 : 447487 : dep_hash (&deps), ref_num (0), section (section),
3265 : 447487 : writing_local_entities (false)
3266 : : {
3267 : : #if CHECKING_P
3268 : 447487 : importedness = 0;
3269 : : #endif
3270 : 447487 : }
3271 : :
3272 : 447487 : trees_out::~trees_out ()
3273 : : {
3274 : 447487 : }
3275 : :
3276 : : /********************************************************************/
3277 : : /* Location. We're aware of the line-map concept and reproduce it
3278 : : here. Each imported module allocates a contiguous span of ordinary
3279 : : maps, and of macro maps. adhoc maps are serialized by contents,
3280 : : not pre-allocated. The scattered linemaps of a module are
3281 : : coalesced when writing. */
3282 : :
3283 : :
3284 : : /* I use half-open [first,second) ranges. */
3285 : : typedef std::pair<line_map_uint_t,line_map_uint_t> range_t;
3286 : :
3287 : : /* A range of locations. */
3288 : : typedef std::pair<location_t,location_t> loc_range_t;
3289 : :
3290 : : /* Spans of the line maps that are occupied by this TU. I.e. not
3291 : : within imports. Only extended when in an interface unit.
3292 : : Interval zero corresponds to the forced header linemap(s). This
3293 : : is a singleton object. */
3294 : :
3295 : : class loc_spans {
3296 : : public:
3297 : : /* An interval of line maps. The line maps here represent a contiguous
3298 : : non-imported range. */
3299 : 5287 : struct span {
3300 : : loc_range_t ordinary; /* Ordinary map location range. */
3301 : : loc_range_t macro; /* Macro map location range. */
3302 : : /* Add to locs to get serialized loc. */
3303 : : location_diff_t ordinary_delta;
3304 : : location_diff_t macro_delta;
3305 : : };
3306 : :
3307 : : private:
3308 : : vec<span> *spans;
3309 : : bool locs_exhausted_p;
3310 : :
3311 : : public:
3312 : : loc_spans ()
3313 : : /* Do not preallocate spans, as that causes
3314 : : --enable-detailed-mem-stats problems. */
3315 : : : spans (nullptr), locs_exhausted_p (false)
3316 : : {
3317 : : }
3318 : 95119 : ~loc_spans ()
3319 : : {
3320 : 95119 : delete spans;
3321 : 95119 : }
3322 : :
3323 : : public:
3324 : 251 : span &operator[] (unsigned ix)
3325 : : {
3326 : 502 : return (*spans)[ix];
3327 : : }
3328 : : unsigned length () const
3329 : : {
3330 : : return spans->length ();
3331 : : }
3332 : :
3333 : : public:
3334 : 13736 : bool init_p () const
3335 : : {
3336 : 13736 : return spans != nullptr;
3337 : : }
3338 : : /* Initializer. */
3339 : : void init (const line_maps *lmaps, const line_map_ordinary *map);
3340 : :
3341 : : /* Slightly skewed preprocessed files can cause us to miss an
3342 : : initialization in some places. Fallback initializer. */
3343 : 5137 : void maybe_init ()
3344 : : {
3345 : 5137 : if (!init_p ())
3346 : 6 : init (line_table, nullptr);
3347 : 5137 : }
3348 : :
3349 : : public:
3350 : : enum {
3351 : : SPAN_RESERVED = 0, /* Reserved (fixed) locations. */
3352 : : SPAN_FIRST = 1, /* LWM of locations to stream */
3353 : : SPAN_MAIN = 2 /* Main file and onwards. */
3354 : : };
3355 : :
3356 : : public:
3357 : 429713 : location_t main_start () const
3358 : : {
3359 : 429713 : return (*spans)[SPAN_MAIN].ordinary.first;
3360 : : }
3361 : :
3362 : : public:
3363 : : void open (location_t);
3364 : : void close ();
3365 : :
3366 : : public:
3367 : : /* Propagate imported linemaps to us, if needed. */
3368 : : bool maybe_propagate (module_state *import, location_t loc);
3369 : :
3370 : : public:
3371 : : /* Whether we can no longer represent new imported locations. */
3372 : 0 : bool locations_exhausted_p () const
3373 : : {
3374 : 0 : return locs_exhausted_p;
3375 : : }
3376 : 0 : void report_location_exhaustion (location_t loc)
3377 : : {
3378 : 0 : if (!locs_exhausted_p)
3379 : : {
3380 : : /* Just give the notice once. */
3381 : 0 : locs_exhausted_p = true;
3382 : 0 : inform (loc, "unable to represent further imported source locations");
3383 : : }
3384 : : }
3385 : :
3386 : : public:
3387 : : const span *ordinary (location_t);
3388 : : const span *macro (location_t);
3389 : : };
3390 : :
3391 : : static loc_spans spans;
3392 : :
3393 : : /* Information about ordinary locations we stream out. */
3394 : : struct ord_loc_info
3395 : : {
3396 : : const line_map_ordinary *src; // line map we're based on
3397 : : line_map_uint_t offset; // offset to this line
3398 : : line_map_uint_t span; // number of locs we span
3399 : : line_map_uint_t remap; // serialization
3400 : :
3401 : 191189136 : static int compare (const void *a_, const void *b_)
3402 : : {
3403 : 191189136 : auto *a = static_cast<const ord_loc_info *> (a_);
3404 : 191189136 : auto *b = static_cast<const ord_loc_info *> (b_);
3405 : :
3406 : 191189136 : if (a->src != b->src)
3407 : 43281973 : return a->src < b->src ? -1 : +1;
3408 : :
3409 : : // Ensure no overlap
3410 : 161936646 : gcc_checking_assert (a->offset + a->span <= b->offset
3411 : : || b->offset + b->span <= a->offset);
3412 : :
3413 : 161936646 : gcc_checking_assert (a->offset != b->offset);
3414 : 161936646 : return a->offset < b->offset ? -1 : +1;
3415 : : }
3416 : : };
3417 : : struct ord_loc_traits
3418 : : {
3419 : : typedef ord_loc_info value_type;
3420 : : typedef value_type compare_type;
3421 : :
3422 : : static const bool empty_zero_p = false;
3423 : :
3424 : 82489925 : static hashval_t hash (const value_type &v)
3425 : : {
3426 : 70517283 : auto h = pointer_hash<const line_map_ordinary>::hash (v.src);
3427 : 82489925 : return iterative_hash_hashval_t (v.offset, h);
3428 : : }
3429 : 90573641 : static bool equal (const value_type &v, const compare_type p)
3430 : : {
3431 : 90573641 : return v.src == p.src && v.offset == p.offset;
3432 : : }
3433 : :
3434 : 5793528 : static void mark_empty (value_type &v)
3435 : : {
3436 : 5793528 : v.src = nullptr;
3437 : : }
3438 : 258257608 : static bool is_empty (value_type &v)
3439 : : {
3440 : 258257608 : return !v.src;
3441 : : }
3442 : :
3443 : : static bool is_deleted (value_type &) { return false; }
3444 : : static void mark_deleted (value_type &) { gcc_unreachable (); }
3445 : :
3446 : 1454362 : static void remove (value_type &) {}
3447 : : };
3448 : : /* Table keyed by ord_loc_info, used for noting. */
3449 : : static hash_table<ord_loc_traits> *ord_loc_table;
3450 : : /* Sorted vector, used for writing. */
3451 : : static vec<ord_loc_info> *ord_loc_remap;
3452 : :
3453 : : /* Information about macro locations we stream out. */
3454 : : struct macro_loc_info
3455 : : {
3456 : : const line_map_macro *src; // original expansion
3457 : : line_map_uint_t remap; // serialization
3458 : :
3459 : 6577483 : static int compare (const void *a_, const void *b_)
3460 : : {
3461 : 6577483 : auto *a = static_cast<const macro_loc_info *> (a_);
3462 : 6577483 : auto *b = static_cast<const macro_loc_info *> (b_);
3463 : :
3464 : 6577483 : gcc_checking_assert (MAP_START_LOCATION (a->src)
3465 : : != MAP_START_LOCATION (b->src));
3466 : 6577483 : if (MAP_START_LOCATION (a->src) < MAP_START_LOCATION (b->src))
3467 : : return -1;
3468 : : else
3469 : 3172088 : return +1;
3470 : : }
3471 : : };
3472 : : struct macro_loc_traits
3473 : : {
3474 : : typedef macro_loc_info value_type;
3475 : : typedef const line_map_macro *compare_type;
3476 : :
3477 : : static const bool empty_zero_p = false;
3478 : :
3479 : 6753406 : static hashval_t hash (compare_type p)
3480 : : {
3481 : 6753406 : return pointer_hash<const line_map_macro>::hash (p);
3482 : : }
3483 : 5785538 : static hashval_t hash (const value_type &v)
3484 : : {
3485 : 5785538 : return hash (v.src);
3486 : : }
3487 : : static bool equal (const value_type &v, const compare_type p)
3488 : : {
3489 : : return v.src == p;
3490 : : }
3491 : :
3492 : 479391 : static void mark_empty (value_type &v)
3493 : : {
3494 : 479391 : v.src = nullptr;
3495 : : }
3496 : 20629173 : static bool is_empty (value_type &v)
3497 : : {
3498 : 20629173 : return !v.src;
3499 : : }
3500 : :
3501 : : static bool is_deleted (value_type &) { return false; }
3502 : : static void mark_deleted (value_type &) { gcc_unreachable (); }
3503 : :
3504 : 113509 : static void remove (value_type &) {}
3505 : : };
3506 : : /* Table keyed by line_map_macro, used for noting. */
3507 : : static hash_table<macro_loc_traits> *macro_loc_table;
3508 : : /* Sorted vector, used for writing. */
3509 : : static vec<macro_loc_info> *macro_loc_remap;
3510 : :
3511 : : /* Indirection to allow bsearching imports by ordinary location. */
3512 : : static vec<module_state *> *ool;
3513 : :
3514 : : /********************************************************************/
3515 : : /* Data needed by a module during the process of loading. */
3516 : : struct GTY(()) slurping {
3517 : :
3518 : : /* Remap import's module numbering to our numbering. Values are
3519 : : shifted by 1. Bit0 encodes if the import is direct. */
3520 : : vec<unsigned, va_heap, vl_embed> *
3521 : : GTY((skip)) remap; /* Module owner remapping. */
3522 : :
3523 : : elf_in *GTY((skip)) from; /* The elf loader. */
3524 : :
3525 : : /* This map is only for header imports themselves -- the global
3526 : : headers bitmap hold it for the current TU. */
3527 : : bitmap headers; /* Transitive set of direct imports, including
3528 : : self. Used for macro visibility and
3529 : : priority. */
3530 : :
3531 : : /* These objects point into the mmapped area, unless we're not doing
3532 : : that, or we got frozen or closed. In those cases they point to
3533 : : buffers we own. */
3534 : : bytes_in macro_defs; /* Macro definitions. */
3535 : : bytes_in macro_tbl; /* Macro table. */
3536 : :
3537 : : /* Location remapping. first->ordinary, second->macro. */
3538 : : range_t GTY((skip)) loc_deltas;
3539 : :
3540 : : unsigned current; /* Section currently being loaded. */
3541 : : unsigned remaining; /* Number of lazy sections yet to read. */
3542 : : unsigned lru; /* An LRU counter. */
3543 : :
3544 : : public:
3545 : : slurping (elf_in *);
3546 : : ~slurping ();
3547 : :
3548 : : public:
3549 : : /* Close the ELF file, if it's open. */
3550 : 4672 : void close ()
3551 : : {
3552 : 4672 : if (from)
3553 : : {
3554 : 2554 : from->end ();
3555 : 5108 : delete from;
3556 : 2554 : from = NULL;
3557 : : }
3558 : 4672 : }
3559 : :
3560 : : public:
3561 : : void release_macros ();
3562 : :
3563 : : public:
3564 : 2612 : void alloc_remap (unsigned size)
3565 : : {
3566 : 2612 : gcc_assert (!remap);
3567 : 2612 : vec_safe_reserve (remap, size);
3568 : 5544 : for (unsigned ix = size; ix--;)
3569 : 2932 : remap->quick_push (0);
3570 : 2612 : }
3571 : 866242 : unsigned remap_module (unsigned owner)
3572 : : {
3573 : 866242 : if (owner < remap->length ())
3574 : 866242 : return (*remap)[owner] >> 1;
3575 : : return 0;
3576 : : }
3577 : :
3578 : : public:
3579 : : /* GC allocation. But we must explicitly delete it. */
3580 : 2630 : static void *operator new (size_t x)
3581 : : {
3582 : 5260 : return ggc_alloc_atomic (x);
3583 : : }
3584 : 2554 : static void operator delete (void *p)
3585 : : {
3586 : 2554 : ggc_free (p);
3587 : 2554 : }
3588 : : };
3589 : :
3590 : 2630 : slurping::slurping (elf_in *from)
3591 : 2630 : : remap (NULL), from (from),
3592 : 2630 : headers (BITMAP_GGC_ALLOC ()), macro_defs (), macro_tbl (),
3593 : 2630 : loc_deltas (0, 0),
3594 : 2630 : current (~0u), remaining (0), lru (0)
3595 : : {
3596 : 2630 : }
3597 : :
3598 : 2554 : slurping::~slurping ()
3599 : : {
3600 : 2554 : vec_free (remap);
3601 : 2554 : remap = NULL;
3602 : 2554 : release_macros ();
3603 : 2554 : close ();
3604 : 2554 : }
3605 : :
3606 : 4672 : void slurping::release_macros ()
3607 : : {
3608 : 4672 : if (macro_defs.size)
3609 : 845 : elf_in::release (from, macro_defs);
3610 : 4672 : if (macro_tbl.size)
3611 : 0 : elf_in::release (from, macro_tbl);
3612 : 4672 : }
3613 : :
3614 : : /* Flags for extensions that end up being streamed. */
3615 : :
3616 : : enum streamed_extensions {
3617 : : SE_OPENMP_SIMD = 1 << 0,
3618 : : SE_OPENMP = 1 << 1,
3619 : : SE_OPENACC = 1 << 2,
3620 : : SE_BITS = 3
3621 : : };
3622 : :
3623 : : /* Counter indices. */
3624 : : enum module_state_counts
3625 : : {
3626 : : MSC_sec_lwm,
3627 : : MSC_sec_hwm,
3628 : : MSC_pendings,
3629 : : MSC_entities,
3630 : : MSC_namespaces,
3631 : : MSC_bindings,
3632 : : MSC_macros,
3633 : : MSC_inits,
3634 : : MSC_HWM
3635 : : };
3636 : :
3637 : : /********************************************************************/
3638 : : struct module_state_config;
3639 : :
3640 : : /* Increasing levels of loadedness. */
3641 : : enum module_loadedness {
3642 : : ML_NONE, /* Not loaded. */
3643 : : ML_CONFIG, /* Config loaed. */
3644 : : ML_PREPROCESSOR, /* Preprocessor loaded. */
3645 : : ML_LANGUAGE, /* Language loaded. */
3646 : : };
3647 : :
3648 : : /* Increasing levels of directness (toplevel) of import. */
3649 : : enum module_directness {
3650 : : MD_NONE, /* Not direct. */
3651 : : MD_PARTITION_DIRECT, /* Direct import of a partition. */
3652 : : MD_DIRECT, /* Direct import. */
3653 : : MD_PURVIEW_DIRECT, /* direct import in purview. */
3654 : : };
3655 : :
3656 : : /* State of a particular module. */
3657 : :
3658 : : class GTY((chain_next ("%h.parent"), for_user)) module_state {
3659 : : public:
3660 : : /* We always import & export ourselves. */
3661 : : bitmap imports; /* Transitive modules we're importing. */
3662 : : bitmap exports; /* Subset of that, that we're exporting. */
3663 : :
3664 : : module_state *parent;
3665 : : tree name; /* Name of the module. */
3666 : :
3667 : : slurping *slurp; /* Data for loading. */
3668 : :
3669 : : const char *flatname; /* Flatname of module. */
3670 : : char *filename; /* CMI Filename */
3671 : :
3672 : : /* Indices into the entity_ary. */
3673 : : unsigned entity_lwm;
3674 : : unsigned entity_num;
3675 : :
3676 : : /* Location ranges for this module. adhoc-locs are decomposed, so
3677 : : don't have a range. */
3678 : : loc_range_t GTY((skip)) ordinary_locs;
3679 : : loc_range_t GTY((skip)) macro_locs; // [lwm,num)
3680 : :
3681 : : /* LOC is first set too the importing location. When initially
3682 : : loaded it refers to a module loc whose parent is the importing
3683 : : location. */
3684 : : location_t loc; /* Location referring to module itself. */
3685 : : unsigned crc; /* CRC we saw reading it in. */
3686 : :
3687 : : unsigned mod; /* Module owner number. */
3688 : : unsigned remap; /* Remapping during writing. */
3689 : :
3690 : : unsigned short subst; /* Mangle subst if !0. */
3691 : :
3692 : : /* How loaded this module is. */
3693 : : enum module_loadedness loadedness : 2;
3694 : :
3695 : : bool module_p : 1; /* /The/ module of this TU. */
3696 : : bool header_p : 1; /* Is a header unit. */
3697 : : bool interface_p : 1; /* An interface. */
3698 : : bool partition_p : 1; /* A partition. */
3699 : :
3700 : : /* How directly this module is imported. */
3701 : : enum module_directness directness : 2;
3702 : :
3703 : : bool exported_p : 1; /* directness != MD_NONE && exported. */
3704 : : bool cmi_noted_p : 1; /* We've told the user about the CMI, don't
3705 : : do it again */
3706 : : bool active_init_p : 1; /* This module's global initializer needs
3707 : : calling. */
3708 : : bool inform_cmi_p : 1; /* Inform of a read/write. */
3709 : : bool visited_p : 1; /* A walk-once flag. */
3710 : : /* Record extensions emitted or permitted. */
3711 : : unsigned extensions : SE_BITS;
3712 : : /* 14 bits used, 2 bits remain */
3713 : :
3714 : : public:
3715 : : module_state (tree name, module_state *, bool);
3716 : : ~module_state ();
3717 : :
3718 : : public:
3719 : 2604 : void release ()
3720 : : {
3721 : 2604 : imports = exports = NULL;
3722 : 2604 : slurped ();
3723 : 2554 : }
3724 : 4722 : void slurped ()
3725 : : {
3726 : 4722 : delete slurp;
3727 : 4722 : slurp = NULL;
3728 : 4722 : }
3729 : 1021401 : elf_in *from () const
3730 : : {
3731 : 1021401 : return slurp->from;
3732 : : }
3733 : :
3734 : : public:
3735 : : /* Kind of this module. */
3736 : 96875 : bool is_module () const
3737 : : {
3738 : 96875 : return module_p;
3739 : : }
3740 : 1581036 : bool is_header () const
3741 : : {
3742 : 1581036 : return header_p;
3743 : : }
3744 : 513 : bool is_interface () const
3745 : : {
3746 : 513 : return interface_p;
3747 : : }
3748 : 146326 : bool is_partition () const
3749 : : {
3750 : 146326 : return partition_p;
3751 : : }
3752 : :
3753 : : /* How this module is used in the current TU. */
3754 : 33 : bool is_exported () const
3755 : : {
3756 : 33 : return exported_p;
3757 : : }
3758 : 18138 : bool is_direct () const
3759 : : {
3760 : 18138 : return directness >= MD_DIRECT;
3761 : : }
3762 : 236 : bool is_purview_direct () const
3763 : : {
3764 : 236 : return directness == MD_PURVIEW_DIRECT;
3765 : : }
3766 : 362 : bool is_partition_direct () const
3767 : : {
3768 : 362 : return directness == MD_PARTITION_DIRECT;
3769 : : }
3770 : :
3771 : : public:
3772 : : /* Is this a real module? */
3773 : 14231 : bool has_location () const
3774 : : {
3775 : 14231 : return loc != UNKNOWN_LOCATION;
3776 : : }
3777 : :
3778 : : public:
3779 : : bool check_not_purview (location_t loc);
3780 : :
3781 : : public:
3782 : : void mangle (bool include_partition);
3783 : :
3784 : : public:
3785 : : void set_import (module_state const *, bool is_export);
3786 : : void announce (const char *) const;
3787 : :
3788 : : public:
3789 : : /* Read and write module. */
3790 : : bool write_begin (elf_out *to, cpp_reader *,
3791 : : module_state_config &, unsigned &crc);
3792 : : void write_end (elf_out *to, cpp_reader *,
3793 : : module_state_config &, unsigned &crc);
3794 : : bool read_initial (cpp_reader *);
3795 : : bool read_preprocessor (bool);
3796 : : bool read_language (bool);
3797 : :
3798 : : public:
3799 : : /* Read a section. */
3800 : : bool load_section (unsigned snum, binding_slot *mslot);
3801 : : /* Lazily read a section. */
3802 : : bool lazy_load (unsigned index, binding_slot *mslot);
3803 : :
3804 : : public:
3805 : : /* Juggle a limited number of file numbers. */
3806 : : static void freeze_an_elf ();
3807 : : bool maybe_defrost ();
3808 : :
3809 : : public:
3810 : : void maybe_completed_reading ();
3811 : : bool check_read (bool outermost, bool ok);
3812 : :
3813 : : private:
3814 : : /* The README, for human consumption. */
3815 : : void write_readme (elf_out *to, cpp_reader *, const char *dialect);
3816 : : void write_env (elf_out *to);
3817 : :
3818 : : private:
3819 : : /* Import tables. */
3820 : : void write_imports (bytes_out &cfg, bool direct);
3821 : : unsigned read_imports (bytes_in &cfg, cpp_reader *, line_maps *maps);
3822 : :
3823 : : private:
3824 : : void write_imports (elf_out *to, unsigned *crc_ptr);
3825 : : bool read_imports (cpp_reader *, line_maps *);
3826 : :
3827 : : private:
3828 : : void write_partitions (elf_out *to, unsigned, unsigned *crc_ptr);
3829 : : bool read_partitions (unsigned);
3830 : :
3831 : : private:
3832 : : void write_config (elf_out *to, struct module_state_config &, unsigned crc);
3833 : : bool read_config (struct module_state_config &);
3834 : : static void write_counts (elf_out *to, unsigned [MSC_HWM], unsigned *crc_ptr);
3835 : : bool read_counts (unsigned *);
3836 : :
3837 : : public:
3838 : : void note_cmi_name ();
3839 : :
3840 : : private:
3841 : : static unsigned write_bindings (elf_out *to, vec<depset *> depsets,
3842 : : unsigned *crc_ptr);
3843 : : bool read_bindings (unsigned count, unsigned lwm, unsigned hwm);
3844 : :
3845 : : static void write_namespace (bytes_out &sec, depset *ns_dep);
3846 : : tree read_namespace (bytes_in &sec);
3847 : :
3848 : : void write_namespaces (elf_out *to, vec<depset *> spaces,
3849 : : unsigned, unsigned *crc_ptr);
3850 : : bool read_namespaces (unsigned);
3851 : :
3852 : : void intercluster_seed (trees_out &sec, unsigned index, depset *dep);
3853 : : unsigned write_cluster (elf_out *to, depset *depsets[], unsigned size,
3854 : : depset::hash &, unsigned *counts, unsigned *crc_ptr);
3855 : : bool read_cluster (unsigned snum);
3856 : :
3857 : : private:
3858 : : unsigned write_inits (elf_out *to, depset::hash &, unsigned *crc_ptr);
3859 : : bool read_inits (unsigned count);
3860 : :
3861 : : private:
3862 : : unsigned write_pendings (elf_out *to, vec<depset *> depsets,
3863 : : depset::hash &, unsigned *crc_ptr);
3864 : : bool read_pendings (unsigned count);
3865 : :
3866 : : private:
3867 : : void write_entities (elf_out *to, vec<depset *> depsets,
3868 : : unsigned count, unsigned *crc_ptr);
3869 : : bool read_entities (unsigned count, unsigned lwm, unsigned hwm);
3870 : :
3871 : : private:
3872 : : void write_init_maps ();
3873 : : range_t write_prepare_maps (module_state_config *, bool);
3874 : : bool read_prepare_maps (const module_state_config *);
3875 : :
3876 : : void write_ordinary_maps (elf_out *to, range_t &,
3877 : : bool, unsigned *crc_ptr);
3878 : : bool read_ordinary_maps (line_map_uint_t, unsigned);
3879 : : void write_macro_maps (elf_out *to, range_t &, unsigned *crc_ptr);
3880 : : bool read_macro_maps (line_map_uint_t);
3881 : :
3882 : : void write_diagnostic_classification (elf_out *, diagnostic_context *,
3883 : : unsigned *);
3884 : : bool read_diagnostic_classification (diagnostic_context *);
3885 : :
3886 : : private:
3887 : : void write_define (bytes_out &, const cpp_macro *);
3888 : : cpp_macro *read_define (bytes_in &, cpp_reader *) const;
3889 : : vec<cpp_hashnode *> *prepare_macros (cpp_reader *);
3890 : : unsigned write_macros (elf_out *to, vec<cpp_hashnode *> *, unsigned *crc_ptr);
3891 : : bool read_macros ();
3892 : : void install_macros ();
3893 : :
3894 : : public:
3895 : : void import_macros ();
3896 : :
3897 : : public:
3898 : : static void undef_macro (cpp_reader *, location_t, cpp_hashnode *);
3899 : : static cpp_macro *deferred_macro (cpp_reader *, location_t, cpp_hashnode *);
3900 : :
3901 : : public:
3902 : : static bool note_location (location_t);
3903 : : static void write_location (bytes_out &, location_t);
3904 : : location_t read_location (bytes_in &) const;
3905 : :
3906 : : public:
3907 : : void set_flatname ();
3908 : 41523 : const char *get_flatname () const
3909 : : {
3910 : 41523 : return flatname;
3911 : : }
3912 : : location_t imported_from () const;
3913 : :
3914 : : public:
3915 : : void set_filename (const Cody::Packet &);
3916 : : bool do_import (cpp_reader *, bool outermost);
3917 : : };
3918 : :
3919 : : /* Hash module state by name. This cannot be a member of
3920 : : module_state, because of GTY restrictions. We never delete from
3921 : : the hash table, but ggc_ptr_hash doesn't support that
3922 : : simplification. */
3923 : :
3924 : : struct module_state_hash : ggc_ptr_hash<module_state> {
3925 : : typedef std::pair<tree,uintptr_t> compare_type; /* {name,parent} */
3926 : :
3927 : : static inline hashval_t hash (const value_type m);
3928 : : static inline hashval_t hash (const compare_type &n);
3929 : : static inline bool equal (const value_type existing,
3930 : : const compare_type &candidate);
3931 : : };
3932 : :
3933 : 10128 : module_state::module_state (tree name, module_state *parent, bool partition)
3934 : 10128 : : imports (BITMAP_GGC_ALLOC ()), exports (BITMAP_GGC_ALLOC ()),
3935 : 10128 : parent (parent), name (name), slurp (NULL),
3936 : 10128 : flatname (NULL), filename (NULL),
3937 : 10128 : entity_lwm (~0u >> 1), entity_num (0),
3938 : 10128 : ordinary_locs (0, 0), macro_locs (0, 0),
3939 : 10128 : loc (UNKNOWN_LOCATION),
3940 : 10128 : crc (0), mod (MODULE_UNKNOWN), remap (0), subst (0)
3941 : : {
3942 : 10128 : loadedness = ML_NONE;
3943 : :
3944 : 10128 : module_p = header_p = interface_p = partition_p = false;
3945 : :
3946 : 10128 : directness = MD_NONE;
3947 : 10128 : exported_p = false;
3948 : :
3949 : 10128 : cmi_noted_p = false;
3950 : 10128 : active_init_p = false;
3951 : :
3952 : 10128 : partition_p = partition;
3953 : :
3954 : 10128 : inform_cmi_p = false;
3955 : 10128 : visited_p = false;
3956 : :
3957 : 10128 : extensions = 0;
3958 : 10128 : if (name && TREE_CODE (name) == STRING_CST)
3959 : : {
3960 : 1813 : header_p = true;
3961 : :
3962 : 1813 : const char *string = TREE_STRING_POINTER (name);
3963 : 1813 : gcc_checking_assert (string[0] == '.'
3964 : : ? IS_DIR_SEPARATOR (string[1])
3965 : : : IS_ABSOLUTE_PATH (string));
3966 : : }
3967 : :
3968 : 10128 : gcc_checking_assert (!(parent && header_p));
3969 : 10128 : }
3970 : :
3971 : 50 : module_state::~module_state ()
3972 : : {
3973 : 50 : release ();
3974 : 50 : }
3975 : :
3976 : : /* Hash module state. */
3977 : : static hashval_t
3978 : 14557 : module_name_hash (const_tree name)
3979 : : {
3980 : 14557 : if (TREE_CODE (name) == STRING_CST)
3981 : 3348 : return htab_hash_string (TREE_STRING_POINTER (name));
3982 : : else
3983 : 11209 : return IDENTIFIER_HASH_VALUE (name);
3984 : : }
3985 : :
3986 : : hashval_t
3987 : 3867 : module_state_hash::hash (const value_type m)
3988 : : {
3989 : 3867 : hashval_t ph = pointer_hash<void>::hash
3990 : 3867 : (reinterpret_cast<void *> (reinterpret_cast<uintptr_t> (m->parent)
3991 : 3867 : | m->is_partition ()));
3992 : 3867 : hashval_t nh = module_name_hash (m->name);
3993 : 3867 : return iterative_hash_hashval_t (ph, nh);
3994 : : }
3995 : :
3996 : : /* Hash a name. */
3997 : : hashval_t
3998 : 10690 : module_state_hash::hash (const compare_type &c)
3999 : : {
4000 : 10690 : hashval_t ph = pointer_hash<void>::hash (reinterpret_cast<void *> (c.second));
4001 : 10690 : hashval_t nh = module_name_hash (c.first);
4002 : :
4003 : 10690 : return iterative_hash_hashval_t (ph, nh);
4004 : : }
4005 : :
4006 : : bool
4007 : 7154 : module_state_hash::equal (const value_type existing,
4008 : : const compare_type &candidate)
4009 : : {
4010 : 7154 : uintptr_t ep = (reinterpret_cast<uintptr_t> (existing->parent)
4011 : 7154 : | existing->is_partition ());
4012 : 7154 : if (ep != candidate.second)
4013 : : return false;
4014 : :
4015 : : /* Identifier comparison is by pointer. If the string_csts happen
4016 : : to be the same object, then they're equal too. */
4017 : 5930 : if (existing->name == candidate.first)
4018 : : return true;
4019 : :
4020 : : /* If neither are string csts, they can't be equal. */
4021 : 1210 : if (TREE_CODE (candidate.first) != STRING_CST
4022 : 483 : || TREE_CODE (existing->name) != STRING_CST)
4023 : : return false;
4024 : :
4025 : : /* String equality. */
4026 : 417 : if (TREE_STRING_LENGTH (existing->name)
4027 : 417 : == TREE_STRING_LENGTH (candidate.first)
4028 : 417 : && !memcmp (TREE_STRING_POINTER (existing->name),
4029 : 414 : TREE_STRING_POINTER (candidate.first),
4030 : 414 : TREE_STRING_LENGTH (existing->name)))
4031 : 131 : return true;
4032 : :
4033 : : return false;
4034 : : }
4035 : :
4036 : : /********************************************************************/
4037 : : /* Global state */
4038 : :
4039 : : /* Mapper name. */
4040 : : static const char *module_mapper_name;
4041 : :
4042 : : /* Deferred import queue (FIFO). */
4043 : : static vec<module_state *, va_heap, vl_embed> *pending_imports;
4044 : :
4045 : : /* CMI repository path and workspace. */
4046 : : static char *cmi_repo;
4047 : : static size_t cmi_repo_length;
4048 : : static char *cmi_path;
4049 : : static size_t cmi_path_alloc;
4050 : :
4051 : : /* Count of available and loaded clusters. */
4052 : : static unsigned available_clusters;
4053 : : static unsigned loaded_clusters;
4054 : :
4055 : : /* What the current TU is. */
4056 : : unsigned module_kind;
4057 : :
4058 : : /* Global trees. */
4059 : : static const std::pair<tree *, unsigned> global_tree_arys[] =
4060 : : {
4061 : : std::pair<tree *, unsigned> (sizetype_tab, stk_type_kind_last),
4062 : : std::pair<tree *, unsigned> (integer_types, itk_none),
4063 : : std::pair<tree *, unsigned> (global_trees, TI_MODULE_HWM),
4064 : : std::pair<tree *, unsigned> (c_global_trees, CTI_MODULE_HWM),
4065 : : std::pair<tree *, unsigned> (cp_global_trees, CPTI_MODULE_HWM),
4066 : : std::pair<tree *, unsigned> (NULL, 0)
4067 : : };
4068 : : static GTY(()) vec<tree, va_gc> *fixed_trees;
4069 : : static unsigned global_crc;
4070 : :
4071 : : /* Lazy loading can open many files concurrently, there are
4072 : : per-process limits on that. We pay attention to the process limit,
4073 : : and attempt to increase it when we run out. Otherwise we use an
4074 : : LRU scheme to figure out who to flush. Note that if the import
4075 : : graph /depth/ exceeds lazy_limit, we'll exceed the limit. */
4076 : : static unsigned lazy_lru; /* LRU counter. */
4077 : : static unsigned lazy_open; /* Number of open modules */
4078 : : static unsigned lazy_limit; /* Current limit of open modules. */
4079 : : static unsigned lazy_hard_limit; /* Hard limit on open modules. */
4080 : : /* Account for source, assembler and dump files & directory searches.
4081 : : We don't keep the source file's open, so we don't have to account
4082 : : for #include depth. I think dump files are opened and closed per
4083 : : pass, but ICBW. */
4084 : : #define LAZY_HEADROOM 15 /* File descriptor headroom. */
4085 : :
4086 : : /* Vector of module state. Indexed by OWNER. Index 0 is reserved for the
4087 : : current TU; imports start at 1. */
4088 : : static GTY(()) vec<module_state *, va_gc> *modules;
4089 : :
4090 : : /* Hash of module state, findable by {name, parent}. */
4091 : : static GTY(()) hash_table<module_state_hash> *modules_hash;
4092 : :
4093 : : /* Map of imported entities. We map DECL_UID to index of entity
4094 : : vector. */
4095 : : typedef hash_map<unsigned/*UID*/, unsigned/*index*/,
4096 : : simple_hashmap_traits<int_hash<unsigned,0>, unsigned>
4097 : : > entity_map_t;
4098 : : static entity_map_t *entity_map;
4099 : : /* Doesn't need GTYing, because any tree referenced here is also
4100 : : findable by, symbol table, specialization table, return type of
4101 : : reachable function. */
4102 : : static vec<binding_slot, va_heap, vl_embed> *entity_ary;
4103 : :
4104 : : /* Members entities of imported classes that are defined in this TU.
4105 : : These are where the entity's context is not from the current TU.
4106 : : We need to emit the definition (but not the enclosing class).
4107 : :
4108 : : We could find these by walking ALL the imported classes that we
4109 : : could provide a member definition. But that's expensive,
4110 : : especially when you consider lazy implicit member declarations,
4111 : : which could be ANY imported class. */
4112 : : static GTY(()) vec<tree, va_gc> *class_members;
4113 : :
4114 : : /* The same problem exists for class template partial
4115 : : specializations. Now that we have constraints, the invariant of
4116 : : expecting them in the instantiation table no longer holds. One of
4117 : : the constrained partial specializations will be there, but the
4118 : : others not so much. It's not even an unconstrained partial
4119 : : spacialization in the table :( so any partial template declaration
4120 : : is added to this list too. */
4121 : : static GTY(()) vec<tree, va_gc> *partial_specializations;
4122 : :
4123 : : /********************************************************************/
4124 : :
4125 : : /* Our module mapper (created lazily). */
4126 : : module_client *mapper;
4127 : :
4128 : : static module_client *make_mapper (location_t loc, class mkdeps *deps);
4129 : 29928 : inline module_client *get_mapper (location_t loc, class mkdeps *deps)
4130 : : {
4131 : 29928 : auto *res = mapper;
4132 : 274 : if (!res)
4133 : 4289 : res = make_mapper (loc, deps);
4134 : 29928 : return res;
4135 : : }
4136 : :
4137 : : /********************************************************************/
4138 : : static tree
4139 : 255184 : get_clone_target (tree decl)
4140 : : {
4141 : 255184 : tree target;
4142 : :
4143 : 255184 : if (TREE_CODE (decl) == TEMPLATE_DECL)
4144 : : {
4145 : 28276 : tree res_orig = DECL_CLONED_FUNCTION (DECL_TEMPLATE_RESULT (decl));
4146 : :
4147 : 28276 : target = DECL_TI_TEMPLATE (res_orig);
4148 : : }
4149 : : else
4150 : 226908 : target = DECL_CLONED_FUNCTION (decl);
4151 : :
4152 : 255184 : gcc_checking_assert (DECL_MAYBE_IN_CHARGE_CDTOR_P (target));
4153 : :
4154 : 255184 : return target;
4155 : : }
4156 : :
4157 : : /* Like FOR_EACH_CLONE, but will walk cloned templates. */
4158 : : #define FOR_EVERY_CLONE(CLONE, FN) \
4159 : : if (!DECL_MAYBE_IN_CHARGE_CDTOR_P (FN)); \
4160 : : else \
4161 : : for (CLONE = DECL_CHAIN (FN); \
4162 : : CLONE && DECL_CLONED_FUNCTION_P (CLONE); \
4163 : : CLONE = DECL_CHAIN (CLONE))
4164 : :
4165 : : /* It'd be nice if USE_TEMPLATE was a field of template_info
4166 : : (a) it'd solve the enum case dealt with below,
4167 : : (b) both class templates and decl templates would store this in the
4168 : : same place
4169 : : (c) this function wouldn't need the by-ref arg, which is annoying. */
4170 : :
4171 : : static tree
4172 : 173174091 : node_template_info (tree decl, int &use)
4173 : : {
4174 : 173174091 : tree ti = NULL_TREE;
4175 : 173174091 : int use_tpl = -1;
4176 : 173174091 : if (DECL_IMPLICIT_TYPEDEF_P (decl))
4177 : : {
4178 : 18498698 : tree type = TREE_TYPE (decl);
4179 : :
4180 : 18498698 : ti = TYPE_TEMPLATE_INFO (type);
4181 : 18498698 : if (ti)
4182 : : {
4183 : 3208215 : if (TYPE_LANG_SPECIFIC (type))
4184 : 3203033 : use_tpl = CLASSTYPE_USE_TEMPLATE (type);
4185 : : else
4186 : : {
4187 : : /* An enum, where we don't explicitly encode use_tpl.
4188 : : If the containing context (a type or a function), is
4189 : : an ({im,ex}plicit) instantiation, then this is too.
4190 : : If it's a partial or explicit specialization, then
4191 : : this is not!. */
4192 : 5182 : tree ctx = CP_DECL_CONTEXT (decl);
4193 : 5182 : if (TYPE_P (ctx))
4194 : 5072 : ctx = TYPE_NAME (ctx);
4195 : 5182 : node_template_info (ctx, use);
4196 : 5182 : use_tpl = use != 2 ? use : 0;
4197 : : }
4198 : : }
4199 : : }
4200 : 154675393 : else if (DECL_LANG_SPECIFIC (decl)
4201 : 154675393 : && (VAR_P (decl)
4202 : : || TREE_CODE (decl) == TYPE_DECL
4203 : : || TREE_CODE (decl) == FUNCTION_DECL
4204 : : || TREE_CODE (decl) == FIELD_DECL
4205 : : || TREE_CODE (decl) == CONCEPT_DECL
4206 : : || TREE_CODE (decl) == TEMPLATE_DECL))
4207 : : {
4208 : 81654459 : use_tpl = DECL_USE_TEMPLATE (decl);
4209 : 81654459 : ti = DECL_TEMPLATE_INFO (decl);
4210 : : }
4211 : :
4212 : 173174091 : use = use_tpl;
4213 : 173174091 : return ti;
4214 : : }
4215 : :
4216 : : /* Find the index in entity_ary for an imported DECL. It should
4217 : : always be there, but bugs can cause it to be missing, and that can
4218 : : crash the crash reporting -- let's not do that! When streaming
4219 : : out we place entities from this module there too -- with negated
4220 : : indices. */
4221 : :
4222 : : static unsigned
4223 : 1012493 : import_entity_index (tree decl, bool null_ok = false)
4224 : : {
4225 : 1012493 : if (unsigned *slot = entity_map->get (DECL_UID (decl)))
4226 : 1012448 : return *slot;
4227 : :
4228 : 45 : gcc_checking_assert (null_ok);
4229 : : return ~(~0u >> 1);
4230 : : }
4231 : :
4232 : : /* Find the module for an imported entity at INDEX in the entity ary.
4233 : : There must be one. */
4234 : :
4235 : : static module_state *
4236 : 37238 : import_entity_module (unsigned index)
4237 : : {
4238 : 37238 : if (index > ~(~0u >> 1))
4239 : : /* This is an index for an exported entity. */
4240 : 18 : return (*modules)[0];
4241 : :
4242 : : /* Do not include the current TU (not an off-by-one error). */
4243 : 37220 : unsigned pos = 1;
4244 : 37220 : unsigned len = modules->length () - pos;
4245 : 81948 : while (len)
4246 : : {
4247 : 44728 : unsigned half = len / 2;
4248 : 44728 : module_state *probe = (*modules)[pos + half];
4249 : 44728 : if (index < probe->entity_lwm)
4250 : : len = half;
4251 : 37338 : else if (index < probe->entity_lwm + probe->entity_num)
4252 : : return probe;
4253 : : else
4254 : : {
4255 : 118 : pos += half + 1;
4256 : 118 : len = len - (half + 1);
4257 : : }
4258 : : }
4259 : 0 : gcc_unreachable ();
4260 : : }
4261 : :
4262 : :
4263 : : /********************************************************************/
4264 : : /* A dumping machinery. */
4265 : :
4266 : : class dumper {
4267 : : public:
4268 : : enum {
4269 : : LOCATION = TDF_LINENO, /* -lineno:Source location streaming. */
4270 : : DEPEND = TDF_GRAPH, /* -graph:Dependency graph construction. */
4271 : : CLUSTER = TDF_BLOCKS, /* -blocks:Clusters. */
4272 : : TREE = TDF_UID, /* -uid:Tree streaming. */
4273 : : MERGE = TDF_ALIAS, /* -alias:Mergeable Entities. */
4274 : : ELF = TDF_ASMNAME, /* -asmname:Elf data. */
4275 : : MACRO = TDF_VOPS /* -vops:Macros. */
4276 : : };
4277 : :
4278 : : private:
4279 : : struct impl {
4280 : : typedef vec<module_state *, va_heap, vl_embed> stack_t;
4281 : :
4282 : : FILE *stream; /* Dump stream. */
4283 : : unsigned indent; /* Local indentation. */
4284 : : bool bol; /* Beginning of line. */
4285 : : stack_t stack; /* Trailing array of module_state. */
4286 : :
4287 : : bool nested_name (tree); /* Dump a name following DECL_CONTEXT. */
4288 : : };
4289 : :
4290 : : public:
4291 : : /* The dumper. */
4292 : : impl *dumps;
4293 : : dump_flags_t flags;
4294 : :
4295 : : public:
4296 : : /* Push/pop module state dumping. */
4297 : : unsigned push (module_state *);
4298 : : void pop (unsigned);
4299 : :
4300 : : public:
4301 : : /* Change local indentation. */
4302 : 273574639 : void indent ()
4303 : : {
4304 : 282 : if (dumps)
4305 : 499161 : dumps->indent++;
4306 : : }
4307 : 273574639 : void outdent ()
4308 : : {
4309 : 273574639 : if (dumps)
4310 : : {
4311 : 499161 : gcc_checking_assert (dumps->indent);
4312 : 499161 : dumps->indent--;
4313 : : }
4314 : 273574639 : }
4315 : :
4316 : : public:
4317 : : /* Is dump enabled?. */
4318 : 167806857 : bool operator () (int mask = 0)
4319 : : {
4320 : 3439187 : if (!dumps || !dumps->stream)
4321 : : return false;
4322 : 373562 : if (mask && !(mask & flags))
4323 : 3582 : return false;
4324 : : return true;
4325 : : }
4326 : : /* Dump some information. */
4327 : : bool operator () (const char *, ...);
4328 : : };
4329 : :
4330 : : /* The dumper. */
4331 : : static dumper dump = {0, dump_flags_t (0)};
4332 : :
4333 : : /* Push to dumping M. Return previous indentation level. */
4334 : :
4335 : : unsigned
4336 : 87443 : dumper::push (module_state *m)
4337 : : {
4338 : 87443 : FILE *stream = NULL;
4339 : 87443 : if (!dumps || !dumps->stack.length ())
4340 : : {
4341 : 86322 : stream = dump_begin (module_dump_id, &flags);
4342 : 86322 : if (!stream)
4343 : : return 0;
4344 : : }
4345 : :
4346 : 6690 : if (!dumps || !dumps->stack.space (1))
4347 : : {
4348 : : /* Create or extend the dump implementor. */
4349 : 1090 : unsigned current = dumps ? dumps->stack.length () : 0;
4350 : 575 : unsigned count = current ? current * 2 : EXPERIMENT (1, 20);
4351 : 1090 : size_t alloc = (offsetof (impl, stack)
4352 : 1090 : + impl::stack_t::embedded_size (count));
4353 : 1090 : dumps = XRESIZEVAR (impl, dumps, alloc);
4354 : 1090 : dumps->stack.embedded_init (count, current);
4355 : : }
4356 : 6690 : if (stream)
4357 : 5569 : dumps->stream = stream;
4358 : :
4359 : 6690 : unsigned n = dumps->indent;
4360 : 6690 : dumps->indent = 0;
4361 : 6690 : dumps->bol = true;
4362 : 6690 : dumps->stack.quick_push (m);
4363 : 6690 : if (m)
4364 : : {
4365 : 1789 : module_state *from = NULL;
4366 : :
4367 : 1789 : if (dumps->stack.length () > 1)
4368 : 570 : from = dumps->stack[dumps->stack.length () - 2];
4369 : : else
4370 : 1219 : dump ("");
4371 : 3245 : dump (from ? "Starting module %M (from %M)"
4372 : : : "Starting module %M", m, from);
4373 : : }
4374 : :
4375 : : return n;
4376 : : }
4377 : :
4378 : : /* Pop from dumping. Restore indentation to N. */
4379 : :
4380 : 87403 : void dumper::pop (unsigned n)
4381 : : {
4382 : 87403 : if (!dumps)
4383 : : return;
4384 : :
4385 : 13380 : gcc_checking_assert (dump () && !dumps->indent);
4386 : 6690 : if (module_state *m = dumps->stack[dumps->stack.length () - 1])
4387 : : {
4388 : 1789 : module_state *from = (dumps->stack.length () > 1
4389 : 1789 : ? dumps->stack[dumps->stack.length () - 2] : NULL);
4390 : 2026 : dump (from ? "Finishing module %M (returning to %M)"
4391 : : : "Finishing module %M", m, from);
4392 : : }
4393 : 6690 : dumps->stack.pop ();
4394 : 6690 : dumps->indent = n;
4395 : 6690 : if (!dumps->stack.length ())
4396 : : {
4397 : 5569 : dump_end (module_dump_id, dumps->stream);
4398 : 5569 : dumps->stream = NULL;
4399 : : }
4400 : : }
4401 : :
4402 : : /* Dump a nested name for arbitrary tree T. Sometimes it won't have a
4403 : : name. */
4404 : :
4405 : : bool
4406 : 464817 : dumper::impl::nested_name (tree t)
4407 : : {
4408 : 464817 : tree ti = NULL_TREE;
4409 : 464817 : int origin = -1;
4410 : 464817 : tree name = NULL_TREE;
4411 : :
4412 : 464817 : if (t && TREE_CODE (t) == TU_LOCAL_ENTITY)
4413 : 0 : t = TU_LOCAL_ENTITY_NAME (t);
4414 : :
4415 : 464793 : if (t && TREE_CODE (t) == TREE_BINFO)
4416 : 378 : t = BINFO_TYPE (t);
4417 : :
4418 : 464817 : if (t && TYPE_P (t))
4419 : 232581 : t = TYPE_NAME (t);
4420 : :
4421 : 464781 : if (t && DECL_P (t))
4422 : : {
4423 : 393734 : if (t == global_namespace || DECL_TEMPLATE_PARM_P (t))
4424 : : ;
4425 : 362962 : else if (tree ctx = DECL_CONTEXT (t))
4426 : 277213 : if (TREE_CODE (ctx) == TRANSLATION_UNIT_DECL
4427 : 277213 : || nested_name (ctx))
4428 : 277213 : fputs ("::", stream);
4429 : :
4430 : 393734 : int use_tpl;
4431 : 393734 : ti = node_template_info (t, use_tpl);
4432 : 127897 : if (ti && TREE_CODE (TI_TEMPLATE (ti)) == TEMPLATE_DECL
4433 : 521589 : && (DECL_TEMPLATE_RESULT (TI_TEMPLATE (ti)) == t))
4434 : : t = TI_TEMPLATE (ti);
4435 : 393734 : tree not_tmpl = t;
4436 : 393734 : if (TREE_CODE (t) == TEMPLATE_DECL)
4437 : : {
4438 : 22155 : fputs ("template ", stream);
4439 : 22155 : not_tmpl = DECL_TEMPLATE_RESULT (t);
4440 : : }
4441 : :
4442 : 22155 : if (not_tmpl
4443 : 393730 : && DECL_P (not_tmpl)
4444 : 393730 : && DECL_LANG_SPECIFIC (not_tmpl)
4445 : 229058 : && DECL_MODULE_IMPORT_P (not_tmpl))
4446 : : {
4447 : : /* We need to be careful here, so as to not explode on
4448 : : inconsistent data -- we're probably debugging, because
4449 : : Something Is Wrong. */
4450 : 16848 : unsigned index = import_entity_index (t, true);
4451 : 16848 : if (!(index & ~(~0u >> 1)))
4452 : 16290 : origin = import_entity_module (index)->mod;
4453 : 558 : else if (index > ~(~0u >> 1))
4454 : : /* An imported partition member that we're emitting. */
4455 : : origin = 0;
4456 : : else
4457 : 45 : origin = -2;
4458 : : }
4459 : :
4460 : 396745 : name = DECL_NAME (t) ? DECL_NAME (t)
4461 : 3989 : : HAS_DECL_ASSEMBLER_NAME_P (t) ? DECL_ASSEMBLER_NAME_RAW (t)
4462 : : : NULL_TREE;
4463 : : }
4464 : : else
4465 : : name = t;
4466 : :
4467 : 393734 : if (name)
4468 : 434790 : switch (TREE_CODE (name))
4469 : : {
4470 : 12466 : default:
4471 : 12466 : fputs ("#unnamed#", stream);
4472 : 12466 : break;
4473 : :
4474 : 403098 : case IDENTIFIER_NODE:
4475 : 403098 : fwrite (IDENTIFIER_POINTER (name), 1, IDENTIFIER_LENGTH (name), stream);
4476 : 403098 : break;
4477 : :
4478 : 19151 : case INTEGER_CST:
4479 : 19151 : print_hex (wi::to_wide (name), stream);
4480 : 19151 : break;
4481 : :
4482 : 75 : case STRING_CST:
4483 : : /* If TREE_TYPE is NULL, this is a raw string. */
4484 : 150 : fwrite (TREE_STRING_POINTER (name), 1,
4485 : 75 : TREE_STRING_LENGTH (name) - (TREE_TYPE (name) != NULL_TREE),
4486 : : stream);
4487 : 75 : break;
4488 : : }
4489 : : else
4490 : 30027 : fputs ("#null#", stream);
4491 : :
4492 : 464817 : if (origin >= 0)
4493 : : {
4494 : 16803 : const module_state *module = (*modules)[origin];
4495 : 33606 : fprintf (stream, "@%s:%d", !module ? "" : !module->name ? "(unnamed)"
4496 : 16803 : : module->get_flatname (), origin);
4497 : : }
4498 : 448014 : else if (origin == -2)
4499 : 45 : fprintf (stream, "@???");
4500 : :
4501 : 464817 : if (ti)
4502 : : {
4503 : 127897 : tree args = INNERMOST_TEMPLATE_ARGS (TI_ARGS (ti));
4504 : 127897 : fputs ("<", stream);
4505 : 127897 : if (args)
4506 : 320963 : for (int ix = 0; ix != TREE_VEC_LENGTH (args); ix++)
4507 : : {
4508 : 193066 : if (ix)
4509 : 65169 : fputs (",", stream);
4510 : 193066 : nested_name (TREE_VEC_ELT (args, ix));
4511 : : }
4512 : 127897 : fputs (">", stream);
4513 : : }
4514 : :
4515 : 464817 : return true;
4516 : : }
4517 : :
4518 : : /* Formatted dumping. FORMAT begins with '+' do not emit a trailing
4519 : : new line. (Normally it is appended.)
4520 : : Escapes:
4521 : : %C - tree_code
4522 : : %I - identifier
4523 : : %K - location_t or line_map_uint_t
4524 : : %M - module_state
4525 : : %N - name -- DECL_NAME
4526 : : %P - context:name pair
4527 : : %R - unsigned:unsigned ratio
4528 : : %S - symbol -- DECL_ASSEMBLER_NAME
4529 : : %U - long unsigned
4530 : : %V - version
4531 : : --- the following are printf-like, but without its flexibility
4532 : : %d - decimal int
4533 : : %p - pointer
4534 : : %s - string
4535 : : %u - unsigned int
4536 : : %x - hex int
4537 : :
4538 : : We do not implement the printf modifiers. */
4539 : :
4540 : : bool
4541 : 392970 : dumper::operator () (const char *format, ...)
4542 : : {
4543 : 392970 : if (!(*this) ())
4544 : : return false;
4545 : :
4546 : 337566 : bool no_nl = format[0] == '+';
4547 : 337566 : format += no_nl;
4548 : :
4549 : 337566 : if (dumps->bol)
4550 : : {
4551 : : /* Module import indent. */
4552 : 174311 : if (unsigned depth = dumps->stack.length () - 1)
4553 : : {
4554 : 19640 : const char *prefix = ">>>>";
4555 : 39262 : fprintf (dumps->stream, (depth <= strlen (prefix)
4556 : 19622 : ? &prefix[strlen (prefix) - depth]
4557 : : : ">.%d.>"), depth);
4558 : : }
4559 : :
4560 : : /* Local indent. */
4561 : 174311 : if (unsigned indent = dumps->indent)
4562 : : {
4563 : 92595 : const char *prefix = " ";
4564 : 181755 : fprintf (dumps->stream, (indent <= strlen (prefix)
4565 : 89160 : ? &prefix[strlen (prefix) - indent]
4566 : : : " .%d. "), indent);
4567 : : }
4568 : 174311 : dumps->bol = false;
4569 : : }
4570 : :
4571 : 337566 : va_list args;
4572 : 337566 : va_start (args, format);
4573 : 991579 : while (const char *esc = strchr (format, '%'))
4574 : : {
4575 : 654013 : fwrite (format, 1, (size_t)(esc - format), dumps->stream);
4576 : 654013 : format = ++esc;
4577 : 654013 : switch (*format++)
4578 : : {
4579 : 0 : default:
4580 : 0 : gcc_unreachable ();
4581 : :
4582 : 515 : case '%':
4583 : 515 : fputc ('%', dumps->stream);
4584 : 515 : break;
4585 : :
4586 : 101017 : case 'C': /* Code */
4587 : 101017 : {
4588 : 101017 : tree_code code = (tree_code)va_arg (args, unsigned);
4589 : 101017 : fputs (get_tree_code_name (code), dumps->stream);
4590 : : }
4591 : 101017 : break;
4592 : :
4593 : 81 : case 'I': /* Identifier. */
4594 : 81 : {
4595 : 81 : tree t = va_arg (args, tree);
4596 : 81 : dumps->nested_name (t);
4597 : : }
4598 : 81 : break;
4599 : :
4600 : 4063 : case 'K': /* location_t, either 32- or 64-bit. */
4601 : 4063 : {
4602 : 4063 : unsigned long long u = va_arg (args, location_t);
4603 : 4063 : fprintf (dumps->stream, "%llu", u);
4604 : : }
4605 : 4063 : break;
4606 : :
4607 : 6882 : case 'M': /* Module. */
4608 : 6882 : {
4609 : 6882 : const char *str = "(none)";
4610 : 6882 : if (module_state *m = va_arg (args, module_state *))
4611 : : {
4612 : 6882 : if (!m->has_location ())
4613 : : str = "(detached)";
4614 : : else
4615 : 6882 : str = m->get_flatname ();
4616 : : }
4617 : 6882 : fputs (str, dumps->stream);
4618 : : }
4619 : 6882 : break;
4620 : :
4621 : 111016 : case 'N': /* Name. */
4622 : 111016 : {
4623 : 111016 : tree t = va_arg (args, tree);
4624 : 222494 : while (t && TREE_CODE (t) == OVERLOAD)
4625 : 462 : t = OVL_FUNCTION (t);
4626 : 111016 : fputc ('\'', dumps->stream);
4627 : 111016 : dumps->nested_name (t);
4628 : 111016 : fputc ('\'', dumps->stream);
4629 : : }
4630 : 111016 : break;
4631 : :
4632 : 6465 : case 'P': /* Pair. */
4633 : 6465 : {
4634 : 6465 : tree ctx = va_arg (args, tree);
4635 : 6465 : tree name = va_arg (args, tree);
4636 : 6465 : fputc ('\'', dumps->stream);
4637 : 6465 : dumps->nested_name (ctx);
4638 : 6465 : if (ctx && ctx != global_namespace)
4639 : 735 : fputs ("::", dumps->stream);
4640 : 6465 : dumps->nested_name (name);
4641 : 6465 : fputc ('\'', dumps->stream);
4642 : : }
4643 : 6465 : break;
4644 : :
4645 : 753 : case 'R': /* Ratio */
4646 : 753 : {
4647 : 753 : unsigned a = va_arg (args, unsigned);
4648 : 753 : unsigned b = va_arg (args, unsigned);
4649 : 753 : fprintf (dumps->stream, "%.1f", (float) a / (b + !b));
4650 : : }
4651 : 753 : break;
4652 : :
4653 : 32859 : case 'S': /* Symbol name */
4654 : 32859 : {
4655 : 32859 : tree t = va_arg (args, tree);
4656 : 32859 : if (t && TYPE_P (t))
4657 : 11778 : t = TYPE_NAME (t);
4658 : 31425 : if (t && HAS_DECL_ASSEMBLER_NAME_P (t)
4659 : 29997 : && DECL_ASSEMBLER_NAME_SET_P (t))
4660 : : {
4661 : 165 : fputc ('(', dumps->stream);
4662 : 165 : fputs (IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (t)),
4663 : 165 : dumps->stream);
4664 : 165 : fputc (')', dumps->stream);
4665 : : }
4666 : : }
4667 : : break;
4668 : :
4669 : 0 : case 'U': /* long unsigned. */
4670 : 0 : {
4671 : 0 : unsigned long u = va_arg (args, unsigned long);
4672 : 0 : fprintf (dumps->stream, "%lu", u);
4673 : : }
4674 : 0 : break;
4675 : :
4676 : 722 : case 'V': /* Version. */
4677 : 722 : {
4678 : 722 : unsigned v = va_arg (args, unsigned);
4679 : 722 : verstr_t string;
4680 : :
4681 : 722 : version2string (v, string);
4682 : 722 : fputs (string, dumps->stream);
4683 : : }
4684 : 722 : break;
4685 : :
4686 : 0 : case 'c': /* Character. */
4687 : 0 : {
4688 : 0 : int c = va_arg (args, int);
4689 : 0 : fputc (c, dumps->stream);
4690 : : }
4691 : 0 : break;
4692 : :
4693 : 57947 : case 'd': /* Decimal Int. */
4694 : 57947 : {
4695 : 57947 : int d = va_arg (args, int);
4696 : 57947 : fprintf (dumps->stream, "%d", d);
4697 : : }
4698 : 57947 : break;
4699 : :
4700 : 0 : case 'p': /* Pointer. */
4701 : 0 : {
4702 : 0 : void *p = va_arg (args, void *);
4703 : 0 : fprintf (dumps->stream, "%p", p);
4704 : : }
4705 : 0 : break;
4706 : :
4707 : 113103 : case 's': /* String. */
4708 : 113103 : {
4709 : 113103 : const char *s = va_arg (args, char *);
4710 : 113103 : gcc_checking_assert (s);
4711 : 113103 : fputs (s, dumps->stream);
4712 : : }
4713 : 113103 : break;
4714 : :
4715 : 216275 : case 'u': /* Unsigned. */
4716 : 216275 : {
4717 : 216275 : unsigned u = va_arg (args, unsigned);
4718 : 216275 : fprintf (dumps->stream, "%u", u);
4719 : : }
4720 : 216275 : break;
4721 : :
4722 : 2315 : case 'x': /* Hex. */
4723 : 2315 : {
4724 : 2315 : unsigned x = va_arg (args, unsigned);
4725 : 2315 : fprintf (dumps->stream, "%x", x);
4726 : : }
4727 : 2315 : break;
4728 : : }
4729 : : }
4730 : 337566 : fputs (format, dumps->stream);
4731 : 337566 : va_end (args);
4732 : 337566 : if (!no_nl)
4733 : : {
4734 : 174311 : dumps->bol = true;
4735 : 174311 : fputc ('\n', dumps->stream);
4736 : : }
4737 : : return true;
4738 : : }
4739 : :
4740 : : struct note_def_cache_hasher : ggc_cache_ptr_hash<tree_node>
4741 : : {
4742 : 150153 : static int keep_cache_entry (tree t)
4743 : : {
4744 : 150153 : if (!CHECKING_P)
4745 : : /* GTY is unfortunately not clever enough to conditionalize
4746 : : this. */
4747 : : gcc_unreachable ();
4748 : :
4749 : 150153 : if (ggc_marked_p (t))
4750 : : return -1;
4751 : :
4752 : 0 : unsigned n = dump.push (NULL);
4753 : : /* This might or might not be an error. We should note its
4754 : : dropping whichever. */
4755 : 0 : dump () && dump ("Dropping %N from note_defs table", t);
4756 : 0 : dump.pop (n);
4757 : :
4758 : 0 : return 0;
4759 : : }
4760 : : };
4761 : :
4762 : : /* We should stream each definition at most once.
4763 : : This needs to be a cache because there are cases where a definition
4764 : : ends up being not retained, and we need to drop those so we don't
4765 : : get confused if memory is reallocated. */
4766 : : typedef hash_table<note_def_cache_hasher> note_defs_table_t;
4767 : : static GTY((cache)) note_defs_table_t *note_defs;
4768 : :
4769 : : void
4770 : 235880 : trees_in::assert_definition (tree decl ATTRIBUTE_UNUSED,
4771 : : bool installing ATTRIBUTE_UNUSED)
4772 : : {
4773 : : #if CHECKING_P
4774 : 235880 : tree *slot = note_defs->find_slot (decl, installing ? INSERT : NO_INSERT);
4775 : 235880 : tree not_tmpl = STRIP_TEMPLATE (decl);
4776 : 235880 : if (installing)
4777 : : {
4778 : : /* We must be inserting for the first time. */
4779 : 112134 : gcc_assert (!*slot);
4780 : 112134 : *slot = decl;
4781 : : }
4782 : : else
4783 : : /* If this is not the mergeable entity, it should not be in the
4784 : : table. If it is a non-global-module mergeable entity, it
4785 : : should be in the table. Global module entities could have been
4786 : : defined textually in the current TU and so might or might not
4787 : : be present. */
4788 : 123746 : gcc_assert (!is_duplicate (decl)
4789 : : ? !slot
4790 : : : (slot
4791 : : || !DECL_LANG_SPECIFIC (not_tmpl)
4792 : : || !DECL_MODULE_PURVIEW_P (not_tmpl)
4793 : : || (!DECL_MODULE_IMPORT_P (not_tmpl)
4794 : : && header_module_p ())));
4795 : :
4796 : 235880 : if (not_tmpl != decl)
4797 : 139719 : gcc_assert (!note_defs->find_slot (not_tmpl, NO_INSERT));
4798 : : #endif
4799 : 235880 : }
4800 : :
4801 : : void
4802 : 286385 : trees_out::assert_definition (tree decl ATTRIBUTE_UNUSED)
4803 : : {
4804 : : #if CHECKING_P
4805 : 286385 : tree *slot = note_defs->find_slot (decl, INSERT);
4806 : 286385 : gcc_assert (!*slot);
4807 : 286385 : *slot = decl;
4808 : 286385 : if (TREE_CODE (decl) == TEMPLATE_DECL)
4809 : 166958 : gcc_assert (!note_defs->find_slot (DECL_TEMPLATE_RESULT (decl), NO_INSERT));
4810 : : #endif
4811 : 286385 : }
4812 : :
4813 : : /********************************************************************/
4814 : : static bool
4815 : 11080 : noisy_p ()
4816 : : {
4817 : 0 : if (quiet_flag)
4818 : : return false;
4819 : :
4820 : 0 : pp_needs_newline (global_dc->get_reference_printer ()) = true;
4821 : 0 : diagnostic_set_last_function (global_dc, (diagnostic_info *) NULL);
4822 : :
4823 : 0 : return true;
4824 : : }
4825 : :
4826 : : /* Set the cmi repo. Strip trailing '/', '.' becomes NULL. */
4827 : :
4828 : : static void
4829 : 97832 : set_cmi_repo (const char *r)
4830 : : {
4831 : 97832 : XDELETEVEC (cmi_repo);
4832 : 97832 : XDELETEVEC (cmi_path);
4833 : 97832 : cmi_path_alloc = 0;
4834 : :
4835 : 97832 : cmi_repo = NULL;
4836 : 97832 : cmi_repo_length = 0;
4837 : :
4838 : 97832 : if (!r || !r[0])
4839 : : return;
4840 : :
4841 : 4286 : size_t len = strlen (r);
4842 : 4286 : cmi_repo = XNEWVEC (char, len + 1);
4843 : 4286 : memcpy (cmi_repo, r, len + 1);
4844 : :
4845 : 4286 : if (len > 1 && IS_DIR_SEPARATOR (cmi_repo[len-1]))
4846 : 4286 : len--;
4847 : 4286 : if (len == 1 && cmi_repo[0] == '.')
4848 : 21 : len--;
4849 : 4286 : cmi_repo[len] = 0;
4850 : 4286 : cmi_repo_length = len;
4851 : : }
4852 : :
4853 : : /* TO is a repo-relative name. Provide one that we may use from where
4854 : : we are. */
4855 : :
4856 : : static const char *
4857 : 5227 : maybe_add_cmi_prefix (const char *to, size_t *len_p = NULL)
4858 : : {
4859 : 5227 : size_t len = len_p || cmi_repo_length ? strlen (to) : 0;
4860 : :
4861 : 5227 : if (cmi_repo_length && !IS_ABSOLUTE_PATH (to))
4862 : : {
4863 : 5206 : if (cmi_path_alloc < cmi_repo_length + len + 2)
4864 : : {
4865 : 4202 : XDELETEVEC (cmi_path);
4866 : 4202 : cmi_path_alloc = cmi_repo_length + len * 2 + 2;
4867 : 4202 : cmi_path = XNEWVEC (char, cmi_path_alloc);
4868 : :
4869 : 4202 : memcpy (cmi_path, cmi_repo, cmi_repo_length);
4870 : 4202 : cmi_path[cmi_repo_length] = DIR_SEPARATOR;
4871 : : }
4872 : :
4873 : 5206 : memcpy (&cmi_path[cmi_repo_length + 1], to, len + 1);
4874 : 5206 : len += cmi_repo_length + 1;
4875 : 5206 : to = cmi_path;
4876 : : }
4877 : :
4878 : 5227 : if (len_p)
4879 : 2518 : *len_p = len;
4880 : :
4881 : 5227 : return to;
4882 : : }
4883 : :
4884 : : /* Try and create the directories of PATH. */
4885 : :
4886 : : static void
4887 : 34 : create_dirs (char *path)
4888 : : {
4889 : : /* Try and create the missing directories. */
4890 : 2056 : for (char *base = path; *base; base++)
4891 : 2022 : if (IS_DIR_SEPARATOR (*base))
4892 : : {
4893 : 203 : char sep = *base;
4894 : 203 : *base = 0;
4895 : 203 : int failed = mkdir (path, S_IRWXU | S_IRWXG | S_IRWXO);
4896 : 217 : dump () && dump ("Mkdir ('%s') errno:=%u", path, failed ? errno : 0);
4897 : 203 : *base = sep;
4898 : 203 : if (failed
4899 : : /* Maybe racing with another creator (of a *different*
4900 : : module). */
4901 : 41 : && errno != EEXIST)
4902 : : break;
4903 : : }
4904 : 34 : }
4905 : :
4906 : : /* Given a CLASSTYPE_DECL_LIST VALUE get the template friend decl,
4907 : : if that's what this is. */
4908 : :
4909 : : static tree
4910 : 57328 : friend_from_decl_list (tree frnd)
4911 : : {
4912 : 57328 : tree res = frnd;
4913 : :
4914 : 57328 : if (TREE_CODE (frnd) != TEMPLATE_DECL)
4915 : : {
4916 : 33626 : tree tmpl = NULL_TREE;
4917 : 33626 : if (TYPE_P (frnd))
4918 : : {
4919 : 5415 : res = TYPE_NAME (frnd);
4920 : 5331 : if (CLASS_TYPE_P (frnd)
4921 : 10746 : && CLASSTYPE_TEMPLATE_INFO (frnd))
4922 : 5322 : tmpl = CLASSTYPE_TI_TEMPLATE (frnd);
4923 : : }
4924 : 28211 : else if (DECL_TEMPLATE_INFO (frnd))
4925 : : {
4926 : 28211 : tmpl = DECL_TI_TEMPLATE (frnd);
4927 : 28211 : if (TREE_CODE (tmpl) != TEMPLATE_DECL)
4928 : : tmpl = NULL_TREE;
4929 : : }
4930 : :
4931 : 38459 : if (tmpl && DECL_TEMPLATE_RESULT (tmpl) == res)
4932 : : res = tmpl;
4933 : : }
4934 : :
4935 : 57328 : return res;
4936 : : }
4937 : :
4938 : : static tree
4939 : 17377 : find_enum_member (tree ctx, tree name)
4940 : : {
4941 : 17377 : for (tree values = TYPE_VALUES (ctx);
4942 : 377796 : values; values = TREE_CHAIN (values))
4943 : 372820 : if (DECL_NAME (TREE_VALUE (values)) == name)
4944 : : return TREE_VALUE (values);
4945 : :
4946 : : return NULL_TREE;
4947 : : }
4948 : :
4949 : : /********************************************************************/
4950 : : /* Instrumentation gathered writing bytes. */
4951 : :
4952 : : void
4953 : 251 : bytes_out::instrument ()
4954 : : {
4955 : 251 : dump ("Wrote %u bytes in %u blocks", lengths[3], spans[3]);
4956 : 251 : dump ("Wrote %u bits in %u bytes", lengths[0] + lengths[1], lengths[2]);
4957 : 753 : for (unsigned ix = 0; ix < 2; ix++)
4958 : 753 : dump (" %u %s spans of %R bits", spans[ix],
4959 : : ix ? "one" : "zero", lengths[ix], spans[ix]);
4960 : 251 : dump (" %u blocks with %R bits padding", spans[2],
4961 : 251 : lengths[2] * 8 - (lengths[0] + lengths[1]), spans[2]);
4962 : 251 : }
4963 : :
4964 : : /* Instrumentation gathered writing trees. */
4965 : : void
4966 : 2381 : trees_out::instrument ()
4967 : : {
4968 : 2381 : if (dump (""))
4969 : : {
4970 : 251 : bytes_out::instrument ();
4971 : 251 : dump ("Wrote:");
4972 : 251 : dump (" %u decl trees", decl_val_count);
4973 : 251 : dump (" %u other trees", tree_val_count);
4974 : 251 : dump (" %u back references", back_ref_count);
4975 : 251 : dump (" %u TU-local entities", tu_local_count);
4976 : 251 : dump (" %u null trees", null_count);
4977 : : }
4978 : 2381 : }
4979 : :
4980 : : /* Setup and teardown for a tree walk. */
4981 : :
4982 : : void
4983 : 1770628 : trees_out::begin ()
4984 : : {
4985 : 1770628 : gcc_assert (!streaming_p () || !tree_map.elements ());
4986 : :
4987 : 1770628 : mark_trees ();
4988 : 1770628 : if (streaming_p ())
4989 : 223752 : parent::begin ();
4990 : 1770628 : }
4991 : :
4992 : : unsigned
4993 : 223752 : trees_out::end (elf_out *sink, unsigned name, unsigned *crc_ptr)
4994 : : {
4995 : 223752 : gcc_checking_assert (streaming_p ());
4996 : :
4997 : 223752 : unmark_trees ();
4998 : 223752 : return parent::end (sink, name, crc_ptr);
4999 : : }
5000 : :
5001 : : void
5002 : 1546876 : trees_out::end ()
5003 : : {
5004 : 1546876 : gcc_assert (!streaming_p ());
5005 : :
5006 : 1546876 : unmark_trees ();
5007 : : /* Do not parent::end -- we weren't streaming. */
5008 : 1546876 : }
5009 : :
5010 : : void
5011 : 1770628 : trees_out::mark_trees ()
5012 : : {
5013 : 1770628 : if (size_t size = tree_map.elements ())
5014 : : {
5015 : : /* This isn't our first rodeo, destroy and recreate the
5016 : : tree_map. I'm a bad bad man. Use the previous size as a
5017 : : guess for the next one (so not all bad). */
5018 : 1331141 : tree_map.~ptr_int_hash_map ();
5019 : 1331141 : new (&tree_map) ptr_int_hash_map (size);
5020 : : }
5021 : :
5022 : : /* Install the fixed trees, with +ve references. */
5023 : 1770628 : unsigned limit = fixed_trees->length ();
5024 : 341524543 : for (unsigned ix = 0; ix != limit; ix++)
5025 : : {
5026 : 339753915 : tree val = (*fixed_trees)[ix];
5027 : 339753915 : bool existed = tree_map.put (val, ix + tag_fixed);
5028 : 339753915 : gcc_checking_assert (!TREE_VISITED (val) && !existed);
5029 : 339753915 : TREE_VISITED (val) = true;
5030 : : }
5031 : :
5032 : 1770628 : ref_num = 0;
5033 : 1770628 : }
5034 : :
5035 : : /* Unmark the trees we encountered */
5036 : :
5037 : : void
5038 : 1770628 : trees_out::unmark_trees ()
5039 : : {
5040 : 1770628 : ptr_int_hash_map::iterator end (tree_map.end ());
5041 : 401443397 : for (ptr_int_hash_map::iterator iter (tree_map.begin ()); iter != end; ++iter)
5042 : : {
5043 : 399672769 : tree node = reinterpret_cast<tree> ((*iter).first);
5044 : 399672769 : int ref = (*iter).second;
5045 : : /* We should have visited the node, and converted its mergeable
5046 : : reference to a regular reference. */
5047 : 399672769 : gcc_checking_assert (TREE_VISITED (node)
5048 : : && (ref <= tag_backref || ref >= tag_fixed));
5049 : 399672769 : TREE_VISITED (node) = false;
5050 : : }
5051 : 1770628 : }
5052 : :
5053 : : /* Mark DECL for by-value walking. We do this by inserting it into
5054 : : the tree map with a reference of zero. May be called multiple
5055 : : times on the same node. */
5056 : :
5057 : : void
5058 : 2741414 : trees_out::mark_by_value (tree decl)
5059 : : {
5060 : 2741414 : gcc_checking_assert (DECL_P (decl)
5061 : : /* Enum consts are INTEGER_CSTS. */
5062 : : || TREE_CODE (decl) == INTEGER_CST
5063 : : || TREE_CODE (decl) == TREE_BINFO);
5064 : :
5065 : 2741414 : if (TREE_VISITED (decl))
5066 : : /* Must already be forced or fixed. */
5067 : 2264 : gcc_checking_assert (*tree_map.get (decl) >= tag_value);
5068 : : else
5069 : : {
5070 : 2739150 : bool existed = tree_map.put (decl, tag_value);
5071 : 2739150 : gcc_checking_assert (!existed);
5072 : 2739150 : TREE_VISITED (decl) = true;
5073 : : }
5074 : 2741414 : }
5075 : :
5076 : : int
5077 : 73005975 : trees_out::get_tag (tree t)
5078 : : {
5079 : 73005975 : gcc_checking_assert (TREE_VISITED (t));
5080 : 73005975 : return *tree_map.get (t);
5081 : : }
5082 : :
5083 : : /* Insert T into the map, return its tag number. */
5084 : :
5085 : : int
5086 : 59918854 : trees_out::insert (tree t, walk_kind walk)
5087 : : {
5088 : 59918854 : gcc_checking_assert (walk != WK_normal || !TREE_VISITED (t));
5089 : 59918854 : int tag = --ref_num;
5090 : 59918854 : bool existed;
5091 : 59918854 : int &slot = tree_map.get_or_insert (t, &existed);
5092 : 59918854 : gcc_checking_assert (TREE_VISITED (t) == existed
5093 : : && (!existed
5094 : : || (walk == WK_value && slot == tag_value)));
5095 : 59918854 : TREE_VISITED (t) = true;
5096 : 59918854 : slot = tag;
5097 : :
5098 : 59918854 : return tag;
5099 : : }
5100 : :
5101 : : /* Insert T into the backreference array. Return its back reference
5102 : : number. */
5103 : :
5104 : : int
5105 : 14545759 : trees_in::insert (tree t)
5106 : : {
5107 : 14545759 : gcc_checking_assert (t || get_overrun ());
5108 : 14545759 : back_refs.safe_push (t);
5109 : 14545759 : return -(int)back_refs.length ();
5110 : : }
5111 : :
5112 : : /* A chained set of decls. */
5113 : :
5114 : : void
5115 : 75814 : trees_out::chained_decls (tree decls)
5116 : : {
5117 : 193062 : for (; decls; decls = DECL_CHAIN (decls))
5118 : 117248 : tree_node (decls);
5119 : 75814 : tree_node (NULL_TREE);
5120 : 75814 : }
5121 : :
5122 : : tree
5123 : 33001 : trees_in::chained_decls ()
5124 : : {
5125 : 33001 : tree decls = NULL_TREE;
5126 : 33001 : for (tree *chain = &decls;;)
5127 : 83879 : if (tree decl = tree_node ())
5128 : : {
5129 : 50878 : if (!DECL_P (decl) || DECL_CHAIN (decl))
5130 : : {
5131 : 0 : set_overrun ();
5132 : 0 : break;
5133 : : }
5134 : 50878 : *chain = decl;
5135 : 50878 : chain = &DECL_CHAIN (decl);
5136 : : }
5137 : : else
5138 : 50878 : break;
5139 : :
5140 : 33001 : return decls;
5141 : : }
5142 : :
5143 : : /* A vector of decls following DECL_CHAIN. */
5144 : :
5145 : : void
5146 : 282424 : trees_out::vec_chained_decls (tree decls)
5147 : : {
5148 : 282424 : if (streaming_p ())
5149 : : {
5150 : : unsigned len = 0;
5151 : :
5152 : 792171 : for (tree decl = decls; decl; decl = DECL_CHAIN (decl))
5153 : 650999 : len++;
5154 : 141172 : u (len);
5155 : : }
5156 : :
5157 : 1584748 : for (tree decl = decls; decl; decl = DECL_CHAIN (decl))
5158 : : {
5159 : 293755 : if (DECL_IMPLICIT_TYPEDEF_P (decl)
5160 : 1313363 : && TYPE_NAME (TREE_TYPE (decl)) != decl)
5161 : : /* An anonynmous struct with a typedef name. An odd thing to
5162 : : write. */
5163 : 8 : tree_node (NULL_TREE);
5164 : : else
5165 : 1302316 : tree_node (decl);
5166 : : }
5167 : 282424 : }
5168 : :
5169 : : vec<tree, va_heap> *
5170 : 102460 : trees_in::vec_chained_decls ()
5171 : : {
5172 : 102460 : vec<tree, va_heap> *v = NULL;
5173 : :
5174 : 102460 : if (unsigned len = u ())
5175 : : {
5176 : 53785 : vec_alloc (v, len);
5177 : :
5178 : 596881 : for (unsigned ix = 0; ix < len; ix++)
5179 : : {
5180 : 543096 : tree decl = tree_node ();
5181 : 543096 : if (decl && !DECL_P (decl))
5182 : : {
5183 : 0 : set_overrun ();
5184 : 0 : break;
5185 : : }
5186 : 543096 : v->quick_push (decl);
5187 : : }
5188 : :
5189 : 53785 : if (get_overrun ())
5190 : : {
5191 : 0 : vec_free (v);
5192 : 0 : v = NULL;
5193 : : }
5194 : : }
5195 : :
5196 : 102460 : return v;
5197 : : }
5198 : :
5199 : : /* A vector of trees. */
5200 : :
5201 : : void
5202 : 201828 : trees_out::tree_vec (vec<tree, va_gc> *v)
5203 : : {
5204 : 201828 : unsigned len = vec_safe_length (v);
5205 : 201828 : if (streaming_p ())
5206 : 100894 : u (len);
5207 : 260988 : for (unsigned ix = 0; ix != len; ix++)
5208 : 59160 : tree_node ((*v)[ix]);
5209 : 201828 : }
5210 : :
5211 : : vec<tree, va_gc> *
5212 : 73136 : trees_in::tree_vec ()
5213 : : {
5214 : 73136 : vec<tree, va_gc> *v = NULL;
5215 : 73136 : if (unsigned len = u ())
5216 : : {
5217 : 19523 : vec_alloc (v, len);
5218 : 40934 : for (unsigned ix = 0; ix != len; ix++)
5219 : 21411 : v->quick_push (tree_node ());
5220 : : }
5221 : 73136 : return v;
5222 : : }
5223 : :
5224 : : /* A vector of tree pairs. */
5225 : :
5226 : : void
5227 : 4820 : trees_out::tree_pair_vec (vec<tree_pair_s, va_gc> *v)
5228 : : {
5229 : 4820 : unsigned len = vec_safe_length (v);
5230 : 4820 : if (streaming_p ())
5231 : 2410 : u (len);
5232 : 4820 : if (len)
5233 : 25734 : for (unsigned ix = 0; ix != len; ix++)
5234 : : {
5235 : 21022 : tree_pair_s const &s = (*v)[ix];
5236 : 21022 : tree_node (s.purpose);
5237 : 21022 : tree_node (s.value);
5238 : : }
5239 : 4820 : }
5240 : :
5241 : : vec<tree_pair_s, va_gc> *
5242 : 2078 : trees_in::tree_pair_vec ()
5243 : : {
5244 : 2078 : vec<tree_pair_s, va_gc> *v = NULL;
5245 : 2078 : if (unsigned len = u ())
5246 : : {
5247 : 2032 : vec_alloc (v, len);
5248 : 11293 : for (unsigned ix = 0; ix != len; ix++)
5249 : : {
5250 : 9261 : tree_pair_s s;
5251 : 9261 : s.purpose = tree_node ();
5252 : 9261 : s.value = tree_node ();
5253 : 9261 : v->quick_push (s);
5254 : : }
5255 : : }
5256 : 2078 : return v;
5257 : : }
5258 : :
5259 : : void
5260 : 296342 : trees_out::tree_list (tree list, bool has_purpose)
5261 : : {
5262 : 1188183 : for (; list; list = TREE_CHAIN (list))
5263 : : {
5264 : 891841 : gcc_checking_assert (TREE_VALUE (list));
5265 : 891841 : tree_node (TREE_VALUE (list));
5266 : 891841 : if (has_purpose)
5267 : 864061 : tree_node (TREE_PURPOSE (list));
5268 : : }
5269 : 296342 : tree_node (NULL_TREE);
5270 : 296342 : }
5271 : :
5272 : : tree
5273 : 108868 : trees_in::tree_list (bool has_purpose)
5274 : : {
5275 : 108868 : tree res = NULL_TREE;
5276 : :
5277 : 483109 : for (tree *chain = &res; tree value = tree_node ();
5278 : 748482 : chain = &TREE_CHAIN (*chain))
5279 : : {
5280 : 374241 : tree purpose = has_purpose ? tree_node () : NULL_TREE;
5281 : 374241 : *chain = build_tree_list (purpose, value);
5282 : 374241 : }
5283 : :
5284 : 108868 : return res;
5285 : : }
5286 : :
5287 : : #define CASE_OMP_SIMD_CODE \
5288 : : case OMP_SIMD: \
5289 : : case OMP_STRUCTURED_BLOCK: \
5290 : : case OMP_LOOP: \
5291 : : case OMP_ORDERED: \
5292 : : case OMP_TILE: \
5293 : : case OMP_UNROLL
5294 : : #define CASE_OMP_CODE \
5295 : : case OMP_PARALLEL: \
5296 : : case OMP_TASK: \
5297 : : case OMP_FOR: \
5298 : : case OMP_DISTRIBUTE: \
5299 : : case OMP_TASKLOOP: \
5300 : : case OMP_TEAMS: \
5301 : : case OMP_TARGET_DATA: \
5302 : : case OMP_TARGET: \
5303 : : case OMP_SECTIONS: \
5304 : : case OMP_CRITICAL: \
5305 : : case OMP_SINGLE: \
5306 : : case OMP_SCOPE: \
5307 : : case OMP_TASKGROUP: \
5308 : : case OMP_MASKED: \
5309 : : case OMP_DISPATCH: \
5310 : : case OMP_INTEROP: \
5311 : : case OMP_MASTER: \
5312 : : case OMP_TARGET_UPDATE: \
5313 : : case OMP_TARGET_ENTER_DATA: \
5314 : : case OMP_TARGET_EXIT_DATA: \
5315 : : case OMP_METADIRECTIVE: \
5316 : : case OMP_ATOMIC: \
5317 : : case OMP_ATOMIC_READ: \
5318 : : case OMP_ATOMIC_CAPTURE_OLD: \
5319 : : case OMP_ATOMIC_CAPTURE_NEW
5320 : : #define CASE_OACC_CODE \
5321 : : case OACC_PARALLEL: \
5322 : : case OACC_KERNELS: \
5323 : : case OACC_SERIAL: \
5324 : : case OACC_DATA: \
5325 : : case OACC_HOST_DATA: \
5326 : : case OACC_LOOP: \
5327 : : case OACC_CACHE: \
5328 : : case OACC_DECLARE: \
5329 : : case OACC_ENTER_DATA: \
5330 : : case OACC_EXIT_DATA: \
5331 : : case OACC_UPDATE
5332 : :
5333 : : /* Start tree write. Write information to allocate the receiving
5334 : : node. */
5335 : :
5336 : : void
5337 : 12093432 : trees_out::start (tree t, bool code_streamed)
5338 : : {
5339 : 12093432 : if (TYPE_P (t))
5340 : : {
5341 : 500110 : enum tree_code code = TREE_CODE (t);
5342 : 500110 : gcc_checking_assert (TYPE_MAIN_VARIANT (t) == t);
5343 : : /* All these types are TYPE_NON_COMMON. */
5344 : 500110 : gcc_checking_assert (code == RECORD_TYPE
5345 : : || code == UNION_TYPE
5346 : : || code == ENUMERAL_TYPE
5347 : : || code == TEMPLATE_TYPE_PARM
5348 : : || code == TEMPLATE_TEMPLATE_PARM
5349 : : || code == BOUND_TEMPLATE_TEMPLATE_PARM);
5350 : : }
5351 : :
5352 : 12093432 : if (!code_streamed)
5353 : 11645866 : u (TREE_CODE (t));
5354 : :
5355 : 12093432 : switch (TREE_CODE (t))
5356 : : {
5357 : 10707638 : default:
5358 : 10707638 : if (VL_EXP_CLASS_P (t))
5359 : 447279 : u (VL_EXP_OPERAND_LENGTH (t));
5360 : : break;
5361 : :
5362 : 548974 : case INTEGER_CST:
5363 : 548974 : u (TREE_INT_CST_NUNITS (t));
5364 : 548974 : u (TREE_INT_CST_EXT_NUNITS (t));
5365 : 548974 : break;
5366 : :
5367 : 12 : case OMP_CLAUSE:
5368 : 12 : u (OMP_CLAUSE_CODE (t));
5369 : 12 : break;
5370 : :
5371 : 6 : CASE_OMP_SIMD_CODE:
5372 : 6 : state->extensions |= SE_OPENMP_SIMD;
5373 : 6 : break;
5374 : :
5375 : 3 : CASE_OMP_CODE:
5376 : 3 : state->extensions |= SE_OPENMP;
5377 : 3 : break;
5378 : :
5379 : 6 : CASE_OACC_CODE:
5380 : 6 : state->extensions |= SE_OPENACC;
5381 : 6 : break;
5382 : :
5383 : 39760 : case STRING_CST:
5384 : 39760 : str (TREE_STRING_POINTER (t), TREE_STRING_LENGTH (t));
5385 : 39760 : break;
5386 : :
5387 : 18 : case RAW_DATA_CST:
5388 : 18 : if (RAW_DATA_OWNER (t) == NULL_TREE)
5389 : : {
5390 : : /* Stream RAW_DATA_CST with no owner (i.e. data pointing
5391 : : into libcpp buffers) as something we can stream in as
5392 : : STRING_CST which owns the data. */
5393 : 6 : u (0);
5394 : : /* Can't use str (RAW_DATA_POINTER (t), RAW_DATA_LENGTH (t));
5395 : : here as there isn't a null termination after it. */
5396 : 6 : z (RAW_DATA_LENGTH (t));
5397 : 6 : if (RAW_DATA_LENGTH (t))
5398 : 6 : if (void *ptr = buf (RAW_DATA_LENGTH (t) + 1))
5399 : : {
5400 : 6 : memcpy (ptr, RAW_DATA_POINTER (t), RAW_DATA_LENGTH (t));
5401 : 6 : ((char *) ptr)[RAW_DATA_LENGTH (t)] = '\0';
5402 : : }
5403 : : }
5404 : : else
5405 : : {
5406 : 12 : gcc_assert (RAW_DATA_LENGTH (t));
5407 : 12 : u (RAW_DATA_LENGTH (t));
5408 : : }
5409 : : break;
5410 : :
5411 : 18 : case VECTOR_CST:
5412 : 18 : u (VECTOR_CST_LOG2_NPATTERNS (t));
5413 : 18 : u (VECTOR_CST_NELTS_PER_PATTERN (t));
5414 : 18 : break;
5415 : :
5416 : 98484 : case TREE_BINFO:
5417 : 98484 : u (BINFO_N_BASE_BINFOS (t));
5418 : 98484 : break;
5419 : :
5420 : 698513 : case TREE_VEC:
5421 : 698513 : u (TREE_VEC_LENGTH (t));
5422 : 698513 : break;
5423 : :
5424 : 0 : case FIXED_CST:
5425 : 0 : gcc_unreachable (); /* Not supported in C++. */
5426 : 0 : break;
5427 : :
5428 : 0 : case IDENTIFIER_NODE:
5429 : 0 : case SSA_NAME:
5430 : 0 : case TARGET_MEM_REF:
5431 : 0 : case TRANSLATION_UNIT_DECL:
5432 : : /* We shouldn't meet these. */
5433 : 0 : gcc_unreachable ();
5434 : 12093432 : break;
5435 : : }
5436 : 12093432 : }
5437 : :
5438 : : /* Start tree read. Allocate the receiving node. */
5439 : :
5440 : : tree
5441 : 10321519 : trees_in::start (unsigned code)
5442 : : {
5443 : 10321519 : tree t = NULL_TREE;
5444 : :
5445 : 10321519 : if (!code)
5446 : 9317279 : code = u ();
5447 : :
5448 : 10321519 : switch (code)
5449 : : {
5450 : 9177049 : default:
5451 : 9177049 : if (code >= MAX_TREE_CODES)
5452 : : {
5453 : 0 : fail:
5454 : 0 : set_overrun ();
5455 : 0 : return NULL_TREE;
5456 : : }
5457 : 9177049 : else if (TREE_CODE_CLASS (code) == tcc_vl_exp)
5458 : : {
5459 : 390699 : unsigned ops = u ();
5460 : 390699 : t = build_vl_exp (tree_code (code), ops);
5461 : : }
5462 : : else
5463 : 8786350 : t = make_node (tree_code (code));
5464 : : break;
5465 : :
5466 : 461615 : case INTEGER_CST:
5467 : 461615 : {
5468 : 461615 : unsigned n = u ();
5469 : 461615 : unsigned e = u ();
5470 : 461615 : t = make_int_cst (n, e);
5471 : : }
5472 : 461615 : break;
5473 : :
5474 : 12 : case OMP_CLAUSE:
5475 : 12 : t = build_omp_clause (UNKNOWN_LOCATION, omp_clause_code (u ()));
5476 : 12 : break;
5477 : :
5478 : 9 : CASE_OMP_SIMD_CODE:
5479 : 9 : if (!(state->extensions & SE_OPENMP_SIMD))
5480 : 0 : goto fail;
5481 : 9 : t = make_node (tree_code (code));
5482 : 9 : break;
5483 : :
5484 : 3 : CASE_OMP_CODE:
5485 : 3 : if (!(state->extensions & SE_OPENMP))
5486 : 0 : goto fail;
5487 : 3 : t = make_node (tree_code (code));
5488 : 3 : break;
5489 : :
5490 : 6 : CASE_OACC_CODE:
5491 : 6 : if (!(state->extensions & SE_OPENACC))
5492 : 0 : goto fail;
5493 : 6 : t = make_node (tree_code (code));
5494 : 6 : break;
5495 : :
5496 : 38157 : case STRING_CST:
5497 : 38157 : {
5498 : 38157 : size_t l;
5499 : 38157 : const char *chars = str (&l);
5500 : 38157 : t = build_string (l, chars);
5501 : : }
5502 : 38157 : break;
5503 : :
5504 : 9 : case RAW_DATA_CST:
5505 : 9 : {
5506 : 9 : size_t l = u ();
5507 : 9 : if (l == 0)
5508 : : {
5509 : : /* Stream in RAW_DATA_CST with no owner as STRING_CST
5510 : : which owns the data. */
5511 : 3 : const char *chars = str (&l);
5512 : 3 : t = build_string (l, chars);
5513 : : }
5514 : : else
5515 : : {
5516 : 6 : t = make_node (RAW_DATA_CST);
5517 : 6 : RAW_DATA_LENGTH (t) = l;
5518 : : }
5519 : : }
5520 : 9 : break;
5521 : :
5522 : 24 : case VECTOR_CST:
5523 : 24 : {
5524 : 24 : unsigned log2_npats = u ();
5525 : 24 : unsigned elts_per = u ();
5526 : 24 : t = make_vector (log2_npats, elts_per);
5527 : : }
5528 : 24 : break;
5529 : :
5530 : 71058 : case TREE_BINFO:
5531 : 71058 : t = make_tree_binfo (u ());
5532 : 71058 : break;
5533 : :
5534 : 573577 : case TREE_VEC:
5535 : 573577 : t = make_tree_vec (u ());
5536 : 573577 : break;
5537 : :
5538 : 0 : case FIXED_CST:
5539 : 0 : case IDENTIFIER_NODE:
5540 : 0 : case SSA_NAME:
5541 : 0 : case TARGET_MEM_REF:
5542 : 0 : case TRANSLATION_UNIT_DECL:
5543 : 0 : goto fail;
5544 : : }
5545 : :
5546 : : return t;
5547 : : }
5548 : :
5549 : : /* The kinds of interface an importer could have for a decl. */
5550 : :
5551 : : enum class importer_interface {
5552 : : unknown, /* The definition may or may not need to be emitted. */
5553 : : external, /* The definition can always be found in another TU. */
5554 : : internal, /* The definition should be emitted in the importer's TU. */
5555 : : always_emit, /* The definition must be emitted in the importer's TU,
5556 : : regardless of if it's used or not. */
5557 : : };
5558 : :
5559 : : /* Returns what kind of interface an importer will have of DECL. */
5560 : :
5561 : : static importer_interface
5562 : 422435 : get_importer_interface (tree decl)
5563 : : {
5564 : : /* Internal linkage entities must be emitted in each importer if
5565 : : there is a definition available. */
5566 : 422435 : if (!TREE_PUBLIC (decl))
5567 : : return importer_interface::internal;
5568 : :
5569 : : /* Other entities that aren't vague linkage are either not definitions
5570 : : or will be publicly emitted in this TU, so importers can just refer
5571 : : to an external definition. */
5572 : 203351 : if (!vague_linkage_p (decl))
5573 : : return importer_interface::external;
5574 : :
5575 : : /* For explicit instantiations, importers can always rely on there
5576 : : being a definition in another TU, unless this is a definition
5577 : : in a header module: in which case the importer will always need
5578 : : to emit it. */
5579 : 198912 : if (DECL_LANG_SPECIFIC (decl)
5580 : 198912 : && DECL_EXPLICIT_INSTANTIATION (decl))
5581 : 18959 : return (header_module_p () && !DECL_EXTERNAL (decl)
5582 : 18959 : ? importer_interface::always_emit
5583 : : : importer_interface::external);
5584 : :
5585 : : /* A gnu_inline function is never emitted in any TU. */
5586 : 179953 : if (TREE_CODE (decl) == FUNCTION_DECL
5587 : 140596 : && DECL_DECLARED_INLINE_P (decl)
5588 : 315579 : && lookup_attribute ("gnu_inline", DECL_ATTRIBUTES (decl)))
5589 : : return importer_interface::external;
5590 : :
5591 : : /* Everything else has vague linkage. */
5592 : : return importer_interface::unknown;
5593 : : }
5594 : :
5595 : : /* The structure streamers access the raw fields, because the
5596 : : alternative, of using the accessor macros can require using
5597 : : different accessors for the same underlying field, depending on the
5598 : : tree code. That's both confusing and annoying. */
5599 : :
5600 : : /* Read & write the core boolean flags. */
5601 : :
5602 : : void
5603 : 12111769 : trees_out::core_bools (tree t, bits_out& bits)
5604 : : {
5605 : : #define WB(X) (bits.b (X))
5606 : : /* Stream X if COND holds, and if !COND stream a dummy value so that the
5607 : : overall number of bits streamed is independent of the runtime value
5608 : : of COND, which allows the compiler to better optimize this function. */
5609 : : #define WB_IF(COND, X) WB ((COND) ? (X) : false)
5610 : 12111769 : tree_code code = TREE_CODE (t);
5611 : :
5612 : 12111769 : WB (t->base.side_effects_flag);
5613 : 12111769 : WB (t->base.constant_flag);
5614 : 12111769 : WB (t->base.addressable_flag);
5615 : 12111769 : WB (t->base.volatile_flag);
5616 : 12111769 : WB (t->base.readonly_flag);
5617 : : /* base.asm_written_flag is a property of the current TU's use of
5618 : : this decl. */
5619 : 12111769 : WB (t->base.nowarning_flag);
5620 : : /* base.visited read as zero (it's set for writer, because that's
5621 : : how we mark nodes). */
5622 : : /* base.used_flag is not streamed. Readers may set TREE_USED of
5623 : : decls they use. */
5624 : 12111769 : WB (t->base.nothrow_flag);
5625 : 12111769 : WB (t->base.static_flag);
5626 : : /* This is TYPE_CACHED_VALUES_P for types. */
5627 : 12111769 : WB_IF (TREE_CODE_CLASS (code) != tcc_type, t->base.public_flag);
5628 : 12111769 : WB (t->base.private_flag);
5629 : 12111769 : WB (t->base.protected_flag);
5630 : 12111769 : WB (t->base.deprecated_flag);
5631 : 12111769 : WB (t->base.default_def_flag);
5632 : :
5633 : 12111769 : switch (code)
5634 : : {
5635 : : case CALL_EXPR:
5636 : : case INTEGER_CST:
5637 : : case SSA_NAME:
5638 : : case TARGET_MEM_REF:
5639 : : case TREE_VEC:
5640 : : /* These use different base.u fields. */
5641 : : return;
5642 : :
5643 : 10427338 : default:
5644 : 10427338 : WB (t->base.u.bits.lang_flag_0);
5645 : 10427338 : bool flag_1 = t->base.u.bits.lang_flag_1;
5646 : 10427338 : if (!flag_1)
5647 : : ;
5648 : 312020 : else if (code == TEMPLATE_INFO)
5649 : : /* This is TI_PENDING_TEMPLATE_FLAG, not relevant to reader. */
5650 : : flag_1 = false;
5651 : 308747 : else if (code == VAR_DECL)
5652 : : {
5653 : : /* This is DECL_INITIALIZED_P. */
5654 : 52269 : if (TREE_CODE (DECL_CONTEXT (t)) != FUNCTION_DECL)
5655 : : /* We'll set this when reading the definition. */
5656 : 10427338 : flag_1 = false;
5657 : : }
5658 : 10427338 : WB (flag_1);
5659 : 10427338 : WB (t->base.u.bits.lang_flag_2);
5660 : 10427338 : WB (t->base.u.bits.lang_flag_3);
5661 : 10427338 : WB (t->base.u.bits.lang_flag_4);
5662 : 10427338 : WB (t->base.u.bits.lang_flag_5);
5663 : 10427338 : WB (t->base.u.bits.lang_flag_6);
5664 : 10427338 : WB (t->base.u.bits.saturating_flag);
5665 : 10427338 : WB (t->base.u.bits.unsigned_flag);
5666 : 10427338 : WB (t->base.u.bits.packed_flag);
5667 : 10427338 : WB (t->base.u.bits.user_align);
5668 : 10427338 : WB (t->base.u.bits.nameless_flag);
5669 : 10427338 : WB (t->base.u.bits.atomic_flag);
5670 : 10427338 : WB (t->base.u.bits.unavailable_flag);
5671 : 10427338 : break;
5672 : : }
5673 : :
5674 : 10427338 : if (TREE_CODE_CLASS (code) == tcc_type)
5675 : : {
5676 : 518447 : WB (t->type_common.no_force_blk_flag);
5677 : 518447 : WB (t->type_common.needs_constructing_flag);
5678 : 518447 : WB (t->type_common.transparent_aggr_flag);
5679 : 518447 : WB (t->type_common.restrict_flag);
5680 : 518447 : WB (t->type_common.string_flag);
5681 : 518447 : WB (t->type_common.lang_flag_0);
5682 : 518447 : WB (t->type_common.lang_flag_1);
5683 : 518447 : WB (t->type_common.lang_flag_2);
5684 : 518447 : WB (t->type_common.lang_flag_3);
5685 : 518447 : WB (t->type_common.lang_flag_4);
5686 : 518447 : WB (t->type_common.lang_flag_5);
5687 : 518447 : WB (t->type_common.lang_flag_6);
5688 : 518447 : WB (t->type_common.typeless_storage);
5689 : : }
5690 : :
5691 : 10427338 : if (TREE_CODE_CLASS (code) != tcc_declaration)
5692 : : return;
5693 : :
5694 : 2716360 : if (CODE_CONTAINS_STRUCT (code, TS_DECL_COMMON))
5695 : : {
5696 : 2716360 : WB (t->decl_common.nonlocal_flag);
5697 : 2716360 : WB (t->decl_common.virtual_flag);
5698 : 2716360 : WB (t->decl_common.ignored_flag);
5699 : 2716360 : WB (t->decl_common.abstract_flag);
5700 : 2716360 : WB (t->decl_common.artificial_flag);
5701 : 2716360 : WB (t->decl_common.preserve_flag);
5702 : 2716360 : WB (t->decl_common.debug_expr_is_from);
5703 : 2716360 : WB (t->decl_common.lang_flag_0);
5704 : 2716360 : WB (t->decl_common.lang_flag_1);
5705 : 2716360 : WB (t->decl_common.lang_flag_2);
5706 : 2716360 : WB (t->decl_common.lang_flag_3);
5707 : 2716360 : WB (t->decl_common.lang_flag_4);
5708 : :
5709 : 2716360 : {
5710 : : /* This is DECL_INTERFACE_KNOWN: We should redetermine whether
5711 : : we need to import or export any vague-linkage entities on
5712 : : stream-in. */
5713 : 2716360 : bool interface_known = t->decl_common.lang_flag_5;
5714 : 2716360 : if (interface_known
5715 : 2716360 : && get_importer_interface (t) == importer_interface::unknown)
5716 : : interface_known = false;
5717 : 2716360 : WB (interface_known);
5718 : : }
5719 : :
5720 : 2716360 : WB (t->decl_common.lang_flag_6);
5721 : 2716360 : WB (t->decl_common.lang_flag_7);
5722 : 2716360 : WB (t->decl_common.lang_flag_8);
5723 : 2716360 : WB (t->decl_common.decl_flag_0);
5724 : :
5725 : 2716360 : {
5726 : : /* DECL_EXTERNAL -> decl_flag_1
5727 : : == it is defined elsewhere
5728 : : DECL_NOT_REALLY_EXTERN -> base.not_really_extern
5729 : : == that was a lie, it is here */
5730 : :
5731 : 2716360 : bool is_external = t->decl_common.decl_flag_1;
5732 : : /* maybe_emit_vtables relies on vtables being marked as
5733 : : DECL_EXTERNAL and DECL_NOT_REALLY_EXTERN before processing. */
5734 : 2716360 : if (!is_external && VAR_P (t) && DECL_VTABLE_OR_VTT_P (t))
5735 : : is_external = true;
5736 : : /* Things we emit here might well be external from the POV of an
5737 : : importer. */
5738 : 2716161 : if (!is_external
5739 : 2282416 : && VAR_OR_FUNCTION_DECL_P (t)
5740 : 2903283 : && get_importer_interface (t) == importer_interface::external)
5741 : : is_external = true;
5742 : 2716360 : WB (is_external);
5743 : : }
5744 : :
5745 : 2716360 : WB (t->decl_common.decl_flag_2);
5746 : 2716360 : WB (t->decl_common.decl_flag_3);
5747 : 2716360 : WB (t->decl_common.not_gimple_reg_flag);
5748 : 2716360 : WB (t->decl_common.decl_by_reference_flag);
5749 : 2716360 : WB (t->decl_common.decl_read_flag);
5750 : 2716360 : WB (t->decl_common.decl_nonshareable_flag);
5751 : 2716360 : WB (t->decl_common.decl_not_flexarray);
5752 : : }
5753 : : else
5754 : : return;
5755 : :
5756 : 2716360 : if (CODE_CONTAINS_STRUCT (code, TS_DECL_WITH_VIS))
5757 : : {
5758 : 1283481 : WB (t->decl_with_vis.defer_output);
5759 : 1283481 : WB (t->decl_with_vis.hard_register);
5760 : 1283481 : WB (t->decl_with_vis.common_flag);
5761 : 1283481 : WB (t->decl_with_vis.in_text_section);
5762 : 1283481 : WB (t->decl_with_vis.in_constant_pool);
5763 : 1283481 : WB (t->decl_with_vis.dllimport_flag);
5764 : 1283481 : WB (t->decl_with_vis.weak_flag);
5765 : 1283481 : WB (t->decl_with_vis.seen_in_bind_expr);
5766 : 1283481 : WB (t->decl_with_vis.comdat_flag);
5767 : 1283481 : WB (t->decl_with_vis.visibility_specified);
5768 : 1283481 : WB (t->decl_with_vis.init_priority_p);
5769 : 1283481 : WB (t->decl_with_vis.shadowed_for_var_p);
5770 : 1283481 : WB (t->decl_with_vis.cxx_constructor);
5771 : 1283481 : WB (t->decl_with_vis.cxx_destructor);
5772 : 1283481 : WB (t->decl_with_vis.final);
5773 : 1283481 : WB (t->decl_with_vis.regdecl_flag);
5774 : : }
5775 : : else
5776 : : return;
5777 : :
5778 : 1283481 : if (CODE_CONTAINS_STRUCT (code, TS_FUNCTION_DECL))
5779 : : {
5780 : 369962 : WB (t->function_decl.static_ctor_flag);
5781 : 369962 : WB (t->function_decl.static_dtor_flag);
5782 : 369962 : WB (t->function_decl.uninlinable);
5783 : 369962 : WB (t->function_decl.possibly_inlined);
5784 : 369962 : WB (t->function_decl.novops_flag);
5785 : 369962 : WB (t->function_decl.returns_twice_flag);
5786 : 369962 : WB (t->function_decl.malloc_flag);
5787 : 369962 : WB (t->function_decl.declared_inline_flag);
5788 : 369962 : WB (t->function_decl.no_inline_warning_flag);
5789 : 369962 : WB (t->function_decl.no_instrument_function_entry_exit);
5790 : 369962 : WB (t->function_decl.no_limit_stack);
5791 : 369962 : WB (t->function_decl.disregard_inline_limits);
5792 : 369962 : WB (t->function_decl.pure_flag);
5793 : 369962 : WB (t->function_decl.looping_const_or_pure_flag);
5794 : :
5795 : 369962 : WB (t->function_decl.has_debug_args_flag);
5796 : 369962 : WB (t->function_decl.versioned_function);
5797 : 369962 : WB (t->function_decl.replaceable_operator);
5798 : :
5799 : : /* decl_type is a (misnamed) 2 bit discriminator. */
5800 : 369962 : unsigned kind = (unsigned)t->function_decl.decl_type;
5801 : 369962 : WB ((kind >> 0) & 1);
5802 : 369962 : WB ((kind >> 1) & 1);
5803 : : }
5804 : : #undef WB_IF
5805 : : #undef WB
5806 : : }
5807 : :
5808 : : bool
5809 : 10336080 : trees_in::core_bools (tree t, bits_in& bits)
5810 : : {
5811 : : #define RB(X) ((X) = bits.b ())
5812 : : /* See the comment for WB_IF in trees_out::core_bools. */
5813 : : #define RB_IF(COND, X) ((COND) ? RB (X) : bits.b ())
5814 : :
5815 : 10336080 : tree_code code = TREE_CODE (t);
5816 : :
5817 : 10336080 : RB (t->base.side_effects_flag);
5818 : 10336080 : RB (t->base.constant_flag);
5819 : 10336080 : RB (t->base.addressable_flag);
5820 : 10336080 : RB (t->base.volatile_flag);
5821 : 10336080 : RB (t->base.readonly_flag);
5822 : : /* base.asm_written_flag is not streamed. */
5823 : 10336080 : RB (t->base.nowarning_flag);
5824 : : /* base.visited is not streamed. */
5825 : : /* base.used_flag is not streamed. */
5826 : 10336080 : RB (t->base.nothrow_flag);
5827 : 10336080 : RB (t->base.static_flag);
5828 : 10336080 : RB_IF (TREE_CODE_CLASS (code) != tcc_type, t->base.public_flag);
5829 : 10336080 : RB (t->base.private_flag);
5830 : 10336080 : RB (t->base.protected_flag);
5831 : 10336080 : RB (t->base.deprecated_flag);
5832 : 10336080 : RB (t->base.default_def_flag);
5833 : :
5834 : 10336080 : switch (code)
5835 : : {
5836 : 1417064 : case CALL_EXPR:
5837 : 1417064 : case INTEGER_CST:
5838 : 1417064 : case SSA_NAME:
5839 : 1417064 : case TARGET_MEM_REF:
5840 : 1417064 : case TREE_VEC:
5841 : : /* These use different base.u fields. */
5842 : 1417064 : goto done;
5843 : :
5844 : 8919016 : default:
5845 : 8919016 : RB (t->base.u.bits.lang_flag_0);
5846 : 8919016 : RB (t->base.u.bits.lang_flag_1);
5847 : 8919016 : RB (t->base.u.bits.lang_flag_2);
5848 : 8919016 : RB (t->base.u.bits.lang_flag_3);
5849 : 8919016 : RB (t->base.u.bits.lang_flag_4);
5850 : 8919016 : RB (t->base.u.bits.lang_flag_5);
5851 : 8919016 : RB (t->base.u.bits.lang_flag_6);
5852 : 8919016 : RB (t->base.u.bits.saturating_flag);
5853 : 8919016 : RB (t->base.u.bits.unsigned_flag);
5854 : 8919016 : RB (t->base.u.bits.packed_flag);
5855 : 8919016 : RB (t->base.u.bits.user_align);
5856 : 8919016 : RB (t->base.u.bits.nameless_flag);
5857 : 8919016 : RB (t->base.u.bits.atomic_flag);
5858 : 8919016 : RB (t->base.u.bits.unavailable_flag);
5859 : 8919016 : break;
5860 : : }
5861 : :
5862 : 8919016 : if (TREE_CODE_CLASS (code) == tcc_type)
5863 : : {
5864 : 407770 : RB (t->type_common.no_force_blk_flag);
5865 : 407770 : RB (t->type_common.needs_constructing_flag);
5866 : 407770 : RB (t->type_common.transparent_aggr_flag);
5867 : 407770 : RB (t->type_common.restrict_flag);
5868 : 407770 : RB (t->type_common.string_flag);
5869 : 407770 : RB (t->type_common.lang_flag_0);
5870 : 407770 : RB (t->type_common.lang_flag_1);
5871 : 407770 : RB (t->type_common.lang_flag_2);
5872 : 407770 : RB (t->type_common.lang_flag_3);
5873 : 407770 : RB (t->type_common.lang_flag_4);
5874 : 407770 : RB (t->type_common.lang_flag_5);
5875 : 407770 : RB (t->type_common.lang_flag_6);
5876 : 407770 : RB (t->type_common.typeless_storage);
5877 : : }
5878 : :
5879 : 8919016 : if (TREE_CODE_CLASS (code) != tcc_declaration)
5880 : 6650152 : goto done;
5881 : :
5882 : 2268864 : if (CODE_CONTAINS_STRUCT (code, TS_DECL_COMMON))
5883 : : {
5884 : 2268864 : RB (t->decl_common.nonlocal_flag);
5885 : 2268864 : RB (t->decl_common.virtual_flag);
5886 : 2268864 : RB (t->decl_common.ignored_flag);
5887 : 2268864 : RB (t->decl_common.abstract_flag);
5888 : 2268864 : RB (t->decl_common.artificial_flag);
5889 : 2268864 : RB (t->decl_common.preserve_flag);
5890 : 2268864 : RB (t->decl_common.debug_expr_is_from);
5891 : 2268864 : RB (t->decl_common.lang_flag_0);
5892 : 2268864 : RB (t->decl_common.lang_flag_1);
5893 : 2268864 : RB (t->decl_common.lang_flag_2);
5894 : 2268864 : RB (t->decl_common.lang_flag_3);
5895 : 2268864 : RB (t->decl_common.lang_flag_4);
5896 : 2268864 : RB (t->decl_common.lang_flag_5);
5897 : 2268864 : RB (t->decl_common.lang_flag_6);
5898 : 2268864 : RB (t->decl_common.lang_flag_7);
5899 : 2268864 : RB (t->decl_common.lang_flag_8);
5900 : 2268864 : RB (t->decl_common.decl_flag_0);
5901 : 2268864 : RB (t->decl_common.decl_flag_1);
5902 : 2268864 : RB (t->decl_common.decl_flag_2);
5903 : 2268864 : RB (t->decl_common.decl_flag_3);
5904 : 2268864 : RB (t->decl_common.not_gimple_reg_flag);
5905 : 2268864 : RB (t->decl_common.decl_by_reference_flag);
5906 : 2268864 : RB (t->decl_common.decl_read_flag);
5907 : 2268864 : RB (t->decl_common.decl_nonshareable_flag);
5908 : 2268864 : RB (t->decl_common.decl_not_flexarray);
5909 : : }
5910 : : else
5911 : 0 : goto done;
5912 : :
5913 : 2268864 : if (CODE_CONTAINS_STRUCT (code, TS_DECL_WITH_VIS))
5914 : : {
5915 : 1049617 : RB (t->decl_with_vis.defer_output);
5916 : 1049617 : RB (t->decl_with_vis.hard_register);
5917 : 1049617 : RB (t->decl_with_vis.common_flag);
5918 : 1049617 : RB (t->decl_with_vis.in_text_section);
5919 : 1049617 : RB (t->decl_with_vis.in_constant_pool);
5920 : 1049617 : RB (t->decl_with_vis.dllimport_flag);
5921 : 1049617 : RB (t->decl_with_vis.weak_flag);
5922 : 1049617 : RB (t->decl_with_vis.seen_in_bind_expr);
5923 : 1049617 : RB (t->decl_with_vis.comdat_flag);
5924 : 1049617 : RB (t->decl_with_vis.visibility_specified);
5925 : 1049617 : RB (t->decl_with_vis.init_priority_p);
5926 : 1049617 : RB (t->decl_with_vis.shadowed_for_var_p);
5927 : 1049617 : RB (t->decl_with_vis.cxx_constructor);
5928 : 1049617 : RB (t->decl_with_vis.cxx_destructor);
5929 : 1049617 : RB (t->decl_with_vis.final);
5930 : 1049617 : RB (t->decl_with_vis.regdecl_flag);
5931 : : }
5932 : : else
5933 : 1219247 : goto done;
5934 : :
5935 : 1049617 : if (CODE_CONTAINS_STRUCT (code, TS_FUNCTION_DECL))
5936 : : {
5937 : 319895 : RB (t->function_decl.static_ctor_flag);
5938 : 319895 : RB (t->function_decl.static_dtor_flag);
5939 : 319895 : RB (t->function_decl.uninlinable);
5940 : 319895 : RB (t->function_decl.possibly_inlined);
5941 : 319895 : RB (t->function_decl.novops_flag);
5942 : 319895 : RB (t->function_decl.returns_twice_flag);
5943 : 319895 : RB (t->function_decl.malloc_flag);
5944 : 319895 : RB (t->function_decl.declared_inline_flag);
5945 : 319895 : RB (t->function_decl.no_inline_warning_flag);
5946 : 319895 : RB (t->function_decl.no_instrument_function_entry_exit);
5947 : 319895 : RB (t->function_decl.no_limit_stack);
5948 : 319895 : RB (t->function_decl.disregard_inline_limits);
5949 : 319895 : RB (t->function_decl.pure_flag);
5950 : 319895 : RB (t->function_decl.looping_const_or_pure_flag);
5951 : :
5952 : 319895 : RB (t->function_decl.has_debug_args_flag);
5953 : 319895 : RB (t->function_decl.versioned_function);
5954 : 319895 : RB (t->function_decl.replaceable_operator);
5955 : :
5956 : : /* decl_type is a (misnamed) 2 bit discriminator. */
5957 : 319895 : unsigned kind = 0;
5958 : 319895 : kind |= unsigned (bits.b ()) << 0;
5959 : 319895 : kind |= unsigned (bits.b ()) << 1;
5960 : 319895 : t->function_decl.decl_type = function_decl_type (kind);
5961 : : }
5962 : : #undef RB_IF
5963 : : #undef RB
5964 : 729722 : done:
5965 : 10336080 : return !get_overrun ();
5966 : : }
5967 : :
5968 : : void
5969 : 1737568 : trees_out::lang_decl_bools (tree t, bits_out& bits)
5970 : : {
5971 : : #define WB(X) (bits.b (X))
5972 : 1737568 : const struct lang_decl *lang = DECL_LANG_SPECIFIC (t);
5973 : :
5974 : 1737568 : bits.bflush ();
5975 : 1737568 : WB (lang->u.base.language == lang_cplusplus);
5976 : 1737568 : WB ((lang->u.base.use_template >> 0) & 1);
5977 : 1737568 : WB ((lang->u.base.use_template >> 1) & 1);
5978 : : /* Do not write lang->u.base.not_really_extern, importer will set
5979 : : when reading the definition (if any). */
5980 : 1737568 : WB (lang->u.base.initialized_in_class);
5981 : 1737568 : WB (lang->u.base.threadprivate_or_deleted_p);
5982 : : /* Do not write lang->u.base.anticipated_p, it is a property of the
5983 : : current TU. */
5984 : 1737568 : WB (lang->u.base.friend_or_tls);
5985 : 1737568 : WB (lang->u.base.unknown_bound_p);
5986 : : /* Do not write lang->u.base.odr_used, importer will recalculate if
5987 : : they do ODR use this decl. */
5988 : 1737568 : WB (lang->u.base.concept_p);
5989 : 1737568 : WB (lang->u.base.var_declared_inline_p);
5990 : 1737568 : WB (lang->u.base.dependent_init_p);
5991 : : /* When building a header unit, everthing is marked as purview, (so
5992 : : we know which decls to write). But when we import them we do not
5993 : : want to mark them as in module purview. */
5994 : 3399912 : WB (lang->u.base.module_purview_p && !header_module_p ());
5995 : 1737568 : WB (lang->u.base.module_attach_p);
5996 : 1737568 : WB (lang->u.base.module_keyed_decls_p);
5997 : 1737568 : switch (lang->u.base.selector)
5998 : : {
5999 : 0 : default:
6000 : 0 : gcc_unreachable ();
6001 : :
6002 : 369962 : case lds_fn: /* lang_decl_fn. */
6003 : 369962 : WB (lang->u.fn.global_ctor_p);
6004 : 369962 : WB (lang->u.fn.global_dtor_p);
6005 : 369962 : WB (lang->u.fn.static_function);
6006 : 369962 : WB (lang->u.fn.pure_virtual);
6007 : 369962 : WB (lang->u.fn.defaulted_p);
6008 : 369962 : WB (lang->u.fn.has_in_charge_parm_p);
6009 : 369962 : WB (lang->u.fn.has_vtt_parm_p);
6010 : : /* There shouldn't be a pending inline at this point. */
6011 : 369962 : gcc_assert (!lang->u.fn.pending_inline_p);
6012 : 369962 : WB (lang->u.fn.nonconverting);
6013 : 369962 : WB (lang->u.fn.thunk_p);
6014 : 369962 : WB (lang->u.fn.this_thunk_p);
6015 : : /* Do not stream lang->u.hidden_friend_p, it is a property of
6016 : : the TU. */
6017 : 369962 : WB (lang->u.fn.omp_declare_reduction_p);
6018 : 369962 : WB (lang->u.fn.has_dependent_explicit_spec_p);
6019 : 369962 : WB (lang->u.fn.immediate_fn_p);
6020 : 369962 : WB (lang->u.fn.maybe_deleted);
6021 : 369962 : WB (lang->u.fn.implicit_constexpr);
6022 : 369962 : WB (lang->u.fn.escalated_p);
6023 : 369962 : WB (lang->u.fn.xobj_func);
6024 : 369962 : goto lds_min;
6025 : :
6026 : 1378 : case lds_decomp: /* lang_decl_decomp. */
6027 : : /* No bools. */
6028 : 1378 : goto lds_min;
6029 : :
6030 : : case lds_min: /* lang_decl_min. */
6031 : 1737568 : lds_min:
6032 : : /* No bools. */
6033 : : break;
6034 : :
6035 : : case lds_ns: /* lang_decl_ns. */
6036 : : /* No bools. */
6037 : : break;
6038 : :
6039 : : case lds_parm: /* lang_decl_parm. */
6040 : : /* No bools. */
6041 : : break;
6042 : : }
6043 : : #undef WB
6044 : 1737568 : }
6045 : :
6046 : : bool
6047 : 1444130 : trees_in::lang_decl_bools (tree t, bits_in& bits)
6048 : : {
6049 : : #define RB(X) ((X) = bits.b ())
6050 : 1444130 : struct lang_decl *lang = DECL_LANG_SPECIFIC (t);
6051 : :
6052 : 1444130 : bits.bflush ();
6053 : 1444130 : lang->u.base.language = bits.b () ? lang_cplusplus : lang_c;
6054 : 1444130 : unsigned v;
6055 : 1444130 : v = bits.b () << 0;
6056 : 1444130 : v |= bits.b () << 1;
6057 : 1444130 : lang->u.base.use_template = v;
6058 : : /* lang->u.base.not_really_extern is not streamed. */
6059 : 1444130 : RB (lang->u.base.initialized_in_class);
6060 : 1444130 : RB (lang->u.base.threadprivate_or_deleted_p);
6061 : : /* lang->u.base.anticipated_p is not streamed. */
6062 : 1444130 : RB (lang->u.base.friend_or_tls);
6063 : 1444130 : RB (lang->u.base.unknown_bound_p);
6064 : : /* lang->u.base.odr_used is not streamed. */
6065 : 1444130 : RB (lang->u.base.concept_p);
6066 : 1444130 : RB (lang->u.base.var_declared_inline_p);
6067 : 1444130 : RB (lang->u.base.dependent_init_p);
6068 : 1444130 : RB (lang->u.base.module_purview_p);
6069 : 1444130 : RB (lang->u.base.module_attach_p);
6070 : 1444130 : RB (lang->u.base.module_keyed_decls_p);
6071 : 1444130 : switch (lang->u.base.selector)
6072 : : {
6073 : 0 : default:
6074 : 0 : gcc_unreachable ();
6075 : :
6076 : 319895 : case lds_fn: /* lang_decl_fn. */
6077 : 319895 : RB (lang->u.fn.global_ctor_p);
6078 : 319895 : RB (lang->u.fn.global_dtor_p);
6079 : 319895 : RB (lang->u.fn.static_function);
6080 : 319895 : RB (lang->u.fn.pure_virtual);
6081 : 319895 : RB (lang->u.fn.defaulted_p);
6082 : 319895 : RB (lang->u.fn.has_in_charge_parm_p);
6083 : 319895 : RB (lang->u.fn.has_vtt_parm_p);
6084 : 319895 : RB (lang->u.fn.nonconverting);
6085 : 319895 : RB (lang->u.fn.thunk_p);
6086 : 319895 : RB (lang->u.fn.this_thunk_p);
6087 : : /* lang->u.fn.hidden_friend_p is not streamed. */
6088 : 319895 : RB (lang->u.fn.omp_declare_reduction_p);
6089 : 319895 : RB (lang->u.fn.has_dependent_explicit_spec_p);
6090 : 319895 : RB (lang->u.fn.immediate_fn_p);
6091 : 319895 : RB (lang->u.fn.maybe_deleted);
6092 : 319895 : RB (lang->u.fn.implicit_constexpr);
6093 : 319895 : RB (lang->u.fn.escalated_p);
6094 : 319895 : RB (lang->u.fn.xobj_func);
6095 : 319895 : goto lds_min;
6096 : :
6097 : 1303 : case lds_decomp: /* lang_decl_decomp. */
6098 : : /* No bools. */
6099 : 1303 : goto lds_min;
6100 : :
6101 : : case lds_min: /* lang_decl_min. */
6102 : 1444130 : lds_min:
6103 : : /* No bools. */
6104 : : break;
6105 : :
6106 : : case lds_ns: /* lang_decl_ns. */
6107 : : /* No bools. */
6108 : : break;
6109 : :
6110 : : case lds_parm: /* lang_decl_parm. */
6111 : : /* No bools. */
6112 : : break;
6113 : : }
6114 : : #undef RB
6115 : 1444130 : return !get_overrun ();
6116 : : }
6117 : :
6118 : : void
6119 : 143056 : trees_out::lang_type_bools (tree t, bits_out& bits)
6120 : : {
6121 : : #define WB(X) (bits.b (X))
6122 : 143056 : const struct lang_type *lang = TYPE_LANG_SPECIFIC (t);
6123 : :
6124 : 143056 : bits.bflush ();
6125 : 143056 : WB (lang->has_type_conversion);
6126 : 143056 : WB (lang->has_copy_ctor);
6127 : 143056 : WB (lang->has_default_ctor);
6128 : 143056 : WB (lang->const_needs_init);
6129 : 143056 : WB (lang->ref_needs_init);
6130 : 143056 : WB (lang->has_const_copy_assign);
6131 : 143056 : WB ((lang->use_template >> 0) & 1);
6132 : 143056 : WB ((lang->use_template >> 1) & 1);
6133 : :
6134 : 143056 : WB (lang->has_mutable);
6135 : 143056 : WB (lang->com_interface);
6136 : 143056 : WB (lang->non_pod_class);
6137 : 143056 : WB (lang->nearly_empty_p);
6138 : 143056 : WB (lang->user_align);
6139 : 143056 : WB (lang->has_copy_assign);
6140 : 143056 : WB (lang->has_new);
6141 : 143056 : WB (lang->has_array_new);
6142 : :
6143 : 143056 : WB ((lang->gets_delete >> 0) & 1);
6144 : 143056 : WB ((lang->gets_delete >> 1) & 1);
6145 : 143056 : WB (lang->interface_only);
6146 : 143056 : WB (lang->interface_unknown);
6147 : 143056 : WB (lang->contains_empty_class_p);
6148 : 143056 : WB (lang->anon_aggr);
6149 : 143056 : WB (lang->non_zero_init);
6150 : 143056 : WB (lang->empty_p);
6151 : :
6152 : 143056 : WB (lang->vec_new_uses_cookie);
6153 : 143056 : WB (lang->declared_class);
6154 : 143056 : WB (lang->diamond_shaped);
6155 : 143056 : WB (lang->repeated_base);
6156 : 143056 : gcc_assert (!lang->being_defined);
6157 : : // lang->debug_requested
6158 : 143056 : WB (lang->fields_readonly);
6159 : 143056 : WB (lang->ptrmemfunc_flag);
6160 : :
6161 : 143056 : WB (lang->lazy_default_ctor);
6162 : 143056 : WB (lang->lazy_copy_ctor);
6163 : 143056 : WB (lang->lazy_copy_assign);
6164 : 143056 : WB (lang->lazy_destructor);
6165 : 143056 : WB (lang->has_const_copy_ctor);
6166 : 143056 : WB (lang->has_complex_copy_ctor);
6167 : 143056 : WB (lang->has_complex_copy_assign);
6168 : 143056 : WB (lang->non_aggregate);
6169 : :
6170 : 143056 : WB (lang->has_complex_dflt);
6171 : 143056 : WB (lang->has_list_ctor);
6172 : 143056 : WB (lang->non_std_layout);
6173 : 143056 : WB (lang->is_literal);
6174 : 143056 : WB (lang->lazy_move_ctor);
6175 : 143056 : WB (lang->lazy_move_assign);
6176 : 143056 : WB (lang->has_complex_move_ctor);
6177 : 143056 : WB (lang->has_complex_move_assign);
6178 : :
6179 : 143056 : WB (lang->has_constexpr_ctor);
6180 : 143056 : WB (lang->unique_obj_representations);
6181 : 143056 : WB (lang->unique_obj_representations_set);
6182 : : #undef WB
6183 : 143056 : }
6184 : :
6185 : : bool
6186 : 110296 : trees_in::lang_type_bools (tree t, bits_in& bits)
6187 : : {
6188 : : #define RB(X) ((X) = bits.b ())
6189 : 110296 : struct lang_type *lang = TYPE_LANG_SPECIFIC (t);
6190 : :
6191 : 110296 : bits.bflush ();
6192 : 110296 : RB (lang->has_type_conversion);
6193 : 110296 : RB (lang->has_copy_ctor);
6194 : 110296 : RB (lang->has_default_ctor);
6195 : 110296 : RB (lang->const_needs_init);
6196 : 110296 : RB (lang->ref_needs_init);
6197 : 110296 : RB (lang->has_const_copy_assign);
6198 : 110296 : unsigned v;
6199 : 110296 : v = bits.b () << 0;
6200 : 110296 : v |= bits.b () << 1;
6201 : 110296 : lang->use_template = v;
6202 : :
6203 : 110296 : RB (lang->has_mutable);
6204 : 110296 : RB (lang->com_interface);
6205 : 110296 : RB (lang->non_pod_class);
6206 : 110296 : RB (lang->nearly_empty_p);
6207 : 110296 : RB (lang->user_align);
6208 : 110296 : RB (lang->has_copy_assign);
6209 : 110296 : RB (lang->has_new);
6210 : 110296 : RB (lang->has_array_new);
6211 : :
6212 : 110296 : v = bits.b () << 0;
6213 : 110296 : v |= bits.b () << 1;
6214 : 110296 : lang->gets_delete = v;
6215 : 110296 : RB (lang->interface_only);
6216 : 110296 : RB (lang->interface_unknown);
6217 : 110296 : RB (lang->contains_empty_class_p);
6218 : 110296 : RB (lang->anon_aggr);
6219 : 110296 : RB (lang->non_zero_init);
6220 : 110296 : RB (lang->empty_p);
6221 : :
6222 : 110296 : RB (lang->vec_new_uses_cookie);
6223 : 110296 : RB (lang->declared_class);
6224 : 110296 : RB (lang->diamond_shaped);
6225 : 110296 : RB (lang->repeated_base);
6226 : 110296 : gcc_assert (!lang->being_defined);
6227 : 110296 : gcc_assert (!lang->debug_requested);
6228 : 110296 : RB (lang->fields_readonly);
6229 : 110296 : RB (lang->ptrmemfunc_flag);
6230 : :
6231 : 110296 : RB (lang->lazy_default_ctor);
6232 : 110296 : RB (lang->lazy_copy_ctor);
6233 : 110296 : RB (lang->lazy_copy_assign);
6234 : 110296 : RB (lang->lazy_destructor);
6235 : 110296 : RB (lang->has_const_copy_ctor);
6236 : 110296 : RB (lang->has_complex_copy_ctor);
6237 : 110296 : RB (lang->has_complex_copy_assign);
6238 : 110296 : RB (lang->non_aggregate);
6239 : :
6240 : 110296 : RB (lang->has_complex_dflt);
6241 : 110296 : RB (lang->has_list_ctor);
6242 : 110296 : RB (lang->non_std_layout);
6243 : 110296 : RB (lang->is_literal);
6244 : 110296 : RB (lang->lazy_move_ctor);
6245 : 110296 : RB (lang->lazy_move_assign);
6246 : 110296 : RB (lang->has_complex_move_ctor);
6247 : 110296 : RB (lang->has_complex_move_assign);
6248 : :
6249 : 110296 : RB (lang->has_constexpr_ctor);
6250 : 110296 : RB (lang->unique_obj_representations);
6251 : 110296 : RB (lang->unique_obj_representations_set);
6252 : : #undef RB
6253 : 110296 : return !get_overrun ();
6254 : : }
6255 : :
6256 : : /* Read & write the core values and pointers. */
6257 : :
6258 : : void
6259 : 31332970 : trees_out::core_vals (tree t)
6260 : : {
6261 : : #define WU(X) (u (X))
6262 : : #define WT(X) (tree_node (X))
6263 : 31332970 : tree_code code = TREE_CODE (t);
6264 : :
6265 : : /* First by shape of the tree. */
6266 : :
6267 : 31332970 : if (CODE_CONTAINS_STRUCT (code, TS_DECL_MINIMAL))
6268 : : {
6269 : : /* Write this early, for better log information. */
6270 : 7100533 : WT (t->decl_minimal.name);
6271 : 7100533 : if (!DECL_TEMPLATE_PARM_P (t))
6272 : 5477317 : WT (t->decl_minimal.context);
6273 : :
6274 : 7100533 : if (state)
6275 : 5842799 : state->write_location (*this, t->decl_minimal.locus);
6276 : :
6277 : 7100533 : if (streaming_p ())
6278 : 2716360 : if (has_warning_spec (t))
6279 : 483 : u (get_warning_spec (t));
6280 : : }
6281 : :
6282 : 31332970 : if (CODE_CONTAINS_STRUCT (code, TS_TYPE_COMMON))
6283 : : {
6284 : : /* The only types we write also have TYPE_NON_COMMON. */
6285 : 1798952 : gcc_checking_assert (CODE_CONTAINS_STRUCT (code, TS_TYPE_NON_COMMON));
6286 : :
6287 : : /* We only stream the main variant. */
6288 : 1798952 : gcc_checking_assert (TYPE_MAIN_VARIANT (t) == t);
6289 : :
6290 : : /* Stream the name & context first, for better log information */
6291 : 1798952 : WT (t->type_common.name);
6292 : 1798952 : WT (t->type_common.context);
6293 : :
6294 : : /* By construction we want to make sure we have the canonical
6295 : : and main variants already in the type table, so emit them
6296 : : now. */
6297 : 1798952 : WT (t->type_common.main_variant);
6298 : :
6299 : 1798952 : tree canonical = t->type_common.canonical;
6300 : 1798952 : if (canonical && DECL_TEMPLATE_PARM_P (TYPE_NAME (t)))
6301 : : /* We do not want to wander into different templates.
6302 : : Reconstructed on stream in. */
6303 : : canonical = t;
6304 : 1798952 : WT (canonical);
6305 : :
6306 : : /* type_common.next_variant is internally manipulated. */
6307 : : /* type_common.pointer_to, type_common.reference_to. */
6308 : :
6309 : 1798952 : if (streaming_p ())
6310 : : {
6311 : 500110 : WU (t->type_common.precision);
6312 : 500110 : WU (t->type_common.contains_placeholder_bits);
6313 : 500110 : WU (t->type_common.mode);
6314 : 500110 : WU (t->type_common.align);
6315 : : }
6316 : :
6317 : 1798952 : if (!RECORD_OR_UNION_CODE_P (code))
6318 : : {
6319 : 1471632 : WT (t->type_common.size);
6320 : 1471632 : WT (t->type_common.size_unit);
6321 : : }
6322 : 1798952 : WT (t->type_common.attributes);
6323 : :
6324 : 1798952 : WT (t->type_common.common.chain); /* TYPE_STUB_DECL. */
6325 : : }
6326 : :
6327 : 31332970 : if (CODE_CONTAINS_STRUCT (code, TS_DECL_COMMON))
6328 : : {
6329 : 7100533 : if (streaming_p ())
6330 : : {
6331 : 2716360 : WU (t->decl_common.mode);
6332 : 2716360 : WU (t->decl_common.off_align);
6333 : 2716360 : WU (t->decl_common.align);
6334 : : }
6335 : :
6336 : : /* For templates these hold instantiation (partial and/or
6337 : : specialization) information. */
6338 : 7100533 : if (code != TEMPLATE_DECL)
6339 : : {
6340 : 6537062 : WT (t->decl_common.size);
6341 : 6537062 : WT (t->decl_common.size_unit);
6342 : : }
6343 : :
6344 : 7100533 : WT (t->decl_common.attributes);
6345 : : // FIXME: Does this introduce cross-decl links? For instance
6346 : : // from instantiation to the template. If so, we'll need more
6347 : : // deduplication logic. I think we'll need to walk the blocks
6348 : : // of the owning function_decl's abstract origin in tandem, to
6349 : : // generate the locating data needed?
6350 : 7100533 : WT (t->decl_common.abstract_origin);
6351 : : }
6352 : :
6353 : 31332970 : if (CODE_CONTAINS_STRUCT (code, TS_DECL_WITH_VIS))
6354 : : {
6355 : 3375248 : WT (t->decl_with_vis.assembler_name);
6356 : 3375248 : if (streaming_p ())
6357 : 1283481 : WU (t->decl_with_vis.visibility);
6358 : : }
6359 : :
6360 : 31332970 : if (CODE_CONTAINS_STRUCT (code, TS_TYPE_NON_COMMON))
6361 : : {
6362 : 1798952 : if (code == ENUMERAL_TYPE)
6363 : : {
6364 : : /* These fields get set even for opaque enums that lack a
6365 : : definition, so we stream them directly for each ENUMERAL_TYPE.
6366 : : We stream TYPE_VALUES as part of the definition. */
6367 : 7793 : WT (t->type_non_common.maxval);
6368 : 7793 : WT (t->type_non_common.minval);
6369 : : }
6370 : : /* Records and unions hold FIELDS, VFIELD & BINFO on these
6371 : : things. */
6372 : 1791159 : else if (!RECORD_OR_UNION_CODE_P (code))
6373 : : {
6374 : : // FIXME: These are from tpl_parm_value's 'type' writing.
6375 : : // Perhaps it should just be doing them directly?
6376 : 1463839 : gcc_checking_assert (code == TEMPLATE_TYPE_PARM
6377 : : || code == TEMPLATE_TEMPLATE_PARM
6378 : : || code == BOUND_TEMPLATE_TEMPLATE_PARM);
6379 : 1463839 : gcc_checking_assert (!TYPE_CACHED_VALUES_P (t));
6380 : 1463839 : WT (t->type_non_common.values);
6381 : 1463839 : WT (t->type_non_common.maxval);
6382 : 1463839 : WT (t->type_non_common.minval);
6383 : : }
6384 : :
6385 : 1798952 : WT (t->type_non_common.lang_1);
6386 : : }
6387 : :
6388 : 31332970 : if (CODE_CONTAINS_STRUCT (code, TS_EXP))
6389 : : {
6390 : 9099988 : if (state)
6391 : 8915216 : state->write_location (*this, t->exp.locus);
6392 : :
6393 : 9099988 : if (streaming_p ())
6394 : 4415996 : if (has_warning_spec (t))
6395 : 356019 : u (get_warning_spec (t));
6396 : :
6397 : : /* Walk in forward order, as (for instance) REQUIRES_EXPR has a
6398 : : bunch of unscoped parms on its first operand. It's safer to
6399 : : create those in order. */
6400 : 9099988 : bool vl = TREE_CODE_CLASS (code) == tcc_vl_exp;
6401 : 25281563 : for (unsigned limit = (vl ? VL_EXP_OPERAND_LENGTH (t)
6402 : 9099988 : : TREE_OPERAND_LENGTH (t)),
6403 : 25281563 : ix = unsigned (vl); ix != limit; ix++)
6404 : 16181575 : WT (TREE_OPERAND (t, ix));
6405 : : }
6406 : : else
6407 : : /* The CODE_CONTAINS tables were inaccurate when I started. */
6408 : 22232982 : gcc_checking_assert (TREE_CODE_CLASS (code) != tcc_expression
6409 : : && TREE_CODE_CLASS (code) != tcc_binary
6410 : : && TREE_CODE_CLASS (code) != tcc_unary
6411 : : && TREE_CODE_CLASS (code) != tcc_reference
6412 : : && TREE_CODE_CLASS (code) != tcc_comparison
6413 : : && TREE_CODE_CLASS (code) != tcc_statement
6414 : : && TREE_CODE_CLASS (code) != tcc_vl_exp);
6415 : :
6416 : : /* Then by CODE. Special cases and/or 1:1 tree shape
6417 : : correspondance. */
6418 : 31332970 : switch (code)
6419 : : {
6420 : : default:
6421 : : break;
6422 : :
6423 : 0 : case ARGUMENT_PACK_SELECT: /* Transient during instantiation. */
6424 : 0 : case DEFERRED_PARSE: /* Expanded upon completion of
6425 : : outermost class. */
6426 : 0 : case IDENTIFIER_NODE: /* Streamed specially. */
6427 : 0 : case BINDING_VECTOR: /* Only in namespace-scope symbol
6428 : : table. */
6429 : 0 : case SSA_NAME:
6430 : 0 : case TRANSLATION_UNIT_DECL: /* There is only one, it is a
6431 : : global_tree. */
6432 : 0 : case USERDEF_LITERAL: /* Expanded during parsing. */
6433 : 0 : gcc_unreachable (); /* Should never meet. */
6434 : :
6435 : : /* Constants. */
6436 : 18 : case COMPLEX_CST:
6437 : 18 : WT (TREE_REALPART (t));
6438 : 18 : WT (TREE_IMAGPART (t));
6439 : 18 : break;
6440 : :
6441 : 0 : case FIXED_CST:
6442 : 0 : gcc_unreachable (); /* Not supported in C++. */
6443 : :
6444 : 2992107 : case INTEGER_CST:
6445 : 2992107 : if (streaming_p ())
6446 : : {
6447 : 548974 : unsigned num = TREE_INT_CST_EXT_NUNITS (t);
6448 : 1099414 : for (unsigned ix = 0; ix != num; ix++)
6449 : 550440 : wu (TREE_INT_CST_ELT (t, ix));
6450 : : }
6451 : : break;
6452 : :
6453 : 0 : case POLY_INT_CST:
6454 : 0 : if (streaming_p ())
6455 : 0 : for (unsigned ix = 0; ix != NUM_POLY_INT_COEFFS; ix++)
6456 : 0 : WT (POLY_INT_CST_COEFF (t, ix));
6457 : : break;
6458 : :
6459 : 24768 : case REAL_CST:
6460 : 24768 : if (streaming_p ())
6461 : 12360 : buf (TREE_REAL_CST_PTR (t), sizeof (real_value));
6462 : : break;
6463 : :
6464 : : case STRING_CST:
6465 : : /* Streamed during start. */
6466 : : break;
6467 : :
6468 : 36 : case RAW_DATA_CST:
6469 : 36 : if (RAW_DATA_OWNER (t) == NULL_TREE)
6470 : : break; /* Streamed as STRING_CST during start. */
6471 : 24 : WT (RAW_DATA_OWNER (t));
6472 : 24 : if (streaming_p ())
6473 : : {
6474 : 12 : if (TREE_CODE (RAW_DATA_OWNER (t)) == RAW_DATA_CST)
6475 : 6 : z (RAW_DATA_POINTER (t) - RAW_DATA_POINTER (RAW_DATA_OWNER (t)));
6476 : 6 : else if (TREE_CODE (RAW_DATA_OWNER (t)) == STRING_CST)
6477 : 6 : z (RAW_DATA_POINTER (t)
6478 : 6 : - TREE_STRING_POINTER (RAW_DATA_OWNER (t)));
6479 : : else
6480 : 0 : gcc_unreachable ();
6481 : : }
6482 : : break;
6483 : :
6484 : 36 : case VECTOR_CST:
6485 : 102 : for (unsigned ix = vector_cst_encoded_nelts (t); ix--;)
6486 : 66 : WT (VECTOR_CST_ENCODED_ELT (t, ix));
6487 : : break;
6488 : :
6489 : : /* Decls. */
6490 : 343567 : case VAR_DECL:
6491 : 343567 : if (DECL_CONTEXT (t)
6492 : 343567 : && TREE_CODE (DECL_CONTEXT (t)) != FUNCTION_DECL)
6493 : : {
6494 : 71609 : if (DECL_HAS_VALUE_EXPR_P (t))
6495 : 18 : WT (DECL_VALUE_EXPR (t));
6496 : : break;
6497 : : }
6498 : : /* FALLTHROUGH */
6499 : :
6500 : 3158434 : case RESULT_DECL:
6501 : 3158434 : case PARM_DECL:
6502 : 3158434 : if (DECL_HAS_VALUE_EXPR_P (t))
6503 : 25543 : WT (DECL_VALUE_EXPR (t));
6504 : : /* FALLTHROUGH */
6505 : :
6506 : 3295933 : case CONST_DECL:
6507 : 3295933 : case IMPORTED_DECL:
6508 : 3295933 : WT (t->decl_common.initial);
6509 : 3295933 : break;
6510 : :
6511 : 98013 : case FIELD_DECL:
6512 : 98013 : WT (t->field_decl.offset);
6513 : 98013 : WT (t->field_decl.bit_field_type);
6514 : 98013 : WT (t->field_decl.qualifier); /* bitfield unit. */
6515 : 98013 : WT (t->field_decl.bit_offset);
6516 : 98013 : WT (t->field_decl.fcontext);
6517 : 98013 : WT (t->decl_common.initial);
6518 : 98013 : break;
6519 : :
6520 : 28240 : case LABEL_DECL:
6521 : 28240 : if (streaming_p ())
6522 : : {
6523 : 14120 : WU (t->label_decl.label_decl_uid);
6524 : 14120 : WU (t->label_decl.eh_landing_pad_nr);
6525 : : }
6526 : : break;
6527 : :
6528 : 740091 : case FUNCTION_DECL:
6529 : 740091 : if (streaming_p ())
6530 : : {
6531 : : /* Builtins can be streamed by value when a header declares
6532 : : them. */
6533 : 369962 : WU (DECL_BUILT_IN_CLASS (t));
6534 : 369962 : if (DECL_BUILT_IN_CLASS (t) != NOT_BUILT_IN)
6535 : 7164 : WU (DECL_UNCHECKED_FUNCTION_CODE (t));
6536 : : }
6537 : :
6538 : 740091 : WT (t->function_decl.personality);
6539 : 740091 : WT (t->function_decl.function_specific_target);
6540 : 740091 : WT (t->function_decl.function_specific_optimization);
6541 : 740091 : WT (t->function_decl.vindex);
6542 : :
6543 : 740091 : if (DECL_HAS_DEPENDENT_EXPLICIT_SPEC_P (t))
6544 : 4462 : WT (lookup_explicit_specifier (t));
6545 : : break;
6546 : :
6547 : 84743 : case USING_DECL:
6548 : : /* USING_DECL_DECLS */
6549 : 84743 : WT (t->decl_common.initial);
6550 : : /* FALLTHROUGH */
6551 : :
6552 : 2291349 : case TYPE_DECL:
6553 : : /* USING_DECL: USING_DECL_SCOPE */
6554 : : /* TYPE_DECL: DECL_ORIGINAL_TYPE */
6555 : 2291349 : WT (t->decl_non_common.result);
6556 : 2291349 : break;
6557 : :
6558 : : /* Miscellaneous common nodes. */
6559 : 466662 : case BLOCK:
6560 : 466662 : if (state)
6561 : : {
6562 : 466662 : state->write_location (*this, t->block.locus);
6563 : 466662 : state->write_location (*this, t->block.end_locus);
6564 : : }
6565 : :
6566 : : /* DECL_LOCAL_DECL_P decls are first encountered here and
6567 : : streamed by value. */
6568 : 696490 : for (tree decls = t->block.vars; decls; decls = DECL_CHAIN (decls))
6569 : : {
6570 : 229828 : if (VAR_OR_FUNCTION_DECL_P (decls)
6571 : 229828 : && DECL_LOCAL_DECL_P (decls))
6572 : : {
6573 : : /* Make sure this is the first encounter, and mark for
6574 : : walk-by-value. */
6575 : 226 : gcc_checking_assert (!TREE_VISITED (decls)
6576 : : && !DECL_TEMPLATE_INFO (decls));
6577 : 226 : mark_by_value (decls);
6578 : : }
6579 : 229828 : tree_node (decls);
6580 : : }
6581 : 466662 : tree_node (NULL_TREE);
6582 : :
6583 : : /* nonlocalized_vars is a middle-end thing. */
6584 : 466662 : WT (t->block.subblocks);
6585 : 466662 : WT (t->block.supercontext);
6586 : : // FIXME: As for decl's abstract_origin, does this introduce crosslinks?
6587 : 466662 : WT (t->block.abstract_origin);
6588 : : /* fragment_origin, fragment_chain are middle-end things. */
6589 : 466662 : WT (t->block.chain);
6590 : : /* nonlocalized_vars, block_num & die are middle endy/debug
6591 : : things. */
6592 : 466662 : break;
6593 : :
6594 : 915227 : case CALL_EXPR:
6595 : 915227 : if (streaming_p ())
6596 : 436944 : WU (t->base.u.ifn);
6597 : : break;
6598 : :
6599 : : case CONSTRUCTOR:
6600 : : // This must be streamed /after/ we've streamed the type,
6601 : : // because it can directly refer to elements of the type. Eg,
6602 : : // FIELD_DECLs of a RECORD_TYPE.
6603 : : break;
6604 : :
6605 : 24 : case OMP_CLAUSE:
6606 : 24 : {
6607 : : /* The ompcode is serialized in start. */
6608 : 24 : if (streaming_p ())
6609 : 12 : WU (t->omp_clause.subcode.map_kind);
6610 : 24 : if (state)
6611 : 24 : state->write_location (*this, t->omp_clause.locus);
6612 : :
6613 : 24 : unsigned len = omp_clause_num_ops[OMP_CLAUSE_CODE (t)];
6614 : 78 : for (unsigned ix = 0; ix != len; ix++)
6615 : 54 : WT (t->omp_clause.ops[ix]);
6616 : : }
6617 : : break;
6618 : :
6619 : 332149 : case STATEMENT_LIST:
6620 : 1303427 : for (tree stmt : tsi_range (t))
6621 : 971278 : if (stmt)
6622 : 971278 : WT (stmt);
6623 : 332149 : WT (NULL_TREE);
6624 : 332149 : break;
6625 : :
6626 : 0 : case OPTIMIZATION_NODE:
6627 : 0 : case TARGET_OPTION_NODE:
6628 : : // FIXME: Our representation for these two nodes is a cache of
6629 : : // the resulting set of options. Not a record of the options
6630 : : // that got changed by a particular attribute or pragma. Should
6631 : : // we record that, or should we record the diff from the command
6632 : : // line options? The latter seems the right behaviour, but is
6633 : : // (a) harder, and I guess could introduce strangeness if the
6634 : : // importer has set some incompatible set of optimization flags?
6635 : 0 : gcc_unreachable ();
6636 : 197008 : break;
6637 : :
6638 : 197008 : case TREE_BINFO:
6639 : 197008 : {
6640 : 197008 : WT (t->binfo.common.chain);
6641 : 197008 : WT (t->binfo.offset);
6642 : 197008 : WT (t->binfo.inheritance);
6643 : 197008 : WT (t->binfo.vptr_field);
6644 : :
6645 : 197008 : WT (t->binfo.vtable);
6646 : 197008 : WT (t->binfo.virtuals);
6647 : 197008 : WT (t->binfo.vtt_subvtt);
6648 : 197008 : WT (t->binfo.vtt_vptr);
6649 : :
6650 : 197008 : tree_vec (BINFO_BASE_ACCESSES (t));
6651 : 197008 : unsigned num = vec_safe_length (BINFO_BASE_ACCESSES (t));
6652 : 254028 : for (unsigned ix = 0; ix != num; ix++)
6653 : 57020 : WT (BINFO_BASE_BINFO (t, ix));
6654 : : }
6655 : : break;
6656 : :
6657 : 2444911 : case TREE_LIST:
6658 : 2444911 : WT (t->list.purpose);
6659 : 2444911 : WT (t->list.value);
6660 : 2444911 : WT (t->list.common.chain);
6661 : 2444911 : break;
6662 : :
6663 : 2107791 : case TREE_VEC:
6664 : 5775037 : for (unsigned ix = TREE_VEC_LENGTH (t); ix--;)
6665 : 3667246 : WT (TREE_VEC_ELT (t, ix));
6666 : : /* We stash NON_DEFAULT_TEMPLATE_ARGS_COUNT on TREE_CHAIN! */
6667 : 2107791 : gcc_checking_assert (!t->type_common.common.chain
6668 : : || (TREE_CODE (t->type_common.common.chain)
6669 : : == INTEGER_CST));
6670 : 2107791 : WT (t->type_common.common.chain);
6671 : 2107791 : break;
6672 : :
6673 : : /* C++-specific nodes ... */
6674 : 163291 : case BASELINK:
6675 : 163291 : WT (((lang_tree_node *)t)->baselink.binfo);
6676 : 163291 : WT (((lang_tree_node *)t)->baselink.functions);
6677 : 163291 : WT (((lang_tree_node *)t)->baselink.access_binfo);
6678 : 163291 : break;
6679 : :
6680 : 68108 : case CONSTRAINT_INFO:
6681 : 68108 : WT (((lang_tree_node *)t)->constraint_info.template_reqs);
6682 : 68108 : WT (((lang_tree_node *)t)->constraint_info.declarator_reqs);
6683 : 68108 : WT (((lang_tree_node *)t)->constraint_info.associated_constr);
6684 : 68108 : break;
6685 : :
6686 : 11112 : case DEFERRED_NOEXCEPT:
6687 : 11112 : WT (((lang_tree_node *)t)->deferred_noexcept.pattern);
6688 : 11112 : WT (((lang_tree_node *)t)->deferred_noexcept.args);
6689 : 11112 : break;
6690 : :
6691 : 8838 : case LAMBDA_EXPR:
6692 : 8838 : WT (((lang_tree_node *)t)->lambda_expression.capture_list);
6693 : 8838 : WT (((lang_tree_node *)t)->lambda_expression.this_capture);
6694 : 8838 : WT (((lang_tree_node *)t)->lambda_expression.extra_scope);
6695 : 8838 : WT (((lang_tree_node *)t)->lambda_expression.regen_info);
6696 : 8838 : WT (((lang_tree_node *)t)->lambda_expression.extra_args);
6697 : : /* pending_proxies is a parse-time thing. */
6698 : 8838 : gcc_assert (!((lang_tree_node *)t)->lambda_expression.pending_proxies);
6699 : 8838 : if (state)
6700 : 8838 : state->write_location
6701 : 8838 : (*this, ((lang_tree_node *)t)->lambda_expression.locus);
6702 : 8838 : if (streaming_p ())
6703 : : {
6704 : 2997 : WU (((lang_tree_node *)t)->lambda_expression.default_capture_mode);
6705 : 2997 : WU (((lang_tree_node *)t)->lambda_expression.discriminator_scope);
6706 : 2997 : WU (((lang_tree_node *)t)->lambda_expression.discriminator_sig);
6707 : : }
6708 : : break;
6709 : :
6710 : 1470303 : case OVERLOAD:
6711 : 1470303 : WT (((lang_tree_node *)t)->overload.function);
6712 : 1470303 : WT (t->common.chain);
6713 : 1470303 : break;
6714 : :
6715 : 0 : case PTRMEM_CST:
6716 : 0 : WT (((lang_tree_node *)t)->ptrmem.member);
6717 : 0 : break;
6718 : :
6719 : 12870 : case STATIC_ASSERT:
6720 : 12870 : WT (((lang_tree_node *)t)->static_assertion.condition);
6721 : 12870 : WT (((lang_tree_node *)t)->static_assertion.message);
6722 : 12870 : if (state)
6723 : 12870 : state->write_location
6724 : 12870 : (*this, ((lang_tree_node *)t)->static_assertion.location);
6725 : : break;
6726 : :
6727 : 563471 : case TEMPLATE_DECL:
6728 : : /* Streamed with the template_decl node itself. */
6729 : 563471 : gcc_checking_assert
6730 : : (TREE_VISITED (((lang_tree_node *)t)->template_decl.arguments));
6731 : 563471 : gcc_checking_assert
6732 : : (TREE_VISITED (((lang_tree_node *)t)->template_decl.result));
6733 : 563471 : if (DECL_UNINSTANTIATED_TEMPLATE_FRIEND_P (t))
6734 : 9348 : WT (DECL_CHAIN (t));
6735 : : break;
6736 : :
6737 : 1258152 : case TEMPLATE_INFO:
6738 : 1258152 : {
6739 : 1258152 : WT (((lang_tree_node *)t)->template_info.tmpl);
6740 : 1258152 : WT (((lang_tree_node *)t)->template_info.args);
6741 : 1258152 : WT (((lang_tree_node *)t)->template_info.partial);
6742 : :
6743 : 1258152 : const auto *ac = (((lang_tree_node *)t)
6744 : : ->template_info.deferred_access_checks);
6745 : 1258152 : unsigned len = vec_safe_length (ac);
6746 : 1258152 : if (streaming_p ())
6747 : 627642 : u (len);
6748 : 1258152 : if (len)
6749 : : {
6750 : 0 : for (unsigned ix = 0; ix != len; ix++)
6751 : : {
6752 : 0 : const auto &m = (*ac)[ix];
6753 : 0 : WT (m.binfo);
6754 : 0 : WT (m.decl);
6755 : 0 : WT (m.diag_decl);
6756 : 0 : if (state)
6757 : 0 : state->write_location (*this, m.loc);
6758 : : }
6759 : : }
6760 : : }
6761 : : break;
6762 : :
6763 : 1565917 : case TEMPLATE_PARM_INDEX:
6764 : 1565917 : if (streaming_p ())
6765 : : {
6766 : 352987 : WU (((lang_tree_node *)t)->tpi.index);
6767 : 352987 : WU (((lang_tree_node *)t)->tpi.level);
6768 : 352987 : WU (((lang_tree_node *)t)->tpi.orig_level);
6769 : : }
6770 : 1565917 : WT (((lang_tree_node *)t)->tpi.decl);
6771 : : /* TEMPLATE_PARM_DESCENDANTS (AKA TREE_CHAIN) is an internal
6772 : : cache, do not stream. */
6773 : 1565917 : break;
6774 : :
6775 : 24443 : case TRAIT_EXPR:
6776 : 24443 : WT (((lang_tree_node *)t)->trait_expression.type1);
6777 : 24443 : WT (((lang_tree_node *)t)->trait_expression.type2);
6778 : 24443 : if (streaming_p ())
6779 : 9390 : WU (((lang_tree_node *)t)->trait_expression.kind);
6780 : : break;
6781 : : }
6782 : :
6783 : 31332970 : if (CODE_CONTAINS_STRUCT (code, TS_TYPED))
6784 : : {
6785 : : /* We want to stream the type of a expression-like nodes /after/
6786 : : we've streamed the operands. The type often contains (bits
6787 : : of the) types of the operands, and with things like decltype
6788 : : and noexcept in play, we really want to stream the decls
6789 : : defining the type before we try and stream the type on its
6790 : : own. Otherwise we can find ourselves trying to read in a
6791 : : decl, when we're already partially reading in a component of
6792 : : its type. And that's bad. */
6793 : 29516066 : tree type = t->typed.type;
6794 : 29516066 : unsigned prec = 0;
6795 : :
6796 : 29516066 : switch (code)
6797 : : {
6798 : : default:
6799 : : break;
6800 : :
6801 : : case TEMPLATE_DECL:
6802 : : /* We fill in the template's type separately. */
6803 : 29516066 : type = NULL_TREE;
6804 : : break;
6805 : :
6806 : 2206606 : case TYPE_DECL:
6807 : 2206606 : if (DECL_ORIGINAL_TYPE (t) && t == TYPE_NAME (type))
6808 : : /* This is a typedef. We set its type separately. */
6809 : : type = NULL_TREE;
6810 : : break;
6811 : :
6812 : 7793 : case ENUMERAL_TYPE:
6813 : 7793 : if (type && !ENUM_FIXED_UNDERLYING_TYPE_P (t))
6814 : : {
6815 : : /* Type is a restricted range integer type derived from the
6816 : : integer_types. Find the right one. */
6817 : 5007 : prec = TYPE_PRECISION (type);
6818 : 5007 : tree name = DECL_NAME (TYPE_NAME (type));
6819 : :
6820 : 65323 : for (unsigned itk = itk_none; itk--;)
6821 : 65323 : if (integer_types[itk]
6822 : 65323 : && DECL_NAME (TYPE_NAME (integer_types[itk])) == name)
6823 : : {
6824 : : type = integer_types[itk];
6825 : : break;
6826 : : }
6827 : 5007 : gcc_assert (type != t->typed.type);
6828 : : }
6829 : : break;
6830 : : }
6831 : :
6832 : 29516066 : WT (type);
6833 : 29516066 : if (prec && streaming_p ())
6834 : 2502 : WU (prec);
6835 : : }
6836 : :
6837 : 31332970 : if (TREE_CODE (t) == CONSTRUCTOR)
6838 : : {
6839 : 99692 : unsigned len = vec_safe_length (t->constructor.elts);
6840 : 99692 : if (streaming_p ())
6841 : 49169 : WU (len);
6842 : 99692 : if (len)
6843 : 318444 : for (unsigned ix = 0; ix != len; ix++)
6844 : : {
6845 : 271360 : const constructor_elt &elt = (*t->constructor.elts)[ix];
6846 : :
6847 : 271360 : WT (elt.index);
6848 : 271360 : WT (elt.value);
6849 : : }
6850 : : }
6851 : :
6852 : : #undef WT
6853 : : #undef WU
6854 : 31332970 : }
6855 : :
6856 : : // Streaming in a reference to a decl can cause that decl to be
6857 : : // TREE_USED, which is the mark_used behaviour we need most of the
6858 : : // time. The trees_in::unused can be incremented to inhibit this,
6859 : : // which is at least needed for vtables.
6860 : :
6861 : : bool
6862 : 10321519 : trees_in::core_vals (tree t)
6863 : : {
6864 : : #define RU(X) ((X) = u ())
6865 : : #define RUC(T,X) ((X) = T (u ()))
6866 : : #define RT(X) ((X) = tree_node ())
6867 : : #define RTU(X) ((X) = tree_node (true))
6868 : 10321519 : tree_code code = TREE_CODE (t);
6869 : :
6870 : : /* First by tree shape. */
6871 : 10321519 : if (CODE_CONTAINS_STRUCT (code, TS_DECL_MINIMAL))
6872 : : {
6873 : 2268864 : RT (t->decl_minimal.name);
6874 : 2268864 : if (!DECL_TEMPLATE_PARM_P (t))
6875 : 1973433 : RT (t->decl_minimal.context);
6876 : :
6877 : : /* Don't zap the locus just yet, we don't record it correctly
6878 : : and thus lose all location information. */
6879 : 2268864 : t->decl_minimal.locus = state->read_location (*this);
6880 : 2268864 : if (has_warning_spec (t))
6881 : 364 : put_warning_spec (t, u ());
6882 : : }
6883 : :
6884 : 10321519 : if (CODE_CONTAINS_STRUCT (code, TS_TYPE_COMMON))
6885 : : {
6886 : 393209 : RT (t->type_common.name);
6887 : 393209 : RT (t->type_common.context);
6888 : :
6889 : 393209 : RT (t->type_common.main_variant);
6890 : 393209 : RT (t->type_common.canonical);
6891 : :
6892 : : /* type_common.next_variant is internally manipulated. */
6893 : : /* type_common.pointer_to, type_common.reference_to. */
6894 : :
6895 : 393209 : RU (t->type_common.precision);
6896 : 393209 : RU (t->type_common.contains_placeholder_bits);
6897 : 393209 : RUC (machine_mode, t->type_common.mode);
6898 : 393209 : RU (t->type_common.align);
6899 : :
6900 : 393209 : if (!RECORD_OR_UNION_CODE_P (code))
6901 : : {
6902 : 268306 : RT (t->type_common.size);
6903 : 268306 : RT (t->type_common.size_unit);
6904 : : }
6905 : 393209 : RT (t->type_common.attributes);
6906 : :
6907 : 393209 : RT (t->type_common.common.chain); /* TYPE_STUB_DECL. */
6908 : : }
6909 : :
6910 : 10321519 : if (CODE_CONTAINS_STRUCT (code, TS_DECL_COMMON))
6911 : : {
6912 : 2268864 : RUC (machine_mode, t->decl_common.mode);
6913 : 2268864 : RU (t->decl_common.off_align);
6914 : 2268864 : RU (t->decl_common.align);
6915 : :
6916 : 2268864 : if (code != TEMPLATE_DECL)
6917 : : {
6918 : 2039010 : RT (t->decl_common.size);
6919 : 2039010 : RT (t->decl_common.size_unit);
6920 : : }
6921 : :
6922 : 2268864 : RT (t->decl_common.attributes);
6923 : 2268864 : RT (t->decl_common.abstract_origin);
6924 : : }
6925 : :
6926 : 10321519 : if (CODE_CONTAINS_STRUCT (code, TS_DECL_WITH_VIS))
6927 : : {
6928 : 1049617 : RT (t->decl_with_vis.assembler_name);
6929 : 1049617 : RUC (symbol_visibility, t->decl_with_vis.visibility);
6930 : : }
6931 : :
6932 : 10321519 : if (CODE_CONTAINS_STRUCT (code, TS_TYPE_NON_COMMON))
6933 : : {
6934 : 393209 : if (code == ENUMERAL_TYPE)
6935 : : {
6936 : : /* These fields get set even for opaque enums that lack a
6937 : : definition, so we stream them directly for each ENUMERAL_TYPE.
6938 : : We stream TYPE_VALUES as part of the definition. */
6939 : 2522 : RT (t->type_non_common.maxval);
6940 : 2522 : RT (t->type_non_common.minval);
6941 : : }
6942 : : /* Records and unions hold FIELDS, VFIELD & BINFO on these
6943 : : things. */
6944 : 390687 : else if (!RECORD_OR_UNION_CODE_P (code))
6945 : : {
6946 : : /* This is not clobbering TYPE_CACHED_VALUES, because this
6947 : : is a type that doesn't have any. */
6948 : 265784 : gcc_checking_assert (!TYPE_CACHED_VALUES_P (t));
6949 : 265784 : RT (t->type_non_common.values);
6950 : 265784 : RT (t->type_non_common.maxval);
6951 : 265784 : RT (t->type_non_common.minval);
6952 : : }
6953 : :
6954 : 393209 : RT (t->type_non_common.lang_1);
6955 : : }
6956 : :
6957 : 10321519 : if (CODE_CONTAINS_STRUCT (code, TS_EXP))
6958 : : {
6959 : 3910758 : t->exp.locus = state->read_location (*this);
6960 : 3910758 : if (has_warning_spec (t))
6961 : 314382 : put_warning_spec (t, u ());
6962 : :
6963 : 3910758 : bool vl = TREE_CODE_CLASS (code) == tcc_vl_exp;
6964 : 3910758 : for (unsigned limit = (vl ? VL_EXP_OPERAND_LENGTH (t)
6965 : 3910758 : : TREE_OPERAND_LENGTH (t)),
6966 : 10848793 : ix = unsigned (vl); ix != limit; ix++)
6967 : 6938035 : RTU (TREE_OPERAND (t, ix));
6968 : : }
6969 : :
6970 : : /* Then by CODE. Special cases and/or 1:1 tree shape
6971 : : correspondance. */
6972 : 10321519 : switch (code)
6973 : : {
6974 : : default:
6975 : : break;
6976 : :
6977 : : case ARGUMENT_PACK_SELECT:
6978 : : case DEFERRED_PARSE:
6979 : : case IDENTIFIER_NODE:
6980 : : case BINDING_VECTOR:
6981 : : case SSA_NAME:
6982 : : case TRANSLATION_UNIT_DECL:
6983 : : case USERDEF_LITERAL:
6984 : : return false; /* Should never meet. */
6985 : :
6986 : : /* Constants. */
6987 : 9 : case COMPLEX_CST:
6988 : 9 : RT (TREE_REALPART (t));
6989 : 9 : RT (TREE_IMAGPART (t));
6990 : 9 : break;
6991 : :
6992 : : case FIXED_CST:
6993 : : /* Not suported in C++. */
6994 : : return false;
6995 : :
6996 : 461615 : case INTEGER_CST:
6997 : 461615 : {
6998 : 461615 : unsigned num = TREE_INT_CST_EXT_NUNITS (t);
6999 : 924500 : for (unsigned ix = 0; ix != num; ix++)
7000 : 462885 : TREE_INT_CST_ELT (t, ix) = wu ();
7001 : : }
7002 : : break;
7003 : :
7004 : : case POLY_INT_CST:
7005 : 0 : for (unsigned ix = 0; ix != NUM_POLY_INT_COEFFS; ix++)
7006 : 0 : RT (POLY_INT_CST_COEFF (t, ix));
7007 : : break;
7008 : :
7009 : 13444 : case REAL_CST:
7010 : 13444 : if (const void *bytes = buf (sizeof (real_value)))
7011 : 13444 : memcpy (TREE_REAL_CST_PTR (t), bytes, sizeof (real_value));
7012 : : break;
7013 : :
7014 : : case STRING_CST:
7015 : : /* Streamed during start. */
7016 : : break;
7017 : :
7018 : 6 : case RAW_DATA_CST:
7019 : 6 : RT (RAW_DATA_OWNER (t));
7020 : 6 : gcc_assert (TREE_CODE (RAW_DATA_OWNER (t)) == STRING_CST
7021 : : && TREE_STRING_LENGTH (RAW_DATA_OWNER (t)));
7022 : 6 : RAW_DATA_POINTER (t) = TREE_STRING_POINTER (RAW_DATA_OWNER (t)) + z ();
7023 : 6 : break;
7024 : :
7025 : 24 : case VECTOR_CST:
7026 : 63 : for (unsigned ix = vector_cst_encoded_nelts (t); ix--;)
7027 : 39 : RT (VECTOR_CST_ENCODED_ELT (t, ix));
7028 : : break;
7029 : :
7030 : : /* Decls. */
7031 : 148219 : case VAR_DECL:
7032 : 148219 : if (DECL_CONTEXT (t)
7033 : 148219 : && TREE_CODE (DECL_CONTEXT (t)) != FUNCTION_DECL)
7034 : : {
7035 : 28586 : if (DECL_HAS_VALUE_EXPR_P (t))
7036 : : {
7037 : 9 : tree val = tree_node ();
7038 : 9 : SET_DECL_VALUE_EXPR (t, val);
7039 : : }
7040 : : break;
7041 : : }
7042 : : /* FALLTHROUGH */
7043 : :
7044 : 1019383 : case RESULT_DECL:
7045 : 1019383 : case PARM_DECL:
7046 : 1019383 : if (DECL_HAS_VALUE_EXPR_P (t))
7047 : : {
7048 : : /* The DECL_VALUE hash table is a cache, thus if we're
7049 : : reading a duplicate (which we end up discarding), the
7050 : : value expr will also be cleaned up at the next gc. */
7051 : 10116 : tree val = tree_node ();
7052 : 10116 : SET_DECL_VALUE_EXPR (t, val);
7053 : : }
7054 : : /* FALLTHROUGH */
7055 : :
7056 : 1052018 : case CONST_DECL:
7057 : 1052018 : case IMPORTED_DECL:
7058 : 1052018 : RT (t->decl_common.initial);
7059 : 1052018 : break;
7060 : :
7061 : 40471 : case FIELD_DECL:
7062 : 40471 : RT (t->field_decl.offset);
7063 : 40471 : RT (t->field_decl.bit_field_type);
7064 : 40471 : RT (t->field_decl.qualifier);
7065 : 40471 : RT (t->field_decl.bit_offset);
7066 : 40471 : RT (t->field_decl.fcontext);
7067 : 40471 : RT (t->decl_common.initial);
7068 : 40471 : break;
7069 : :
7070 : 11885 : case LABEL_DECL:
7071 : 11885 : RU (t->label_decl.label_decl_uid);
7072 : 11885 : RU (t->label_decl.eh_landing_pad_nr);
7073 : 11885 : break;
7074 : :
7075 : 319895 : case FUNCTION_DECL:
7076 : 319895 : {
7077 : 319895 : unsigned bltin = u ();
7078 : 319895 : t->function_decl.built_in_class = built_in_class (bltin);
7079 : 319895 : if (bltin != NOT_BUILT_IN)
7080 : : {
7081 : 6510 : bltin = u ();
7082 : 6510 : DECL_UNCHECKED_FUNCTION_CODE (t) = built_in_function (bltin);
7083 : : }
7084 : :
7085 : 319895 : RT (t->function_decl.personality);
7086 : 319895 : RT (t->function_decl.function_specific_target);
7087 : 319895 : RT (t->function_decl.function_specific_optimization);
7088 : 319895 : RT (t->function_decl.vindex);
7089 : :
7090 : 319895 : if (DECL_HAS_DEPENDENT_EXPLICIT_SPEC_P (t))
7091 : : {
7092 : 2104 : tree spec;
7093 : 2104 : RT (spec);
7094 : 2104 : store_explicit_specifier (t, spec);
7095 : : }
7096 : : }
7097 : : break;
7098 : :
7099 : 32642 : case USING_DECL:
7100 : : /* USING_DECL_DECLS */
7101 : 32642 : RT (t->decl_common.initial);
7102 : : /* FALLTHROUGH */
7103 : :
7104 : 581374 : case TYPE_DECL:
7105 : : /* USING_DECL: USING_DECL_SCOPE */
7106 : : /* TYPE_DECL: DECL_ORIGINAL_TYPE */
7107 : 581374 : RT (t->decl_non_common.result);
7108 : 581374 : break;
7109 : :
7110 : : /* Miscellaneous common nodes. */
7111 : 205335 : case BLOCK:
7112 : 205335 : t->block.locus = state->read_location (*this);
7113 : 205335 : t->block.end_locus = state->read_location (*this);
7114 : :
7115 : 205335 : for (tree *chain = &t->block.vars;;)
7116 : 310704 : if (tree decl = tree_node ())
7117 : : {
7118 : : /* For a deduplicated local type or enumerator, chain the
7119 : : duplicate decl instead of the canonical in-TU decl. Seeing
7120 : : a duplicate here means the containing function whose body
7121 : : we're streaming in is a duplicate too, so we'll end up
7122 : : discarding this BLOCK (and the rest of the duplicate function
7123 : : body) anyway. */
7124 : 105369 : decl = maybe_duplicate (decl);
7125 : :
7126 : 105369 : if (!DECL_P (decl))
7127 : : {
7128 : 0 : set_overrun ();
7129 : 0 : break;
7130 : : }
7131 : :
7132 : : /* If DECL_CHAIN is already set then this was a backreference to a
7133 : : local type or enumerator from a previous read (PR c++/114630).
7134 : : Let's copy the node so we can keep building the chain for ODR
7135 : : checking later. */
7136 : 105369 : if (DECL_CHAIN (decl))
7137 : : {
7138 : 12 : gcc_checking_assert (TREE_CODE (decl) == TYPE_DECL
7139 : : && find_duplicate (DECL_CONTEXT (decl)));
7140 : 6 : decl = copy_decl (decl);
7141 : : }
7142 : :
7143 : 105369 : *chain = decl;
7144 : 105369 : chain = &DECL_CHAIN (decl);
7145 : : }
7146 : : else
7147 : 105369 : break;
7148 : :
7149 : : /* nonlocalized_vars is middle-end. */
7150 : 205335 : RT (t->block.subblocks);
7151 : 205335 : RT (t->block.supercontext);
7152 : 205335 : RT (t->block.abstract_origin);
7153 : : /* fragment_origin, fragment_chain are middle-end. */
7154 : 205335 : RT (t->block.chain);
7155 : : /* nonlocalized_vars, block_num, die are middle endy/debug
7156 : : things. */
7157 : 205335 : break;
7158 : :
7159 : 381872 : case CALL_EXPR:
7160 : 381872 : RUC (internal_fn, t->base.u.ifn);
7161 : 381872 : break;
7162 : :
7163 : : case CONSTRUCTOR:
7164 : : // Streamed after the node's type.
7165 : : break;
7166 : :
7167 : 12 : case OMP_CLAUSE:
7168 : 12 : {
7169 : 12 : RU (t->omp_clause.subcode.map_kind);
7170 : 12 : t->omp_clause.locus = state->read_location (*this);
7171 : :
7172 : 12 : unsigned len = omp_clause_num_ops[OMP_CLAUSE_CODE (t)];
7173 : 45 : for (unsigned ix = 0; ix != len; ix++)
7174 : 33 : RT (t->omp_clause.ops[ix]);
7175 : : }
7176 : : break;
7177 : :
7178 : 145921 : case STATEMENT_LIST:
7179 : 145921 : {
7180 : 145921 : tree_stmt_iterator iter = tsi_start (t);
7181 : 578915 : for (tree stmt; RT (stmt);)
7182 : : {
7183 : 432994 : if (TREE_CODE (stmt) == DEBUG_BEGIN_STMT
7184 : 0 : && !MAY_HAVE_DEBUG_MARKER_STMTS)
7185 : 0 : continue;
7186 : 432994 : tsi_link_after (&iter, stmt, TSI_CONTINUE_LINKING);
7187 : : }
7188 : : }
7189 : 145921 : break;
7190 : :
7191 : 0 : case OPTIMIZATION_NODE:
7192 : 0 : case TARGET_OPTION_NODE:
7193 : : /* Not yet implemented, see trees_out::core_vals. */
7194 : 0 : gcc_unreachable ();
7195 : 71058 : break;
7196 : :
7197 : 71058 : case TREE_BINFO:
7198 : 71058 : RT (t->binfo.common.chain);
7199 : 71058 : RT (t->binfo.offset);
7200 : 71058 : RT (t->binfo.inheritance);
7201 : 71058 : RT (t->binfo.vptr_field);
7202 : :
7203 : : /* Do not mark the vtables as USED in the address expressions
7204 : : here. */
7205 : 71058 : unused++;
7206 : 71058 : RT (t->binfo.vtable);
7207 : 71058 : RT (t->binfo.virtuals);
7208 : 71058 : RT (t->binfo.vtt_subvtt);
7209 : 71058 : RT (t->binfo.vtt_vptr);
7210 : 71058 : unused--;
7211 : :
7212 : 71058 : BINFO_BASE_ACCESSES (t) = tree_vec ();
7213 : 71058 : if (!get_overrun ())
7214 : : {
7215 : 71058 : unsigned num = vec_safe_length (BINFO_BASE_ACCESSES (t));
7216 : 91454 : for (unsigned ix = 0; ix != num; ix++)
7217 : 20396 : BINFO_BASE_APPEND (t, tree_node ());
7218 : : }
7219 : : break;
7220 : :
7221 : 901307 : case TREE_LIST:
7222 : 901307 : RT (t->list.purpose);
7223 : 901307 : RT (t->list.value);
7224 : 901307 : RT (t->list.common.chain);
7225 : 901307 : break;
7226 : :
7227 : 573577 : case TREE_VEC:
7228 : 1537948 : for (unsigned ix = TREE_VEC_LENGTH (t); ix--;)
7229 : 964371 : RT (TREE_VEC_ELT (t, ix));
7230 : 573577 : RT (t->type_common.common.chain);
7231 : 573577 : break;
7232 : :
7233 : : /* C++-specific nodes ... */
7234 : 66696 : case BASELINK:
7235 : 66696 : RT (((lang_tree_node *)t)->baselink.binfo);
7236 : 66696 : RTU (((lang_tree_node *)t)->baselink.functions);
7237 : 66696 : RT (((lang_tree_node *)t)->baselink.access_binfo);
7238 : 66696 : break;
7239 : :
7240 : 26550 : case CONSTRAINT_INFO:
7241 : 26550 : RT (((lang_tree_node *)t)->constraint_info.template_reqs);
7242 : 26550 : RT (((lang_tree_node *)t)->constraint_info.declarator_reqs);
7243 : 26550 : RT (((lang_tree_node *)t)->constraint_info.associated_constr);
7244 : 26550 : break;
7245 : :
7246 : 4818 : case DEFERRED_NOEXCEPT:
7247 : 4818 : RT (((lang_tree_node *)t)->deferred_noexcept.pattern);
7248 : 4818 : RT (((lang_tree_node *)t)->deferred_noexcept.args);
7249 : 4818 : break;
7250 : :
7251 : 2725 : case LAMBDA_EXPR:
7252 : 2725 : RT (((lang_tree_node *)t)->lambda_expression.capture_list);
7253 : 2725 : RT (((lang_tree_node *)t)->lambda_expression.this_capture);
7254 : 2725 : RT (((lang_tree_node *)t)->lambda_expression.extra_scope);
7255 : 2725 : RT (((lang_tree_node *)t)->lambda_expression.regen_info);
7256 : 2725 : RT (((lang_tree_node *)t)->lambda_expression.extra_args);
7257 : : /* lambda_expression.pending_proxies is NULL */
7258 : 2725 : ((lang_tree_node *)t)->lambda_expression.locus
7259 : 2725 : = state->read_location (*this);
7260 : 2725 : RUC (cp_lambda_default_capture_mode_type,
7261 : : ((lang_tree_node *)t)->lambda_expression.default_capture_mode);
7262 : 2725 : RU (((lang_tree_node *)t)->lambda_expression.discriminator_scope);
7263 : 2725 : RU (((lang_tree_node *)t)->lambda_expression.discriminator_sig);
7264 : 2725 : break;
7265 : :
7266 : 385904 : case OVERLOAD:
7267 : 385904 : RT (((lang_tree_node *)t)->overload.function);
7268 : 385904 : RT (t->common.chain);
7269 : 385904 : break;
7270 : :
7271 : 0 : case PTRMEM_CST:
7272 : 0 : RT (((lang_tree_node *)t)->ptrmem.member);
7273 : 0 : break;
7274 : :
7275 : 5277 : case STATIC_ASSERT:
7276 : 5277 : RT (((lang_tree_node *)t)->static_assertion.condition);
7277 : 5277 : RT (((lang_tree_node *)t)->static_assertion.message);
7278 : 5277 : ((lang_tree_node *)t)->static_assertion.location
7279 : 5277 : = state->read_location (*this);
7280 : 5277 : break;
7281 : :
7282 : 229854 : case TEMPLATE_DECL:
7283 : : /* Streamed when reading the raw template decl itself. */
7284 : 229854 : gcc_assert (((lang_tree_node *)t)->template_decl.arguments);
7285 : 229854 : gcc_assert (((lang_tree_node *)t)->template_decl.result);
7286 : 229854 : if (DECL_UNINSTANTIATED_TEMPLATE_FRIEND_P (t))
7287 : 4453 : RT (DECL_CHAIN (t));
7288 : : break;
7289 : :
7290 : 511894 : case TEMPLATE_INFO:
7291 : 511894 : RT (((lang_tree_node *)t)->template_info.tmpl);
7292 : 511894 : RT (((lang_tree_node *)t)->template_info.args);
7293 : 511894 : RT (((lang_tree_node *)t)->template_info.partial);
7294 : 511894 : if (unsigned len = u ())
7295 : : {
7296 : 0 : auto &ac = (((lang_tree_node *)t)
7297 : : ->template_info.deferred_access_checks);
7298 : 0 : vec_alloc (ac, len);
7299 : 0 : for (unsigned ix = 0; ix != len; ix++)
7300 : : {
7301 : 0 : deferred_access_check m;
7302 : :
7303 : 0 : RT (m.binfo);
7304 : 0 : RT (m.decl);
7305 : 0 : RT (m.diag_decl);
7306 : 0 : m.loc = state->read_location (*this);
7307 : 0 : ac->quick_push (m);
7308 : : }
7309 : : }
7310 : : break;
7311 : :
7312 : 282828 : case TEMPLATE_PARM_INDEX:
7313 : 282828 : RU (((lang_tree_node *)t)->tpi.index);
7314 : 282828 : RU (((lang_tree_node *)t)->tpi.level);
7315 : 282828 : RU (((lang_tree_node *)t)->tpi.orig_level);
7316 : 282828 : RT (((lang_tree_node *)t)->tpi.decl);
7317 : 282828 : break;
7318 : :
7319 : 6619 : case TRAIT_EXPR:
7320 : 6619 : RT (((lang_tree_node *)t)->trait_expression.type1);
7321 : 6619 : RT (((lang_tree_node *)t)->trait_expression.type2);
7322 : 6619 : RUC (cp_trait_kind, ((lang_tree_node *)t)->trait_expression.kind);
7323 : 6619 : break;
7324 : : }
7325 : :
7326 : 10321519 : if (CODE_CONTAINS_STRUCT (code, TS_TYPED))
7327 : : {
7328 : 9567645 : tree type = tree_node ();
7329 : :
7330 : 9567645 : if (type && code == ENUMERAL_TYPE && !ENUM_FIXED_UNDERLYING_TYPE_P (t))
7331 : : {
7332 : 1470 : unsigned precision = u ();
7333 : :
7334 : 1470 : type = build_distinct_type_copy (type);
7335 : 1470 : TYPE_PRECISION (type) = precision;
7336 : 2940 : set_min_and_max_values_for_integral_type (type, precision,
7337 : 1470 : TYPE_SIGN (type));
7338 : : }
7339 : :
7340 : 9567645 : if (code != TEMPLATE_DECL)
7341 : 9337791 : t->typed.type = type;
7342 : : }
7343 : :
7344 : 10321519 : if (TREE_CODE (t) == CONSTRUCTOR)
7345 : 44909 : if (unsigned len = u ())
7346 : : {
7347 : 23854 : vec_alloc (t->constructor.elts, len);
7348 : 161684 : for (unsigned ix = 0; ix != len; ix++)
7349 : : {
7350 : 137830 : constructor_elt elt;
7351 : :
7352 : 137830 : RT (elt.index);
7353 : 137830 : RTU (elt.value);
7354 : 137830 : t->constructor.elts->quick_push (elt);
7355 : : }
7356 : : }
7357 : :
7358 : : #undef RT
7359 : : #undef RM
7360 : : #undef RU
7361 : 10321519 : return !get_overrun ();
7362 : : }
7363 : :
7364 : : void
7365 : 4012083 : trees_out::lang_decl_vals (tree t)
7366 : : {
7367 : 4012083 : const struct lang_decl *lang = DECL_LANG_SPECIFIC (t);
7368 : : #define WU(X) (u (X))
7369 : : #define WT(X) (tree_node (X))
7370 : : /* Module index already written. */
7371 : 4012083 : switch (lang->u.base.selector)
7372 : : {
7373 : 0 : default:
7374 : 0 : gcc_unreachable ();
7375 : :
7376 : 740091 : case lds_fn: /* lang_decl_fn. */
7377 : 740091 : if (streaming_p ())
7378 : : {
7379 : 369962 : if (DECL_NAME (t) && IDENTIFIER_OVL_OP_P (DECL_NAME (t)))
7380 : 61582 : WU (lang->u.fn.ovl_op_code);
7381 : : }
7382 : :
7383 : 937590 : if (DECL_CLASS_SCOPE_P (t) || DECL_UNIQUE_FRIEND_P (t))
7384 : 560120 : WT (lang->u.fn.context);
7385 : :
7386 : 740091 : if (lang->u.fn.thunk_p)
7387 : : {
7388 : : /* The thunked-to function. */
7389 : 1128 : WT (lang->u.fn.befriending_classes);
7390 : 1128 : if (streaming_p ())
7391 : 564 : wi (lang->u.fn.u5.fixed_offset);
7392 : : }
7393 : 738963 : else if (decl_tls_wrapper_p (t))
7394 : : /* The wrapped variable. */
7395 : 18 : WT (lang->u.fn.befriending_classes);
7396 : : else
7397 : 738945 : WT (lang->u.fn.u5.cloned_function);
7398 : :
7399 : 740091 : if (FNDECL_USED_AUTO (t))
7400 : 2278 : WT (lang->u.fn.u.saved_auto_return_type);
7401 : :
7402 : 740091 : goto lds_min;
7403 : :
7404 : 2766 : case lds_decomp: /* lang_decl_decomp. */
7405 : 2766 : WT (lang->u.decomp.base);
7406 : 2766 : goto lds_min;
7407 : :
7408 : 2366555 : case lds_min: /* lang_decl_min. */
7409 : 2366555 : lds_min:
7410 : 2366555 : WT (lang->u.min.template_info);
7411 : 2366555 : {
7412 : 2366555 : tree access = lang->u.min.access;
7413 : :
7414 : : /* DECL_ACCESS needs to be maintained by the definition of the
7415 : : (derived) class that changes the access. The other users
7416 : : of DECL_ACCESS need to write it here. */
7417 : 740091 : if (!DECL_THUNK_P (t)
7418 : 3105518 : && (DECL_CONTEXT (t) && TYPE_P (DECL_CONTEXT (t))))
7419 : : access = NULL_TREE;
7420 : :
7421 : 2366555 : WT (access);
7422 : : }
7423 : 2366555 : break;
7424 : :
7425 : : case lds_ns: /* lang_decl_ns. */
7426 : : break;
7427 : :
7428 : 1645355 : case lds_parm: /* lang_decl_parm. */
7429 : 1645355 : if (streaming_p ())
7430 : : {
7431 : 561330 : WU (lang->u.parm.level);
7432 : 561330 : WU (lang->u.parm.index);
7433 : : }
7434 : : break;
7435 : : }
7436 : : #undef WU
7437 : : #undef WT
7438 : 4012083 : }
7439 : :
7440 : : bool
7441 : 1444130 : trees_in::lang_decl_vals (tree t)
7442 : : {
7443 : 1444130 : struct lang_decl *lang = DECL_LANG_SPECIFIC (t);
7444 : : #define RU(X) ((X) = u ())
7445 : : #define RT(X) ((X) = tree_node ())
7446 : :
7447 : : /* Module index already read. */
7448 : 1444130 : switch (lang->u.base.selector)
7449 : : {
7450 : 0 : default:
7451 : 0 : gcc_unreachable ();
7452 : :
7453 : 319895 : case lds_fn: /* lang_decl_fn. */
7454 : 319895 : if (DECL_NAME (t) && IDENTIFIER_OVL_OP_P (DECL_NAME (t)))
7455 : : {
7456 : 54711 : unsigned code = u ();
7457 : :
7458 : : /* Check consistency. */
7459 : 54711 : if (code >= OVL_OP_MAX
7460 : 54711 : || (ovl_op_info[IDENTIFIER_ASSIGN_OP_P (DECL_NAME (t))][code]
7461 : 54711 : .ovl_op_code) == OVL_OP_ERROR_MARK)
7462 : 0 : set_overrun ();
7463 : : else
7464 : 54711 : lang->u.fn.ovl_op_code = code;
7465 : : }
7466 : :
7467 : 402561 : if (DECL_CLASS_SCOPE_P (t) || DECL_UNIQUE_FRIEND_P (t))
7468 : 245402 : RT (lang->u.fn.context);
7469 : :
7470 : 319895 : if (lang->u.fn.thunk_p)
7471 : : {
7472 : 490 : RT (lang->u.fn.befriending_classes);
7473 : 490 : lang->u.fn.u5.fixed_offset = wi ();
7474 : : }
7475 : 319405 : else if (decl_tls_wrapper_p (t))
7476 : 15 : RT (lang->u.fn.befriending_classes);
7477 : : else
7478 : 319390 : RT (lang->u.fn.u5.cloned_function);
7479 : :
7480 : 319895 : if (FNDECL_USED_AUTO (t))
7481 : 961 : RT (lang->u.fn.u.saved_auto_return_type);
7482 : 319895 : goto lds_min;
7483 : :
7484 : 1303 : case lds_decomp: /* lang_decl_decomp. */
7485 : 1303 : RT (lang->u.decomp.base);
7486 : 1303 : goto lds_min;
7487 : :
7488 : 969502 : case lds_min: /* lang_decl_min. */
7489 : 969502 : lds_min:
7490 : 969502 : RT (lang->u.min.template_info);
7491 : 969502 : RT (lang->u.min.access);
7492 : 969502 : break;
7493 : :
7494 : : case lds_ns: /* lang_decl_ns. */
7495 : : break;
7496 : :
7497 : 474526 : case lds_parm: /* lang_decl_parm. */
7498 : 474526 : RU (lang->u.parm.level);
7499 : 474526 : RU (lang->u.parm.index);
7500 : 474526 : break;
7501 : : }
7502 : : #undef RU
7503 : : #undef RT
7504 : 1444130 : return !get_overrun ();
7505 : : }
7506 : :
7507 : : /* Most of the value contents of lang_type is streamed in
7508 : : define_class. */
7509 : :
7510 : : void
7511 : 286152 : trees_out::lang_type_vals (tree t)
7512 : : {
7513 : 286152 : const struct lang_type *lang = TYPE_LANG_SPECIFIC (t);
7514 : : #define WU(X) (u (X))
7515 : : #define WT(X) (tree_node (X))
7516 : 286152 : if (streaming_p ())
7517 : 143056 : WU (lang->align);
7518 : : #undef WU
7519 : : #undef WT
7520 : 286152 : }
7521 : :
7522 : : bool
7523 : 110296 : trees_in::lang_type_vals (tree t)
7524 : : {
7525 : 110296 : struct lang_type *lang = TYPE_LANG_SPECIFIC (t);
7526 : : #define RU(X) ((X) = u ())
7527 : : #define RT(X) ((X) = tree_node ())
7528 : 110296 : RU (lang->align);
7529 : : #undef RU
7530 : : #undef RT
7531 : 110296 : return !get_overrun ();
7532 : : }
7533 : :
7534 : : /* Write out the bools of T, including information about any
7535 : : LANG_SPECIFIC information. Including allocation of any lang
7536 : : specific object. */
7537 : :
7538 : : void
7539 : 12111769 : trees_out::tree_node_bools (tree t)
7540 : : {
7541 : 12111769 : gcc_checking_assert (streaming_p ());
7542 : :
7543 : : /* We should never stream a namespace. */
7544 : 12111769 : gcc_checking_assert (TREE_CODE (t) != NAMESPACE_DECL
7545 : : || DECL_NAMESPACE_ALIAS (t));
7546 : :
7547 : 12111769 : bits_out bits = stream_bits ();
7548 : 12111769 : core_bools (t, bits);
7549 : :
7550 : 12111769 : switch (TREE_CODE_CLASS (TREE_CODE (t)))
7551 : : {
7552 : 2716360 : case tcc_declaration:
7553 : 2716360 : {
7554 : 2716360 : bool specific = DECL_LANG_SPECIFIC (t) != NULL;
7555 : 2716360 : bits.b (specific);
7556 : 2716360 : if (specific && VAR_P (t))
7557 : 243620 : bits.b (DECL_DECOMPOSITION_P (t));
7558 : 1737568 : if (specific)
7559 : 1737568 : lang_decl_bools (t, bits);
7560 : : }
7561 : : break;
7562 : :
7563 : 518447 : case tcc_type:
7564 : 518447 : {
7565 : 518447 : bool specific = (TYPE_MAIN_VARIANT (t) == t
7566 : 518447 : && TYPE_LANG_SPECIFIC (t) != NULL);
7567 : 518447 : gcc_assert (TYPE_LANG_SPECIFIC (t)
7568 : : == TYPE_LANG_SPECIFIC (TYPE_MAIN_VARIANT (t)));
7569 : :
7570 : 518447 : bits.b (specific);
7571 : 518447 : if (specific)
7572 : 143056 : lang_type_bools (t, bits);
7573 : : }
7574 : : break;
7575 : :
7576 : : default:
7577 : : break;
7578 : : }
7579 : :
7580 : 12111769 : bits.bflush ();
7581 : 12111769 : }
7582 : :
7583 : : bool
7584 : 10336080 : trees_in::tree_node_bools (tree t)
7585 : : {
7586 : 10336080 : bits_in bits = stream_bits ();
7587 : 10336080 : bool ok = core_bools (t, bits);
7588 : :
7589 : 10336080 : if (ok)
7590 : 10336080 : switch (TREE_CODE_CLASS (TREE_CODE (t)))
7591 : : {
7592 : 2268864 : case tcc_declaration:
7593 : 2268864 : if (bits.b ())
7594 : : {
7595 : 1444130 : bool decomp = VAR_P (t) && bits.b ();
7596 : :
7597 : 1444130 : ok = maybe_add_lang_decl_raw (t, decomp);
7598 : 1444130 : if (ok)
7599 : 1444130 : ok = lang_decl_bools (t, bits);
7600 : : }
7601 : : break;
7602 : :
7603 : 407770 : case tcc_type:
7604 : 407770 : if (bits.b ())
7605 : : {
7606 : 110296 : ok = maybe_add_lang_type_raw (t);
7607 : 110296 : if (ok)
7608 : 110296 : ok = lang_type_bools (t, bits);
7609 : : }
7610 : : break;
7611 : :
7612 : : default:
7613 : : break;
7614 : : }
7615 : :
7616 : 10336080 : bits.bflush ();
7617 : 10336080 : if (!ok || get_overrun ())
7618 : 0 : return false;
7619 : :
7620 : : return true;
7621 : 10336080 : }
7622 : :
7623 : :
7624 : : /* Write out the lang-specifc vals of node T. */
7625 : :
7626 : : void
7627 : 31332970 : trees_out::lang_vals (tree t)
7628 : : {
7629 : 31332970 : switch (TREE_CODE_CLASS (TREE_CODE (t)))
7630 : : {
7631 : 7100533 : case tcc_declaration:
7632 : 7100533 : if (DECL_LANG_SPECIFIC (t))
7633 : 4012083 : lang_decl_vals (t);
7634 : : break;
7635 : :
7636 : 1798952 : case tcc_type:
7637 : 1798952 : if (TYPE_MAIN_VARIANT (t) == t && TYPE_LANG_SPECIFIC (t))
7638 : 286152 : lang_type_vals (t);
7639 : : break;
7640 : :
7641 : : default:
7642 : : break;
7643 : : }
7644 : 31332970 : }
7645 : :
7646 : : bool
7647 : 10321519 : trees_in::lang_vals (tree t)
7648 : : {
7649 : 10321519 : bool ok = true;
7650 : :
7651 : 10321519 : switch (TREE_CODE_CLASS (TREE_CODE (t)))
7652 : : {
7653 : 2268864 : case tcc_declaration:
7654 : 2268864 : if (DECL_LANG_SPECIFIC (t))
7655 : 1444130 : ok = lang_decl_vals (t);
7656 : : break;
7657 : :
7658 : 393209 : case tcc_type:
7659 : 393209 : if (TYPE_LANG_SPECIFIC (t))
7660 : 110296 : ok = lang_type_vals (t);
7661 : : else
7662 : 282913 : TYPE_LANG_SPECIFIC (t) = TYPE_LANG_SPECIFIC (TYPE_MAIN_VARIANT (t));
7663 : : break;
7664 : :
7665 : : default:
7666 : : break;
7667 : : }
7668 : :
7669 : 10321519 : return ok;
7670 : : }
7671 : :
7672 : : /* Write out the value fields of node T. */
7673 : :
7674 : : void
7675 : 31332970 : trees_out::tree_node_vals (tree t)
7676 : : {
7677 : 31332970 : core_vals (t);
7678 : 31332970 : lang_vals (t);
7679 : 31332970 : }
7680 : :
7681 : : bool
7682 : 10321519 : trees_in::tree_node_vals (tree t)
7683 : : {
7684 : 10321519 : bool ok = core_vals (t);
7685 : 10321519 : if (ok)
7686 : 10321519 : ok = lang_vals (t);
7687 : :
7688 : 10321519 : return ok;
7689 : : }
7690 : :
7691 : :
7692 : : /* If T is a back reference, fixed reference or NULL, write out its
7693 : : code and return WK_none. Otherwise return WK_value if we must write
7694 : : by value, or WK_normal otherwise. */
7695 : :
7696 : : walk_kind
7697 : 210635754 : trees_out::ref_node (tree t)
7698 : : {
7699 : 210635754 : if (!t)
7700 : : {
7701 : 86683763 : if (streaming_p ())
7702 : : {
7703 : : /* NULL_TREE -> tt_null. */
7704 : 33238177 : null_count++;
7705 : 33238177 : i (tt_null);
7706 : : }
7707 : 86683763 : return WK_none;
7708 : : }
7709 : :
7710 : 123951991 : if (!TREE_VISITED (t))
7711 : : return WK_normal;
7712 : :
7713 : : /* An already-visited tree. It must be in the map. */
7714 : 73005975 : int val = get_tag (t);
7715 : :
7716 : 73005975 : if (val == tag_value)
7717 : : /* An entry we should walk into. */
7718 : : return WK_value;
7719 : :
7720 : 71796140 : const char *kind;
7721 : :
7722 : 71796140 : if (val <= tag_backref)
7723 : : {
7724 : : /* Back reference -> -ve number */
7725 : 55036126 : if (streaming_p ())
7726 : 26022243 : i (val);
7727 : : kind = "backref";
7728 : : }
7729 : 16760014 : else if (val >= tag_fixed)
7730 : : {
7731 : : /* Fixed reference -> tt_fixed */
7732 : 16760014 : val -= tag_fixed;
7733 : 16760014 : if (streaming_p ())
7734 : 5920464 : i (tt_fixed), u (val);
7735 : : kind = "fixed";
7736 : : }
7737 : :
7738 : 71796140 : if (streaming_p ())
7739 : : {
7740 : 31942707 : back_ref_count++;
7741 : 31942707 : dump (dumper::TREE)
7742 : 13191 : && dump ("Wrote %s:%d %C:%N%S", kind, val, TREE_CODE (t), t, t);
7743 : : }
7744 : : return WK_none;
7745 : : }
7746 : :
7747 : : tree
7748 : 22040151 : trees_in::back_ref (int tag)
7749 : : {
7750 : 22040151 : tree res = NULL_TREE;
7751 : :
7752 : 22040151 : if (tag < 0 && unsigned (~tag) < back_refs.length ())
7753 : 22040151 : res = back_refs[~tag];
7754 : :
7755 : 22040151 : if (!res
7756 : : /* Checking TREE_CODE is a dereference, so we know this is not a
7757 : : wild pointer. Checking the code provides evidence we've not
7758 : : corrupted something. */
7759 : 22040151 : || TREE_CODE (res) >= MAX_TREE_CODES)
7760 : 0 : set_overrun ();
7761 : : else
7762 : 22054077 : dump (dumper::TREE) && dump ("Read backref:%d found %C:%N%S", tag,
7763 : : TREE_CODE (res), res, res);
7764 : 22040151 : return res;
7765 : : }
7766 : :
7767 : : unsigned
7768 : 2738860 : trees_out::add_indirect_tpl_parms (tree parms)
7769 : : {
7770 : 2738860 : unsigned len = 0;
7771 : 5119478 : for (; parms; parms = TREE_CHAIN (parms), len++)
7772 : : {
7773 : 2892751 : if (TREE_VISITED (parms))
7774 : : break;
7775 : :
7776 : 2380618 : int tag = insert (parms);
7777 : 2380618 : if (streaming_p ())
7778 : 2380897 : dump (dumper::TREE)
7779 : 153 : && dump ("Indirect:%d template's parameter %u %C:%N",
7780 : 153 : tag, len, TREE_CODE (parms), parms);
7781 : : }
7782 : :
7783 : 2738860 : if (streaming_p ())
7784 : 456289 : u (len);
7785 : :
7786 : 2738860 : return len;
7787 : : }
7788 : :
7789 : : unsigned
7790 : 373618 : trees_in::add_indirect_tpl_parms (tree parms)
7791 : : {
7792 : 373618 : unsigned len = u ();
7793 : 672347 : for (unsigned ix = 0; ix != len; parms = TREE_CHAIN (parms), ix++)
7794 : : {
7795 : 298729 : int tag = insert (parms);
7796 : 299167 : dump (dumper::TREE)
7797 : 159 : && dump ("Indirect:%d template's parameter %u %C:%N",
7798 : 159 : tag, ix, TREE_CODE (parms), parms);
7799 : : }
7800 : :
7801 : 373618 : return len;
7802 : : }
7803 : :
7804 : : /* We've just found DECL by name. Insert nodes that come with it, but
7805 : : cannot be found by name, so we'll not accidentally walk into them. */
7806 : :
7807 : : void
7808 : 6306243 : trees_out::add_indirects (tree decl)
7809 : : {
7810 : 6306243 : unsigned count = 0;
7811 : :
7812 : : // FIXME:OPTIMIZATION We'll eventually want default fn parms of
7813 : : // templates and perhaps default template parms too. The former can
7814 : : // be referenced from instantiations (as they are lazily
7815 : : // instantiated). Also (deferred?) exception specifications of
7816 : : // templates. See the note about PARM_DECLs in trees_out::decl_node.
7817 : 6306243 : tree inner = decl;
7818 : 6306243 : if (TREE_CODE (decl) == TEMPLATE_DECL)
7819 : : {
7820 : 2738860 : count += add_indirect_tpl_parms (DECL_TEMPLATE_PARMS (decl));
7821 : :
7822 : 2738860 : inner = DECL_TEMPLATE_RESULT (decl);
7823 : 2738860 : int tag = insert (inner);
7824 : 2738860 : if (streaming_p ())
7825 : 456289 : dump (dumper::TREE)
7826 : 219 : && dump ("Indirect:%d template's result %C:%N",
7827 : 219 : tag, TREE_CODE (inner), inner);
7828 : 2738860 : count++;
7829 : : }
7830 : :
7831 : 6306243 : if (TREE_CODE (inner) == TYPE_DECL)
7832 : : {
7833 : : /* Make sure the type is in the map too. Otherwise we get
7834 : : different RECORD_TYPEs for the same type, and things go
7835 : : south. */
7836 : 3483559 : tree type = TREE_TYPE (inner);
7837 : 3483559 : gcc_checking_assert (DECL_ORIGINAL_TYPE (inner)
7838 : : || TYPE_NAME (type) == inner);
7839 : 3483559 : int tag = insert (type);
7840 : 3483559 : if (streaming_p ())
7841 : 405611 : dump (dumper::TREE) && dump ("Indirect:%d decl's type %C:%N", tag,
7842 : 360 : TREE_CODE (type), type);
7843 : 3483559 : count++;
7844 : : }
7845 : :
7846 : 6306243 : if (streaming_p ())
7847 : : {
7848 : 978405 : u (count);
7849 : 978918 : dump (dumper::TREE) && dump ("Inserted %u indirects", count);
7850 : : }
7851 : 6306243 : }
7852 : :
7853 : : bool
7854 : 774070 : trees_in::add_indirects (tree decl)
7855 : : {
7856 : 774070 : unsigned count = 0;
7857 : :
7858 : 774070 : tree inner = decl;
7859 : 774070 : if (TREE_CODE (inner) == TEMPLATE_DECL)
7860 : : {
7861 : 373618 : count += add_indirect_tpl_parms (DECL_TEMPLATE_PARMS (decl));
7862 : :
7863 : 373618 : inner = DECL_TEMPLATE_RESULT (decl);
7864 : 373618 : int tag = insert (inner);
7865 : 373618 : dump (dumper::TREE)
7866 : 228 : && dump ("Indirect:%d templates's result %C:%N", tag,
7867 : 228 : TREE_CODE (inner), inner);
7868 : 373618 : count++;
7869 : : }
7870 : :
7871 : 774070 : if (TREE_CODE (inner) == TYPE_DECL)
7872 : : {
7873 : 300221 : tree type = TREE_TYPE (inner);
7874 : 300221 : gcc_checking_assert (DECL_ORIGINAL_TYPE (inner)
7875 : : || TYPE_NAME (type) == inner);
7876 : 300221 : int tag = insert (type);
7877 : 300221 : dump (dumper::TREE)
7878 : 360 : && dump ("Indirect:%d decl's type %C:%N", tag, TREE_CODE (type), type);
7879 : 300221 : count++;
7880 : : }
7881 : :
7882 : 774670 : dump (dumper::TREE) && dump ("Inserted %u indirects", count);
7883 : 774070 : return count == u ();
7884 : : }
7885 : :
7886 : : /* Stream a template parameter. There are 4.5 kinds of parameter:
7887 : : a) Template - TEMPLATE_DECL->TYPE_DECL->TEMPLATE_TEMPLATE_PARM
7888 : : TEMPLATE_TYPE_PARM_INDEX TPI
7889 : : b) Type - TYPE_DECL->TEMPLATE_TYPE_PARM TEMPLATE_TYPE_PARM_INDEX TPI
7890 : : c.1) NonTYPE - PARM_DECL DECL_INITIAL TPI We meet this first
7891 : : c.2) NonTYPE - CONST_DECL DECL_INITIAL Same TPI
7892 : : d) BoundTemplate - TYPE_DECL->BOUND_TEMPLATE_TEMPLATE_PARM
7893 : : TEMPLATE_TYPE_PARM_INDEX->TPI
7894 : : TEMPLATE_TEMPLATE_PARM_INFO->TEMPLATE_INFO
7895 : :
7896 : : All of these point to a TEMPLATE_PARM_INDEX, and #B also has a TEMPLATE_INFO
7897 : : */
7898 : :
7899 : : void
7900 : 1623216 : trees_out::tpl_parm_value (tree parm)
7901 : : {
7902 : 1623216 : gcc_checking_assert (DECL_P (parm) && DECL_TEMPLATE_PARM_P (parm));
7903 : :
7904 : 1623216 : int parm_tag = insert (parm);
7905 : 1623216 : if (streaming_p ())
7906 : : {
7907 : 368323 : i (tt_tpl_parm);
7908 : 368323 : dump (dumper::TREE) && dump ("Writing template parm:%d %C:%N",
7909 : 114 : parm_tag, TREE_CODE (parm), parm);
7910 : 368323 : start (parm);
7911 : 368323 : tree_node_bools (parm);
7912 : : }
7913 : :
7914 : 1623216 : tree inner = parm;
7915 : 1623216 : if (TREE_CODE (inner) == TEMPLATE_DECL)
7916 : : {
7917 : 4786 : inner = DECL_TEMPLATE_RESULT (inner);
7918 : 4786 : int inner_tag = insert (inner);
7919 : 4786 : if (streaming_p ())
7920 : : {
7921 : 1234 : dump (dumper::TREE) && dump ("Writing inner template parm:%d %C:%N",
7922 : 0 : inner_tag, TREE_CODE (inner), inner);
7923 : 1234 : start (inner);
7924 : 1234 : tree_node_bools (inner);
7925 : : }
7926 : : }
7927 : :
7928 : 1623216 : tree type = NULL_TREE;
7929 : 1623216 : if (TREE_CODE (inner) == TYPE_DECL)
7930 : : {
7931 : 1463839 : type = TREE_TYPE (inner);
7932 : 1463839 : int type_tag = insert (type);
7933 : 1463839 : if (streaming_p ())
7934 : : {
7935 : 332584 : dump (dumper::TREE) && dump ("Writing template parm type:%d %C:%N",
7936 : 108 : type_tag, TREE_CODE (type), type);
7937 : 332584 : start (type);
7938 : 332584 : tree_node_bools (type);
7939 : : }
7940 : : }
7941 : :
7942 : 1623216 : if (inner != parm)
7943 : : {
7944 : : /* This is a template-template parameter. */
7945 : 4786 : unsigned tpl_levels = 0;
7946 : 4786 : tpl_header (parm, &tpl_levels);
7947 : 4786 : tpl_parms_fini (parm, tpl_levels);
7948 : : }
7949 : :
7950 : 1623216 : tree_node_vals (parm);
7951 : 1623216 : if (inner != parm)
7952 : 4786 : tree_node_vals (inner);
7953 : 1623216 : if (type)
7954 : : {
7955 : 1463839 : tree_node_vals (type);
7956 : 1463839 : if (DECL_NAME (inner) == auto_identifier
7957 : 1463839 : || DECL_NAME (inner) == decltype_auto_identifier)
7958 : : {
7959 : : /* Placeholder auto. */
7960 : 57654 : tree_node (DECL_INITIAL (inner));
7961 : 57654 : tree_node (DECL_SIZE_UNIT (inner));
7962 : : }
7963 : : }
7964 : :
7965 : 1623216 : if (streaming_p ())
7966 : 368323 : dump (dumper::TREE) && dump ("Wrote template parm:%d %C:%N",
7967 : 114 : parm_tag, TREE_CODE (parm), parm);
7968 : 1623216 : }
7969 : :
7970 : : tree
7971 : 295431 : trees_in::tpl_parm_value ()
7972 : : {
7973 : 295431 : tree parm = start ();
7974 : 295431 : if (!parm || !tree_node_bools (parm))
7975 : 0 : return NULL_TREE;
7976 : :
7977 : 295431 : int parm_tag = insert (parm);
7978 : 295431 : dump (dumper::TREE) && dump ("Reading template parm:%d %C:%N",
7979 : 198 : parm_tag, TREE_CODE (parm), parm);
7980 : :
7981 : 295431 : tree inner = parm;
7982 : 295431 : if (TREE_CODE (inner) == TEMPLATE_DECL)
7983 : : {
7984 : 824 : inner = start ();
7985 : 824 : if (!inner || !tree_node_bools (inner))
7986 : 0 : return NULL_TREE;
7987 : 824 : int inner_tag = insert (inner);
7988 : 824 : dump (dumper::TREE) && dump ("Reading inner template parm:%d %C:%N",
7989 : 0 : inner_tag, TREE_CODE (inner), inner);
7990 : 824 : DECL_TEMPLATE_RESULT (parm) = inner;
7991 : : }
7992 : :
7993 : 295431 : tree type = NULL_TREE;
7994 : 295431 : if (TREE_CODE (inner) == TYPE_DECL)
7995 : : {
7996 : 265784 : type = start ();
7997 : 265784 : if (!type || !tree_node_bools (type))
7998 : 0 : return NULL_TREE;
7999 : 265784 : int type_tag = insert (type);
8000 : 265784 : dump (dumper::TREE) && dump ("Reading template parm type:%d %C:%N",
8001 : 120 : type_tag, TREE_CODE (type), type);
8002 : :
8003 : 265784 : TREE_TYPE (inner) = TREE_TYPE (parm) = type;
8004 : 265784 : TYPE_NAME (type) = parm;
8005 : : }
8006 : :
8007 : 295431 : if (inner != parm)
8008 : : {
8009 : : /* A template template parameter. */
8010 : 824 : unsigned tpl_levels = 0;
8011 : 824 : tpl_header (parm, &tpl_levels);
8012 : 824 : tpl_parms_fini (parm, tpl_levels);
8013 : : }
8014 : :
8015 : 295431 : tree_node_vals (parm);
8016 : 295431 : if (inner != parm)
8017 : 824 : tree_node_vals (inner);
8018 : 295431 : if (type)
8019 : : {
8020 : 265784 : tree_node_vals (type);
8021 : 265784 : if (DECL_NAME (inner) == auto_identifier
8022 : 265784 : || DECL_NAME (inner) == decltype_auto_identifier)
8023 : : {
8024 : : /* Placeholder auto. */
8025 : 21553 : DECL_INITIAL (inner) = tree_node ();
8026 : 21553 : DECL_SIZE_UNIT (inner) = tree_node ();
8027 : : }
8028 : 265784 : if (TYPE_CANONICAL (type))
8029 : : {
8030 : 265784 : gcc_checking_assert (TYPE_CANONICAL (type) == type);
8031 : 265784 : TYPE_CANONICAL (type) = canonical_type_parameter (type);
8032 : : }
8033 : : }
8034 : :
8035 : 295431 : dump (dumper::TREE) && dump ("Read template parm:%d %C:%N",
8036 : 198 : parm_tag, TREE_CODE (parm), parm);
8037 : :
8038 : : return parm;
8039 : : }
8040 : :
8041 : : void
8042 : 1022354 : trees_out::install_entity (tree decl, depset *dep)
8043 : : {
8044 : 1022354 : gcc_checking_assert (streaming_p ());
8045 : :
8046 : : /* Write the entity index, so we can insert it as soon as we
8047 : : know this is new. */
8048 : 1022354 : u (dep ? dep->cluster + 1 : 0);
8049 : 1022354 : if (CHECKING_P && dep)
8050 : : {
8051 : : /* Add it to the entity map, such that we can tell it is
8052 : : part of us. */
8053 : 764373 : bool existed;
8054 : 764373 : unsigned *slot = &entity_map->get_or_insert
8055 : 764373 : (DECL_UID (decl), &existed);
8056 : 764373 : if (existed)
8057 : : /* If it existed, it should match. */
8058 : 813 : gcc_checking_assert (decl == (*entity_ary)[*slot]);
8059 : 764373 : *slot = ~dep->cluster;
8060 : : }
8061 : 1022354 : }
8062 : :
8063 : : bool
8064 : 843825 : trees_in::install_entity (tree decl)
8065 : : {
8066 : 843825 : unsigned entity_index = u ();
8067 : 843825 : if (!entity_index)
8068 : : return false;
8069 : :
8070 : 620584 : if (entity_index > state->entity_num)
8071 : : {
8072 : 0 : set_overrun ();
8073 : 0 : return false;
8074 : : }
8075 : :
8076 : : /* Insert the real decl into the entity ary. */
8077 : 620584 : unsigned ident = state->entity_lwm + entity_index - 1;
8078 : 620584 : (*entity_ary)[ident] = decl;
8079 : :
8080 : : /* And into the entity map, if it's not already there. */
8081 : 620584 : tree not_tmpl = STRIP_TEMPLATE (decl);
8082 : 620584 : if (!DECL_LANG_SPECIFIC (not_tmpl)
8083 : 1157488 : || !DECL_MODULE_ENTITY_P (not_tmpl))
8084 : : {
8085 : 619967 : retrofit_lang_decl (not_tmpl);
8086 : 619967 : DECL_MODULE_ENTITY_P (not_tmpl) = true;
8087 : :
8088 : : /* Insert into the entity hash (it cannot already be there). */
8089 : 619967 : bool existed;
8090 : 619967 : unsigned &slot = entity_map->get_or_insert (DECL_UID (decl), &existed);
8091 : 619967 : gcc_checking_assert (!existed);
8092 : 619967 : slot = ident;
8093 : : }
8094 : : else
8095 : : {
8096 : 617 : unsigned *slot = entity_map->get (DECL_UID (decl));
8097 : :
8098 : : /* The entity must be in the entity map already. However, DECL may
8099 : : be the DECL_TEMPLATE_RESULT of an existing partial specialisation
8100 : : if we matched it while streaming another instantiation; in this
8101 : : case we already registered that TEMPLATE_DECL. */
8102 : 617 : if (!slot)
8103 : : {
8104 : 9 : tree type = TREE_TYPE (decl);
8105 : 9 : gcc_checking_assert (TREE_CODE (decl) == TYPE_DECL
8106 : : && CLASS_TYPE_P (type)
8107 : : && CLASSTYPE_TEMPLATE_SPECIALIZATION (type));
8108 : 9 : slot = entity_map->get (DECL_UID (CLASSTYPE_TI_TEMPLATE (type)));
8109 : : }
8110 : 9 : gcc_checking_assert (slot);
8111 : :
8112 : 617 : if (state->is_partition ())
8113 : : {
8114 : : /* The decl is already in the entity map, but we see it again now
8115 : : from a partition: we want to overwrite if the original decl
8116 : : wasn't also from a (possibly different) partition. Otherwise,
8117 : : for things like template instantiations, make_dependency might
8118 : : not realise that this is also provided from a partition and
8119 : : should be considered part of this module (and thus always
8120 : : emitted into the primary interface's CMI). */
8121 : 150 : module_state *imp = import_entity_module (*slot);
8122 : 150 : if (!imp->is_partition ())
8123 : 24 : *slot = ident;
8124 : : }
8125 : : }
8126 : :
8127 : : return true;
8128 : : }
8129 : :
8130 : : static bool has_definition (tree decl);
8131 : :
8132 : : /* DECL is a decl node that must be written by value. DEP is the
8133 : : decl's depset. */
8134 : :
8135 : : void
8136 : 2820302 : trees_out::decl_value (tree decl, depset *dep)
8137 : : {
8138 : : /* We should not be writing clones or template parms. */
8139 : 2820302 : gcc_checking_assert (DECL_P (decl)
8140 : : && !DECL_CLONED_FUNCTION_P (decl)
8141 : : && !DECL_TEMPLATE_PARM_P (decl));
8142 : :
8143 : : /* We should never be writing non-typedef ptrmemfuncs by value. */
8144 : 2820302 : gcc_checking_assert (TREE_CODE (decl) != TYPE_DECL
8145 : : || DECL_ORIGINAL_TYPE (decl)
8146 : : || !TYPE_PTRMEMFUNC_P (TREE_TYPE (decl)));
8147 : :
8148 : : /* There's no need to walk any of the contents of a known TU-local entity,
8149 : : since importers should never see any of it regardless. But make sure we
8150 : : at least note its location so importers can use it for diagnostics. */
8151 : 5114247 : if (dep && dep->is_tu_local ())
8152 : : {
8153 : 437 : gcc_checking_assert (is_initial_scan ());
8154 : 437 : insert (decl, WK_value);
8155 : 437 : state->note_location (DECL_SOURCE_LOCATION (decl));
8156 : 437 : return;
8157 : : }
8158 : :
8159 : 2819865 : merge_kind mk = get_merge_kind (decl, dep);
8160 : :
8161 : 2819865 : if (CHECKING_P)
8162 : : {
8163 : : /* Never start in the middle of a template. */
8164 : 2819865 : int use_tpl = -1;
8165 : 2819865 : if (tree ti = node_template_info (decl, use_tpl))
8166 : 981959 : gcc_checking_assert (TREE_CODE (TI_TEMPLATE (ti)) == OVERLOAD
8167 : : || TREE_CODE (TI_TEMPLATE (ti)) == FIELD_DECL
8168 : : || (DECL_TEMPLATE_RESULT (TI_TEMPLATE (ti))
8169 : : != decl));
8170 : : }
8171 : :
8172 : 2819865 : if (streaming_p ())
8173 : : {
8174 : : /* A new node -> tt_decl. */
8175 : 1022354 : decl_val_count++;
8176 : 1022354 : i (tt_decl);
8177 : 1022354 : u (mk);
8178 : 1022354 : start (decl);
8179 : :
8180 : 1022354 : if (mk != MK_unique)
8181 : : {
8182 : 874119 : bits_out bits = stream_bits ();
8183 : 874119 : if (!(mk & MK_template_mask) && !state->is_header ())
8184 : : {
8185 : : /* Tell the importer whether this is a global module entity,
8186 : : or a module entity. */
8187 : 103748 : tree o = get_originating_module_decl (decl);
8188 : 103748 : bool is_attached = false;
8189 : :
8190 : 103748 : tree not_tmpl = STRIP_TEMPLATE (o);
8191 : 103748 : if (DECL_LANG_SPECIFIC (not_tmpl)
8192 : 162808 : && DECL_MODULE_ATTACH_P (not_tmpl))
8193 : : is_attached = true;
8194 : :
8195 : 103748 : bits.b (is_attached);
8196 : : }
8197 : 874119 : bits.b (dep && dep->has_defn ());
8198 : 874119 : }
8199 : 1022354 : tree_node_bools (decl);
8200 : : }
8201 : :
8202 : 2819865 : int tag = insert (decl, WK_value);
8203 : 2819865 : if (streaming_p ())
8204 : 1022354 : dump (dumper::TREE)
8205 : 642 : && dump ("Writing %s:%d %C:%N%S", merge_kind_name[mk], tag,
8206 : 642 : TREE_CODE (decl), decl, decl);
8207 : :
8208 : 2819865 : tree inner = decl;
8209 : 2819865 : int inner_tag = 0;
8210 : 2819865 : if (TREE_CODE (decl) == TEMPLATE_DECL)
8211 : : {
8212 : 838003 : inner = DECL_TEMPLATE_RESULT (decl);
8213 : 838003 : inner_tag = insert (inner, WK_value);
8214 : :
8215 : 838003 : if (streaming_p ())
8216 : : {
8217 : 279318 : int code = TREE_CODE (inner);
8218 : 279318 : u (code);
8219 : 279318 : start (inner, true);
8220 : 279318 : tree_node_bools (inner);
8221 : 279318 : dump (dumper::TREE)
8222 : 132 : && dump ("Writing %s:%d %C:%N%S", merge_kind_name[mk], inner_tag,
8223 : 132 : TREE_CODE (inner), inner, inner);
8224 : : }
8225 : : }
8226 : :
8227 : 2819865 : tree type = NULL_TREE;
8228 : 2819865 : int type_tag = 0;
8229 : 2819865 : tree stub_decl = NULL_TREE;
8230 : 2819865 : int stub_tag = 0;
8231 : 2819865 : if (TREE_CODE (inner) == TYPE_DECL)
8232 : : {
8233 : 1098063 : type = TREE_TYPE (inner);
8234 : 1098063 : bool has_type = (type == TYPE_MAIN_VARIANT (type)
8235 : 1098063 : && TYPE_NAME (type) == inner);
8236 : :
8237 : 1098063 : if (streaming_p ())
8238 : 369945 : u (has_type ? TREE_CODE (type) : 0);
8239 : :
8240 : 1098063 : if (has_type)
8241 : : {
8242 : 502639 : type_tag = insert (type, WK_value);
8243 : 502639 : if (streaming_p ())
8244 : : {
8245 : 167526 : start (type, true);
8246 : 167526 : tree_node_bools (type);
8247 : 167526 : dump (dumper::TREE)
8248 : 153 : && dump ("Writing type:%d %C:%N", type_tag,
8249 : 153 : TREE_CODE (type), type);
8250 : : }
8251 : :
8252 : 502639 : stub_decl = TYPE_STUB_DECL (type);
8253 : 502639 : bool has_stub = inner != stub_decl;
8254 : 502639 : if (streaming_p ())
8255 : 167526 : u (has_stub ? TREE_CODE (stub_decl) : 0);
8256 : 502639 : if (has_stub)
8257 : : {
8258 : 2169 : stub_tag = insert (stub_decl);
8259 : 2169 : if (streaming_p ())
8260 : : {
8261 : 722 : start (stub_decl, true);
8262 : 722 : tree_node_bools (stub_decl);
8263 : 722 : dump (dumper::TREE)
8264 : 0 : && dump ("Writing stub_decl:%d %C:%N", stub_tag,
8265 : 0 : TREE_CODE (stub_decl), stub_decl);
8266 : : }
8267 : : }
8268 : : else
8269 : : stub_decl = NULL_TREE;
8270 : : }
8271 : : else
8272 : : /* Regular typedef. */
8273 : : type = NULL_TREE;
8274 : : }
8275 : :
8276 : : /* Stream the container, we want it correctly canonicalized before
8277 : : we start emitting keys for this decl. */
8278 : 2819865 : tree container = decl_container (decl);
8279 : 2819865 : unsigned tpl_levels = 0;
8280 : :
8281 : : /* Also tell the importer whether this is a temploid friend attached
8282 : : to a different module (which has implications for merging), so that
8283 : : importers can reconstruct this information on stream-in. */
8284 : 2819865 : if (TREE_CODE (inner) == FUNCTION_DECL || TREE_CODE (inner) == TYPE_DECL)
8285 : : {
8286 : 2207448 : tree* temploid_friend_slot = imported_temploid_friends->get (decl);
8287 : 2207448 : gcc_checking_assert (!temploid_friend_slot || *temploid_friend_slot);
8288 : 2207448 : tree_node (temploid_friend_slot ? *temploid_friend_slot : NULL_TREE);
8289 : : }
8290 : :
8291 : 2819865 : {
8292 : 2819865 : auto wmk = make_temp_override (dep_hash->writing_merge_key, true);
8293 : 2819865 : if (decl != inner)
8294 : 838003 : tpl_header (decl, &tpl_levels);
8295 : 2819865 : if (TREE_CODE (inner) == FUNCTION_DECL)
8296 : 1109385 : fn_parms_init (inner);
8297 : :
8298 : : /* Now write out the merging information, and then really
8299 : : install the tag values. */
8300 : 2819865 : key_mergeable (tag, mk, decl, inner, container, dep);
8301 : :
8302 : 2819865 : if (streaming_p ())
8303 : 2822391 : dump (dumper::MERGE)
8304 : 798 : && dump ("Wrote:%d's %s merge key %C:%N", tag,
8305 : 798 : merge_kind_name[mk], TREE_CODE (decl), decl);
8306 : 2819865 : }
8307 : :
8308 : 2819865 : if (TREE_CODE (inner) == FUNCTION_DECL)
8309 : 2819865 : fn_parms_fini (inner);
8310 : :
8311 : 2819865 : if (!is_key_order ())
8312 : 2054340 : tree_node_vals (decl);
8313 : :
8314 : 2819865 : if (inner_tag)
8315 : : {
8316 : 838003 : if (!is_key_order ())
8317 : 558685 : tree_node_vals (inner);
8318 : 838003 : tpl_parms_fini (decl, tpl_levels);
8319 : : }
8320 : :
8321 : 2819865 : if (type && !is_key_order ())
8322 : : {
8323 : 335113 : tree_node_vals (type);
8324 : 335113 : if (stub_decl)
8325 : 1447 : tree_node_vals (stub_decl);
8326 : : }
8327 : :
8328 : 2819865 : if (!is_key_order ())
8329 : : {
8330 : 2054340 : if (mk & MK_template_mask
8331 : 1432736 : || mk == MK_partial
8332 : 1432736 : || mk == MK_friend_spec)
8333 : : {
8334 : 30206 : if (mk != MK_partial)
8335 : : {
8336 : : // FIXME: We should make use of the merge-key by
8337 : : // exposing it outside of key_mergeable. But this gets
8338 : : // the job done.
8339 : 621604 : auto *entry = reinterpret_cast <spec_entry *> (dep->deps[0]);
8340 : :
8341 : 621604 : if (streaming_p ())
8342 : 310802 : u (get_mergeable_specialization_flags (mk & MK_tmpl_decl_mask,
8343 : : entry->tmpl, decl));
8344 : 621604 : tree_node (entry->tmpl);
8345 : 621604 : tree_node (entry->args);
8346 : : }
8347 : : else
8348 : : {
8349 : 30206 : tree ti = get_template_info (inner);
8350 : 30206 : tree_node (TI_TEMPLATE (ti));
8351 : 30206 : tree_node (TI_ARGS (ti));
8352 : : }
8353 : : }
8354 : 2054340 : tree_node (get_constraints (decl));
8355 : : }
8356 : :
8357 : 2819865 : if (streaming_p ())
8358 : : {
8359 : : /* Do not stray outside this section. */
8360 : 1022354 : gcc_checking_assert (!dep || dep->section == dep_hash->section);
8361 : :
8362 : : /* Write the entity index, so we can insert it as soon as we
8363 : : know this is new. */
8364 : 1022354 : install_entity (decl, dep);
8365 : : }
8366 : :
8367 : 2819865 : if (DECL_LANG_SPECIFIC (inner)
8368 : 2544977 : && DECL_MODULE_KEYED_DECLS_P (inner)
8369 : 2820370 : && !is_key_order ())
8370 : : {
8371 : : /* Stream the keyed entities. */
8372 : 340 : auto *attach_vec = keyed_table->get (inner);
8373 : 340 : unsigned num = attach_vec->length ();
8374 : 340 : if (streaming_p ())
8375 : 165 : u (num);
8376 : 692 : for (unsigned ix = 0; ix != num; ix++)
8377 : : {
8378 : 352 : tree attached = (*attach_vec)[ix];
8379 : 352 : tree_node (attached);
8380 : 352 : if (streaming_p ())
8381 : 367 : dump (dumper::MERGE)
8382 : 15 : && dump ("Written %d[%u] attached decl %N", tag, ix, attached);
8383 : : }
8384 : : }
8385 : :
8386 : 2819865 : bool is_typedef = false;
8387 : 2819865 : if (!type && TREE_CODE (inner) == TYPE_DECL)
8388 : : {
8389 : 595424 : tree t = TREE_TYPE (inner);
8390 : 595424 : unsigned tdef_flags = 0;
8391 : 595424 : if (DECL_ORIGINAL_TYPE (inner)
8392 : 595424 : && TYPE_NAME (TREE_TYPE (inner)) == inner)
8393 : : {
8394 : 595424 : tdef_flags |= 1;
8395 : 595424 : if (TYPE_STRUCTURAL_EQUALITY_P (t)
8396 : 128493 : && TYPE_DEPENDENT_P_VALID (t)
8397 : 717110 : && TYPE_DEPENDENT_P (t))
8398 : : tdef_flags |= 2;
8399 : : }
8400 : 595424 : if (streaming_p ())
8401 : 202419 : u (tdef_flags);
8402 : :
8403 : 595424 : if (tdef_flags & 1)
8404 : : {
8405 : : /* A typedef type. */
8406 : 595424 : int type_tag = insert (t);
8407 : 595424 : if (streaming_p ())
8408 : 202419 : dump (dumper::TREE)
8409 : 198 : && dump ("Cloned:%d %s %C:%N", type_tag,
8410 : : tdef_flags & 2 ? "depalias" : "typedef",
8411 : 198 : TREE_CODE (t), t);
8412 : :
8413 : : is_typedef = true;
8414 : : }
8415 : : }
8416 : :
8417 : 2819865 : if (streaming_p () && DECL_MAYBE_IN_CHARGE_CDTOR_P (decl))
8418 : : {
8419 : 79736 : bool cloned_p
8420 : 79736 : = (DECL_CHAIN (decl) && DECL_CLONED_FUNCTION_P (DECL_CHAIN (decl)));
8421 : 55766 : bool needs_vtt_parm_p
8422 : 55766 : = (cloned_p && CLASSTYPE_VBASECLASSES (DECL_CONTEXT (decl)));
8423 : 55766 : bool omit_inherited_parms_p
8424 : 55766 : = (cloned_p && DECL_MAYBE_IN_CHARGE_CONSTRUCTOR_P (decl)
8425 : 44088 : && base_ctor_omit_inherited_parms (decl));
8426 : 79736 : unsigned flags = (int (cloned_p) << 0
8427 : 79736 : | int (needs_vtt_parm_p) << 1
8428 : 79736 : | int (omit_inherited_parms_p) << 2);
8429 : 79736 : u (flags);
8430 : 79811 : dump (dumper::TREE) && dump ("CDTOR %N is %scloned",
8431 : : decl, cloned_p ? "" : "not ");
8432 : : }
8433 : :
8434 : 2819865 : if (streaming_p () && VAR_P (decl) && CP_DECL_THREAD_LOCAL_P (decl))
8435 : 172 : u (decl_tls_model (decl));
8436 : :
8437 : 2819865 : if (streaming_p ())
8438 : 1022354 : dump (dumper::TREE) && dump ("Written decl:%d %C:%N", tag,
8439 : 642 : TREE_CODE (decl), decl);
8440 : :
8441 : 2819865 : if (NAMESPACE_SCOPE_P (inner))
8442 : 1728478 : gcc_checking_assert (!dep == (VAR_OR_FUNCTION_DECL_P (inner)
8443 : : && DECL_LOCAL_DECL_P (inner)));
8444 : 1955513 : else if ((TREE_CODE (inner) == TYPE_DECL
8445 : 584785 : && !is_typedef
8446 : 110894 : && TYPE_NAME (TREE_TYPE (inner)) == inner)
8447 : 2429404 : || TREE_CODE (inner) == FUNCTION_DECL)
8448 : : {
8449 : 924165 : bool write_defn = !dep && has_definition (decl);
8450 : 924165 : if (streaming_p ())
8451 : 308198 : u (write_defn);
8452 : 924165 : if (write_defn)
8453 : 0 : write_definition (decl);
8454 : : }
8455 : : }
8456 : :
8457 : : tree
8458 : 843825 : trees_in::decl_value ()
8459 : : {
8460 : 843825 : int tag = 0;
8461 : 843825 : bool is_attached = false;
8462 : 843825 : bool has_defn = false;
8463 : 843825 : unsigned mk_u = u ();
8464 : 843825 : if (mk_u >= MK_hwm || !merge_kind_name[mk_u])
8465 : : {
8466 : 0 : set_overrun ();
8467 : 0 : return NULL_TREE;
8468 : : }
8469 : :
8470 : 843825 : unsigned saved_unused = unused;
8471 : 843825 : unused = 0;
8472 : :
8473 : 843825 : merge_kind mk = merge_kind (mk_u);
8474 : :
8475 : 843825 : tree decl = start ();
8476 : 843825 : if (decl)
8477 : : {
8478 : 843825 : if (mk != MK_unique)
8479 : : {
8480 : 709978 : bits_in bits = stream_bits ();
8481 : 709978 : if (!(mk & MK_template_mask) && !state->is_header ())
8482 : 44655 : is_attached = bits.b ();
8483 : :
8484 : 709978 : has_defn = bits.b ();
8485 : 709978 : }
8486 : :
8487 : 843825 : if (!tree_node_bools (decl))
8488 : 0 : decl = NULL_TREE;
8489 : : }
8490 : :
8491 : : /* Insert into map. */
8492 : 843825 : tag = insert (decl);
8493 : 843825 : if (decl)
8494 : 843825 : dump (dumper::TREE)
8495 : 936 : && dump ("Reading:%d %C", tag, TREE_CODE (decl));
8496 : :
8497 : 843825 : tree inner = decl;
8498 : 843825 : int inner_tag = 0;
8499 : 843825 : if (decl && TREE_CODE (decl) == TEMPLATE_DECL)
8500 : : {
8501 : 229030 : int code = u ();
8502 : 229030 : inner = start (code);
8503 : 229030 : if (inner && tree_node_bools (inner))
8504 : 229030 : DECL_TEMPLATE_RESULT (decl) = inner;
8505 : : else
8506 : 0 : decl = NULL_TREE;
8507 : :
8508 : 229030 : inner_tag = insert (inner);
8509 : 229030 : if (decl)
8510 : 229030 : dump (dumper::TREE)
8511 : 204 : && dump ("Reading:%d %C", inner_tag, TREE_CODE (inner));
8512 : : }
8513 : :
8514 : 843825 : tree type = NULL_TREE;
8515 : 843825 : int type_tag = 0;
8516 : 843825 : tree stub_decl = NULL_TREE;
8517 : 843825 : int stub_tag = 0;
8518 : 843825 : if (decl && TREE_CODE (inner) == TYPE_DECL)
8519 : : {
8520 : 282535 : if (unsigned type_code = u ())
8521 : : {
8522 : 127425 : type = start (type_code);
8523 : 127425 : if (type && tree_node_bools (type))
8524 : : {
8525 : 127425 : TREE_TYPE (inner) = type;
8526 : 127425 : TYPE_NAME (type) = inner;
8527 : : }
8528 : : else
8529 : 0 : decl = NULL_TREE;
8530 : :
8531 : 127425 : type_tag = insert (type);
8532 : 127425 : if (decl)
8533 : 127425 : dump (dumper::TREE)
8534 : 210 : && dump ("Reading type:%d %C", type_tag, TREE_CODE (type));
8535 : :
8536 : 127425 : if (unsigned stub_code = u ())
8537 : : {
8538 : 413 : stub_decl = start (stub_code);
8539 : 413 : if (stub_decl && tree_node_bools (stub_decl))
8540 : : {
8541 : 413 : TREE_TYPE (stub_decl) = type;
8542 : 413 : TYPE_STUB_DECL (type) = stub_decl;
8543 : : }
8544 : : else
8545 : 0 : decl = NULL_TREE;
8546 : :
8547 : 413 : stub_tag = insert (stub_decl);
8548 : 413 : if (decl)
8549 : 413 : dump (dumper::TREE)
8550 : 0 : && dump ("Reading stub_decl:%d %C", stub_tag,
8551 : 0 : TREE_CODE (stub_decl));
8552 : : }
8553 : : }
8554 : : }
8555 : :
8556 : 843825 : if (!decl)
8557 : : {
8558 : 0 : bail:
8559 : 0 : if (inner_tag != 0)
8560 : 0 : back_refs[~inner_tag] = NULL_TREE;
8561 : 0 : if (type_tag != 0)
8562 : 0 : back_refs[~type_tag] = NULL_TREE;
8563 : 0 : if (stub_tag != 0)
8564 : 0 : back_refs[~stub_tag] = NULL_TREE;
8565 : 0 : if (tag != 0)
8566 : 0 : back_refs[~tag] = NULL_TREE;
8567 : 0 : set_overrun ();
8568 : : /* Bail. */
8569 : 0 : unused = saved_unused;
8570 : 0 : return NULL_TREE;
8571 : : }
8572 : :
8573 : : /* Read the container, to ensure it's already been streamed in. */
8574 : 843825 : tree container = decl_container ();
8575 : 843825 : unsigned tpl_levels = 0;
8576 : :
8577 : : /* If this is an imported temploid friend, get the owning decl its
8578 : : attachment is determined by (or NULL_TREE otherwise). */
8579 : 843825 : tree temploid_friend = NULL_TREE;
8580 : 843825 : if (TREE_CODE (inner) == FUNCTION_DECL || TREE_CODE (inner) == TYPE_DECL)
8581 : 602430 : temploid_friend = tree_node ();
8582 : :
8583 : : /* Figure out if this decl is already known about. */
8584 : 843825 : int parm_tag = 0;
8585 : :
8586 : 843825 : if (decl != inner)
8587 : 229030 : if (!tpl_header (decl, &tpl_levels))
8588 : 0 : goto bail;
8589 : 843825 : if (TREE_CODE (inner) == FUNCTION_DECL)
8590 : 319895 : parm_tag = fn_parms_init (inner);
8591 : :
8592 : 843825 : tree existing = key_mergeable (tag, mk, decl, inner, type, container,
8593 : : is_attached, temploid_friend);
8594 : 843825 : tree existing_inner = existing;
8595 : 843825 : if (existing)
8596 : : {
8597 : 409816 : if (existing == error_mark_node)
8598 : 0 : goto bail;
8599 : :
8600 : 409816 : if (TREE_CODE (STRIP_TEMPLATE (existing)) == TYPE_DECL)
8601 : : {
8602 : 155402 : tree etype = TREE_TYPE (existing);
8603 : 155402 : if (TYPE_LANG_SPECIFIC (etype)
8604 : 112216 : && COMPLETE_TYPE_P (etype)
8605 : 221324 : && !CLASSTYPE_MEMBER_VEC (etype))
8606 : : /* Give it a member vec, we're likely gonna be looking
8607 : : inside it. */
8608 : 13974 : set_class_bindings (etype, -1);
8609 : : }
8610 : :
8611 : : /* Install the existing decl into the back ref array. */
8612 : 409816 : register_duplicate (decl, existing);
8613 : 409816 : back_refs[~tag] = existing;
8614 : 409816 : if (inner_tag != 0)
8615 : : {
8616 : 139415 : existing_inner = DECL_TEMPLATE_RESULT (existing);
8617 : 139415 : back_refs[~inner_tag] = existing_inner;
8618 : : }
8619 : :
8620 : 409816 : if (type_tag != 0)
8621 : : {
8622 : 73859 : tree existing_type = TREE_TYPE (existing);
8623 : 73859 : back_refs[~type_tag] = existing_type;
8624 : 73859 : if (stub_tag != 0)
8625 : 254 : back_refs[~stub_tag] = TYPE_STUB_DECL (existing_type);
8626 : : }
8627 : : }
8628 : :
8629 : 843825 : if (parm_tag)
8630 : 319895 : fn_parms_fini (parm_tag, inner, existing_inner, has_defn);
8631 : :
8632 : 843825 : if (!tree_node_vals (decl))
8633 : 0 : goto bail;
8634 : :
8635 : 843825 : if (inner_tag)
8636 : : {
8637 : 229030 : gcc_checking_assert (DECL_TEMPLATE_RESULT (decl) == inner);
8638 : :
8639 : 229030 : if (!tree_node_vals (inner))
8640 : 0 : goto bail;
8641 : :
8642 : 229030 : if (!tpl_parms_fini (decl, tpl_levels))
8643 : 0 : goto bail;
8644 : : }
8645 : :
8646 : 843825 : if (type && (!tree_node_vals (type)
8647 : 127425 : || (stub_decl && !tree_node_vals (stub_decl))))
8648 : 0 : goto bail;
8649 : :
8650 : 843825 : spec_entry spec;
8651 : 843825 : unsigned spec_flags = 0;
8652 : 843825 : if (mk & MK_template_mask
8653 : 585078 : || mk == MK_partial
8654 : 585078 : || mk == MK_friend_spec)
8655 : : {
8656 : 9365 : if (mk == MK_partial)
8657 : : spec_flags = 2;
8658 : : else
8659 : 258747 : spec_flags = u ();
8660 : :
8661 : 268112 : spec.tmpl = tree_node ();
8662 : 268112 : spec.args = tree_node ();
8663 : : }
8664 : : /* Hold constraints on the spec field, for a short while. */
8665 : 843825 : spec.spec = tree_node ();
8666 : :
8667 : 844761 : dump (dumper::TREE) && dump ("Read:%d %C:%N", tag, TREE_CODE (decl), decl);
8668 : :
8669 : 843825 : existing = back_refs[~tag];
8670 : 843825 : bool installed = install_entity (existing);
8671 : 843825 : bool is_new = existing == decl;
8672 : :
8673 : 843825 : if (DECL_LANG_SPECIFIC (inner)
8674 : 1588625 : && DECL_MODULE_KEYED_DECLS_P (inner))
8675 : : {
8676 : : /* Read and maybe install the attached entities. */
8677 : 189 : bool existed;
8678 : 189 : auto &set = keyed_table->get_or_insert (STRIP_TEMPLATE (existing),
8679 : : &existed);
8680 : 189 : unsigned num = u ();
8681 : 189 : if (is_new == existed)
8682 : 0 : set_overrun ();
8683 : 189 : if (is_new)
8684 : 63 : set.reserve (num);
8685 : 390 : for (unsigned ix = 0; !get_overrun () && ix != num; ix++)
8686 : : {
8687 : 201 : tree attached = tree_node ();
8688 : 201 : dump (dumper::MERGE)
8689 : 60 : && dump ("Read %d[%u] %s attached decl %N", tag, ix,
8690 : : is_new ? "new" : "matched", attached);
8691 : 201 : if (is_new)
8692 : 69 : set.quick_push (attached);
8693 : 132 : else if (set[ix] != attached)
8694 : 0 : set_overrun ();
8695 : : }
8696 : : }
8697 : :
8698 : : /* Regular typedefs will have a NULL TREE_TYPE at this point. */
8699 : 843825 : unsigned tdef_flags = 0;
8700 : 843825 : bool is_typedef = false;
8701 : 843825 : if (!type && TREE_CODE (inner) == TYPE_DECL)
8702 : : {
8703 : 155110 : tdef_flags = u ();
8704 : 155110 : if (tdef_flags & 1)
8705 : 155110 : is_typedef = true;
8706 : : }
8707 : :
8708 : 843825 : if (is_new)
8709 : : {
8710 : : /* A newly discovered node. */
8711 : 434009 : if (TREE_CODE (decl) == FUNCTION_DECL && DECL_VIRTUAL_P (decl))
8712 : : /* Mark this identifier as naming a virtual function --
8713 : : lookup_overrides relies on this optimization. */
8714 : 4084 : IDENTIFIER_VIRTUAL_P (DECL_NAME (decl)) = true;
8715 : :
8716 : 434009 : if (installed)
8717 : : {
8718 : : /* Mark the entity as imported. */
8719 : 262186 : retrofit_lang_decl (inner);
8720 : 262186 : DECL_MODULE_IMPORT_P (inner) = true;
8721 : : }
8722 : :
8723 : 434009 : if (temploid_friend)
8724 : 22 : imported_temploid_friends->put (decl, temploid_friend);
8725 : :
8726 : 434009 : if (spec.spec)
8727 : 15615 : set_constraints (decl, spec.spec);
8728 : :
8729 : 434009 : if (TREE_CODE (decl) == INTEGER_CST && !TREE_OVERFLOW (decl))
8730 : : {
8731 : 0 : decl = cache_integer_cst (decl, true);
8732 : 0 : back_refs[~tag] = decl;
8733 : : }
8734 : :
8735 : 434009 : if (is_typedef)
8736 : : {
8737 : : /* Frob it to be ready for cloning. */
8738 : 73567 : TREE_TYPE (inner) = DECL_ORIGINAL_TYPE (inner);
8739 : 73567 : DECL_ORIGINAL_TYPE (inner) = NULL_TREE;
8740 : 73567 : if (TREE_CODE (TREE_TYPE (inner)) != TU_LOCAL_ENTITY)
8741 : : {
8742 : 73564 : set_underlying_type (inner);
8743 : 73564 : if (tdef_flags & 2)
8744 : : {
8745 : : /* Match instantiate_alias_template's handling. */
8746 : 17851 : tree type = TREE_TYPE (inner);
8747 : 17851 : TYPE_DEPENDENT_P (type) = true;
8748 : 17851 : TYPE_DEPENDENT_P_VALID (type) = true;
8749 : 17851 : SET_TYPE_STRUCTURAL_EQUALITY (type);
8750 : : }
8751 : : }
8752 : : }
8753 : :
8754 : 434009 : if (inner_tag)
8755 : : /* Set the TEMPLATE_DECL's type. */
8756 : 89615 : TREE_TYPE (decl) = TREE_TYPE (inner);
8757 : :
8758 : : /* Redetermine whether we need to import or export this declaration
8759 : : for this TU. But for extern templates we know we must import:
8760 : : they'll be defined in a different TU.
8761 : : FIXME: How do dllexport and dllimport interact across a module?
8762 : : See also https://github.com/itanium-cxx-abi/cxx-abi/issues/170.
8763 : : May have to revisit? */
8764 : 434009 : if (type
8765 : 53566 : && CLASS_TYPE_P (type)
8766 : 45287 : && TYPE_LANG_SPECIFIC (type)
8767 : 479296 : && !(CLASSTYPE_EXPLICIT_INSTANTIATION (type)
8768 : 523 : && CLASSTYPE_INTERFACE_KNOWN (type)
8769 : 523 : && CLASSTYPE_INTERFACE_ONLY (type)))
8770 : : {
8771 : 44813 : CLASSTYPE_INTERFACE_ONLY (type) = false;
8772 : 44813 : CLASSTYPE_INTERFACE_UNKNOWN (type) = true;
8773 : : }
8774 : :
8775 : : /* Add to specialization tables now that constraints etc are
8776 : : added. */
8777 : 434009 : if (mk == MK_partial)
8778 : : {
8779 : 3340 : bool is_type = TREE_CODE (inner) == TYPE_DECL;
8780 : 3340 : spec.spec = is_type ? type : inner;
8781 : 3340 : add_mergeable_specialization (!is_type, &spec, decl, spec_flags);
8782 : : }
8783 : 430669 : else if (mk & MK_template_mask)
8784 : : {
8785 : 116131 : bool is_type = !(mk & MK_tmpl_decl_mask);
8786 : 116131 : spec.spec = is_type ? type : mk & MK_tmpl_tmpl_mask ? inner : decl;
8787 : 116131 : add_mergeable_specialization (!is_type, &spec, decl, spec_flags);
8788 : : }
8789 : :
8790 : 434009 : if (NAMESPACE_SCOPE_P (decl)
8791 : 91848 : && (mk == MK_named || mk == MK_unique
8792 : 91848 : || mk == MK_enum || mk == MK_friend_spec)
8793 : 478535 : && !(VAR_OR_FUNCTION_DECL_P (decl) && DECL_LOCAL_DECL_P (decl)))
8794 : 44456 : add_module_namespace_decl (CP_DECL_CONTEXT (decl), decl);
8795 : :
8796 : 434009 : if (DECL_ARTIFICIAL (decl)
8797 : 123718 : && TREE_CODE (decl) == FUNCTION_DECL
8798 : 12314 : && !DECL_TEMPLATE_INFO (decl)
8799 : 12070 : && DECL_CONTEXT (decl) && TYPE_P (DECL_CONTEXT (decl))
8800 : 11933 : && TYPE_SIZE (DECL_CONTEXT (decl))
8801 : 435289 : && !DECL_THUNK_P (decl))
8802 : : /* A new implicit member function, when the class is
8803 : : complete. This means the importee declared it, and
8804 : : we must now add it to the class. Note that implicit
8805 : : member fns of template instantiations do not themselves
8806 : : look like templates. */
8807 : 790 : if (!install_implicit_member (inner))
8808 : 0 : set_overrun ();
8809 : :
8810 : : /* When importing a TLS wrapper from a header unit, we haven't
8811 : : actually emitted its definition yet. Remember it so we can
8812 : : do this later. */
8813 : 434009 : if (state->is_header ()
8814 : 434009 : && decl_tls_wrapper_p (decl))
8815 : 6 : note_vague_linkage_fn (decl);
8816 : :
8817 : : /* Setup aliases for the declaration. */
8818 : 434009 : if (tree alias = lookup_attribute ("alias", DECL_ATTRIBUTES (decl)))
8819 : : {
8820 : 3 : alias = TREE_VALUE (TREE_VALUE (alias));
8821 : 3 : alias = get_identifier (TREE_STRING_POINTER (alias));
8822 : 3 : assemble_alias (decl, alias);
8823 : : }
8824 : : }
8825 : : else
8826 : : {
8827 : : /* DECL is the to-be-discarded decl. Its internal pointers will
8828 : : be to the EXISTING's structure. Frob it to point to its
8829 : : own other structures, so loading its definition will alter
8830 : : it, and not the existing decl. */
8831 : 411251 : dump (dumper::MERGE) && dump ("Deduping %N", existing);
8832 : :
8833 : 409816 : if (inner_tag)
8834 : 139415 : DECL_TEMPLATE_RESULT (decl) = inner;
8835 : :
8836 : 409816 : if (type)
8837 : : {
8838 : : /* Point at the to-be-discarded type & decl. */
8839 : 73859 : TYPE_NAME (type) = inner;
8840 : 73859 : TREE_TYPE (inner) = type;
8841 : :
8842 : 147464 : TYPE_STUB_DECL (type) = stub_decl ? stub_decl : inner;
8843 : 73859 : if (stub_decl)
8844 : 254 : TREE_TYPE (stub_decl) = type;
8845 : :
8846 : 73859 : tree etype = TREE_TYPE (existing);
8847 : :
8848 : : /* Handle separate declarations with different attributes. */
8849 : 73859 : tree &dattr = TYPE_ATTRIBUTES (type);
8850 : 73859 : tree &eattr = TYPE_ATTRIBUTES (etype);
8851 : 73859 : check_abi_tags (existing, decl, eattr, dattr);
8852 : : // TODO: handle other conflicting type attributes
8853 : 73859 : eattr = merge_attributes (eattr, dattr);
8854 : :
8855 : : /* When merging a partial specialisation, the existing decl may have
8856 : : had its TYPE_CANONICAL adjusted. If so we should use structural
8857 : : equality to ensure is_matching_decl doesn't get confused. */
8858 : 73859 : if ((spec_flags & 2)
8859 : 73859 : && TYPE_CANONICAL (type) != TYPE_CANONICAL (etype))
8860 : 3 : SET_TYPE_STRUCTURAL_EQUALITY (type);
8861 : : }
8862 : :
8863 : 409816 : if (inner_tag)
8864 : : /* Set the TEMPLATE_DECL's type. */
8865 : 139415 : TREE_TYPE (decl) = TREE_TYPE (inner);
8866 : :
8867 : 409816 : if (!is_matching_decl (existing, decl, is_typedef))
8868 : 36 : unmatched_duplicate (existing);
8869 : :
8870 : 409816 : if (TREE_CODE (inner) == FUNCTION_DECL)
8871 : : {
8872 : 185660 : tree e_inner = STRIP_TEMPLATE (existing);
8873 : 185660 : for (auto parm = DECL_ARGUMENTS (inner);
8874 : 560404 : parm; parm = DECL_CHAIN (parm))
8875 : 374744 : DECL_CONTEXT (parm) = e_inner;
8876 : : }
8877 : :
8878 : : /* And our result is the existing node. */
8879 : 409816 : decl = existing;
8880 : : }
8881 : :
8882 : 843825 : if (mk == MK_friend_spec)
8883 : : {
8884 : 0 : tree e = match_mergeable_specialization (true, &spec);
8885 : 0 : if (!e)
8886 : : {
8887 : 0 : spec.spec = inner;
8888 : 0 : add_mergeable_specialization (true, &spec, decl, spec_flags);
8889 : : }
8890 : 0 : else if (e != existing)
8891 : 0 : set_overrun ();
8892 : : }
8893 : :
8894 : 843825 : if (is_typedef)
8895 : : {
8896 : : /* Insert the type into the array now. */
8897 : 155110 : tag = insert (TREE_TYPE (decl));
8898 : 155110 : dump (dumper::TREE)
8899 : 243 : && dump ("Cloned:%d typedef %C:%N",
8900 : 243 : tag, TREE_CODE (TREE_TYPE (decl)), TREE_TYPE (decl));
8901 : : }
8902 : :
8903 : 843825 : unused = saved_unused;
8904 : :
8905 : 843825 : if (DECL_MAYBE_IN_CHARGE_CDTOR_P (decl))
8906 : : {
8907 : 69784 : unsigned flags = u ();
8908 : :
8909 : 69784 : if (is_new)
8910 : : {
8911 : 30804 : bool cloned_p = flags & 1;
8912 : 30900 : dump (dumper::TREE) && dump ("CDTOR %N is %scloned",
8913 : : decl, cloned_p ? "" : "not ");
8914 : 30804 : if (cloned_p)
8915 : 45448 : build_cdtor_clones (decl, flags & 2, flags & 4,
8916 : : /* Update the member vec, if there is
8917 : : one (we're in a different cluster
8918 : : to the class defn). */
8919 : 22724 : CLASSTYPE_MEMBER_VEC (DECL_CONTEXT (decl)));
8920 : : }
8921 : : }
8922 : :
8923 : 843825 : if (VAR_P (decl) && CP_DECL_THREAD_LOCAL_P (decl))
8924 : : {
8925 : 162 : enum tls_model model = tls_model (u ());
8926 : 162 : if (is_new)
8927 : 142 : set_decl_tls_model (decl, model);
8928 : : }
8929 : :
8930 : 843825 : if (!NAMESPACE_SCOPE_P (inner)
8931 : 616342 : && ((TREE_CODE (inner) == TYPE_DECL
8932 : 151338 : && !is_typedef
8933 : 27016 : && TYPE_NAME (TREE_TYPE (inner)) == inner)
8934 : 589326 : || TREE_CODE (inner) == FUNCTION_DECL)
8935 : 1108070 : && u ())
8936 : 0 : read_definition (decl);
8937 : :
8938 : : return decl;
8939 : : }
8940 : :
8941 : : /* DECL is an unnameable member of CTX. Return a suitable identifying
8942 : : index. */
8943 : :
8944 : : static unsigned
8945 : 854 : get_field_ident (tree ctx, tree decl)
8946 : : {
8947 : 854 : gcc_checking_assert (TREE_CODE (decl) == USING_DECL
8948 : : || !DECL_NAME (decl)
8949 : : || IDENTIFIER_ANON_P (DECL_NAME (decl)));
8950 : :
8951 : 854 : unsigned ix = 0;
8952 : 854 : for (tree fields = TYPE_FIELDS (ctx);
8953 : 7407 : fields; fields = DECL_CHAIN (fields))
8954 : : {
8955 : 7407 : if (fields == decl)
8956 : 854 : return ix;
8957 : :
8958 : 6553 : if (DECL_CONTEXT (fields) == ctx
8959 : 6553 : && (TREE_CODE (fields) == USING_DECL
8960 : 6542 : || (TREE_CODE (fields) == FIELD_DECL
8961 : 19 : && (!DECL_NAME (fields)
8962 : 7 : || IDENTIFIER_ANON_P (DECL_NAME (fields))))))
8963 : : /* Count this field. */
8964 : 15 : ix++;
8965 : : }
8966 : 0 : gcc_unreachable ();
8967 : : }
8968 : :
8969 : : static tree
8970 : 731 : lookup_field_ident (tree ctx, unsigned ix)
8971 : : {
8972 : 731 : for (tree fields = TYPE_FIELDS (ctx);
8973 : 6131 : fields; fields = DECL_CHAIN (fields))
8974 : 6131 : if (DECL_CONTEXT (fields) == ctx
8975 : 6131 : && (TREE_CODE (fields) == USING_DECL
8976 : 6119 : || (TREE_CODE (fields) == FIELD_DECL
8977 : 743 : && (!DECL_NAME (fields)
8978 : 3 : || IDENTIFIER_ANON_P (DECL_NAME (fields))))))
8979 : 746 : if (!ix--)
8980 : : return fields;
8981 : :
8982 : : return NULL_TREE;
8983 : : }
8984 : :
8985 : : /* Reference DECL. REF indicates the walk kind we are performing.
8986 : : Return true if we should write this decl by value. */
8987 : :
8988 : : bool
8989 : 9307831 : trees_out::decl_node (tree decl, walk_kind ref)
8990 : : {
8991 : 9307831 : gcc_checking_assert (DECL_P (decl) && !DECL_TEMPLATE_PARM_P (decl)
8992 : : && DECL_CONTEXT (decl));
8993 : :
8994 : 9307831 : if (ref == WK_value)
8995 : : {
8996 : 985928 : depset *dep = dep_hash->find_dependency (decl);
8997 : 985928 : decl_value (decl, dep);
8998 : 985928 : return false;
8999 : : }
9000 : :
9001 : 8321903 : switch (TREE_CODE (decl))
9002 : : {
9003 : : default:
9004 : : break;
9005 : :
9006 : 862988 : case FUNCTION_DECL:
9007 : 862988 : gcc_checking_assert (!DECL_LOCAL_DECL_P (decl));
9008 : : break;
9009 : :
9010 : : case RESULT_DECL:
9011 : : /* Unlike PARM_DECLs, RESULT_DECLs are only generated and
9012 : : referenced when we're inside the function itself. */
9013 : : return true;
9014 : :
9015 : 133095 : case PARM_DECL:
9016 : 133095 : {
9017 : 133095 : if (streaming_p ())
9018 : 59839 : i (tt_parm);
9019 : 133095 : tree_node (DECL_CONTEXT (decl));
9020 : :
9021 : : /* That must have put this in the map. */
9022 : 133095 : walk_kind ref = ref_node (decl);
9023 : 133095 : if (ref != WK_none)
9024 : : // FIXME:OPTIMIZATION We can wander into bits of the
9025 : : // template this was instantiated from, for instance
9026 : : // deferred noexcept and default parms, or references
9027 : : // to parms from earlier forward-decls (PR c++/119608).
9028 : : //
9029 : : // Currently we'll end up cloning those bits of tree.
9030 : : // It would be nice to reference those specific nodes.
9031 : : // I think putting those things in the map when we
9032 : : // reference their template by name.
9033 : : //
9034 : : // See the note in add_indirects.
9035 : : return true;
9036 : :
9037 : 0 : if (streaming_p ())
9038 : 0 : dump (dumper::TREE)
9039 : 0 : && dump ("Wrote %s reference %N",
9040 : 0 : TREE_CODE (decl) == PARM_DECL ? "parameter" : "result",
9041 : : decl);
9042 : : }
9043 : : return false;
9044 : :
9045 : : case IMPORTED_DECL:
9046 : : /* This describes a USING_DECL to the ME's debug machinery. It
9047 : : originates from the fortran FE, and has nothing to do with
9048 : : C++ modules. */
9049 : : return true;
9050 : :
9051 : : case LABEL_DECL:
9052 : : return true;
9053 : :
9054 : 35617 : case CONST_DECL:
9055 : 35617 : {
9056 : : /* If I end up cloning enum decls, implementing C++20 using
9057 : : E::v, this will need tweaking. */
9058 : 35617 : if (streaming_p ())
9059 : 6410 : i (tt_enum_decl);
9060 : 35617 : tree ctx = DECL_CONTEXT (decl);
9061 : 35617 : gcc_checking_assert (TREE_CODE (ctx) == ENUMERAL_TYPE);
9062 : 35617 : tree_node (ctx);
9063 : 35617 : tree_node (DECL_NAME (decl));
9064 : :
9065 : 35617 : int tag = insert (decl);
9066 : 35617 : if (streaming_p ())
9067 : 6410 : dump (dumper::TREE)
9068 : 21 : && dump ("Wrote enum decl:%d %C:%N", tag, TREE_CODE (decl), decl);
9069 : : return false;
9070 : : }
9071 : 4244 : break;
9072 : :
9073 : 4244 : case USING_DECL:
9074 : 4244 : if (TREE_CODE (DECL_CONTEXT (decl)) == FUNCTION_DECL)
9075 : : break;
9076 : : /* FALLTHROUGH */
9077 : :
9078 : 111032 : case FIELD_DECL:
9079 : 111032 : {
9080 : 111032 : if (streaming_p ())
9081 : 5855 : i (tt_data_member);
9082 : :
9083 : 111032 : tree ctx = DECL_CONTEXT (decl);
9084 : 111032 : tree_node (ctx);
9085 : :
9086 : 111032 : tree name = NULL_TREE;
9087 : :
9088 : 111032 : if (TREE_CODE (decl) == USING_DECL)
9089 : : ;
9090 : : else
9091 : : {
9092 : 110332 : name = DECL_NAME (decl);
9093 : 214702 : if (name && IDENTIFIER_ANON_P (name))
9094 : : name = NULL_TREE;
9095 : : }
9096 : :
9097 : 111032 : tree_node (name);
9098 : 111032 : if (!name && streaming_p ())
9099 : : {
9100 : 854 : unsigned ix = get_field_ident (ctx, decl);
9101 : 854 : u (ix);
9102 : : }
9103 : :
9104 : 111032 : int tag = insert (decl);
9105 : 111032 : if (streaming_p ())
9106 : 5855 : dump (dumper::TREE)
9107 : 21 : && dump ("Wrote member:%d %C:%N", tag, TREE_CODE (decl), decl);
9108 : : return false;
9109 : : }
9110 : 349309 : break;
9111 : :
9112 : 349309 : case VAR_DECL:
9113 : 349309 : gcc_checking_assert (!DECL_LOCAL_DECL_P (decl));
9114 : 349309 : if (DECL_VTABLE_OR_VTT_P (decl))
9115 : : {
9116 : : /* VTT or VTABLE, they are all on the vtables list. */
9117 : 1354 : tree ctx = CP_DECL_CONTEXT (decl);
9118 : 1354 : tree vtable = CLASSTYPE_VTABLES (ctx);
9119 : 1366 : for (unsigned ix = 0; ; vtable = DECL_CHAIN (vtable), ix++)
9120 : 1366 : if (vtable == decl)
9121 : : {
9122 : 1354 : gcc_checking_assert (DECL_VIRTUAL_P (decl));
9123 : 1354 : if (streaming_p ())
9124 : : {
9125 : 8 : u (tt_vtable);
9126 : 8 : u (ix);
9127 : 8 : dump (dumper::TREE)
9128 : 0 : && dump ("Writing vtable %N[%u]", ctx, ix);
9129 : : }
9130 : 1354 : tree_node (ctx);
9131 : 1354 : return false;
9132 : : }
9133 : : gcc_unreachable ();
9134 : : }
9135 : :
9136 : 347955 : if (DECL_TINFO_P (decl))
9137 : : {
9138 : 5312 : tinfo:
9139 : : /* A typeinfo, tt_tinfo_typedef or tt_tinfo_var. */
9140 : 9755 : bool is_var = VAR_P (decl);
9141 : 9755 : tree type = TREE_TYPE (decl);
9142 : 9755 : unsigned ix = get_pseudo_tinfo_index (type);
9143 : 9755 : if (streaming_p ())
9144 : : {
9145 : 6197 : i (is_var ? tt_tinfo_var : tt_tinfo_typedef);
9146 : 4398 : u (ix);
9147 : : }
9148 : :
9149 : 9755 : if (is_var)
9150 : : {
9151 : : /* We also need the type it is for and mangled name, so
9152 : : the reader doesn't need to complete the type (which
9153 : : would break section ordering). The type it is for is
9154 : : stashed on the name's TREE_TYPE. */
9155 : 5312 : tree name = DECL_NAME (decl);
9156 : 5312 : tree_node (name);
9157 : 5312 : type = TREE_TYPE (name);
9158 : 5312 : tree_node (type);
9159 : : }
9160 : :
9161 : 9755 : int tag = insert (decl);
9162 : 9755 : if (streaming_p ())
9163 : 4398 : dump (dumper::TREE)
9164 : 27 : && dump ("Wrote tinfo_%s:%d %u %N", is_var ? "var" : "type",
9165 : : tag, ix, type);
9166 : :
9167 : 9755 : if (!is_var)
9168 : : {
9169 : 4443 : tag = insert (type);
9170 : 4443 : if (streaming_p ())
9171 : 1799 : dump (dumper::TREE)
9172 : 9 : && dump ("Wrote tinfo_type:%d %u %N", tag, ix, type);
9173 : : }
9174 : 9755 : return false;
9175 : : }
9176 : :
9177 : 342643 : if (DECL_NTTP_OBJECT_P (decl))
9178 : : {
9179 : : /* A NTTP parm object. */
9180 : 33 : if (streaming_p ())
9181 : 7 : i (tt_nttp_var);
9182 : 33 : tree_node (tparm_object_argument (decl));
9183 : 33 : tree_node (DECL_NAME (decl));
9184 : 33 : int tag = insert (decl);
9185 : 33 : if (streaming_p ())
9186 : 7 : dump (dumper::TREE)
9187 : 0 : && dump ("Wrote nttp object:%d %N", tag, DECL_NAME (decl));
9188 : 33 : return false;
9189 : : }
9190 : :
9191 : : break;
9192 : :
9193 : 2966621 : case TYPE_DECL:
9194 : 2966621 : if (DECL_TINFO_P (decl))
9195 : 4443 : goto tinfo;
9196 : : break;
9197 : : }
9198 : :
9199 : 7587294 : if (DECL_THUNK_P (decl))
9200 : : {
9201 : : /* Thunks are similar to binfos -- write the thunked-to decl and
9202 : : then thunk-specific key info. */
9203 : 0 : if (streaming_p ())
9204 : : {
9205 : 0 : i (tt_thunk);
9206 : 0 : i (THUNK_FIXED_OFFSET (decl));
9207 : : }
9208 : :
9209 : : tree target = decl;
9210 : 0 : while (DECL_THUNK_P (target))
9211 : 0 : target = THUNK_TARGET (target);
9212 : 0 : tree_node (target);
9213 : 0 : tree_node (THUNK_VIRTUAL_OFFSET (decl));
9214 : 0 : int tag = insert (decl);
9215 : 0 : if (streaming_p ())
9216 : 0 : dump (dumper::TREE)
9217 : 0 : && dump ("Wrote:%d thunk %N to %N", tag, DECL_NAME (decl), target);
9218 : 0 : return false;
9219 : : }
9220 : :
9221 : 7587294 : if (DECL_CLONED_FUNCTION_P (decl))
9222 : : {
9223 : 255184 : tree target = get_clone_target (decl);
9224 : 255184 : if (streaming_p ())
9225 : 120904 : i (tt_clone_ref);
9226 : :
9227 : 255184 : tree_node (target);
9228 : 255184 : tree_node (DECL_NAME (decl));
9229 : 255184 : if (DECL_VIRTUAL_P (decl))
9230 : 19123 : tree_node (DECL_VINDEX (decl));
9231 : 255184 : int tag = insert (decl);
9232 : 255184 : if (streaming_p ())
9233 : 120904 : dump (dumper::TREE)
9234 : 156 : && dump ("Wrote:%d clone %N of %N", tag, DECL_NAME (decl), target);
9235 : 255184 : return false;
9236 : : }
9237 : :
9238 : : /* Everything left should be a thing that is in the entity table.
9239 : : Mostly things that can be defined outside of their (original
9240 : : declaration) context. */
9241 : 7332110 : gcc_checking_assert (TREE_CODE (decl) == TEMPLATE_DECL
9242 : : || VAR_P (decl)
9243 : : || TREE_CODE (decl) == FUNCTION_DECL
9244 : : || TREE_CODE (decl) == TYPE_DECL
9245 : : || TREE_CODE (decl) == USING_DECL
9246 : : || TREE_CODE (decl) == CONCEPT_DECL
9247 : : || TREE_CODE (decl) == NAMESPACE_DECL);
9248 : :
9249 : 7332110 : int use_tpl = -1;
9250 : 7332110 : tree ti = node_template_info (decl, use_tpl);
9251 : 7332110 : tree tpl = NULL_TREE;
9252 : :
9253 : : /* If this is the TEMPLATE_DECL_RESULT of a TEMPLATE_DECL, get the
9254 : : TEMPLATE_DECL. Note TI_TEMPLATE is not a TEMPLATE_DECL for
9255 : : (some) friends, so we need to check that. */
9256 : : // FIXME: Should local friend template specializations be by value?
9257 : : // They don't get idents so we'll never know they're imported, but I
9258 : : // think we can only reach them from the TU that defines the
9259 : : // befriending class?
9260 : 2769287 : if (ti && TREE_CODE (TI_TEMPLATE (ti)) == TEMPLATE_DECL
9261 : 10101366 : && DECL_TEMPLATE_RESULT (TI_TEMPLATE (ti)) == decl)
9262 : : {
9263 : : tpl = TI_TEMPLATE (ti);
9264 : 720808 : partial_template:
9265 : 720808 : if (streaming_p ())
9266 : : {
9267 : 2251 : i (tt_template);
9268 : 2251 : dump (dumper::TREE)
9269 : 9 : && dump ("Writing implicit template %C:%N%S",
9270 : 9 : TREE_CODE (tpl), tpl, tpl);
9271 : : }
9272 : 720808 : tree_node (tpl);
9273 : :
9274 : : /* Streaming TPL caused us to visit DECL and maybe its type,
9275 : : if it wasn't TU-local. */
9276 : 720808 : if (CHECKING_P && !has_tu_local_dep (tpl))
9277 : : {
9278 : 720781 : gcc_checking_assert (TREE_VISITED (decl));
9279 : 720781 : if (DECL_IMPLICIT_TYPEDEF_P (decl))
9280 : 376260 : gcc_checking_assert (TREE_VISITED (TREE_TYPE (decl)));
9281 : : }
9282 : : return false;
9283 : : }
9284 : :
9285 : 6696270 : tree ctx = CP_DECL_CONTEXT (decl);
9286 : 6696270 : depset *dep = NULL;
9287 : 6696270 : if (streaming_p ())
9288 : 1125621 : dep = dep_hash->find_dependency (decl);
9289 : 5570649 : else if (TREE_CODE (ctx) != FUNCTION_DECL
9290 : 198722 : || TREE_CODE (decl) == TEMPLATE_DECL
9291 : 180496 : || DECL_IMPLICIT_TYPEDEF_P (decl)
9292 : 5728235 : || (DECL_LANG_SPECIFIC (decl)
9293 : 112178 : && DECL_MODULE_IMPORT_P (decl)))
9294 : : {
9295 : 5413063 : auto kind = (TREE_CODE (decl) == NAMESPACE_DECL
9296 : 472662 : && !DECL_NAMESPACE_ALIAS (decl)
9297 : 5413063 : ? depset::EK_NAMESPACE : depset::EK_DECL);
9298 : 5413063 : dep = dep_hash->add_dependency (decl, kind);
9299 : : }
9300 : :
9301 : 12930152 : if (!dep || dep->is_tu_local ())
9302 : : {
9303 : : /* Some internal entity of context. Do by value. */
9304 : 305059 : decl_value (decl, dep);
9305 : 305059 : return false;
9306 : : }
9307 : :
9308 : 6391211 : if (dep->get_entity_kind () == depset::EK_REDIRECT)
9309 : : {
9310 : : /* The DECL_TEMPLATE_RESULT of a partial specialization.
9311 : : Write the partial specialization's template. */
9312 : 84968 : depset *redirect = dep->deps[0];
9313 : 84968 : gcc_checking_assert (redirect->get_entity_kind () == depset::EK_PARTIAL);
9314 : 84968 : tpl = redirect->get_entity ();
9315 : 84968 : goto partial_template;
9316 : : }
9317 : :
9318 : 6306243 : if (streaming_p ())
9319 : : {
9320 : : /* Locate the entity. */
9321 : 978405 : unsigned index = dep->cluster;
9322 : 978405 : unsigned import = 0;
9323 : :
9324 : 978405 : if (dep->is_import ())
9325 : 701 : import = dep->section;
9326 : 977704 : else if (CHECKING_P)
9327 : : /* It should be what we put there. */
9328 : 977704 : gcc_checking_assert (index == ~import_entity_index (decl));
9329 : :
9330 : : #if CHECKING_P
9331 : 701 : gcc_assert (!import || importedness >= 0);
9332 : : #endif
9333 : 978405 : i (tt_entity);
9334 : 978405 : u (import);
9335 : 978405 : u (index);
9336 : : }
9337 : :
9338 : 6306243 : int tag = insert (decl);
9339 : 6306243 : if (streaming_p () && dump (dumper::TREE))
9340 : : {
9341 : 513 : char const *kind = "import";
9342 : 513 : module_state *from = (*modules)[0];
9343 : 513 : if (dep->is_import ())
9344 : : /* Rediscover the unremapped index. */
9345 : 78 : from = import_entity_module (import_entity_index (decl));
9346 : : else
9347 : : {
9348 : 435 : tree o = get_originating_module_decl (decl);
9349 : 435 : o = STRIP_TEMPLATE (o);
9350 : 870 : kind = (DECL_LANG_SPECIFIC (o) && DECL_MODULE_PURVIEW_P (o)
9351 : 435 : ? "purview" : "GMF");
9352 : : }
9353 : 513 : dump ("Wrote %s:%d %C:%N@%M", kind,
9354 : 513 : tag, TREE_CODE (decl), decl, from);
9355 : : }
9356 : :
9357 : 6306243 : add_indirects (decl);
9358 : :
9359 : 6306243 : return false;
9360 : : }
9361 : :
9362 : : void
9363 : 7710855 : trees_out::type_node (tree type)
9364 : : {
9365 : 7710855 : gcc_assert (TYPE_P (type));
9366 : :
9367 : 7710855 : tree root = (TYPE_NAME (type)
9368 : 7710855 : ? TREE_TYPE (TYPE_NAME (type)) : TYPE_MAIN_VARIANT (type));
9369 : :
9370 : 7710855 : if (type != root)
9371 : : {
9372 : 1732799 : if (streaming_p ())
9373 : 363340 : i (tt_variant_type);
9374 : 1732799 : tree_node (root);
9375 : :
9376 : 1732799 : int flags = -1;
9377 : :
9378 : 1732799 : if (TREE_CODE (type) == FUNCTION_TYPE
9379 : 1732799 : || TREE_CODE (type) == METHOD_TYPE)
9380 : : {
9381 : 410565 : int quals = type_memfn_quals (type);
9382 : 410565 : int rquals = type_memfn_rqual (type);
9383 : 410565 : tree raises = TYPE_RAISES_EXCEPTIONS (type);
9384 : 410565 : bool late = TYPE_HAS_LATE_RETURN_TYPE (type);
9385 : :
9386 : 410565 : if (raises != TYPE_RAISES_EXCEPTIONS (root)
9387 : 12231 : || rquals != type_memfn_rqual (root)
9388 : 8185 : || quals != type_memfn_quals (root)
9389 : 418624 : || late != TYPE_HAS_LATE_RETURN_TYPE (root))
9390 : 410565 : flags = rquals | (int (late) << 2) | (quals << 3);
9391 : : }
9392 : : else
9393 : : {
9394 : 1322234 : if (TYPE_USER_ALIGN (type))
9395 : 11945 : flags = TYPE_ALIGN_RAW (type);
9396 : : }
9397 : :
9398 : 1732799 : if (streaming_p ())
9399 : 363340 : i (flags);
9400 : :
9401 : 1732799 : if (flags < 0)
9402 : : ;
9403 : 422510 : else if (TREE_CODE (type) == FUNCTION_TYPE
9404 : 422510 : || TREE_CODE (type) == METHOD_TYPE)
9405 : : {
9406 : 410565 : tree raises = TYPE_RAISES_EXCEPTIONS (type);
9407 : 410565 : if (raises == TYPE_RAISES_EXCEPTIONS (root))
9408 : 12231 : raises = error_mark_node;
9409 : 410565 : tree_node (raises);
9410 : : }
9411 : :
9412 : : /* build_type_attribute_variant creates a new TYPE_MAIN_VARIANT, so
9413 : : variants should all have the same set of attributes. */
9414 : 1732799 : gcc_checking_assert (TYPE_ATTRIBUTES (type)
9415 : : == TYPE_ATTRIBUTES (TYPE_MAIN_VARIANT (type)));
9416 : :
9417 : 1732799 : if (streaming_p ())
9418 : : {
9419 : : /* Qualifiers. */
9420 : 363340 : int rquals = cp_type_quals (root);
9421 : 363340 : int quals = cp_type_quals (type);
9422 : 363340 : if (quals == rquals)
9423 : 168588 : quals = -1;
9424 : 363340 : i (quals);
9425 : : }
9426 : :
9427 : 1732799 : if (ref_node (type) != WK_none)
9428 : : {
9429 : 1732799 : int tag = insert (type);
9430 : 1732799 : if (streaming_p ())
9431 : : {
9432 : 363340 : i (0);
9433 : 363340 : dump (dumper::TREE)
9434 : 183 : && dump ("Wrote:%d variant type %C", tag, TREE_CODE (type));
9435 : : }
9436 : : }
9437 : 1732799 : return;
9438 : : }
9439 : :
9440 : 5978056 : if (tree name = TYPE_NAME (type))
9441 : 2361351 : if ((TREE_CODE (name) == TYPE_DECL && DECL_ORIGINAL_TYPE (name))
9442 : 1844385 : || DECL_TEMPLATE_PARM_P (name)
9443 : 1238361 : || TREE_CODE (type) == RECORD_TYPE
9444 : 219585 : || TREE_CODE (type) == UNION_TYPE
9445 : 2575405 : || TREE_CODE (type) == ENUMERAL_TYPE)
9446 : : {
9447 : : /* We can meet template parms that we didn't meet in the
9448 : : tpl_parms walk, because we're referring to a derived type
9449 : : that was previously constructed from equivalent template
9450 : : parms. */
9451 : 2202961 : if (streaming_p ())
9452 : : {
9453 : 153816 : i (tt_typedef_type);
9454 : 153816 : dump (dumper::TREE)
9455 : 57 : && dump ("Writing %stypedef %C:%N",
9456 : 57 : DECL_IMPLICIT_TYPEDEF_P (name) ? "implicit " : "",
9457 : 57 : TREE_CODE (name), name);
9458 : : }
9459 : 2202961 : tree_node (name);
9460 : 2202961 : if (streaming_p ())
9461 : 153816 : dump (dumper::TREE) && dump ("Wrote typedef %C:%N%S",
9462 : 57 : TREE_CODE (name), name, name);
9463 : :
9464 : : /* We'll have either visited this type or have newly discovered
9465 : : that it's TU-local; either way we won't need to visit it again. */
9466 : 2202961 : gcc_checking_assert (TREE_VISITED (type) || has_tu_local_dep (name));
9467 : 2202961 : return;
9468 : : }
9469 : :
9470 : 3775095 : if (TYPE_PTRMEMFUNC_P (type))
9471 : : {
9472 : : /* This is a distinct type node, masquerading as a structure. */
9473 : 6639 : tree fn_type = TYPE_PTRMEMFUNC_FN_TYPE (type);
9474 : 6639 : if (streaming_p ())
9475 : 2017 : i (tt_ptrmem_type);
9476 : 6639 : tree_node (fn_type);
9477 : 6639 : int tag = insert (type);
9478 : 6639 : if (streaming_p ())
9479 : 2020 : dump (dumper::TREE) && dump ("Written:%d ptrmem type", tag);
9480 : 6639 : return;
9481 : : }
9482 : :
9483 : 3768456 : if (streaming_p ())
9484 : : {
9485 : 1202621 : u (tt_derived_type);
9486 : 1202621 : u (TREE_CODE (type));
9487 : : }
9488 : :
9489 : 3768456 : tree_node (TREE_TYPE (type));
9490 : 3768456 : switch (TREE_CODE (type))
9491 : : {
9492 : 0 : default:
9493 : : /* We should never meet a type here that is indescribable in
9494 : : terms of other types. */
9495 : 0 : gcc_unreachable ();
9496 : :
9497 : 61328 : case ARRAY_TYPE:
9498 : 61328 : tree_node (TYPE_DOMAIN (type));
9499 : 61328 : if (streaming_p ())
9500 : : /* Dependent arrays are constructed with TYPE_DEPENENT_P
9501 : : already set. */
9502 : 19556 : u (TYPE_DEPENDENT_P (type));
9503 : : break;
9504 : :
9505 : : case COMPLEX_TYPE:
9506 : : /* No additional data. */
9507 : : break;
9508 : :
9509 : 12 : case BOOLEAN_TYPE:
9510 : : /* A non-standard boolean type. */
9511 : 12 : if (streaming_p ())
9512 : 6 : u (TYPE_PRECISION (type));
9513 : : break;
9514 : :
9515 : 56082 : case INTEGER_TYPE:
9516 : 56082 : if (TREE_TYPE (type))
9517 : : {
9518 : : /* A range type (representing an array domain). */
9519 : 53433 : tree_node (TYPE_MIN_VALUE (type));
9520 : 53433 : tree_node (TYPE_MAX_VALUE (type));
9521 : : }
9522 : : else
9523 : : {
9524 : : /* A new integral type (representing a bitfield). */
9525 : 2649 : if (streaming_p ())
9526 : : {
9527 : 636 : unsigned prec = TYPE_PRECISION (type);
9528 : 636 : bool unsigned_p = TYPE_UNSIGNED (type);
9529 : :
9530 : 636 : u ((prec << 1) | unsigned_p);
9531 : : }
9532 : : }
9533 : : break;
9534 : :
9535 : 827506 : case METHOD_TYPE:
9536 : 827506 : case FUNCTION_TYPE:
9537 : 827506 : {
9538 : 827506 : gcc_checking_assert (type_memfn_rqual (type) == REF_QUAL_NONE);
9539 : :
9540 : 827506 : tree arg_types = TYPE_ARG_TYPES (type);
9541 : 827506 : if (TREE_CODE (type) == METHOD_TYPE)
9542 : : {
9543 : 513800 : tree_node (TREE_TYPE (TREE_VALUE (arg_types)));
9544 : 513800 : arg_types = TREE_CHAIN (arg_types);
9545 : : }
9546 : 827506 : tree_node (arg_types);
9547 : : }
9548 : 827506 : break;
9549 : :
9550 : 1282 : case OFFSET_TYPE:
9551 : 1282 : tree_node (TYPE_OFFSET_BASETYPE (type));
9552 : 1282 : break;
9553 : :
9554 : : case POINTER_TYPE:
9555 : : /* No additional data. */
9556 : : break;
9557 : :
9558 : 640981 : case REFERENCE_TYPE:
9559 : 640981 : if (streaming_p ())
9560 : 142458 : u (TYPE_REF_IS_RVALUE (type));
9561 : : break;
9562 : :
9563 : 725731 : case DECLTYPE_TYPE:
9564 : 725731 : case TYPEOF_TYPE:
9565 : 725731 : case DEPENDENT_OPERATOR_TYPE:
9566 : 725731 : tree_node (TYPE_VALUES_RAW (type));
9567 : 725731 : if (TREE_CODE (type) == DECLTYPE_TYPE)
9568 : : /* We stash a whole bunch of things into decltype's
9569 : : flags. */
9570 : 53605 : if (streaming_p ())
9571 : 18337 : tree_node_bools (type);
9572 : : break;
9573 : :
9574 : 7803 : case TRAIT_TYPE:
9575 : 7803 : tree_node (TRAIT_TYPE_KIND_RAW (type));
9576 : 7803 : tree_node (TRAIT_TYPE_TYPE1 (type));
9577 : 7803 : tree_node (TRAIT_TYPE_TYPE2 (type));
9578 : 7803 : break;
9579 : :
9580 : : case TYPE_ARGUMENT_PACK:
9581 : : /* No additional data. */
9582 : : break;
9583 : :
9584 : 136917 : case TYPE_PACK_EXPANSION:
9585 : 136917 : if (streaming_p ())
9586 : 55561 : u (PACK_EXPANSION_LOCAL_P (type));
9587 : 273834 : tree_node (PACK_EXPANSION_PARAMETER_PACKS (type));
9588 : 136917 : tree_node (PACK_EXPANSION_EXTRA_ARGS (type));
9589 : 136917 : break;
9590 : :
9591 : 22 : case PACK_INDEX_TYPE:
9592 : 22 : tree_node (PACK_INDEX_PACK (type));
9593 : 22 : tree_node (PACK_INDEX_INDEX (type));
9594 : 22 : break;
9595 : :
9596 : 160404 : case TYPENAME_TYPE:
9597 : 160404 : {
9598 : 160404 : tree_node (TYPE_CONTEXT (type));
9599 : 160404 : tree_node (DECL_NAME (TYPE_NAME (type)));
9600 : 160404 : tree_node (TYPENAME_TYPE_FULLNAME (type));
9601 : 160404 : if (streaming_p ())
9602 : : {
9603 : 56976 : enum tag_types tag_type = none_type;
9604 : 56976 : if (TYPENAME_IS_ENUM_P (type))
9605 : : tag_type = enum_type;
9606 : 56976 : else if (TYPENAME_IS_CLASS_P (type))
9607 : 0 : tag_type = class_type;
9608 : 56976 : u (int (tag_type));
9609 : : }
9610 : : }
9611 : : break;
9612 : :
9613 : 246 : case UNBOUND_CLASS_TEMPLATE:
9614 : 246 : {
9615 : 246 : tree decl = TYPE_NAME (type);
9616 : 246 : tree_node (DECL_CONTEXT (decl));
9617 : 246 : tree_node (DECL_NAME (decl));
9618 : 246 : tree_node (DECL_TEMPLATE_PARMS (decl));
9619 : : }
9620 : 246 : break;
9621 : :
9622 : 42 : case VECTOR_TYPE:
9623 : 42 : if (streaming_p ())
9624 : : {
9625 : 21 : poly_uint64 nunits = TYPE_VECTOR_SUBPARTS (type);
9626 : 42 : for (unsigned ix = 0; ix != NUM_POLY_INT_COEFFS; ix++)
9627 : 21 : wu (nunits.coeffs[ix]);
9628 : : }
9629 : : break;
9630 : : }
9631 : :
9632 : 3768456 : tree_node (TYPE_ATTRIBUTES (type));
9633 : :
9634 : : /* We may have met the type during emitting the above. */
9635 : 3768456 : if (ref_node (type) != WK_none)
9636 : : {
9637 : 3432393 : int tag = insert (type);
9638 : 3432393 : if (streaming_p ())
9639 : : {
9640 : 1044755 : i (0);
9641 : 1044755 : dump (dumper::TREE)
9642 : 528 : && dump ("Wrote:%d derived type %C", tag, TREE_CODE (type));
9643 : : }
9644 : : }
9645 : :
9646 : : return;
9647 : : }
9648 : :
9649 : : /* T is (mostly*) a non-mergeable node that must be written by value.
9650 : : The mergeable case is a BINFO, which are as-if DECLSs. */
9651 : :
9652 : : void
9653 : 23029375 : trees_out::tree_value (tree t)
9654 : : {
9655 : : /* We should never be writing a type by value. tree_type should
9656 : : have streamed it, or we're going via its TYPE_DECL. */
9657 : 23029375 : gcc_checking_assert (!TYPE_P (t));
9658 : :
9659 : 23029375 : if (DECL_P (t))
9660 : : /* No template, type, var or function, except anonymous
9661 : : non-context vars. */
9662 : 595890 : gcc_checking_assert ((TREE_CODE (t) != TEMPLATE_DECL
9663 : : && TREE_CODE (t) != TYPE_DECL
9664 : : && (TREE_CODE (t) != VAR_DECL
9665 : : || (!DECL_NAME (t) && !DECL_CONTEXT (t)))
9666 : : && TREE_CODE (t) != FUNCTION_DECL));
9667 : :
9668 : 23029375 : if (streaming_p ())
9669 : : {
9670 : : /* A new node -> tt_node. */
9671 : 9167349 : tree_val_count++;
9672 : 9167349 : i (tt_node);
9673 : 9167349 : start (t);
9674 : 9167349 : tree_node_bools (t);
9675 : : }
9676 : :
9677 : 23029375 : if (TREE_CODE (t) == TREE_BINFO)
9678 : : /* Binfos are decl-like and need merging information. */
9679 : 197008 : binfo_mergeable (t);
9680 : :
9681 : 23029375 : int tag = insert (t, WK_value);
9682 : 23029375 : if (streaming_p ())
9683 : 9167349 : dump (dumper::TREE)
9684 : 2463 : && dump ("Writing tree:%d %C:%N", tag, TREE_CODE (t), t);
9685 : :
9686 : 23029375 : tree_node_vals (t);
9687 : :
9688 : 23029375 : if (streaming_p ())
9689 : 9169812 : dump (dumper::TREE) && dump ("Written tree:%d %C:%N", tag, TREE_CODE (t), t);
9690 : 23029375 : }
9691 : :
9692 : : tree
9693 : 7911415 : trees_in::tree_value ()
9694 : : {
9695 : 7911415 : tree t = start ();
9696 : 7911415 : if (!t || !tree_node_bools (t))
9697 : 0 : return NULL_TREE;
9698 : :
9699 : 7911415 : tree existing = t;
9700 : 7911415 : if (TREE_CODE (t) == TREE_BINFO)
9701 : : {
9702 : 71058 : tree type;
9703 : 71058 : unsigned ix = binfo_mergeable (&type);
9704 : 71058 : if (TYPE_BINFO (type))
9705 : : {
9706 : : /* We already have a definition, this must be a duplicate. */
9707 : 40486 : dump (dumper::MERGE)
9708 : 232 : && dump ("Deduping binfo %N[%u]", type, ix);
9709 : 40486 : existing = TYPE_BINFO (type);
9710 : 55616 : while (existing && ix--)
9711 : 15130 : existing = TREE_CHAIN (existing);
9712 : 40486 : if (existing)
9713 : 40486 : register_duplicate (t, existing);
9714 : : else
9715 : : /* Error, mismatch -- diagnose in read_class_def's
9716 : : checking. */
9717 : : existing = t;
9718 : : }
9719 : : }
9720 : :
9721 : : /* Insert into map. */
9722 : 7911415 : int tag = insert (existing);
9723 : 7911415 : dump (dumper::TREE)
9724 : 3351 : && dump ("Reading tree:%d %C", tag, TREE_CODE (t));
9725 : :
9726 : 7911415 : if (!tree_node_vals (t))
9727 : : {
9728 : 0 : back_refs[~tag] = NULL_TREE;
9729 : 0 : set_overrun ();
9730 : : /* Bail. */
9731 : 0 : return NULL_TREE;
9732 : : }
9733 : :
9734 : 7911415 : if (TREE_CODE (t) == LAMBDA_EXPR
9735 : 7911415 : && CLASSTYPE_LAMBDA_EXPR (TREE_TYPE (t)))
9736 : : {
9737 : 1747 : existing = CLASSTYPE_LAMBDA_EXPR (TREE_TYPE (t));
9738 : 1747 : back_refs[~tag] = existing;
9739 : : }
9740 : :
9741 : 7914766 : dump (dumper::TREE) && dump ("Read tree:%d %C:%N", tag, TREE_CODE (t), t);
9742 : :
9743 : 7911415 : if (TREE_CODE (existing) == INTEGER_CST && !TREE_OVERFLOW (existing))
9744 : : {
9745 : 461615 : existing = cache_integer_cst (t, true);
9746 : 461615 : back_refs[~tag] = existing;
9747 : : }
9748 : :
9749 : : return existing;
9750 : : }
9751 : :
9752 : : /* Whether DECL has a TU-local dependency in the hash. */
9753 : :
9754 : : bool
9755 : 721142 : trees_out::has_tu_local_dep (tree decl) const
9756 : : {
9757 : : /* Only the contexts of fields or enums remember that they're
9758 : : TU-local. */
9759 : 721142 : if (DECL_CONTEXT (decl)
9760 : 721142 : && (TREE_CODE (decl) == FIELD_DECL
9761 : 721139 : || TREE_CODE (decl) == CONST_DECL))
9762 : 3 : decl = TYPE_NAME (DECL_CONTEXT (decl));
9763 : :
9764 : 721142 : depset *dep = dep_hash->find_dependency (decl);
9765 : 721142 : if (!dep)
9766 : : {
9767 : : /* This might be the DECL_TEMPLATE_RESULT of a TEMPLATE_DECL
9768 : : which we found was TU-local and gave up early. */
9769 : 8501 : int use_tpl = -1;
9770 : 8501 : if (tree ti = node_template_info (decl, use_tpl))
9771 : 1729 : dep = dep_hash->find_dependency (TI_TEMPLATE (ti));
9772 : : }
9773 : :
9774 : 721157 : return dep && dep->is_tu_local ();
9775 : : }
9776 : :
9777 : : /* If T depends on a TU-local entity, return that decl. */
9778 : :
9779 : : tree
9780 : 366 : trees_out::find_tu_local_decl (tree t)
9781 : : {
9782 : : /* We need to have walked all deps first before we can check. */
9783 : 366 : gcc_checking_assert (!is_initial_scan ());
9784 : :
9785 : 894 : auto walker = [](tree *tp, int *walk_subtrees, void *data) -> tree
9786 : : {
9787 : 528 : auto self = (trees_out *)data;
9788 : :
9789 : 528 : tree decl = NULL_TREE;
9790 : 528 : if (TYPE_P (*tp))
9791 : : {
9792 : : /* A PMF type is a record type, which we otherwise wouldn't walk;
9793 : : return whether the function type is TU-local. */
9794 : 354 : if (TYPE_PTRMEMFUNC_P (*tp))
9795 : : {
9796 : 3 : *walk_subtrees = 0;
9797 : 3 : return self->find_tu_local_decl (TYPE_PTRMEMFUNC_FN_TYPE (*tp));
9798 : : }
9799 : : else
9800 : 351 : decl = TYPE_MAIN_DECL (*tp);
9801 : : }
9802 : 174 : else if (DECL_P (*tp))
9803 : : decl = *tp;
9804 : :
9805 : 357 : if (decl)
9806 : : {
9807 : : /* We found a DECL, this will tell us whether we're TU-local. */
9808 : 57 : *walk_subtrees = 0;
9809 : 57 : return self->has_tu_local_dep (decl) ? decl : NULL_TREE;
9810 : : }
9811 : : return NULL_TREE;
9812 : : };
9813 : :
9814 : : /* We need to walk without duplicates so that we step into the pointed-to
9815 : : types of array types. */
9816 : 366 : return cp_walk_tree_without_duplicates (&t, walker, this);
9817 : : }
9818 : :
9819 : : /* Stream out tree node T. We automatically create local back
9820 : : references, which is essentially a single pass lisp
9821 : : self-referential structure pretty-printer. */
9822 : :
9823 : : void
9824 : 204610186 : trees_out::tree_node (tree t)
9825 : : {
9826 : 204610186 : dump.indent ();
9827 : 204610186 : walk_kind ref = ref_node (t);
9828 : 204610186 : if (ref == WK_none)
9829 : 157752622 : goto done;
9830 : :
9831 : : /* Find TU-local entities and intercept streaming to instead write a
9832 : : placeholder value; this way we don't need to emit such decls.
9833 : : We only need to do this when writing a definition of an entity
9834 : : that we know names a TU-local entity. */
9835 : 54980536 : if (!is_initial_scan () && writing_local_entities)
9836 : : {
9837 : 846 : tree local_decl = NULL_TREE;
9838 : 846 : if (DECL_P (t) && has_tu_local_dep (t))
9839 : : local_decl = t;
9840 : : /* Consider a type to be TU-local if it refers to any TU-local decl,
9841 : : no matter how deep.
9842 : :
9843 : : This worsens diagnostics slightly, as we often no longer point
9844 : : directly to the at-fault entity when instantiating. However, this
9845 : : reduces the module size slightly and means that much less of pt.cc
9846 : : needs to know about us. */
9847 : 750 : else if (TYPE_P (t))
9848 : 141 : local_decl = find_tu_local_decl (t);
9849 : 609 : else if (EXPR_P (t))
9850 : 222 : local_decl = find_tu_local_decl (TREE_TYPE (t));
9851 : :
9852 : 459 : if (local_decl)
9853 : : {
9854 : 150 : int tag = insert (t, WK_value);
9855 : 150 : if (streaming_p ())
9856 : : {
9857 : 150 : tu_local_count++;
9858 : 150 : i (tt_tu_local);
9859 : 150 : dump (dumper::TREE)
9860 : 0 : && dump ("Writing TU-local entity:%d %C:%N",
9861 : 0 : tag, TREE_CODE (t), t);
9862 : : }
9863 : : /* TODO: Get a more descriptive name? */
9864 : 150 : tree_node (DECL_NAME (local_decl));
9865 : 150 : if (state)
9866 : 150 : state->write_location (*this, DECL_SOURCE_LOCATION (local_decl));
9867 : 150 : goto done;
9868 : : }
9869 : : }
9870 : :
9871 : 46857414 : if (ref != WK_normal)
9872 : 1209835 : goto skip_normal;
9873 : :
9874 : 45647579 : if (TREE_CODE (t) == IDENTIFIER_NODE)
9875 : : {
9876 : : /* An identifier node -> tt_id, tt_conv_id, tt_anon_id, tt_lambda_id. */
9877 : 5696825 : int code = tt_id;
9878 : 5696825 : if (IDENTIFIER_ANON_P (t))
9879 : 20947 : code = IDENTIFIER_LAMBDA_P (t) ? tt_lambda_id : tt_anon_id;
9880 : 5675878 : else if (IDENTIFIER_CONV_OP_P (t))
9881 : 8322 : code = tt_conv_id;
9882 : :
9883 : 5696825 : if (streaming_p ())
9884 : 1158848 : i (code);
9885 : :
9886 : 5696825 : if (code == tt_conv_id)
9887 : : {
9888 : 8322 : tree type = TREE_TYPE (t);
9889 : 8322 : gcc_checking_assert (type || t == conv_op_identifier);
9890 : 8322 : tree_node (type);
9891 : : }
9892 : 5688503 : else if (code == tt_id && streaming_p ())
9893 : 1149476 : str (IDENTIFIER_POINTER (t), IDENTIFIER_LENGTH (t));
9894 : :
9895 : 5696825 : int tag = insert (t);
9896 : 5696825 : if (streaming_p ())
9897 : : {
9898 : : /* We know the ordering of the 4 id tags. */
9899 : 1158848 : static const char *const kinds[] =
9900 : : {"", "conv_op ", "anon ", "lambda "};
9901 : 1158848 : dump (dumper::TREE)
9902 : 1008 : && dump ("Written:%d %sidentifier:%N", tag,
9903 : 1005 : kinds[code - tt_id],
9904 : 3 : code == tt_conv_id ? TREE_TYPE (t) : t);
9905 : : }
9906 : 5696825 : goto done;
9907 : : }
9908 : :
9909 : 39950754 : if (TREE_CODE (t) == TREE_BINFO)
9910 : : {
9911 : : /* A BINFO -> tt_binfo.
9912 : : We must do this by reference. We stream the binfo tree
9913 : : itself when streaming its owning RECORD_TYPE. That we got
9914 : : here means the dominating type is not in this SCC. */
9915 : 45500 : if (streaming_p ())
9916 : 1095 : i (tt_binfo);
9917 : 45500 : binfo_mergeable (t);
9918 : 45500 : gcc_checking_assert (!TREE_VISITED (t));
9919 : 45500 : int tag = insert (t);
9920 : 45500 : if (streaming_p ())
9921 : 1095 : dump (dumper::TREE) && dump ("Inserting binfo:%d %N", tag, t);
9922 : 45500 : goto done;
9923 : : }
9924 : :
9925 : 39905254 : if (TREE_CODE (t) == INTEGER_CST
9926 : 2985838 : && !TREE_OVERFLOW (t)
9927 : 42891092 : && TREE_CODE (TREE_TYPE (t)) == ENUMERAL_TYPE)
9928 : : {
9929 : : /* An integral constant of enumeral type. See if it matches one
9930 : : of the enumeration values. */
9931 : 22210 : for (tree values = TYPE_VALUES (TREE_TYPE (t));
9932 : 656052 : values; values = TREE_CHAIN (values))
9933 : : {
9934 : 654472 : tree decl = TREE_VALUE (values);
9935 : 654472 : if (tree_int_cst_equal (DECL_INITIAL (decl), t))
9936 : : {
9937 : 20630 : if (streaming_p ())
9938 : 5387 : u (tt_enum_value);
9939 : 20630 : tree_node (decl);
9940 : 20672 : dump (dumper::TREE) && dump ("Written enum value %N", decl);
9941 : 20630 : goto done;
9942 : : }
9943 : : }
9944 : : /* It didn't match. We'll write it a an explicit INTEGER_CST
9945 : : node. */
9946 : : }
9947 : :
9948 : 39884624 : if (TYPE_P (t))
9949 : : {
9950 : 7710855 : type_node (t);
9951 : 7710855 : goto done;
9952 : : }
9953 : :
9954 : 32173769 : if (DECL_P (t))
9955 : : {
9956 : 9964191 : if (DECL_TEMPLATE_PARM_P (t))
9957 : : {
9958 : 1623216 : tpl_parm_value (t);
9959 : 1623216 : goto done;
9960 : : }
9961 : :
9962 : 8340975 : if (!DECL_CONTEXT (t))
9963 : : {
9964 : : /* There are a few cases of decls with no context. We'll write
9965 : : these by value, but first assert they are cases we expect. */
9966 : 19072 : gcc_checking_assert (ref == WK_normal);
9967 : 19072 : switch (TREE_CODE (t))
9968 : : {
9969 : 0 : default: gcc_unreachable ();
9970 : :
9971 : 6114 : case LABEL_DECL:
9972 : : /* CASE_LABEL_EXPRs contain uncontexted LABEL_DECLs. */
9973 : 6114 : gcc_checking_assert (!DECL_NAME (t));
9974 : : break;
9975 : :
9976 : 574 : case VAR_DECL:
9977 : : /* AGGR_INIT_EXPRs cons up anonymous uncontexted VAR_DECLs. */
9978 : 574 : gcc_checking_assert (!DECL_NAME (t)
9979 : : && DECL_ARTIFICIAL (t));
9980 : : break;
9981 : :
9982 : : case PARM_DECL:
9983 : : /* REQUIRES_EXPRs have a tree list of uncontexted
9984 : : PARM_DECLS. It'd be nice if they had a
9985 : : distinguishing flag to double check. */
9986 : : break;
9987 : : }
9988 : 19072 : goto by_value;
9989 : : }
9990 : : }
9991 : :
9992 : 22209578 : skip_normal:
9993 : 31741316 : if (DECL_P (t) && !decl_node (t, ref))
9994 : 8731013 : goto done;
9995 : :
9996 : : /* Otherwise by value */
9997 : 23029375 : by_value:
9998 : 23029375 : tree_value (t);
9999 : :
10000 : 204610186 : done:
10001 : : /* And, breath out. */
10002 : 204610186 : dump.outdent ();
10003 : 204610186 : }
10004 : :
10005 : : /* Stream in a tree node. */
10006 : :
10007 : : tree
10008 : 66773120 : trees_in::tree_node (bool is_use)
10009 : : {
10010 : 66773120 : if (get_overrun ())
10011 : : return NULL_TREE;
10012 : :
10013 : 66773120 : dump.indent ();
10014 : 66773120 : int tag = i ();
10015 : 66773120 : tree res = NULL_TREE;
10016 : 66773120 : switch (tag)
10017 : : {
10018 : 21760123 : default:
10019 : : /* backref, pull it out of the map. */
10020 : 21760123 : res = back_ref (tag);
10021 : 21760123 : break;
10022 : :
10023 : : case tt_null:
10024 : : /* NULL_TREE. */
10025 : : break;
10026 : :
10027 : 150 : case tt_tu_local:
10028 : 150 : {
10029 : : /* A translation-unit-local entity. */
10030 : 150 : res = make_node (TU_LOCAL_ENTITY);
10031 : 150 : int tag = insert (res);
10032 : :
10033 : 150 : TU_LOCAL_ENTITY_NAME (res) = tree_node ();
10034 : 150 : TU_LOCAL_ENTITY_LOCATION (res) = state->read_location (*this);
10035 : 150 : dump (dumper::TREE) && dump ("Read TU-local entity:%d %N", tag, res);
10036 : : }
10037 : : break;
10038 : :
10039 : 5050073 : case tt_fixed:
10040 : : /* A fixed ref, find it in the fixed_ref array. */
10041 : 5050073 : {
10042 : 5050073 : unsigned fix = u ();
10043 : 5050073 : if (fix < (*fixed_trees).length ())
10044 : : {
10045 : 5050073 : res = (*fixed_trees)[fix];
10046 : 5050073 : dump (dumper::TREE) && dump ("Read fixed:%u %C:%N%S", fix,
10047 : 4851 : TREE_CODE (res), res, res);
10048 : : }
10049 : :
10050 : 5045222 : if (!res)
10051 : 0 : set_overrun ();
10052 : : }
10053 : : break;
10054 : :
10055 : 51834 : case tt_parm:
10056 : 51834 : {
10057 : 51834 : tree fn = tree_node ();
10058 : 51834 : if (fn && TREE_CODE (fn) == FUNCTION_DECL)
10059 : 51834 : res = tree_node ();
10060 : 51834 : if (res)
10061 : 51834 : dump (dumper::TREE)
10062 : 15 : && dump ("Read %s reference %N",
10063 : 15 : TREE_CODE (res) == PARM_DECL ? "parameter" : "result",
10064 : : res);
10065 : : }
10066 : : break;
10067 : :
10068 : 7911415 : case tt_node:
10069 : : /* A new node. Stream it in. */
10070 : 7911415 : res = tree_value ();
10071 : 7911415 : break;
10072 : :
10073 : 843825 : case tt_decl:
10074 : : /* A new decl. Stream it in. */
10075 : 843825 : res = decl_value ();
10076 : 843825 : break;
10077 : :
10078 : 295431 : case tt_tpl_parm:
10079 : : /* A template parameter. Stream it in. */
10080 : 295431 : res = tpl_parm_value ();
10081 : 295431 : break;
10082 : :
10083 : 906153 : case tt_id:
10084 : : /* An identifier node. */
10085 : 906153 : {
10086 : 906153 : size_t l;
10087 : 906153 : const char *chars = str (&l);
10088 : 906153 : res = get_identifier_with_length (chars, l);
10089 : 906153 : int tag = insert (res);
10090 : 906153 : dump (dumper::TREE)
10091 : 1428 : && dump ("Read identifier:%d %N", tag, res);
10092 : : }
10093 : 906153 : break;
10094 : :
10095 : 2272 : case tt_conv_id:
10096 : : /* A conversion operator. Get the type and recreate the
10097 : : identifier. */
10098 : 2272 : {
10099 : 2272 : tree type = tree_node ();
10100 : 2272 : if (!get_overrun ())
10101 : : {
10102 : 2272 : res = type ? make_conv_op_name (type) : conv_op_identifier;
10103 : 2272 : int tag = insert (res);
10104 : 2272 : dump (dumper::TREE)
10105 : 27 : && dump ("Created conv_op:%d %S for %N", tag, res, type);
10106 : : }
10107 : : }
10108 : : break;
10109 : :
10110 : 4974 : case tt_anon_id:
10111 : 4974 : case tt_lambda_id:
10112 : : /* An anonymous or lambda id. */
10113 : 4974 : {
10114 : 4974 : res = make_anon_name ();
10115 : 4974 : if (tag == tt_lambda_id)
10116 : 2625 : IDENTIFIER_LAMBDA_P (res) = true;
10117 : 4974 : int tag = insert (res);
10118 : 4977 : dump (dumper::TREE)
10119 : 3 : && dump ("Read %s identifier:%d %N",
10120 : 3 : IDENTIFIER_LAMBDA_P (res) ? "lambda" : "anon", tag, res);
10121 : : }
10122 : : break;
10123 : :
10124 : 129949 : case tt_typedef_type:
10125 : 129949 : res = tree_node ();
10126 : 129949 : if (res)
10127 : : {
10128 : 129949 : dump (dumper::TREE)
10129 : 72 : && dump ("Read %stypedef %C:%N",
10130 : 72 : DECL_IMPLICIT_TYPEDEF_P (res) ? "implicit " : "",
10131 : 72 : TREE_CODE (res), res);
10132 : 129949 : res = TREE_TYPE (res);
10133 : : }
10134 : : break;
10135 : :
10136 : 1014036 : case tt_derived_type:
10137 : : /* A type derived from some other type. */
10138 : 1014036 : {
10139 : 1014036 : enum tree_code code = tree_code (u ());
10140 : 1014036 : res = tree_node ();
10141 : :
10142 : 1014036 : switch (code)
10143 : : {
10144 : 0 : default:
10145 : 0 : set_overrun ();
10146 : 0 : break;
10147 : :
10148 : 16044 : case ARRAY_TYPE:
10149 : 16044 : {
10150 : 16044 : tree domain = tree_node ();
10151 : 16044 : int dep = u ();
10152 : 16044 : if (!get_overrun ())
10153 : 16044 : res = build_cplus_array_type (res, domain, dep);
10154 : : }
10155 : : break;
10156 : :
10157 : 87 : case COMPLEX_TYPE:
10158 : 87 : if (!get_overrun ())
10159 : 87 : res = build_complex_type (res);
10160 : : break;
10161 : :
10162 : 9 : case BOOLEAN_TYPE:
10163 : 9 : {
10164 : 9 : unsigned precision = u ();
10165 : 9 : if (!get_overrun ())
10166 : 9 : res = build_nonstandard_boolean_type (precision);
10167 : : }
10168 : : break;
10169 : :
10170 : 14558 : case INTEGER_TYPE:
10171 : 14558 : if (res)
10172 : : {
10173 : : /* A range type (representing an array domain). */
10174 : 13997 : tree min = tree_node ();
10175 : 13997 : tree max = tree_node ();
10176 : :
10177 : 13997 : if (!get_overrun ())
10178 : 13997 : res = build_range_type (res, min, max);
10179 : : }
10180 : : else
10181 : : {
10182 : : /* A new integral type (representing a bitfield). */
10183 : 561 : unsigned enc = u ();
10184 : 561 : if (!get_overrun ())
10185 : 561 : res = build_nonstandard_integer_type (enc >> 1, enc & 1);
10186 : : }
10187 : : break;
10188 : :
10189 : 290410 : case FUNCTION_TYPE:
10190 : 290410 : case METHOD_TYPE:
10191 : 290410 : {
10192 : 290410 : tree klass = code == METHOD_TYPE ? tree_node () : NULL_TREE;
10193 : 290410 : tree args = tree_node ();
10194 : 290410 : if (!get_overrun ())
10195 : : {
10196 : 290410 : if (klass)
10197 : 179132 : res = build_method_type_directly (klass, res, args);
10198 : : else
10199 : 111278 : res = build_function_type (res, args);
10200 : : }
10201 : : }
10202 : : break;
10203 : :
10204 : 231 : case OFFSET_TYPE:
10205 : 231 : {
10206 : 231 : tree base = tree_node ();
10207 : 231 : if (!get_overrun ())
10208 : 231 : res = build_offset_type (base, res);
10209 : : }
10210 : : break;
10211 : :
10212 : 140757 : case POINTER_TYPE:
10213 : 140757 : if (!get_overrun ())
10214 : 140757 : res = build_pointer_type (res);
10215 : : break;
10216 : :
10217 : 117552 : case REFERENCE_TYPE:
10218 : 117552 : {
10219 : 117552 : bool rval = bool (u ());
10220 : 117552 : if (!get_overrun ())
10221 : 117552 : res = cp_build_reference_type (res, rval);
10222 : : }
10223 : : break;
10224 : :
10225 : 301192 : case DECLTYPE_TYPE:
10226 : 301192 : case TYPEOF_TYPE:
10227 : 301192 : case DEPENDENT_OPERATOR_TYPE:
10228 : 301192 : {
10229 : 301192 : tree expr = tree_node ();
10230 : 301192 : if (!get_overrun ())
10231 : : {
10232 : 301192 : res = cxx_make_type (code);
10233 : 301192 : TYPE_VALUES_RAW (res) = expr;
10234 : 301192 : if (code == DECLTYPE_TYPE)
10235 : 14561 : tree_node_bools (res);
10236 : 301192 : SET_TYPE_STRUCTURAL_EQUALITY (res);
10237 : : }
10238 : : }
10239 : : break;
10240 : :
10241 : 2040 : case TRAIT_TYPE:
10242 : 2040 : {
10243 : 2040 : tree kind = tree_node ();
10244 : 2040 : tree type1 = tree_node ();
10245 : 2040 : tree type2 = tree_node ();
10246 : 2040 : if (!get_overrun ())
10247 : : {
10248 : 2040 : res = cxx_make_type (TRAIT_TYPE);
10249 : 2040 : TRAIT_TYPE_KIND_RAW (res) = kind;
10250 : 2040 : TRAIT_TYPE_TYPE1 (res) = type1;
10251 : 2040 : TRAIT_TYPE_TYPE2 (res) = type2;
10252 : 2040 : SET_TYPE_STRUCTURAL_EQUALITY (res);
10253 : : }
10254 : : }
10255 : : break;
10256 : :
10257 : 42494 : case TYPE_ARGUMENT_PACK:
10258 : 42494 : if (!get_overrun ())
10259 : : {
10260 : 42494 : tree pack = cxx_make_type (TYPE_ARGUMENT_PACK);
10261 : 42494 : ARGUMENT_PACK_ARGS (pack) = res;
10262 : : res = pack;
10263 : : }
10264 : : break;
10265 : :
10266 : 42497 : case TYPE_PACK_EXPANSION:
10267 : 42497 : {
10268 : 42497 : bool local = u ();
10269 : 42497 : tree param_packs = tree_node ();
10270 : 42497 : tree extra_args = tree_node ();
10271 : 42497 : if (!get_overrun ())
10272 : : {
10273 : 42497 : tree expn = cxx_make_type (TYPE_PACK_EXPANSION);
10274 : 42497 : SET_TYPE_STRUCTURAL_EQUALITY (expn);
10275 : 42497 : PACK_EXPANSION_PATTERN (expn) = res;
10276 : 84994 : PACK_EXPANSION_PARAMETER_PACKS (expn) = param_packs;
10277 : 42497 : PACK_EXPANSION_EXTRA_ARGS (expn) = extra_args;
10278 : 42497 : PACK_EXPANSION_LOCAL_P (expn) = local;
10279 : 42497 : res = expn;
10280 : : }
10281 : : }
10282 : : break;
10283 : :
10284 : 13 : case PACK_INDEX_TYPE:
10285 : 13 : {
10286 : 13 : tree pack = tree_node ();
10287 : 13 : tree index = tree_node ();
10288 : 13 : if (!get_overrun ())
10289 : 13 : res = make_pack_index (pack, index);
10290 : : }
10291 : : break;
10292 : :
10293 : 46078 : case TYPENAME_TYPE:
10294 : 46078 : {
10295 : 46078 : tree ctx = tree_node ();
10296 : 46078 : tree name = tree_node ();
10297 : 46078 : tree fullname = tree_node ();
10298 : 46078 : enum tag_types tag_type = tag_types (u ());
10299 : :
10300 : 46078 : if (!get_overrun ())
10301 : 46078 : res = build_typename_type (ctx, name, fullname, tag_type);
10302 : : }
10303 : : break;
10304 : :
10305 : 44 : case UNBOUND_CLASS_TEMPLATE:
10306 : 44 : {
10307 : 44 : tree ctx = tree_node ();
10308 : 44 : tree name = tree_node ();
10309 : 44 : tree parms = tree_node ();
10310 : :
10311 : 44 : if (!get_overrun ())
10312 : 44 : res = make_unbound_class_template_raw (ctx, name, parms);
10313 : : }
10314 : : break;
10315 : :
10316 : : case VECTOR_TYPE:
10317 : : {
10318 : : poly_uint64 nunits;
10319 : 60 : for (unsigned ix = 0; ix != NUM_POLY_INT_COEFFS; ix++)
10320 : 30 : nunits.coeffs[ix] = wu ();
10321 : 30 : if (!get_overrun ())
10322 : 30 : res = build_vector_type (res, nunits);
10323 : : }
10324 : : break;
10325 : : }
10326 : :
10327 : : /* In the exporting TU, a derived type with attributes was built by
10328 : : build_type_attribute_variant as a distinct copy, with itself as
10329 : : TYPE_MAIN_VARIANT. We repeat that on import to get the version
10330 : : without attributes as TYPE_CANONICAL. */
10331 : 1014036 : if (tree attribs = tree_node ())
10332 : 13078 : res = cp_build_type_attribute_variant (res, attribs);
10333 : :
10334 : 1014036 : int tag = i ();
10335 : 1014036 : if (!tag)
10336 : : {
10337 : 870722 : tag = insert (res);
10338 : 870722 : if (res)
10339 : 870722 : dump (dumper::TREE)
10340 : 654 : && dump ("Created:%d derived type %C", tag, code);
10341 : : }
10342 : : else
10343 : 143314 : res = back_ref (tag);
10344 : : }
10345 : : break;
10346 : :
10347 : 299429 : case tt_variant_type:
10348 : : /* Variant of some type. */
10349 : 299429 : {
10350 : 299429 : res = tree_node ();
10351 : 299429 : int flags = i ();
10352 : 299429 : if (get_overrun ())
10353 : : ;
10354 : 299429 : else if (flags < 0)
10355 : : /* No change. */;
10356 : 144455 : else if (TREE_CODE (res) == FUNCTION_TYPE
10357 : 144455 : || TREE_CODE (res) == METHOD_TYPE)
10358 : : {
10359 : 143155 : cp_ref_qualifier rqual = cp_ref_qualifier (flags & 3);
10360 : 143155 : bool late = (flags >> 2) & 1;
10361 : 143155 : cp_cv_quals quals = cp_cv_quals (flags >> 3);
10362 : :
10363 : 143155 : tree raises = tree_node ();
10364 : 143155 : if (raises == error_mark_node)
10365 : 5076 : raises = TYPE_RAISES_EXCEPTIONS (res);
10366 : :
10367 : 143155 : res = build_cp_fntype_variant (res, rqual, raises, late);
10368 : 143155 : if (TREE_CODE (res) == FUNCTION_TYPE)
10369 : 53291 : res = apply_memfn_quals (res, quals, rqual);
10370 : : }
10371 : : else
10372 : : {
10373 : 1300 : res = build_aligned_type (res, (1u << flags) >> 1);
10374 : 1300 : TYPE_USER_ALIGN (res) = true;
10375 : : }
10376 : :
10377 : 299429 : int quals = i ();
10378 : 299429 : if (quals >= 0 && !get_overrun ())
10379 : 155956 : res = cp_build_qualified_type (res, quals);
10380 : :
10381 : 299429 : int tag = i ();
10382 : 299429 : if (!tag)
10383 : : {
10384 : 299429 : tag = insert (res);
10385 : 299429 : if (res)
10386 : 299429 : dump (dumper::TREE)
10387 : 276 : && dump ("Created:%d variant type %C", tag, TREE_CODE (res));
10388 : : }
10389 : : else
10390 : 0 : res = back_ref (tag);
10391 : : }
10392 : : break;
10393 : :
10394 : 3808 : case tt_tinfo_var:
10395 : 3808 : case tt_tinfo_typedef:
10396 : : /* A tinfo var or typedef. */
10397 : 3808 : {
10398 : 3808 : bool is_var = tag == tt_tinfo_var;
10399 : 3808 : unsigned ix = u ();
10400 : 3808 : tree type = NULL_TREE;
10401 : :
10402 : 3808 : if (is_var)
10403 : : {
10404 : 2278 : tree name = tree_node ();
10405 : 2278 : type = tree_node ();
10406 : :
10407 : 2278 : if (!get_overrun ())
10408 : 2278 : res = get_tinfo_decl_direct (type, name, int (ix));
10409 : : }
10410 : : else
10411 : : {
10412 : 1530 : if (!get_overrun ())
10413 : : {
10414 : 1530 : type = get_pseudo_tinfo_type (ix);
10415 : 1530 : res = TYPE_NAME (type);
10416 : : }
10417 : : }
10418 : 3808 : if (res)
10419 : : {
10420 : 3808 : int tag = insert (res);
10421 : 3808 : dump (dumper::TREE)
10422 : 36 : && dump ("Created tinfo_%s:%d %S:%u for %N",
10423 : : is_var ? "var" : "decl", tag, res, ix, type);
10424 : 3808 : if (!is_var)
10425 : : {
10426 : 1530 : tag = insert (type);
10427 : 1530 : dump (dumper::TREE)
10428 : 12 : && dump ("Created tinfo_type:%d %u %N", tag, ix, type);
10429 : : }
10430 : : }
10431 : : }
10432 : : break;
10433 : :
10434 : 1113 : case tt_ptrmem_type:
10435 : : /* A pointer to member function. */
10436 : 1113 : {
10437 : 1113 : tree type = tree_node ();
10438 : 1113 : if (type && TREE_CODE (type) == POINTER_TYPE
10439 : 2226 : && TREE_CODE (TREE_TYPE (type)) == METHOD_TYPE)
10440 : : {
10441 : 1113 : res = build_ptrmemfunc_type (type);
10442 : 1113 : int tag = insert (res);
10443 : 1116 : dump (dumper::TREE) && dump ("Created:%d ptrmem type", tag);
10444 : : }
10445 : : else
10446 : 0 : set_overrun ();
10447 : : }
10448 : : break;
10449 : :
10450 : 6 : case tt_nttp_var:
10451 : : /* An NTTP object. */
10452 : 6 : {
10453 : 6 : tree init = tree_node ();
10454 : 6 : tree name = tree_node ();
10455 : 6 : if (!get_overrun ())
10456 : : {
10457 : : /* We don't want to check the initializer as that may require
10458 : : name lookup, which could recursively start lazy loading.
10459 : : Instead we know that INIT is already valid so we can just
10460 : : apply that directly. */
10461 : 6 : res = get_template_parm_object (init, name, /*check_init=*/false);
10462 : 6 : int tag = insert (res);
10463 : 6 : dump (dumper::TREE)
10464 : 0 : && dump ("Created nttp object:%d %N", tag, name);
10465 : : }
10466 : : }
10467 : : break;
10468 : :
10469 : 4671 : case tt_enum_value:
10470 : : /* An enum const value. */
10471 : 4671 : {
10472 : 4671 : if (tree decl = tree_node ())
10473 : : {
10474 : 4689 : dump (dumper::TREE) && dump ("Read enum value %N", decl);
10475 : 4671 : res = DECL_INITIAL (decl);
10476 : : }
10477 : :
10478 : 4671 : if (!res)
10479 : 0 : set_overrun ();
10480 : : }
10481 : : break;
10482 : :
10483 : 5788 : case tt_enum_decl:
10484 : : /* An enum decl. */
10485 : 5788 : {
10486 : 5788 : tree ctx = tree_node ();
10487 : 5788 : tree name = tree_node ();
10488 : :
10489 : 5788 : if (!get_overrun ()
10490 : 5788 : && TREE_CODE (ctx) == ENUMERAL_TYPE)
10491 : 5788 : res = find_enum_member (ctx, name);
10492 : :
10493 : 5788 : if (!res)
10494 : 0 : set_overrun ();
10495 : : else
10496 : : {
10497 : 5788 : int tag = insert (res);
10498 : 5788 : dump (dumper::TREE)
10499 : 18 : && dump ("Read enum decl:%d %C:%N", tag, TREE_CODE (res), res);
10500 : : }
10501 : : }
10502 : : break;
10503 : :
10504 : 5340 : case tt_data_member:
10505 : : /* A data member. */
10506 : 5340 : {
10507 : 5340 : tree ctx = tree_node ();
10508 : 5340 : tree name = tree_node ();
10509 : :
10510 : 5340 : if (!get_overrun ()
10511 : 5340 : && RECORD_OR_UNION_TYPE_P (ctx))
10512 : : {
10513 : 5340 : if (name)
10514 : 4609 : res = lookup_class_binding (ctx, name);
10515 : : else
10516 : 731 : res = lookup_field_ident (ctx, u ());
10517 : :
10518 : 5340 : if (!res
10519 : 5340 : || (TREE_CODE (res) != FIELD_DECL
10520 : 5340 : && TREE_CODE (res) != USING_DECL)
10521 : 10680 : || DECL_CONTEXT (res) != ctx)
10522 : : res = NULL_TREE;
10523 : : }
10524 : :
10525 : 5340 : if (!res)
10526 : 0 : set_overrun ();
10527 : : else
10528 : : {
10529 : 5340 : int tag = insert (res);
10530 : 5340 : dump (dumper::TREE)
10531 : 21 : && dump ("Read member:%d %C:%N", tag, TREE_CODE (res), res);
10532 : : }
10533 : : }
10534 : : break;
10535 : :
10536 : 974 : case tt_binfo:
10537 : : /* A BINFO. Walk the tree of the dominating type. */
10538 : 974 : {
10539 : 974 : tree type;
10540 : 974 : unsigned ix = binfo_mergeable (&type);
10541 : 974 : if (type)
10542 : : {
10543 : 974 : res = TYPE_BINFO (type);
10544 : 1037 : for (; ix && res; res = TREE_CHAIN (res))
10545 : 63 : ix--;
10546 : 974 : if (!res)
10547 : 0 : set_overrun ();
10548 : : }
10549 : :
10550 : 974 : if (get_overrun ())
10551 : : break;
10552 : :
10553 : : /* Insert binfo into backreferences. */
10554 : 974 : tag = insert (res);
10555 : 974 : dump (dumper::TREE) && dump ("Read binfo:%d %N", tag, res);
10556 : : }
10557 : 974 : break;
10558 : :
10559 : 9 : case tt_vtable:
10560 : 9 : {
10561 : 9 : unsigned ix = u ();
10562 : 9 : tree ctx = tree_node ();
10563 : 9 : dump (dumper::TREE) && dump ("Reading vtable %N[%u]", ctx, ix);
10564 : 9 : if (TREE_CODE (ctx) == RECORD_TYPE && TYPE_LANG_SPECIFIC (ctx))
10565 : 9 : for (res = CLASSTYPE_VTABLES (ctx); res; res = DECL_CHAIN (res))
10566 : 9 : if (!ix--)
10567 : : break;
10568 : 9 : if (!res)
10569 : 0 : set_overrun ();
10570 : : }
10571 : : break;
10572 : :
10573 : 0 : case tt_thunk:
10574 : 0 : {
10575 : 0 : int fixed = i ();
10576 : 0 : tree target = tree_node ();
10577 : 0 : tree virt = tree_node ();
10578 : :
10579 : 0 : for (tree thunk = DECL_THUNKS (target);
10580 : 0 : thunk; thunk = DECL_CHAIN (thunk))
10581 : 0 : if (THUNK_FIXED_OFFSET (thunk) == fixed
10582 : 0 : && !THUNK_VIRTUAL_OFFSET (thunk) == !virt
10583 : 0 : && (!virt
10584 : 0 : || tree_int_cst_equal (virt, THUNK_VIRTUAL_OFFSET (thunk))))
10585 : : {
10586 : : res = thunk;
10587 : : break;
10588 : : }
10589 : :
10590 : 0 : int tag = insert (res);
10591 : 0 : if (res)
10592 : 0 : dump (dumper::TREE)
10593 : 0 : && dump ("Read:%d thunk %N to %N", tag, DECL_NAME (res), target);
10594 : : else
10595 : 0 : set_overrun ();
10596 : : }
10597 : : break;
10598 : :
10599 : 104248 : case tt_clone_ref:
10600 : 104248 : {
10601 : 104248 : tree target = tree_node ();
10602 : 104248 : tree name = tree_node ();
10603 : :
10604 : 104248 : if (DECL_P (target) && DECL_MAYBE_IN_CHARGE_CDTOR_P (target))
10605 : : {
10606 : 104248 : tree clone;
10607 : 162909 : FOR_EVERY_CLONE (clone, target)
10608 : 162909 : if (DECL_NAME (clone) == name)
10609 : : {
10610 : : res = clone;
10611 : : break;
10612 : : }
10613 : : }
10614 : :
10615 : : /* A clone might have a different vtable entry. */
10616 : 104248 : if (res && DECL_VIRTUAL_P (res))
10617 : 6684 : DECL_VINDEX (res) = tree_node ();
10618 : :
10619 : 104248 : if (!res)
10620 : 0 : set_overrun ();
10621 : 104248 : int tag = insert (res);
10622 : 104248 : if (res)
10623 : 104248 : dump (dumper::TREE)
10624 : 222 : && dump ("Read:%d clone %N of %N", tag, DECL_NAME (res), target);
10625 : : else
10626 : 0 : set_overrun ();
10627 : : }
10628 : : break;
10629 : :
10630 : 774070 : case tt_entity:
10631 : : /* Index into the entity table. Perhaps not loaded yet! */
10632 : 774070 : {
10633 : 774070 : unsigned origin = state->slurp->remap_module (u ());
10634 : 774070 : unsigned ident = u ();
10635 : 774070 : module_state *from = (*modules)[origin];
10636 : :
10637 : 774070 : if (!origin || ident >= from->entity_num)
10638 : 0 : set_overrun ();
10639 : 774070 : if (!get_overrun ())
10640 : : {
10641 : 774070 : binding_slot *slot = &(*entity_ary)[from->entity_lwm + ident];
10642 : 774070 : if (slot->is_lazy ())
10643 : 24388 : if (!from->lazy_load (ident, slot))
10644 : 0 : set_overrun ();
10645 : 774070 : res = *slot;
10646 : : }
10647 : :
10648 : 774070 : if (res)
10649 : : {
10650 : 774070 : const char *kind = (origin != state->mod ? "Imported" : "Named");
10651 : 774070 : int tag = insert (res);
10652 : 774070 : dump (dumper::TREE)
10653 : 600 : && dump ("%s:%d %C:%N@%M", kind, tag, TREE_CODE (res),
10654 : 600 : res, (*modules)[origin]);
10655 : :
10656 : 774070 : if (!add_indirects (res))
10657 : : {
10658 : 0 : set_overrun ();
10659 : 0 : res = NULL_TREE;
10660 : : }
10661 : : }
10662 : : }
10663 : : break;
10664 : :
10665 : 2053 : case tt_template:
10666 : : /* A template. */
10667 : 2053 : if (tree tpl = tree_node ())
10668 : : {
10669 : 2053 : res = (TREE_CODE (tpl) == TU_LOCAL_ENTITY ?
10670 : 2053 : tpl : DECL_TEMPLATE_RESULT (tpl));
10671 : 2053 : dump (dumper::TREE)
10672 : 9 : && dump ("Read template %C:%N", TREE_CODE (res), res);
10673 : : }
10674 : : break;
10675 : : }
10676 : :
10677 : 66773120 : if (is_use && !unused && res && DECL_P (res) && !TREE_USED (res))
10678 : : {
10679 : : /* Mark decl used as mark_used does -- we cannot call
10680 : : mark_used in the middle of streaming, we only need a subset
10681 : : of its functionality. */
10682 : 451760 : TREE_USED (res) = true;
10683 : :
10684 : : /* And for structured bindings also the underlying decl. */
10685 : 451760 : if (DECL_DECOMPOSITION_P (res) && !DECL_DECOMP_IS_BASE (res))
10686 : 860 : TREE_USED (DECL_DECOMP_BASE (res)) = true;
10687 : :
10688 : 451760 : if (DECL_CLONED_FUNCTION_P (res))
10689 : 4209 : TREE_USED (DECL_CLONED_FUNCTION (res)) = true;
10690 : : }
10691 : :
10692 : 66773120 : dump.outdent ();
10693 : 66773120 : return res;
10694 : : }
10695 : :
10696 : : void
10697 : 1380071 : trees_out::tpl_parms (tree parms, unsigned &tpl_levels)
10698 : : {
10699 : 1380071 : if (!parms)
10700 : : return;
10701 : :
10702 : 928500 : if (TREE_VISITED (parms))
10703 : : {
10704 : 391218 : ref_node (parms);
10705 : 391218 : return;
10706 : : }
10707 : :
10708 : 537282 : tpl_parms (TREE_CHAIN (parms), tpl_levels);
10709 : :
10710 : 537282 : tree vec = TREE_VALUE (parms);
10711 : 537282 : unsigned len = TREE_VEC_LENGTH (vec);
10712 : : /* Depth. */
10713 : 537282 : int tag = insert (parms);
10714 : 537282 : if (streaming_p ())
10715 : : {
10716 : 145477 : i (len + 1);
10717 : 145543 : dump (dumper::TREE)
10718 : 66 : && dump ("Writing template parms:%d level:%N length:%d",
10719 : 66 : tag, TREE_PURPOSE (parms), len);
10720 : : }
10721 : 537282 : tree_node (TREE_PURPOSE (parms));
10722 : :
10723 : 1474216 : for (unsigned ix = 0; ix != len; ix++)
10724 : : {
10725 : 936934 : tree parm = TREE_VEC_ELT (vec, ix);
10726 : 936934 : tree decl = TREE_VALUE (parm);
10727 : :
10728 : 936934 : gcc_checking_assert (DECL_TEMPLATE_PARM_P (decl));
10729 : 936934 : if (CHECKING_P)
10730 : 936934 : switch (TREE_CODE (decl))
10731 : : {
10732 : 0 : default: gcc_unreachable ();
10733 : :
10734 : 2757 : case TEMPLATE_DECL:
10735 : 2757 : gcc_assert ((TREE_CODE (TREE_TYPE (decl)) == TEMPLATE_TEMPLATE_PARM)
10736 : : && (TREE_CODE (DECL_TEMPLATE_RESULT (decl)) == TYPE_DECL)
10737 : : && (TYPE_NAME (TREE_TYPE (decl)) == decl));
10738 : : break;
10739 : :
10740 : 876833 : case TYPE_DECL:
10741 : 876833 : gcc_assert ((TREE_CODE (TREE_TYPE (decl)) == TEMPLATE_TYPE_PARM)
10742 : : && (TYPE_NAME (TREE_TYPE (decl)) == decl));
10743 : : break;
10744 : :
10745 : 57344 : case PARM_DECL:
10746 : 57344 : gcc_assert ((TREE_CODE (DECL_INITIAL (decl)) == TEMPLATE_PARM_INDEX)
10747 : : && (TREE_CODE (TEMPLATE_PARM_DECL (DECL_INITIAL (decl)))
10748 : : == CONST_DECL)
10749 : : && (DECL_TEMPLATE_PARM_P
10750 : : (TEMPLATE_PARM_DECL (DECL_INITIAL (decl)))));
10751 : : break;
10752 : : }
10753 : :
10754 : 936934 : tree_node (decl);
10755 : 936934 : tree_node (TEMPLATE_PARM_CONSTRAINTS (parm));
10756 : : }
10757 : :
10758 : 537282 : tpl_levels++;
10759 : : }
10760 : :
10761 : : tree
10762 : 229854 : trees_in::tpl_parms (unsigned &tpl_levels)
10763 : : {
10764 : 229854 : tree parms = NULL_TREE;
10765 : :
10766 : 482553 : while (int len = i ())
10767 : : {
10768 : 252699 : if (len < 0)
10769 : : {
10770 : 136714 : parms = back_ref (len);
10771 : 136714 : continue;
10772 : : }
10773 : :
10774 : 115985 : len -= 1;
10775 : 115985 : parms = tree_cons (NULL_TREE, NULL_TREE, parms);
10776 : 115985 : int tag = insert (parms);
10777 : 115985 : TREE_PURPOSE (parms) = tree_node ();
10778 : :
10779 : 115985 : dump (dumper::TREE)
10780 : 105 : && dump ("Reading template parms:%d level:%N length:%d",
10781 : 105 : tag, TREE_PURPOSE (parms), len);
10782 : :
10783 : 115985 : tree vec = make_tree_vec (len);
10784 : 308129 : for (int ix = 0; ix != len; ix++)
10785 : : {
10786 : 192144 : tree decl = tree_node ();
10787 : 192144 : if (!decl)
10788 : : return NULL_TREE;
10789 : :
10790 : 192144 : tree parm = build_tree_list (NULL, decl);
10791 : 192144 : TEMPLATE_PARM_CONSTRAINTS (parm) = tree_node ();
10792 : :
10793 : 192144 : TREE_VEC_ELT (vec, ix) = parm;
10794 : : }
10795 : :
10796 : 115985 : TREE_VALUE (parms) = vec;
10797 : 115985 : tpl_levels++;
10798 : : }
10799 : :
10800 : : return parms;
10801 : : }
10802 : :
10803 : : void
10804 : 842789 : trees_out::tpl_parms_fini (tree tmpl, unsigned tpl_levels)
10805 : : {
10806 : 842789 : for (tree parms = DECL_TEMPLATE_PARMS (tmpl);
10807 : 1380071 : tpl_levels--; parms = TREE_CHAIN (parms))
10808 : : {
10809 : 537282 : tree vec = TREE_VALUE (parms);
10810 : :
10811 : 537282 : tree_node (TREE_TYPE (vec));
10812 : 1474216 : for (unsigned ix = TREE_VEC_LENGTH (vec); ix--;)
10813 : : {
10814 : 936934 : tree parm = TREE_VEC_ELT (vec, ix);
10815 : 936934 : tree dflt = TREE_PURPOSE (parm);
10816 : 936934 : tree_node (dflt);
10817 : :
10818 : : /* Template template parameters need a context of their owning
10819 : : template. This is quite tricky to infer correctly on stream-in
10820 : : (see PR c++/98881) so we'll just provide it directly. */
10821 : 936934 : tree decl = TREE_VALUE (parm);
10822 : 936934 : if (TREE_CODE (decl) == TEMPLATE_DECL)
10823 : 2757 : tree_node (DECL_CONTEXT (decl));
10824 : : }
10825 : : }
10826 : 842789 : }
10827 : :
10828 : : bool
10829 : 229854 : trees_in::tpl_parms_fini (tree tmpl, unsigned tpl_levels)
10830 : : {
10831 : 229854 : for (tree parms = DECL_TEMPLATE_PARMS (tmpl);
10832 : 345839 : tpl_levels--; parms = TREE_CHAIN (parms))
10833 : : {
10834 : 115985 : tree vec = TREE_VALUE (parms);
10835 : :
10836 : 115985 : TREE_TYPE (vec) = tree_node ();
10837 : 308129 : for (unsigned ix = TREE_VEC_LENGTH (vec); ix--;)
10838 : : {
10839 : 192144 : tree parm = TREE_VEC_ELT (vec, ix);
10840 : 192144 : tree dflt = tree_node ();
10841 : 192144 : TREE_PURPOSE (parm) = dflt;
10842 : :
10843 : 192144 : tree decl = TREE_VALUE (parm);
10844 : 192144 : if (TREE_CODE (decl) == TEMPLATE_DECL)
10845 : 612 : DECL_CONTEXT (decl) = tree_node ();
10846 : :
10847 : 192144 : if (get_overrun ())
10848 : : return false;
10849 : : }
10850 : : }
10851 : : return true;
10852 : : }
10853 : :
10854 : : /* PARMS is a LIST, one node per level.
10855 : : TREE_VALUE is a TREE_VEC of parm info for that level.
10856 : : each ELT is a TREE_LIST
10857 : : TREE_VALUE is PARM_DECL, TYPE_DECL or TEMPLATE_DECL
10858 : : TREE_PURPOSE is the default value. */
10859 : :
10860 : : void
10861 : 842789 : trees_out::tpl_header (tree tpl, unsigned *tpl_levels)
10862 : : {
10863 : 842789 : tree parms = DECL_TEMPLATE_PARMS (tpl);
10864 : 842789 : tpl_parms (parms, *tpl_levels);
10865 : :
10866 : : /* Mark end. */
10867 : 842789 : if (streaming_p ())
10868 : 280552 : u (0);
10869 : :
10870 : 842789 : if (*tpl_levels)
10871 : 506738 : tree_node (TEMPLATE_PARMS_CONSTRAINTS (parms));
10872 : 842789 : }
10873 : :
10874 : : bool
10875 : 229854 : trees_in::tpl_header (tree tpl, unsigned *tpl_levels)
10876 : : {
10877 : 229854 : tree parms = tpl_parms (*tpl_levels);
10878 : 229854 : if (!parms)
10879 : : return false;
10880 : :
10881 : 229854 : DECL_TEMPLATE_PARMS (tpl) = parms;
10882 : :
10883 : 229854 : if (*tpl_levels)
10884 : 114551 : TEMPLATE_PARMS_CONSTRAINTS (parms) = tree_node ();
10885 : :
10886 : : return true;
10887 : : }
10888 : :
10889 : : /* Stream skeleton parm nodes, with their flags, type & parm indices.
10890 : : All the parms will have consecutive tags. */
10891 : :
10892 : : void
10893 : 1109385 : trees_out::fn_parms_init (tree fn)
10894 : : {
10895 : : /* First init them. */
10896 : 1109385 : int base_tag = ref_num - 1;
10897 : 1109385 : int ix = 0;
10898 : 1109385 : for (tree parm = DECL_ARGUMENTS (fn);
10899 : 3371554 : parm; parm = DECL_CHAIN (parm), ix++)
10900 : : {
10901 : 2262169 : if (streaming_p ())
10902 : : {
10903 : 754022 : start (parm);
10904 : 754022 : tree_node_bools (parm);
10905 : : }
10906 : 2262169 : int tag = insert (parm);
10907 : 2262169 : gcc_checking_assert (base_tag - ix == tag);
10908 : : }
10909 : : /* Mark the end. */
10910 : 1109385 : if (streaming_p ())
10911 : 369962 : u (0);
10912 : :
10913 : : /* Now stream their contents. */
10914 : 1109385 : ix = 0;
10915 : 1109385 : for (tree parm = DECL_ARGUMENTS (fn);
10916 : 3371554 : parm; parm = DECL_CHAIN (parm), ix++)
10917 : : {
10918 : 2262169 : if (streaming_p ())
10919 : 754022 : dump (dumper::TREE)
10920 : 207 : && dump ("Writing parm:%d %u (%N) of %N",
10921 : : base_tag - ix, ix, parm, fn);
10922 : 2262169 : tree_node_vals (parm);
10923 : : }
10924 : :
10925 : 1109385 : if (!streaming_p ())
10926 : : {
10927 : : /* We must walk contract attrs so the dependency graph is complete. */
10928 : 739423 : for (tree contract = DECL_CONTRACTS (fn);
10929 : 739531 : contract;
10930 : 108 : contract = CONTRACT_CHAIN (contract))
10931 : 108 : tree_node (contract);
10932 : : }
10933 : :
10934 : : /* Write a reference to contracts pre/post functions, if any, to avoid
10935 : : regenerating them in importers. */
10936 : 1109385 : tree_node (DECL_PRE_FN (fn));
10937 : 1109385 : tree_node (DECL_POST_FN (fn));
10938 : 1109385 : }
10939 : :
10940 : : /* Build skeleton parm nodes, read their flags, type & parm indices. */
10941 : :
10942 : : int
10943 : 319895 : trees_in::fn_parms_init (tree fn)
10944 : : {
10945 : 319895 : int base_tag = ~(int)back_refs.length ();
10946 : :
10947 : 319895 : tree *parm_ptr = &DECL_ARGUMENTS (fn);
10948 : 319895 : int ix = 0;
10949 : 967267 : for (; int code = u (); ix++)
10950 : : {
10951 : 647372 : tree parm = start (code);
10952 : 647372 : if (!tree_node_bools (parm))
10953 : : return 0;
10954 : :
10955 : 647372 : int tag = insert (parm);
10956 : 647372 : gcc_checking_assert (base_tag - ix == tag);
10957 : 647372 : *parm_ptr = parm;
10958 : 647372 : parm_ptr = &DECL_CHAIN (parm);
10959 : 647372 : }
10960 : :
10961 : 319895 : ix = 0;
10962 : 319895 : for (tree parm = DECL_ARGUMENTS (fn);
10963 : 967267 : parm; parm = DECL_CHAIN (parm), ix++)
10964 : : {
10965 : 647372 : dump (dumper::TREE)
10966 : 351 : && dump ("Reading parm:%d %u (%N) of %N",
10967 : : base_tag - ix, ix, parm, fn);
10968 : 647372 : if (!tree_node_vals (parm))
10969 : : return 0;
10970 : : }
10971 : :
10972 : : /* Reload references to contract functions, if any. */
10973 : 319895 : tree pre_fn = tree_node ();
10974 : 319895 : tree post_fn = tree_node ();
10975 : 319895 : set_contract_functions (fn, pre_fn, post_fn);
10976 : :
10977 : 319895 : return base_tag;
10978 : : }
10979 : :
10980 : : /* Read the remaining parm node data. Replace with existing (if
10981 : : non-null) in the map. */
10982 : :
10983 : : void
10984 : 319895 : trees_in::fn_parms_fini (int tag, tree fn, tree existing, bool is_defn)
10985 : : {
10986 : 505555 : tree existing_parm = existing ? DECL_ARGUMENTS (existing) : NULL_TREE;
10987 : 319895 : tree parms = DECL_ARGUMENTS (fn);
10988 : 319895 : unsigned ix = 0;
10989 : 967267 : for (tree parm = parms; parm; parm = DECL_CHAIN (parm), ix++)
10990 : : {
10991 : 647372 : if (existing_parm)
10992 : : {
10993 : 550497 : if (is_defn && !DECL_SAVED_TREE (existing))
10994 : : {
10995 : : /* If we're about to become the definition, set the
10996 : : names of the parms from us. */
10997 : 13614 : DECL_NAME (existing_parm) = DECL_NAME (parm);
10998 : 13614 : DECL_SOURCE_LOCATION (existing_parm) = DECL_SOURCE_LOCATION (parm);
10999 : : }
11000 : :
11001 : 373433 : back_refs[~tag] = existing_parm;
11002 : 373433 : existing_parm = DECL_CHAIN (existing_parm);
11003 : : }
11004 : 647372 : tag--;
11005 : : }
11006 : 319895 : }
11007 : :
11008 : : /* Encode into KEY the position of the local type (class or enum)
11009 : : declaration DECL within FN. The position is encoded as the
11010 : : index of the innermost BLOCK (numbered in BFS order) along with
11011 : : the index within its BLOCK_VARS list. */
11012 : :
11013 : : void
11014 : 9846 : trees_out::key_local_type (merge_key& key, tree decl, tree fn)
11015 : : {
11016 : 9846 : auto_vec<tree, 4> blocks;
11017 : 9846 : blocks.quick_push (DECL_INITIAL (fn));
11018 : 9846 : unsigned block_ix = 0;
11019 : 45276 : while (block_ix != blocks.length ())
11020 : : {
11021 : 17715 : tree block = blocks[block_ix];
11022 : 17715 : unsigned decl_ix = 0;
11023 : 51282 : for (tree var = BLOCK_VARS (block); var; var = DECL_CHAIN (var))
11024 : : {
11025 : 43413 : if (TREE_CODE (var) != TYPE_DECL)
11026 : 26277 : continue;
11027 : 17136 : if (var == decl)
11028 : : {
11029 : 9846 : key.index = (block_ix << 10) | decl_ix;
11030 : 9846 : return;
11031 : : }
11032 : 7290 : ++decl_ix;
11033 : : }
11034 : 16431 : for (tree sub = BLOCK_SUBBLOCKS (block); sub; sub = BLOCK_CHAIN (sub))
11035 : 8562 : blocks.safe_push (sub);
11036 : 7869 : ++block_ix;
11037 : : }
11038 : :
11039 : : /* Not-found value. */
11040 : 0 : key.index = 1023;
11041 : 9846 : }
11042 : :
11043 : : /* Look up the local type corresponding at the position encoded by
11044 : : KEY within FN and named NAME. */
11045 : :
11046 : : tree
11047 : 2906 : trees_in::key_local_type (const merge_key& key, tree fn, tree name)
11048 : : {
11049 : 2906 : if (!DECL_INITIAL (fn))
11050 : : return NULL_TREE;
11051 : :
11052 : 1773 : const unsigned block_pos = key.index >> 10;
11053 : 1773 : const unsigned decl_pos = key.index & 1023;
11054 : :
11055 : 1773 : if (decl_pos == 1023)
11056 : : return NULL_TREE;
11057 : :
11058 : 1773 : auto_vec<tree, 4> blocks;
11059 : 1773 : blocks.quick_push (DECL_INITIAL (fn));
11060 : 1773 : unsigned block_ix = 0;
11061 : 8207 : while (block_ix != blocks.length ())
11062 : : {
11063 : 3217 : tree block = blocks[block_ix];
11064 : 3217 : if (block_ix == block_pos)
11065 : : {
11066 : 1773 : unsigned decl_ix = 0;
11067 : 4745 : for (tree var = BLOCK_VARS (block); var; var = DECL_CHAIN (var))
11068 : : {
11069 : 4745 : if (TREE_CODE (var) != TYPE_DECL)
11070 : 2052 : continue;
11071 : : /* Prefer using the identifier as the key for more robustness
11072 : : to ODR violations, except for anonymous types since their
11073 : : compiler-generated identifiers aren't stable. */
11074 : 5386 : if (IDENTIFIER_ANON_P (name)
11075 : 2693 : ? decl_ix == decl_pos
11076 : 302 : : DECL_NAME (var) == name)
11077 : : return var;
11078 : 920 : ++decl_ix;
11079 : : }
11080 : : return NULL_TREE;
11081 : : }
11082 : 2998 : for (tree sub = BLOCK_SUBBLOCKS (block); sub; sub = BLOCK_CHAIN (sub))
11083 : 1554 : blocks.safe_push (sub);
11084 : 1444 : ++block_ix;
11085 : : }
11086 : :
11087 : : return NULL_TREE;
11088 : 1773 : }
11089 : :
11090 : : /* DEP is the depset of some decl we're streaming by value. Determine
11091 : : the merging behaviour. */
11092 : :
11093 : : merge_kind
11094 : 2819865 : trees_out::get_merge_kind (tree decl, depset *dep)
11095 : : {
11096 : 2819865 : if (!dep)
11097 : : {
11098 : 526357 : if (VAR_OR_FUNCTION_DECL_P (decl))
11099 : : {
11100 : : /* Any var or function with template info should have DEP. */
11101 : 279094 : gcc_checking_assert (!DECL_LANG_SPECIFIC (decl)
11102 : : || !DECL_TEMPLATE_INFO (decl));
11103 : 279094 : if (DECL_LOCAL_DECL_P (decl))
11104 : : return MK_unique;
11105 : : }
11106 : :
11107 : : /* Either unique, or some member of a class that cannot have an
11108 : : out-of-class definition. For instance a FIELD_DECL. */
11109 : 526131 : tree ctx = CP_DECL_CONTEXT (decl);
11110 : 526131 : if (TREE_CODE (ctx) == FUNCTION_DECL)
11111 : : {
11112 : : /* USING_DECLs and NAMESPACE_DECLs cannot have DECL_TEMPLATE_INFO --
11113 : : this isn't permitting them to have one. */
11114 : 304802 : gcc_checking_assert (TREE_CODE (decl) == USING_DECL
11115 : : || TREE_CODE (decl) == NAMESPACE_DECL
11116 : : || !DECL_LANG_SPECIFIC (decl)
11117 : : || !DECL_TEMPLATE_INFO (decl));
11118 : :
11119 : : return MK_unique;
11120 : : }
11121 : :
11122 : 221329 : if (TREE_CODE (decl) == TEMPLATE_DECL
11123 : 221329 : && DECL_UNINSTANTIATED_TEMPLATE_FRIEND_P (decl))
11124 : : return MK_local_friend;
11125 : :
11126 : 221329 : gcc_checking_assert (TYPE_P (ctx));
11127 : 221329 : if (TREE_CODE (decl) == USING_DECL)
11128 : : return MK_field;
11129 : :
11130 : 140130 : if (TREE_CODE (decl) == FIELD_DECL)
11131 : : {
11132 : 98013 : if (DECL_NAME (decl))
11133 : : {
11134 : : /* Anonymous FIELD_DECLs have a NULL name. */
11135 : 76328 : gcc_checking_assert (!IDENTIFIER_ANON_P (DECL_NAME (decl)));
11136 : : return MK_named;
11137 : : }
11138 : :
11139 : 21685 : if (!DECL_NAME (decl)
11140 : 21685 : && !RECORD_OR_UNION_TYPE_P (TREE_TYPE (decl))
11141 : 22593 : && !DECL_BIT_FIELD_REPRESENTATIVE (decl))
11142 : : {
11143 : : /* The underlying storage unit for a bitfield. We do not
11144 : : need to dedup it, because it's only reachable through
11145 : : the bitfields it represents. And those are deduped. */
11146 : : // FIXME: Is that assertion correct -- do we ever fish it
11147 : : // out and put it in an expr?
11148 : 354 : gcc_checking_assert ((TREE_CODE (TREE_TYPE (decl)) == ARRAY_TYPE
11149 : : ? TREE_CODE (TREE_TYPE (TREE_TYPE (decl)))
11150 : : : TREE_CODE (TREE_TYPE (decl)))
11151 : : == INTEGER_TYPE);
11152 : : return MK_unique;
11153 : : }
11154 : :
11155 : : return MK_field;
11156 : : }
11157 : :
11158 : 42117 : if (TREE_CODE (decl) == CONST_DECL)
11159 : : return MK_named;
11160 : :
11161 : 6764 : if (TREE_CODE (decl) == VAR_DECL
11162 : 6764 : && DECL_VTABLE_OR_VTT_P (decl))
11163 : : return MK_vtable;
11164 : :
11165 : 1128 : if (DECL_THUNK_P (decl))
11166 : : /* Thunks are unique-enough, because they're only referenced
11167 : : from the vtable. And that's either new (so we want the
11168 : : thunks), or it's a duplicate (so it will be dropped). */
11169 : : return MK_unique;
11170 : :
11171 : : /* There should be no other cases. */
11172 : 0 : gcc_unreachable ();
11173 : : }
11174 : :
11175 : 2293508 : gcc_checking_assert (TREE_CODE (decl) != FIELD_DECL
11176 : : && TREE_CODE (decl) != USING_DECL
11177 : : && TREE_CODE (decl) != CONST_DECL);
11178 : :
11179 : 2293508 : if (is_key_order ())
11180 : : {
11181 : : /* When doing the mergeablilty graph, there's an indirection to
11182 : : the actual depset. */
11183 : 764373 : gcc_assert (dep->is_special ());
11184 : 764373 : dep = dep->deps[0];
11185 : : }
11186 : :
11187 : 2293508 : gcc_checking_assert (decl == dep->get_entity ());
11188 : :
11189 : 2293508 : merge_kind mk = MK_named;
11190 : 2293508 : switch (dep->get_entity_kind ())
11191 : : {
11192 : 0 : default:
11193 : 0 : gcc_unreachable ();
11194 : :
11195 : : case depset::EK_PARTIAL:
11196 : : mk = MK_partial;
11197 : : break;
11198 : :
11199 : 1315793 : case depset::EK_DECL:
11200 : 1315793 : {
11201 : 1315793 : tree ctx = CP_DECL_CONTEXT (decl);
11202 : :
11203 : 1315793 : switch (TREE_CODE (ctx))
11204 : : {
11205 : 0 : default:
11206 : 0 : gcc_unreachable ();
11207 : :
11208 : 9846 : case FUNCTION_DECL:
11209 : 9846 : gcc_checking_assert
11210 : : (DECL_IMPLICIT_TYPEDEF_P (STRIP_TEMPLATE (decl)));
11211 : :
11212 : : mk = MK_local_type;
11213 : : break;
11214 : :
11215 : 1305947 : case RECORD_TYPE:
11216 : 1305947 : case UNION_TYPE:
11217 : 1305947 : case NAMESPACE_DECL:
11218 : 1305947 : if (DECL_NAME (decl) == as_base_identifier)
11219 : : {
11220 : : mk = MK_as_base;
11221 : : break;
11222 : : }
11223 : :
11224 : : /* A lambda may have a class as its context, even though it
11225 : : isn't a member in the traditional sense; see the test
11226 : : g++.dg/modules/lambda-6_a.C. */
11227 : 1676722 : if (DECL_IMPLICIT_TYPEDEF_P (STRIP_TEMPLATE (decl))
11228 : 1440889 : && LAMBDA_TYPE_P (TREE_TYPE (decl)))
11229 : : {
11230 : 559 : if (tree scope = LAMBDA_TYPE_EXTRA_SCOPE (TREE_TYPE (decl)))
11231 : : {
11232 : : /* Lambdas attached to fields are keyed to its class. */
11233 : 541 : if (TREE_CODE (scope) == FIELD_DECL)
11234 : 9 : scope = TYPE_NAME (DECL_CONTEXT (scope));
11235 : 541 : if (DECL_LANG_SPECIFIC (scope)
11236 : 1082 : && DECL_MODULE_KEYED_DECLS_P (scope))
11237 : : {
11238 : : mk = MK_keyed;
11239 : : break;
11240 : : }
11241 : : }
11242 : : /* Lambdas not attached to any mangling scope are TU-local. */
11243 : : mk = MK_unique;
11244 : : break;
11245 : : }
11246 : :
11247 : 1243645 : if (TREE_CODE (decl) == TEMPLATE_DECL
11248 : 1243645 : && DECL_UNINSTANTIATED_TEMPLATE_FRIEND_P (decl))
11249 : : {
11250 : : mk = MK_local_friend;
11251 : : break;
11252 : : }
11253 : :
11254 : 1229623 : if (DECL_DECOMPOSITION_P (decl))
11255 : : {
11256 : : mk = MK_unique;
11257 : : break;
11258 : : }
11259 : :
11260 : 1229146 : if (IDENTIFIER_ANON_P (DECL_NAME (decl)))
11261 : : {
11262 : 20932 : if (RECORD_OR_UNION_TYPE_P (ctx))
11263 : : mk = MK_field;
11264 : 1008 : else if (DECL_IMPLICIT_TYPEDEF_P (decl)
11265 : 1008 : && UNSCOPED_ENUM_P (TREE_TYPE (decl))
11266 : 2016 : && TYPE_VALUES (TREE_TYPE (decl)))
11267 : : /* Keyed by first enum value, and underlying type. */
11268 : : mk = MK_enum;
11269 : : else
11270 : : /* No way to merge it, it is an ODR land-mine. */
11271 : : mk = MK_unique;
11272 : : }
11273 : : }
11274 : : }
11275 : : break;
11276 : :
11277 : 932406 : case depset::EK_SPECIALIZATION:
11278 : 932406 : {
11279 : 932406 : gcc_checking_assert (dep->is_special ());
11280 : :
11281 : 932406 : if (TREE_CODE (DECL_CONTEXT (decl)) == FUNCTION_DECL)
11282 : : /* An block-scope classes of templates are themselves
11283 : : templates. */
11284 : 4140 : gcc_checking_assert (DECL_IMPLICIT_TYPEDEF_P (decl));
11285 : :
11286 : 932406 : if (dep->is_friend_spec ())
11287 : : mk = MK_friend_spec;
11288 : 932406 : else if (dep->is_type_spec ())
11289 : : mk = MK_type_spec;
11290 : : else
11291 : 647637 : mk = MK_decl_spec;
11292 : :
11293 : 932406 : if (TREE_CODE (decl) == TEMPLATE_DECL)
11294 : : {
11295 : 65196 : spec_entry *entry = reinterpret_cast <spec_entry *> (dep->deps[0]);
11296 : 65196 : if (TREE_CODE (entry->spec) != TEMPLATE_DECL)
11297 : 6999 : mk = merge_kind (mk | MK_tmpl_tmpl_mask);
11298 : : }
11299 : : }
11300 : : break;
11301 : : }
11302 : :
11303 : : return mk;
11304 : : }
11305 : :
11306 : :
11307 : : /* The container of DECL -- not necessarily its context! */
11308 : :
11309 : : tree
11310 : 2819865 : trees_out::decl_container (tree decl)
11311 : : {
11312 : 2819865 : int use_tpl;
11313 : 2819865 : tree tpl = NULL_TREE;
11314 : 2819865 : if (tree template_info = node_template_info (decl, use_tpl))
11315 : 981959 : tpl = TI_TEMPLATE (template_info);
11316 : 2819865 : if (tpl == decl)
11317 : 0 : tpl = nullptr;
11318 : :
11319 : : /* Stream the template we're instantiated from. */
11320 : 2819865 : tree_node (tpl);
11321 : :
11322 : 2819865 : tree container = NULL_TREE;
11323 : 2819865 : if (TREE_CODE (decl) == TEMPLATE_DECL
11324 : 2819865 : && DECL_UNINSTANTIATED_TEMPLATE_FRIEND_P (decl))
11325 : 14022 : container = DECL_CHAIN (decl);
11326 : : else
11327 : 2805843 : container = CP_DECL_CONTEXT (decl);
11328 : :
11329 : 2819865 : if (TYPE_P (container))
11330 : 1650693 : container = TYPE_NAME (container);
11331 : :
11332 : 2819865 : tree_node (container);
11333 : :
11334 : 2819865 : return container;
11335 : : }
11336 : :
11337 : : tree
11338 : 843825 : trees_in::decl_container ()
11339 : : {
11340 : : /* The maybe-template. */
11341 : 843825 : (void)tree_node ();
11342 : :
11343 : 843825 : tree container = tree_node ();
11344 : :
11345 : 843825 : return container;
11346 : : }
11347 : :
11348 : : /* Write out key information about a mergeable DEP. Does not write
11349 : : the contents of DEP itself. The context has already been
11350 : : written. The container has already been streamed. */
11351 : :
11352 : : void
11353 : 2819865 : trees_out::key_mergeable (int tag, merge_kind mk, tree decl, tree inner,
11354 : : tree container, depset *dep)
11355 : : {
11356 : 2819865 : if (dep && is_key_order ())
11357 : : {
11358 : 764373 : gcc_checking_assert (dep->is_special ());
11359 : 764373 : dep = dep->deps[0];
11360 : : }
11361 : :
11362 : 2819865 : if (streaming_p ())
11363 : 1022354 : dump (dumper::MERGE)
11364 : 798 : && dump ("Writing:%d's %s merge key (%s) %C:%N", tag, merge_kind_name[mk],
11365 : 723 : dep ? dep->entity_kind_name () : "contained",
11366 : 798 : TREE_CODE (decl), decl);
11367 : :
11368 : : /* Now write the locating information. */
11369 : 2819865 : if (mk & MK_template_mask)
11370 : : {
11371 : : /* Specializations are located via their originating template,
11372 : : and the set of template args they specialize. */
11373 : 932406 : gcc_checking_assert (dep && dep->is_special ());
11374 : 932406 : spec_entry *entry = reinterpret_cast <spec_entry *> (dep->deps[0]);
11375 : :
11376 : 932406 : tree_node (entry->tmpl);
11377 : 932406 : tree_node (entry->args);
11378 : 932406 : if (mk & MK_tmpl_decl_mask)
11379 : 647637 : if (flag_concepts && TREE_CODE (inner) == VAR_DECL)
11380 : : {
11381 : : /* Variable template partial specializations might need
11382 : : constraints (see spec_hasher::equal). It's simpler to
11383 : : write NULL when we don't need them. */
11384 : 12993 : tree constraints = NULL_TREE;
11385 : :
11386 : 12993 : if (uses_template_parms (entry->args))
11387 : 396 : constraints = get_constraints (inner);
11388 : 12993 : tree_node (constraints);
11389 : : }
11390 : :
11391 : 932406 : if (CHECKING_P)
11392 : : {
11393 : : /* Make sure we can locate the decl. */
11394 : 932406 : tree existing = match_mergeable_specialization
11395 : 932406 : (bool (mk & MK_tmpl_decl_mask), entry);
11396 : :
11397 : 932406 : gcc_assert (existing);
11398 : 932406 : if (mk & MK_tmpl_decl_mask)
11399 : : {
11400 : 647637 : if (mk & MK_tmpl_tmpl_mask)
11401 : 5559 : existing = DECL_TI_TEMPLATE (existing);
11402 : : }
11403 : : else
11404 : : {
11405 : 284769 : if (mk & MK_tmpl_tmpl_mask)
11406 : 1440 : existing = CLASSTYPE_TI_TEMPLATE (existing);
11407 : : else
11408 : 283329 : existing = TYPE_NAME (existing);
11409 : : }
11410 : :
11411 : : /* The walkabout should have found ourselves. */
11412 : 932406 : gcc_checking_assert (TREE_CODE (decl) == TYPE_DECL
11413 : : ? same_type_p (TREE_TYPE (decl),
11414 : : TREE_TYPE (existing))
11415 : : : existing == decl);
11416 : : }
11417 : : }
11418 : 1887459 : else if (mk != MK_unique)
11419 : : {
11420 : 1580454 : merge_key key;
11421 : 1580454 : tree name = DECL_NAME (decl);
11422 : :
11423 : 1580454 : switch (mk)
11424 : : {
11425 : 0 : default:
11426 : 0 : gcc_unreachable ();
11427 : :
11428 : 1319895 : case MK_named:
11429 : 1319895 : case MK_friend_spec:
11430 : 1319895 : if (IDENTIFIER_CONV_OP_P (name))
11431 : 4471 : name = conv_op_identifier;
11432 : :
11433 : 1319895 : if (TREE_CODE (inner) == FUNCTION_DECL)
11434 : : {
11435 : : /* Functions are distinguished by parameter types. */
11436 : 703310 : tree fn_type = TREE_TYPE (inner);
11437 : :
11438 : 703310 : key.ref_q = type_memfn_rqual (fn_type);
11439 : 703310 : key.args = TYPE_ARG_TYPES (fn_type);
11440 : :
11441 : 703310 : if (tree reqs = get_constraints (inner))
11442 : : {
11443 : 46197 : if (cxx_dialect < cxx20)
11444 : 42 : reqs = CI_ASSOCIATED_CONSTRAINTS (reqs);
11445 : : else
11446 : 92352 : reqs = CI_DECLARATOR_REQS (reqs);
11447 : 46197 : key.constraints = reqs;
11448 : : }
11449 : :
11450 : 703310 : if (IDENTIFIER_CONV_OP_P (name)
11451 : 703310 : || (decl != inner
11452 : 376824 : && !(name == fun_identifier
11453 : : /* In case the user names something _FUN */
11454 : 66 : && LAMBDA_TYPE_P (DECL_CONTEXT (inner)))))
11455 : : /* And a function template, or conversion operator needs
11456 : : the return type. Except for the _FUN thunk of a
11457 : : generic lambda, which has a recursive decl_type'd
11458 : : return type. */
11459 : : // FIXME: What if the return type is a voldemort?
11460 : 381262 : key.ret = fndecl_declared_return_type (inner);
11461 : : }
11462 : : break;
11463 : :
11464 : 122454 : case MK_field:
11465 : 122454 : {
11466 : 122454 : unsigned ix = 0;
11467 : 122454 : if (TREE_CODE (inner) != FIELD_DECL)
11468 : : name = NULL_TREE;
11469 : : else
11470 : 21331 : gcc_checking_assert (!name || !IDENTIFIER_ANON_P (name));
11471 : :
11472 : 122454 : for (tree field = TYPE_FIELDS (TREE_TYPE (container));
11473 : 2901303 : ; field = DECL_CHAIN (field))
11474 : : {
11475 : 3023757 : tree finner = STRIP_TEMPLATE (field);
11476 : 3023757 : if (TREE_CODE (finner) == TREE_CODE (inner))
11477 : : {
11478 : 1023686 : if (finner == inner)
11479 : : break;
11480 : 901232 : ix++;
11481 : : }
11482 : 2901303 : }
11483 : 122454 : key.index = ix;
11484 : : }
11485 : 122454 : break;
11486 : :
11487 : 5636 : case MK_vtable:
11488 : 5636 : {
11489 : 5636 : tree vtable = CLASSTYPE_VTABLES (TREE_TYPE (container));
11490 : 7196 : for (unsigned ix = 0; ; vtable = DECL_CHAIN (vtable), ix++)
11491 : 7196 : if (vtable == decl)
11492 : : {
11493 : 5636 : key.index = ix;
11494 : 5636 : break;
11495 : : }
11496 : 5636 : name = NULL_TREE;
11497 : : }
11498 : 5636 : break;
11499 : :
11500 : 61743 : case MK_as_base:
11501 : 61743 : gcc_checking_assert
11502 : : (decl == TYPE_NAME (CLASSTYPE_AS_BASE (TREE_TYPE (container))));
11503 : : break;
11504 : :
11505 : 14022 : case MK_local_friend:
11506 : 14022 : {
11507 : : /* Find by index on the class's DECL_LIST */
11508 : 14022 : unsigned ix = 0;
11509 : 14022 : for (tree decls = CLASSTYPE_DECL_LIST (TREE_CHAIN (decl));
11510 : 383121 : decls; decls = TREE_CHAIN (decls))
11511 : 383121 : if (!TREE_PURPOSE (decls))
11512 : : {
11513 : 54531 : tree frnd = friend_from_decl_list (TREE_VALUE (decls));
11514 : 54531 : if (frnd == decl)
11515 : : break;
11516 : 40509 : ix++;
11517 : : }
11518 : 14022 : key.index = ix;
11519 : 14022 : name = NULL_TREE;
11520 : : }
11521 : 14022 : break;
11522 : :
11523 : 9846 : case MK_local_type:
11524 : 9846 : key_local_type (key, STRIP_TEMPLATE (decl), container);
11525 : 9846 : break;
11526 : :
11527 : 1008 : case MK_enum:
11528 : 1008 : {
11529 : : /* Anonymous enums are located by their first identifier,
11530 : : and underlying type. */
11531 : 1008 : tree type = TREE_TYPE (decl);
11532 : :
11533 : 1008 : gcc_checking_assert (UNSCOPED_ENUM_P (type));
11534 : : /* Using the type name drops the bit precision we might
11535 : : have been using on the enum. */
11536 : 1008 : key.ret = TYPE_NAME (ENUM_UNDERLYING_TYPE (type));
11537 : 1008 : if (tree values = TYPE_VALUES (type))
11538 : 1008 : name = DECL_NAME (TREE_VALUE (values));
11539 : : }
11540 : : break;
11541 : :
11542 : 541 : case MK_keyed:
11543 : 541 : {
11544 : 1082 : gcc_checking_assert (LAMBDA_TYPE_P (TREE_TYPE (inner)));
11545 : 541 : tree scope = LAMBDA_TYPE_EXTRA_SCOPE (TREE_TYPE (inner));
11546 : 541 : gcc_checking_assert (TREE_CODE (scope) == VAR_DECL
11547 : : || TREE_CODE (scope) == FIELD_DECL
11548 : : || TREE_CODE (scope) == PARM_DECL
11549 : : || TREE_CODE (scope) == TYPE_DECL
11550 : : || TREE_CODE (scope) == CONCEPT_DECL);
11551 : : /* Lambdas attached to fields are keyed to the class. */
11552 : 541 : if (TREE_CODE (scope) == FIELD_DECL)
11553 : 9 : scope = TYPE_NAME (DECL_CONTEXT (scope));
11554 : 541 : auto *root = keyed_table->get (scope);
11555 : 541 : unsigned ix = root->length ();
11556 : : /* If we don't find it, we'll write a really big number
11557 : : that the reader will ignore. */
11558 : 559 : while (ix--)
11559 : 559 : if ((*root)[ix] == inner)
11560 : : break;
11561 : :
11562 : : /* Use the keyed-to decl as the 'name'. */
11563 : 541 : name = scope;
11564 : 541 : key.index = ix;
11565 : : }
11566 : 541 : break;
11567 : :
11568 : 45309 : case MK_partial:
11569 : 45309 : {
11570 : 45309 : tree ti = get_template_info (inner);
11571 : 45309 : key.constraints = get_constraints (inner);
11572 : 45309 : key.ret = TI_TEMPLATE (ti);
11573 : 45309 : key.args = TI_ARGS (ti);
11574 : : }
11575 : 45309 : break;
11576 : : }
11577 : :
11578 : 1580454 : tree_node (name);
11579 : 1580454 : if (streaming_p ())
11580 : : {
11581 : 563317 : unsigned code = (key.ref_q << 0) | (key.index << 2);
11582 : 563317 : u (code);
11583 : : }
11584 : :
11585 : 1580454 : if (mk == MK_enum)
11586 : 1008 : tree_node (key.ret);
11587 : 1579446 : else if (mk == MK_partial
11588 : 1534137 : || (mk == MK_named && inner
11589 : 1319895 : && TREE_CODE (inner) == FUNCTION_DECL))
11590 : : {
11591 : 748619 : tree_node (key.ret);
11592 : 748619 : tree arg = key.args;
11593 : 748619 : if (mk == MK_named)
11594 : 2053687 : while (arg && arg != void_list_node)
11595 : : {
11596 : 1350377 : tree_node (TREE_VALUE (arg));
11597 : 1350377 : arg = TREE_CHAIN (arg);
11598 : : }
11599 : 748619 : tree_node (arg);
11600 : 748619 : tree_node (key.constraints);
11601 : : }
11602 : : }
11603 : 2819865 : }
11604 : :
11605 : : /* DECL is a new declaration that may be duplicated in OVL. Use RET &
11606 : : ARGS to find its clone, or NULL. If DECL's DECL_NAME is NULL, this
11607 : : has been found by a proxy. It will be an enum type located by its
11608 : : first member.
11609 : :
11610 : : We're conservative with matches, so ambiguous decls will be
11611 : : registered as different, then lead to a lookup error if the two
11612 : : modules are both visible. Perhaps we want to do something similar
11613 : : to duplicate decls to get ODR errors on loading? We already have
11614 : : some special casing for namespaces. */
11615 : :
11616 : : static tree
11617 : 258051 : check_mergeable_decl (merge_kind mk, tree decl, tree ovl, merge_key const &key)
11618 : : {
11619 : 258051 : tree found = NULL_TREE;
11620 : 1060481 : for (ovl_iterator iter (ovl); !found && iter; ++iter)
11621 : : {
11622 : 513888 : tree match = *iter;
11623 : :
11624 : 513888 : tree d_inner = decl;
11625 : 513888 : tree m_inner = match;
11626 : :
11627 : 596689 : again:
11628 : 596689 : if (TREE_CODE (d_inner) != TREE_CODE (m_inner))
11629 : : {
11630 : 78705 : if (TREE_CODE (match) == NAMESPACE_DECL
11631 : 78705 : && !DECL_NAMESPACE_ALIAS (match))
11632 : : /* Namespaces are never overloaded. */
11633 : : found = match;
11634 : :
11635 : 78705 : continue;
11636 : : }
11637 : :
11638 : 517984 : switch (TREE_CODE (d_inner))
11639 : : {
11640 : 170211 : case TEMPLATE_DECL:
11641 : 170211 : if (template_heads_equivalent_p (d_inner, m_inner))
11642 : : {
11643 : 82801 : d_inner = DECL_TEMPLATE_RESULT (d_inner);
11644 : 82801 : m_inner = DECL_TEMPLATE_RESULT (m_inner);
11645 : 82801 : if (d_inner == error_mark_node
11646 : 82801 : && TYPE_DECL_ALIAS_P (m_inner))
11647 : : {
11648 : : found = match;
11649 : : break;
11650 : : }
11651 : 82801 : goto again;
11652 : : }
11653 : : break;
11654 : :
11655 : 254131 : case FUNCTION_DECL:
11656 : 254131 : if (tree m_type = TREE_TYPE (m_inner))
11657 : 254131 : if ((!key.ret
11658 : 132183 : || same_type_p (key.ret, fndecl_declared_return_type (m_inner)))
11659 : 237367 : && type_memfn_rqual (m_type) == key.ref_q
11660 : 237107 : && compparms (key.args, TYPE_ARG_TYPES (m_type))
11661 : : /* Reject if old is a "C" builtin and new is not "C".
11662 : : Matches decls_match behaviour. */
11663 : 121569 : && (!DECL_IS_UNDECLARED_BUILTIN (m_inner)
11664 : 5088 : || !DECL_EXTERN_C_P (m_inner)
11665 : 4962 : || DECL_EXTERN_C_P (d_inner))
11666 : : /* Reject if one is a different member of a
11667 : : guarded/pre/post fn set. */
11668 : 121564 : && (!flag_contracts
11669 : 346 : || (DECL_IS_PRE_FN_P (d_inner)
11670 : 173 : == DECL_IS_PRE_FN_P (m_inner)))
11671 : 375695 : && (!flag_contracts
11672 : 346 : || (DECL_IS_POST_FN_P (d_inner)
11673 : 173 : == DECL_IS_POST_FN_P (m_inner))))
11674 : : {
11675 : 121564 : tree m_reqs = get_constraints (m_inner);
11676 : 121564 : if (m_reqs)
11677 : : {
11678 : 8255 : if (cxx_dialect < cxx20)
11679 : 8 : m_reqs = CI_ASSOCIATED_CONSTRAINTS (m_reqs);
11680 : : else
11681 : 16502 : m_reqs = CI_DECLARATOR_REQS (m_reqs);
11682 : : }
11683 : :
11684 : 121564 : if (cp_tree_equal (key.constraints, m_reqs))
11685 : 156302 : found = match;
11686 : : }
11687 : : break;
11688 : :
11689 : 58846 : case TYPE_DECL:
11690 : 117692 : if (DECL_IMPLICIT_TYPEDEF_P (d_inner)
11691 : 58846 : == DECL_IMPLICIT_TYPEDEF_P (m_inner))
11692 : : {
11693 : 58820 : if (!IDENTIFIER_ANON_P (DECL_NAME (m_inner)))
11694 : 58693 : return match;
11695 : 127 : else if (mk == MK_enum
11696 : 127 : && (TYPE_NAME (ENUM_UNDERLYING_TYPE (TREE_TYPE (m_inner)))
11697 : 127 : == key.ret))
11698 : : found = match;
11699 : : }
11700 : : break;
11701 : :
11702 : : default:
11703 : : found = match;
11704 : : break;
11705 : : }
11706 : : }
11707 : :
11708 : 199358 : return found;
11709 : : }
11710 : :
11711 : : /* DECL, INNER & TYPE are a skeleton set of nodes for a decl. Only
11712 : : the bools have been filled in. Read its merging key and merge it.
11713 : : Returns the existing decl if there is one. */
11714 : :
11715 : : tree
11716 : 843825 : trees_in::key_mergeable (int tag, merge_kind mk, tree decl, tree inner,
11717 : : tree type, tree container, bool is_attached,
11718 : : bool is_imported_temploid_friend)
11719 : : {
11720 : 843825 : const char *kind = "new";
11721 : 843825 : tree existing = NULL_TREE;
11722 : :
11723 : 843825 : if (mk & MK_template_mask)
11724 : : {
11725 : : // FIXME: We could stream the specialization hash?
11726 : 258747 : spec_entry spec;
11727 : 258747 : spec.tmpl = tree_node ();
11728 : 258747 : spec.args = tree_node ();
11729 : :
11730 : 258747 : if (get_overrun ())
11731 : 0 : return error_mark_node;
11732 : :
11733 : 258747 : DECL_NAME (decl) = DECL_NAME (spec.tmpl);
11734 : 258747 : DECL_CONTEXT (decl) = DECL_CONTEXT (spec.tmpl);
11735 : 258747 : DECL_NAME (inner) = DECL_NAME (decl);
11736 : 258747 : DECL_CONTEXT (inner) = DECL_CONTEXT (decl);
11737 : :
11738 : 258747 : tree constr = NULL_TREE;
11739 : 258747 : bool is_decl = mk & MK_tmpl_decl_mask;
11740 : 258747 : if (is_decl)
11741 : : {
11742 : 183726 : if (flag_concepts && TREE_CODE (inner) == VAR_DECL)
11743 : : {
11744 : 3805 : constr = tree_node ();
11745 : 3805 : if (constr)
11746 : 0 : set_constraints (inner, constr);
11747 : : }
11748 : 365751 : spec.spec = (mk & MK_tmpl_tmpl_mask) ? inner : decl;
11749 : : }
11750 : : else
11751 : 75021 : spec.spec = type;
11752 : 258747 : existing = match_mergeable_specialization (is_decl, &spec);
11753 : 258747 : if (constr)
11754 : : /* We'll add these back later, if this is the new decl. */
11755 : 0 : remove_constraints (inner);
11756 : :
11757 : 258747 : if (!existing)
11758 : : ; /* We'll add to the table once read. */
11759 : 142616 : else if (mk & MK_tmpl_decl_mask)
11760 : : {
11761 : : /* A declaration specialization. */
11762 : 98664 : if (mk & MK_tmpl_tmpl_mask)
11763 : 1024 : existing = DECL_TI_TEMPLATE (existing);
11764 : : }
11765 : : else
11766 : : {
11767 : : /* A type specialization. */
11768 : 43952 : if (mk & MK_tmpl_tmpl_mask)
11769 : 231 : existing = CLASSTYPE_TI_TEMPLATE (existing);
11770 : : else
11771 : 43721 : existing = TYPE_NAME (existing);
11772 : : }
11773 : : }
11774 : 585078 : else if (mk == MK_unique)
11775 : : kind = "unique";
11776 : : else
11777 : : {
11778 : 451231 : tree name = tree_node ();
11779 : :
11780 : 451231 : merge_key key;
11781 : 451231 : unsigned code = u ();
11782 : 451231 : key.ref_q = cp_ref_qualifier ((code >> 0) & 3);
11783 : 451231 : key.index = code >> 2;
11784 : :
11785 : 451231 : if (mk == MK_enum)
11786 : 219 : key.ret = tree_node ();
11787 : 451012 : else if (mk == MK_partial
11788 : 441647 : || ((mk == MK_named || mk == MK_friend_spec)
11789 : 372849 : && TREE_CODE (inner) == FUNCTION_DECL))
11790 : : {
11791 : 210759 : key.ret = tree_node ();
11792 : 210759 : tree arg, *arg_ptr = &key.args;
11793 : 210759 : while ((arg = tree_node ())
11794 : 593730 : && arg != void_list_node
11795 : 988666 : && mk != MK_partial)
11796 : : {
11797 : 384271 : *arg_ptr = tree_cons (NULL_TREE, arg, NULL_TREE);
11798 : 384271 : arg_ptr = &TREE_CHAIN (*arg_ptr);
11799 : : }
11800 : 210759 : *arg_ptr = arg;
11801 : 210759 : key.constraints = tree_node ();
11802 : : }
11803 : :
11804 : 451231 : if (get_overrun ())
11805 : 0 : return error_mark_node;
11806 : :
11807 : 451231 : if (mk < MK_indirect_lwm)
11808 : : {
11809 : 443440 : DECL_NAME (decl) = name;
11810 : 443440 : DECL_CONTEXT (decl) = FROB_CONTEXT (container);
11811 : : }
11812 : 451231 : DECL_NAME (inner) = DECL_NAME (decl);
11813 : 451231 : DECL_CONTEXT (inner) = DECL_CONTEXT (decl);
11814 : :
11815 : 451231 : if (mk == MK_partial)
11816 : : {
11817 : 9365 : for (tree spec = DECL_TEMPLATE_SPECIALIZATIONS (key.ret);
11818 : 49352 : spec; spec = TREE_CHAIN (spec))
11819 : : {
11820 : 46012 : tree tmpl = TREE_VALUE (spec);
11821 : 46012 : tree ti = get_template_info (tmpl);
11822 : 46012 : if (template_args_equal (key.args, TI_ARGS (ti))
11823 : 52631 : && cp_tree_equal (key.constraints,
11824 : : get_constraints
11825 : 6619 : (DECL_TEMPLATE_RESULT (tmpl))))
11826 : : {
11827 : : existing = tmpl;
11828 : : break;
11829 : : }
11830 : : }
11831 : : }
11832 : 441866 : else if (mk == MK_keyed
11833 : 213 : && DECL_LANG_SPECIFIC (name)
11834 : 442079 : && DECL_MODULE_KEYED_DECLS_P (name))
11835 : : {
11836 : 213 : gcc_checking_assert (TREE_CODE (container) == NAMESPACE_DECL
11837 : : || TREE_CODE (container) == TYPE_DECL);
11838 : 213 : if (auto *set = keyed_table->get (name))
11839 : 451369 : if (key.index < set->length ())
11840 : : {
11841 : 138 : existing = (*set)[key.index];
11842 : 138 : if (existing)
11843 : : {
11844 : 138 : gcc_checking_assert
11845 : : (DECL_IMPLICIT_TYPEDEF_P (existing));
11846 : 138 : if (inner != decl)
11847 : 103 : existing
11848 : 103 : = CLASSTYPE_TI_TEMPLATE (TREE_TYPE (existing));
11849 : : }
11850 : : }
11851 : : }
11852 : : else
11853 : 441653 : switch (TREE_CODE (container))
11854 : : {
11855 : 0 : default:
11856 : 0 : gcc_unreachable ();
11857 : :
11858 : 115087 : case NAMESPACE_DECL:
11859 : 115087 : if (is_attached
11860 : 115087 : && !is_imported_temploid_friend
11861 : 115087 : && !(state->is_module () || state->is_partition ()))
11862 : : kind = "unique";
11863 : : else
11864 : : {
11865 : 113418 : gcc_checking_assert (mk == MK_named || mk == MK_enum);
11866 : 113418 : tree mvec;
11867 : 113418 : tree *vslot = mergeable_namespace_slots (container, name,
11868 : : is_attached, &mvec);
11869 : 113418 : existing = check_mergeable_decl (mk, decl, *vslot, key);
11870 : 113418 : if (!existing)
11871 : 42622 : add_mergeable_namespace_entity (vslot, decl);
11872 : : else
11873 : : {
11874 : : /* Note that we now have duplicates to deal with in
11875 : : name lookup. */
11876 : 70796 : if (is_attached)
11877 : 33 : BINDING_VECTOR_PARTITION_DUPS_P (mvec) = true;
11878 : : else
11879 : 70763 : BINDING_VECTOR_GLOBAL_DUPS_P (mvec) = true;
11880 : : }
11881 : : }
11882 : : break;
11883 : :
11884 : 2906 : case FUNCTION_DECL:
11885 : 2906 : gcc_checking_assert (mk == MK_local_type);
11886 : 2906 : existing = key_local_type (key, container, name);
11887 : 2906 : if (existing && inner != decl)
11888 : 3224 : existing = TYPE_TI_TEMPLATE (TREE_TYPE (existing));
11889 : : break;
11890 : :
11891 : 323660 : case TYPE_DECL:
11892 : 323660 : gcc_checking_assert (!is_imported_temploid_friend);
11893 : 3988 : if (is_attached && !(state->is_module () || state->is_partition ())
11894 : : /* Implicit member functions can come from
11895 : : anywhere. */
11896 : 326731 : && !(DECL_ARTIFICIAL (decl)
11897 : 1799 : && TREE_CODE (decl) == FUNCTION_DECL
11898 : 565 : && !DECL_THUNK_P (decl)))
11899 : : kind = "unique";
11900 : : else
11901 : : {
11902 : 321154 : tree ctx = TREE_TYPE (container);
11903 : :
11904 : : /* For some reason templated enumeral types are not marked
11905 : : as COMPLETE_TYPE_P, even though they have members.
11906 : : This may well be a bug elsewhere. */
11907 : 321154 : if (TREE_CODE (ctx) == ENUMERAL_TYPE)
11908 : 11589 : existing = find_enum_member (ctx, name);
11909 : 309565 : else if (COMPLETE_TYPE_P (ctx))
11910 : : {
11911 : 182810 : switch (mk)
11912 : : {
11913 : 0 : default:
11914 : 0 : gcc_unreachable ();
11915 : :
11916 : 144989 : case MK_named:
11917 : 144989 : existing = lookup_class_binding (ctx, name);
11918 : 144989 : if (existing)
11919 : : {
11920 : 144633 : tree inner = decl;
11921 : 144633 : if (TREE_CODE (inner) == TEMPLATE_DECL
11922 : 144633 : && !DECL_MEMBER_TEMPLATE_P (inner))
11923 : 66927 : inner = DECL_TEMPLATE_RESULT (inner);
11924 : :
11925 : 144633 : existing = check_mergeable_decl
11926 : 144633 : (mk, inner, existing, key);
11927 : :
11928 : 144633 : if (!existing && DECL_ALIAS_TEMPLATE_P (decl))
11929 : : {} // FIXME: Insert into specialization
11930 : : // tables, we'll need the arguments for that!
11931 : : }
11932 : : break;
11933 : :
11934 : 26283 : case MK_field:
11935 : 26283 : {
11936 : 26283 : unsigned ix = key.index;
11937 : 26283 : for (tree field = TYPE_FIELDS (ctx);
11938 : 889741 : field; field = DECL_CHAIN (field))
11939 : : {
11940 : 889741 : tree finner = STRIP_TEMPLATE (field);
11941 : 889741 : if (TREE_CODE (finner) == TREE_CODE (inner))
11942 : 298673 : if (!ix--)
11943 : : {
11944 : : existing = field;
11945 : : break;
11946 : : }
11947 : : }
11948 : : }
11949 : : break;
11950 : :
11951 : 1384 : case MK_vtable:
11952 : 1384 : {
11953 : 1384 : unsigned ix = key.index;
11954 : 1384 : for (tree vtable = CLASSTYPE_VTABLES (ctx);
11955 : 1761 : vtable; vtable = DECL_CHAIN (vtable))
11956 : 1596 : if (!ix--)
11957 : : {
11958 : : existing = vtable;
11959 : : break;
11960 : : }
11961 : : }
11962 : : break;
11963 : :
11964 : 7357 : case MK_as_base:
11965 : 7357 : {
11966 : 7357 : tree as_base = CLASSTYPE_AS_BASE (ctx);
11967 : 7357 : if (as_base && as_base != ctx)
11968 : 7357 : existing = TYPE_NAME (as_base);
11969 : : }
11970 : : break;
11971 : :
11972 : 2797 : case MK_local_friend:
11973 : 2797 : {
11974 : 2797 : unsigned ix = key.index;
11975 : 2797 : for (tree decls = CLASSTYPE_DECL_LIST (ctx);
11976 : 77594 : decls; decls = TREE_CHAIN (decls))
11977 : 77594 : if (!TREE_PURPOSE (decls) && !ix--)
11978 : : {
11979 : 2797 : existing
11980 : 2797 : = friend_from_decl_list (TREE_VALUE (decls));
11981 : 2797 : break;
11982 : : }
11983 : : }
11984 : : break;
11985 : : }
11986 : :
11987 : 182810 : if (existing && mk < MK_indirect_lwm && mk != MK_partial
11988 : 179058 : && TREE_CODE (decl) == TEMPLATE_DECL
11989 : 267573 : && !DECL_MEMBER_TEMPLATE_P (decl))
11990 : : {
11991 : 68798 : tree ti;
11992 : 68798 : if (DECL_IMPLICIT_TYPEDEF_P (existing))
11993 : 863 : ti = TYPE_TEMPLATE_INFO (TREE_TYPE (existing));
11994 : : else
11995 : 67935 : ti = DECL_TEMPLATE_INFO (existing);
11996 : 68798 : existing = TI_TEMPLATE (ti);
11997 : : }
11998 : : }
11999 : : }
12000 : : }
12001 : : }
12002 : :
12003 : 843825 : dump (dumper::MERGE)
12004 : 2401 : && dump ("Read:%d's %s merge key (%s) %C:%N", tag, merge_kind_name[mk],
12005 : 2401 : existing ? "matched" : kind, TREE_CODE (decl), decl);
12006 : :
12007 : : return existing;
12008 : : }
12009 : :
12010 : : void
12011 : 242508 : trees_out::binfo_mergeable (tree binfo)
12012 : : {
12013 : 242508 : tree dom = binfo;
12014 : 309878 : while (tree parent = BINFO_INHERITANCE_CHAIN (dom))
12015 : : dom = parent;
12016 : 242508 : tree type = BINFO_TYPE (dom);
12017 : 242508 : gcc_checking_assert (TYPE_BINFO (type) == dom);
12018 : 242508 : tree_node (type);
12019 : 242508 : if (streaming_p ())
12020 : : {
12021 : : unsigned ix = 0;
12022 : 134512 : for (; dom != binfo; dom = TREE_CHAIN (dom))
12023 : 34933 : ix++;
12024 : 99579 : u (ix);
12025 : : }
12026 : 242508 : }
12027 : :
12028 : : unsigned
12029 : 72032 : trees_in::binfo_mergeable (tree *type)
12030 : : {
12031 : 72032 : *type = tree_node ();
12032 : 72032 : return u ();
12033 : : }
12034 : :
12035 : : /* DECL is a just streamed declaration with attributes DATTR that should
12036 : : have matching ABI tags as EXISTING's attributes EATTR. Check that the
12037 : : ABI tags match, and report an error if not. */
12038 : :
12039 : : void
12040 : 275523 : trees_in::check_abi_tags (tree existing, tree decl, tree &eattr, tree &dattr)
12041 : : {
12042 : 275523 : tree etags = lookup_attribute ("abi_tag", eattr);
12043 : 275523 : tree dtags = lookup_attribute ("abi_tag", dattr);
12044 : 275523 : if ((etags == nullptr) != (dtags == nullptr)
12045 : 275523 : || (etags && !attribute_value_equal (etags, dtags)))
12046 : : {
12047 : 30 : if (etags)
12048 : 21 : etags = TREE_VALUE (etags);
12049 : 30 : if (dtags)
12050 : 24 : dtags = TREE_VALUE (dtags);
12051 : :
12052 : : /* We only error if mangling wouldn't consider the tags equivalent. */
12053 : 30 : if (!equal_abi_tags (etags, dtags))
12054 : : {
12055 : 21 : auto_diagnostic_group d;
12056 : 21 : if (dtags)
12057 : 15 : error_at (DECL_SOURCE_LOCATION (decl),
12058 : : "mismatching abi tags for %qD with tags %qE",
12059 : : decl, dtags);
12060 : : else
12061 : 6 : error_at (DECL_SOURCE_LOCATION (decl),
12062 : : "mismatching abi tags for %qD with no tags", decl);
12063 : 21 : if (etags)
12064 : 15 : inform (DECL_SOURCE_LOCATION (existing),
12065 : : "existing declaration here with tags %qE", etags);
12066 : : else
12067 : 6 : inform (DECL_SOURCE_LOCATION (existing),
12068 : : "existing declaration here with no tags");
12069 : 21 : }
12070 : :
12071 : : /* Always use the existing abi_tags as the canonical set so that
12072 : : later processing doesn't get confused. */
12073 : 30 : if (dtags)
12074 : 24 : dattr = remove_attribute ("abi_tag", dattr);
12075 : 30 : if (etags)
12076 : 21 : duplicate_one_attribute (&dattr, eattr, "abi_tag");
12077 : : }
12078 : 275523 : }
12079 : :
12080 : : /* DECL is a just streamed mergeable decl that should match EXISTING. Check
12081 : : it does and issue an appropriate diagnostic if not. Merge any
12082 : : bits from DECL to EXISTING. This is stricter matching than
12083 : : decls_match, because we can rely on ODR-sameness, and we cannot use
12084 : : decls_match because it can cause instantiations of constraints. */
12085 : :
12086 : : bool
12087 : 409816 : trees_in::is_matching_decl (tree existing, tree decl, bool is_typedef)
12088 : : {
12089 : : // FIXME: We should probably do some duplicate decl-like stuff here
12090 : : // (beware, default parms should be the same?) Can we just call
12091 : : // duplicate_decls and teach it how to handle the module-specific
12092 : : // permitted/required duplications?
12093 : :
12094 : : // We know at this point that the decls have matched by key, so we
12095 : : // can elide some of the checking
12096 : 409816 : gcc_checking_assert (TREE_CODE (existing) == TREE_CODE (decl));
12097 : :
12098 : 409816 : tree d_inner = decl;
12099 : 409816 : tree e_inner = existing;
12100 : 409816 : if (TREE_CODE (decl) == TEMPLATE_DECL)
12101 : : {
12102 : 139415 : d_inner = DECL_TEMPLATE_RESULT (d_inner);
12103 : 139415 : e_inner = DECL_TEMPLATE_RESULT (e_inner);
12104 : 139415 : gcc_checking_assert (TREE_CODE (e_inner) == TREE_CODE (d_inner));
12105 : : }
12106 : :
12107 : : // FIXME: do more precise errors at point of mismatch
12108 : 409816 : const char *mismatch_msg = nullptr;
12109 : 409816 : if (TREE_CODE (d_inner) == FUNCTION_DECL)
12110 : : {
12111 : 185660 : tree e_ret = fndecl_declared_return_type (existing);
12112 : 185660 : tree d_ret = fndecl_declared_return_type (decl);
12113 : :
12114 : 81333 : if (decl != d_inner && DECL_NAME (d_inner) == fun_identifier
12115 : 185678 : && LAMBDA_TYPE_P (DECL_CONTEXT (d_inner)))
12116 : : /* This has a recursive type that will compare different. */;
12117 : 185651 : else if (!same_type_p (d_ret, e_ret))
12118 : : {
12119 : 5 : mismatch_msg = G_("conflicting type for imported declaration %#qD");
12120 : 5 : goto mismatch;
12121 : : }
12122 : :
12123 : 185655 : tree e_type = TREE_TYPE (e_inner);
12124 : 185655 : tree d_type = TREE_TYPE (d_inner);
12125 : :
12126 : 185655 : if (DECL_EXTERN_C_P (d_inner) != DECL_EXTERN_C_P (e_inner))
12127 : : {
12128 : 0 : mismatch_msg = G_("conflicting language linkage for imported "
12129 : : "declaration %#qD");
12130 : 0 : goto mismatch;
12131 : : }
12132 : :
12133 : 185655 : for (tree e_args = TYPE_ARG_TYPES (e_type),
12134 : 185655 : d_args = TYPE_ARG_TYPES (d_type);
12135 : 318672 : e_args != d_args && (e_args || d_args);
12136 : 133017 : e_args = TREE_CHAIN (e_args), d_args = TREE_CHAIN (d_args))
12137 : : {
12138 : 133017 : if (!(e_args && d_args))
12139 : : {
12140 : 0 : mismatch_msg = G_("conflicting argument list for imported "
12141 : : "declaration %#qD");
12142 : 0 : goto mismatch;
12143 : : }
12144 : :
12145 : 133017 : if (!same_type_p (TREE_VALUE (d_args), TREE_VALUE (e_args)))
12146 : : {
12147 : 0 : mismatch_msg = G_("conflicting argument types for imported "
12148 : : "declaration %#qD");
12149 : 0 : goto mismatch;
12150 : : }
12151 : : }
12152 : :
12153 : : /* If EXISTING has an undeduced or uninstantiated exception
12154 : : specification, but DECL does not, propagate the exception
12155 : : specification. Otherwise we end up asserting or trying to
12156 : : instantiate it in the middle of loading. */
12157 : 185655 : tree e_spec = TYPE_RAISES_EXCEPTIONS (e_type);
12158 : 185655 : tree d_spec = TYPE_RAISES_EXCEPTIONS (d_type);
12159 : 271636 : if (DECL_MAYBE_DELETED (e_inner) || DEFERRED_NOEXCEPT_SPEC_P (e_spec))
12160 : : {
12161 : 15151 : if (!DEFERRED_NOEXCEPT_SPEC_P (d_spec)
12162 : 45096 : || (UNEVALUATED_NOEXCEPT_SPEC_P (e_spec)
12163 : 12299 : && !UNEVALUATED_NOEXCEPT_SPEC_P (d_spec)))
12164 : : {
12165 : 249 : dump (dumper::MERGE)
12166 : 3 : && dump ("Propagating instantiated noexcept to %N", existing);
12167 : 249 : TREE_TYPE (existing) = d_type;
12168 : :
12169 : : /* Propagate to existing clones. */
12170 : 249 : tree clone;
12171 : 519 : FOR_EACH_CLONE (clone, existing)
12172 : : {
12173 : 270 : if (TREE_TYPE (clone) == e_type)
12174 : 270 : TREE_TYPE (clone) = d_type;
12175 : : else
12176 : 0 : TREE_TYPE (clone)
12177 : 0 : = build_exception_variant (TREE_TYPE (clone), d_spec);
12178 : : }
12179 : : }
12180 : : }
12181 : 170457 : else if (!DECL_MAYBE_DELETED (d_inner)
12182 : 242066 : && !DEFERRED_NOEXCEPT_SPEC_P (d_spec)
12183 : 340913 : && !comp_except_specs (d_spec, e_spec, ce_type))
12184 : : {
12185 : 761 : mismatch_msg = G_("conflicting %<noexcept%> specifier for "
12186 : : "imported declaration %#qD");
12187 : 761 : goto mismatch;
12188 : : }
12189 : :
12190 : : /* Similarly if EXISTING has an undeduced return type, but DECL's
12191 : : is already deduced. */
12192 : 184894 : if (undeduced_auto_decl (existing) && !undeduced_auto_decl (decl))
12193 : : {
12194 : 13 : dump (dumper::MERGE)
12195 : 0 : && dump ("Propagating deduced return type to %N", existing);
12196 : 13 : FNDECL_USED_AUTO (e_inner) = true;
12197 : 13 : DECL_SAVED_AUTO_RETURN_TYPE (existing) = TREE_TYPE (e_type);
12198 : 13 : TREE_TYPE (existing) = change_return_type (TREE_TYPE (d_type), e_type);
12199 : : }
12200 : 184881 : else if (type_uses_auto (d_ret)
12201 : 184881 : && !same_type_p (TREE_TYPE (d_type), TREE_TYPE (e_type)))
12202 : : {
12203 : 3 : mismatch_msg = G_("conflicting deduced return type for "
12204 : : "imported declaration %#qD");
12205 : 3 : goto mismatch;
12206 : : }
12207 : :
12208 : : /* Similarly if EXISTING has undeduced constexpr, but DECL's
12209 : : is already deduced. */
12210 : 184891 : if (DECL_DECLARED_CONSTEXPR_P (e_inner)
12211 : 184891 : == DECL_DECLARED_CONSTEXPR_P (d_inner))
12212 : : /* Already matches. */;
12213 : 2 : else if (DECL_DECLARED_CONSTEXPR_P (d_inner)
12214 : 2 : && (DECL_MAYBE_DELETED (e_inner)
12215 : 0 : || decl_implicit_constexpr_p (d_inner)))
12216 : : /* DECL was deduced, copy to EXISTING. */
12217 : : {
12218 : 1 : DECL_DECLARED_CONSTEXPR_P (e_inner) = true;
12219 : 1 : if (decl_implicit_constexpr_p (d_inner))
12220 : 0 : DECL_LANG_SPECIFIC (e_inner)->u.fn.implicit_constexpr = true;
12221 : : }
12222 : 1 : else if (DECL_DECLARED_CONSTEXPR_P (e_inner)
12223 : 1 : && (DECL_MAYBE_DELETED (d_inner)
12224 : 0 : || decl_implicit_constexpr_p (e_inner)))
12225 : : /* EXISTING was deduced, leave it alone. */;
12226 : : else
12227 : : {
12228 : 0 : mismatch_msg = G_("conflicting %<constexpr%> for imported "
12229 : : "declaration %#qD");
12230 : 0 : goto mismatch;
12231 : : }
12232 : :
12233 : : /* Don't synthesize a defaulted function if we're importing one
12234 : : we've already determined. */
12235 : 184891 : if (!DECL_MAYBE_DELETED (d_inner))
12236 : 184784 : DECL_MAYBE_DELETED (e_inner) = false;
12237 : : }
12238 : 224156 : else if (is_typedef)
12239 : : {
12240 : 81543 : if (!DECL_ORIGINAL_TYPE (e_inner)
12241 : 81543 : || !same_type_p (DECL_ORIGINAL_TYPE (d_inner),
12242 : : DECL_ORIGINAL_TYPE (e_inner)))
12243 : : {
12244 : 3 : mismatch_msg = G_("conflicting imported declaration %q#D");
12245 : 3 : goto mismatch;
12246 : : }
12247 : : }
12248 : : /* Using cp_tree_equal because we can meet TYPE_ARGUMENT_PACKs
12249 : : here. I suspect the entities that directly do that are things
12250 : : that shouldn't go to duplicate_decls (FIELD_DECLs etc). */
12251 : 142613 : else if (!cp_tree_equal (TREE_TYPE (decl), TREE_TYPE (existing)))
12252 : : {
12253 : : mismatch_msg = G_("conflicting type for imported declaration %#qD");
12254 : 1443 : mismatch:
12255 : 1443 : if (DECL_IS_UNDECLARED_BUILTIN (existing))
12256 : : /* Just like duplicate_decls, presum the user knows what
12257 : : they're doing in overriding a builtin. */
12258 : 763 : TREE_TYPE (existing) = TREE_TYPE (decl);
12259 : 680 : else if (decl_function_context (decl))
12260 : : /* The type of a mergeable local entity (such as a function scope
12261 : : capturing lambda's closure type fields) can depend on an
12262 : : unmergeable local entity (such as a local variable), so type
12263 : : equality isn't feasible in general for local entities. */;
12264 : : else
12265 : : {
12266 : 15 : gcc_checking_assert (mismatch_msg);
12267 : 15 : auto_diagnostic_group d;
12268 : 15 : error_at (DECL_SOURCE_LOCATION (decl), mismatch_msg, decl);
12269 : 15 : inform (DECL_SOURCE_LOCATION (existing),
12270 : : "existing declaration %#qD", existing);
12271 : 15 : return false;
12272 : 15 : }
12273 : : }
12274 : :
12275 : 409801 : if (DECL_IS_UNDECLARED_BUILTIN (existing)
12276 : 409801 : && !DECL_IS_UNDECLARED_BUILTIN (decl))
12277 : : {
12278 : : /* We're matching a builtin that the user has yet to declare.
12279 : : We are the one! This is very much duplicate-decl
12280 : : shenanigans. */
12281 : 946 : DECL_SOURCE_LOCATION (existing) = DECL_SOURCE_LOCATION (decl);
12282 : 946 : if (TREE_CODE (decl) != TYPE_DECL)
12283 : : {
12284 : : /* Propagate exceptions etc. */
12285 : 935 : TREE_TYPE (existing) = TREE_TYPE (decl);
12286 : 935 : TREE_NOTHROW (existing) = TREE_NOTHROW (decl);
12287 : : }
12288 : : /* This is actually an import! */
12289 : 946 : DECL_MODULE_IMPORT_P (existing) = true;
12290 : :
12291 : : /* Yay, sliced! */
12292 : 946 : existing->base = decl->base;
12293 : :
12294 : 946 : if (TREE_CODE (decl) == FUNCTION_DECL)
12295 : : {
12296 : : /* Ew :( */
12297 : 935 : memcpy (&existing->decl_common.size,
12298 : : &decl->decl_common.size,
12299 : : (offsetof (tree_decl_common, pt_uid)
12300 : : - offsetof (tree_decl_common, size)));
12301 : 935 : auto bltin_class = DECL_BUILT_IN_CLASS (decl);
12302 : 935 : existing->function_decl.built_in_class = bltin_class;
12303 : 935 : auto fncode = DECL_UNCHECKED_FUNCTION_CODE (decl);
12304 : 935 : DECL_UNCHECKED_FUNCTION_CODE (existing) = fncode;
12305 : 935 : if (existing->function_decl.built_in_class == BUILT_IN_NORMAL)
12306 : : {
12307 : 815 : if (builtin_decl_explicit_p (built_in_function (fncode)))
12308 : 815 : switch (fncode)
12309 : : {
12310 : 0 : case BUILT_IN_STPCPY:
12311 : 0 : set_builtin_decl_implicit_p
12312 : 0 : (built_in_function (fncode), true);
12313 : 0 : break;
12314 : 815 : default:
12315 : 815 : set_builtin_decl_declared_p
12316 : 815 : (built_in_function (fncode), true);
12317 : 815 : break;
12318 : : }
12319 : 815 : copy_attributes_to_builtin (decl);
12320 : : }
12321 : : }
12322 : : }
12323 : :
12324 : 409801 : if (VAR_OR_FUNCTION_DECL_P (decl)
12325 : 409801 : && DECL_TEMPLATE_INSTANTIATED (decl))
12326 : : /* Don't instantiate again! */
12327 : 8041 : DECL_TEMPLATE_INSTANTIATED (existing) = true;
12328 : :
12329 : 409801 : if (TREE_CODE (d_inner) == FUNCTION_DECL
12330 : 409801 : && DECL_DECLARED_INLINE_P (d_inner))
12331 : : {
12332 : 150931 : DECL_DECLARED_INLINE_P (e_inner) = true;
12333 : 150931 : if (!DECL_SAVED_TREE (e_inner)
12334 : 75018 : && lookup_attribute ("gnu_inline", DECL_ATTRIBUTES (d_inner))
12335 : 150937 : && !lookup_attribute ("gnu_inline", DECL_ATTRIBUTES (e_inner)))
12336 : : {
12337 : 18 : DECL_INTERFACE_KNOWN (e_inner)
12338 : 6 : |= DECL_INTERFACE_KNOWN (d_inner);
12339 : 6 : DECL_DISREGARD_INLINE_LIMITS (e_inner)
12340 : 6 : |= DECL_DISREGARD_INLINE_LIMITS (d_inner);
12341 : : // TODO: we will eventually want to merge all decl attributes
12342 : 6 : duplicate_one_attribute (&DECL_ATTRIBUTES (e_inner),
12343 : 6 : DECL_ATTRIBUTES (d_inner), "gnu_inline");
12344 : : }
12345 : : }
12346 : 409801 : if (!DECL_EXTERNAL (d_inner))
12347 : 192832 : DECL_EXTERNAL (e_inner) = false;
12348 : :
12349 : 409801 : if (VAR_OR_FUNCTION_DECL_P (d_inner))
12350 : 403328 : check_abi_tags (existing, decl,
12351 : 201664 : DECL_ATTRIBUTES (e_inner), DECL_ATTRIBUTES (d_inner));
12352 : :
12353 : 409801 : if (TREE_CODE (decl) == TEMPLATE_DECL)
12354 : : {
12355 : : /* Merge default template arguments. */
12356 : 139413 : tree d_parms = DECL_INNERMOST_TEMPLATE_PARMS (decl);
12357 : 139413 : tree e_parms = DECL_INNERMOST_TEMPLATE_PARMS (existing);
12358 : 139413 : gcc_checking_assert (TREE_VEC_LENGTH (d_parms)
12359 : : == TREE_VEC_LENGTH (e_parms));
12360 : 401659 : for (int i = 0; i < TREE_VEC_LENGTH (d_parms); ++i)
12361 : : {
12362 : 262255 : tree d_default = TREE_PURPOSE (TREE_VEC_ELT (d_parms, i));
12363 : 262255 : tree& e_default = TREE_PURPOSE (TREE_VEC_ELT (e_parms, i));
12364 : 262255 : if (e_default == NULL_TREE)
12365 : 222338 : e_default = d_default;
12366 : 39917 : else if (d_default != NULL_TREE
12367 : 39917 : && !cp_tree_equal (d_default, e_default))
12368 : : {
12369 : 9 : auto_diagnostic_group d;
12370 : 9 : tree d_parm = TREE_VALUE (TREE_VEC_ELT (d_parms, i));
12371 : 9 : tree e_parm = TREE_VALUE (TREE_VEC_ELT (e_parms, i));
12372 : 9 : error_at (DECL_SOURCE_LOCATION (d_parm),
12373 : : "conflicting default argument for %#qD", d_parm);
12374 : 9 : inform (DECL_SOURCE_LOCATION (e_parm),
12375 : : "existing default declared here");
12376 : 9 : return false;
12377 : 9 : }
12378 : : }
12379 : : }
12380 : :
12381 : 409792 : if (TREE_CODE (d_inner) == FUNCTION_DECL)
12382 : : {
12383 : : /* Merge default function arguments. */
12384 : 185651 : tree d_parm = FUNCTION_FIRST_USER_PARMTYPE (d_inner);
12385 : 185651 : tree e_parm = FUNCTION_FIRST_USER_PARMTYPE (e_inner);
12386 : 185651 : int i = 0;
12387 : 442661 : for (; d_parm && d_parm != void_list_node;
12388 : 257010 : d_parm = TREE_CHAIN (d_parm), e_parm = TREE_CHAIN (e_parm), ++i)
12389 : : {
12390 : 257022 : tree d_default = TREE_PURPOSE (d_parm);
12391 : 257022 : tree& e_default = TREE_PURPOSE (e_parm);
12392 : 257022 : if (e_default == NULL_TREE)
12393 : 242180 : e_default = d_default;
12394 : 14842 : else if (d_default != NULL_TREE
12395 : 14842 : && !cp_tree_equal (d_default, e_default))
12396 : : {
12397 : 12 : auto_diagnostic_group d;
12398 : 12 : error_at (get_fndecl_argument_location (d_inner, i),
12399 : : "conflicting default argument for parameter %P of %#qD",
12400 : : i, decl);
12401 : 12 : inform (get_fndecl_argument_location (e_inner, i),
12402 : : "existing default declared here");
12403 : 12 : return false;
12404 : 12 : }
12405 : : }
12406 : : }
12407 : :
12408 : : return true;
12409 : : }
12410 : :
12411 : : /* FN is an implicit member function that we've discovered is new to
12412 : : the class. Add it to the TYPE_FIELDS chain and the method vector.
12413 : : Reset the appropriate classtype lazy flag. */
12414 : :
12415 : : bool
12416 : 790 : trees_in::install_implicit_member (tree fn)
12417 : : {
12418 : 790 : tree ctx = DECL_CONTEXT (fn);
12419 : 790 : tree name = DECL_NAME (fn);
12420 : : /* We know these are synthesized, so the set of expected prototypes
12421 : : is quite restricted. We're not validating correctness, just
12422 : : distinguishing beteeen the small set of possibilities. */
12423 : 790 : tree parm_type = TREE_VALUE (FUNCTION_FIRST_USER_PARMTYPE (fn));
12424 : 790 : if (IDENTIFIER_CTOR_P (name))
12425 : : {
12426 : 526 : if (CLASSTYPE_LAZY_DEFAULT_CTOR (ctx)
12427 : 526 : && VOID_TYPE_P (parm_type))
12428 : 124 : CLASSTYPE_LAZY_DEFAULT_CTOR (ctx) = false;
12429 : 402 : else if (!TYPE_REF_P (parm_type))
12430 : : return false;
12431 : 402 : else if (CLASSTYPE_LAZY_COPY_CTOR (ctx)
12432 : 402 : && !TYPE_REF_IS_RVALUE (parm_type))
12433 : 204 : CLASSTYPE_LAZY_COPY_CTOR (ctx) = false;
12434 : 198 : else if (CLASSTYPE_LAZY_MOVE_CTOR (ctx))
12435 : 198 : CLASSTYPE_LAZY_MOVE_CTOR (ctx) = false;
12436 : : else
12437 : : return false;
12438 : : }
12439 : 264 : else if (IDENTIFIER_DTOR_P (name))
12440 : : {
12441 : 200 : if (CLASSTYPE_LAZY_DESTRUCTOR (ctx))
12442 : 200 : CLASSTYPE_LAZY_DESTRUCTOR (ctx) = false;
12443 : : else
12444 : : return false;
12445 : 200 : if (DECL_VIRTUAL_P (fn))
12446 : : /* A virtual dtor should have been created when the class
12447 : : became complete. */
12448 : : return false;
12449 : : }
12450 : 64 : else if (name == assign_op_identifier)
12451 : : {
12452 : 64 : if (!TYPE_REF_P (parm_type))
12453 : : return false;
12454 : 64 : else if (CLASSTYPE_LAZY_COPY_ASSIGN (ctx)
12455 : 64 : && !TYPE_REF_IS_RVALUE (parm_type))
12456 : 32 : CLASSTYPE_LAZY_COPY_ASSIGN (ctx) = false;
12457 : 32 : else if (CLASSTYPE_LAZY_MOVE_ASSIGN (ctx))
12458 : 32 : CLASSTYPE_LAZY_MOVE_ASSIGN (ctx) = false;
12459 : : else
12460 : : return false;
12461 : : }
12462 : : else
12463 : : return false;
12464 : :
12465 : 820 : dump (dumper::MERGE) && dump ("Adding implicit member %N", fn);
12466 : :
12467 : 790 : DECL_CHAIN (fn) = TYPE_FIELDS (ctx);
12468 : 790 : TYPE_FIELDS (ctx) = fn;
12469 : :
12470 : 790 : add_method (ctx, fn, false);
12471 : :
12472 : : /* Propagate TYPE_FIELDS. */
12473 : 790 : fixup_type_variants (ctx);
12474 : :
12475 : 790 : return true;
12476 : : }
12477 : :
12478 : : /* Return non-zero if DECL has a definition that would be interesting to
12479 : : write out. */
12480 : :
12481 : : static bool
12482 : 1194268 : has_definition (tree decl)
12483 : : {
12484 : 1194268 : bool is_tmpl = TREE_CODE (decl) == TEMPLATE_DECL;
12485 : 1194268 : if (is_tmpl)
12486 : 290044 : decl = DECL_TEMPLATE_RESULT (decl);
12487 : :
12488 : 1194268 : switch (TREE_CODE (decl))
12489 : : {
12490 : : default:
12491 : : break;
12492 : :
12493 : 430175 : case FUNCTION_DECL:
12494 : 430175 : if (!DECL_SAVED_TREE (decl))
12495 : : /* Not defined. */
12496 : : break;
12497 : :
12498 : 185563 : if (DECL_DECLARED_INLINE_P (decl))
12499 : : return true;
12500 : :
12501 : 11073 : if (header_module_p ())
12502 : : /* We always need to write definitions in header modules,
12503 : : since there's no TU to emit them in otherwise. */
12504 : : return true;
12505 : :
12506 : 3198 : if (DECL_TEMPLATE_INFO (decl))
12507 : : {
12508 : 2597 : int use_tpl = DECL_USE_TEMPLATE (decl);
12509 : :
12510 : : // FIXME: Partial specializations have definitions too.
12511 : 2597 : if (use_tpl < 2)
12512 : : return true;
12513 : : }
12514 : : break;
12515 : :
12516 : 516145 : case TYPE_DECL:
12517 : 516145 : {
12518 : 516145 : tree type = TREE_TYPE (decl);
12519 : 516145 : if (type == TYPE_MAIN_VARIANT (type)
12520 : 241951 : && decl == TYPE_NAME (type)
12521 : 758096 : && (TREE_CODE (type) == ENUMERAL_TYPE
12522 : 241951 : ? TYPE_VALUES (type) : TYPE_FIELDS (type)))
12523 : : return true;
12524 : : }
12525 : : break;
12526 : :
12527 : 52407 : case VAR_DECL:
12528 : : /* DECL_INITIALIZED_P might not be set on a dependent VAR_DECL. */
12529 : 52407 : if (DECL_LANG_SPECIFIC (decl)
12530 : 52326 : && DECL_TEMPLATE_INFO (decl)
12531 : 87057 : && DECL_INITIAL (decl))
12532 : : return true;
12533 : : else
12534 : : {
12535 : 21137 : if (!DECL_INITIALIZED_P (decl))
12536 : : /* Not defined. */
12537 : : return false;
12538 : :
12539 : 16572 : if (header_module_p ())
12540 : : /* We always need to write definitions in header modules,
12541 : : since there's no TU to emit them in otherwise. */
12542 : : return true;
12543 : :
12544 : 2729 : if (decl_maybe_constant_var_p (decl))
12545 : : /* We might need its constant value. */
12546 : : return true;
12547 : :
12548 : 336 : if (vague_linkage_p (decl))
12549 : : /* These are emitted as needed. */
12550 : : return true;
12551 : :
12552 : : return false;
12553 : : }
12554 : 5828 : break;
12555 : :
12556 : 5828 : case CONCEPT_DECL:
12557 : 5828 : if (DECL_INITIAL (decl))
12558 : : return true;
12559 : :
12560 : : break;
12561 : : }
12562 : :
12563 : : return false;
12564 : : }
12565 : :
12566 : : uintptr_t *
12567 : 471674 : trees_in::find_duplicate (tree existing)
12568 : : {
12569 : 235794 : if (!duplicates)
12570 : : return NULL;
12571 : :
12572 : 334652 : return duplicates->get (existing);
12573 : : }
12574 : :
12575 : : /* We're starting to read a duplicate DECL. EXISTING is the already
12576 : : known node. */
12577 : :
12578 : : void
12579 : 589717 : trees_in::register_duplicate (tree decl, tree existing)
12580 : : {
12581 : 589717 : if (!duplicates)
12582 : 94200 : duplicates = new duplicate_hash_map (40);
12583 : :
12584 : 589717 : bool existed;
12585 : 589717 : uintptr_t &slot = duplicates->get_or_insert (existing, &existed);
12586 : 589717 : gcc_checking_assert (!existed);
12587 : 589717 : slot = reinterpret_cast<uintptr_t> (decl);
12588 : :
12589 : 589717 : if (TREE_CODE (decl) == TEMPLATE_DECL)
12590 : : /* Also register the DECL_TEMPLATE_RESULT as a duplicate so
12591 : : that passing decl's _RESULT to maybe_duplicate naturally
12592 : : gives us existing's _RESULT back. */
12593 : 278830 : register_duplicate (DECL_TEMPLATE_RESULT (decl),
12594 : 139415 : DECL_TEMPLATE_RESULT (existing));
12595 : 589717 : }
12596 : :
12597 : : /* We've read a definition of MAYBE_EXISTING. If not a duplicate,
12598 : : return MAYBE_EXISTING (into which the definition should be
12599 : : installed). Otherwise return NULL if already known bad, or the
12600 : : duplicate we read (for ODR checking, or extracting additional merge
12601 : : information). */
12602 : :
12603 : : tree
12604 : 235880 : trees_in::odr_duplicate (tree maybe_existing, bool has_defn)
12605 : : {
12606 : 235880 : tree res = NULL_TREE;
12607 : :
12608 : 378347 : if (uintptr_t *dup = find_duplicate (maybe_existing))
12609 : : {
12610 : 135133 : if (!(*dup & 1))
12611 : 135130 : res = reinterpret_cast<tree> (*dup);
12612 : : }
12613 : : else
12614 : : res = maybe_existing;
12615 : :
12616 : 235880 : assert_definition (maybe_existing, res && !has_defn);
12617 : :
12618 : : // FIXME: We probably need to return the template, so that the
12619 : : // template header can be checked?
12620 : 235880 : return res ? STRIP_TEMPLATE (res) : NULL_TREE;
12621 : : }
12622 : :
12623 : : /* The following writer functions rely on the current behaviour of
12624 : : depset::hash::add_dependency making the decl and defn depset nodes
12625 : : depend on eachother. That way we don't have to worry about seeding
12626 : : the tree map with named decls that cannot be looked up by name (I.e
12627 : : template and function parms). We know the decl and definition will
12628 : : be in the same cluster, which is what we want. */
12629 : :
12630 : : void
12631 : 347323 : trees_out::write_function_def (tree decl)
12632 : : {
12633 : 347323 : tree_node (DECL_RESULT (decl));
12634 : :
12635 : 347323 : {
12636 : : /* The function body for a non-inline function or function template
12637 : : is ignored for determining exposures. This should only matter
12638 : : for templates (we don't emit the bodies of non-inline functions
12639 : : to begin with). */
12640 : 347323 : auto ovr = make_temp_override (dep_hash->ignore_tu_local,
12641 : 347323 : !DECL_DECLARED_INLINE_P (decl));
12642 : 347323 : tree_node (DECL_INITIAL (decl));
12643 : 347323 : tree_node (DECL_SAVED_TREE (decl));
12644 : 347323 : }
12645 : :
12646 : 741612 : tree_node (DECL_FRIEND_CONTEXT (decl));
12647 : :
12648 : 347323 : constexpr_fundef *cexpr = retrieve_constexpr_fundef (decl);
12649 : :
12650 : 347323 : if (streaming_p ())
12651 : 173630 : u (cexpr != nullptr);
12652 : 347323 : if (cexpr)
12653 : : {
12654 : 74962 : chained_decls (cexpr->parms);
12655 : 74962 : tree_node (cexpr->result);
12656 : 74962 : tree_node (cexpr->body);
12657 : : }
12658 : :
12659 : 347323 : function* f = DECL_STRUCT_FUNCTION (decl);
12660 : :
12661 : 347323 : if (streaming_p ())
12662 : : {
12663 : 173630 : unsigned flags = 0;
12664 : :
12665 : : /* Whether the importer should emit this definition, if used. */
12666 : 173630 : flags |= 1 * (DECL_NOT_REALLY_EXTERN (decl)
12667 : 173630 : && (get_importer_interface (decl)
12668 : : != importer_interface::external));
12669 : :
12670 : 173630 : if (f)
12671 : : {
12672 : 173263 : flags |= 2;
12673 : : /* These flags are needed in tsubst_lambda_expr. */
12674 : 173263 : flags |= 4 * f->language->returns_value;
12675 : 173263 : flags |= 8 * f->language->returns_null;
12676 : 173263 : flags |= 16 * f->language->returns_abnormally;
12677 : 173263 : flags |= 32 * f->language->infinite_loop;
12678 : : }
12679 : :
12680 : 173630 : u (flags);
12681 : : }
12682 : :
12683 : 347323 : if (state && f)
12684 : : {
12685 : 346589 : state->write_location (*this, f->function_start_locus);
12686 : 346589 : state->write_location (*this, f->function_end_locus);
12687 : : }
12688 : 347323 : }
12689 : :
12690 : : void
12691 : 0 : trees_out::mark_function_def (tree)
12692 : : {
12693 : 0 : }
12694 : :
12695 : : bool
12696 : 151370 : trees_in::read_function_def (tree decl, tree maybe_template)
12697 : : {
12698 : 151799 : dump () && dump ("Reading function definition %N", decl);
12699 : 151370 : tree result = tree_node ();
12700 : 151370 : tree initial = tree_node ();
12701 : 151370 : tree saved = tree_node ();
12702 : 151370 : tree context = tree_node ();
12703 : 151370 : constexpr_fundef cexpr;
12704 : 151370 : post_process_data pdata {};
12705 : 151370 : pdata.decl = maybe_template;
12706 : :
12707 : 151370 : tree maybe_dup = odr_duplicate (maybe_template, DECL_SAVED_TREE (decl));
12708 : 302737 : bool installing = maybe_dup && !DECL_SAVED_TREE (decl);
12709 : :
12710 : 151370 : if (u ())
12711 : : {
12712 : 32625 : cexpr.parms = chained_decls ();
12713 : 32625 : cexpr.result = tree_node ();
12714 : 32625 : cexpr.body = tree_node ();
12715 : 32625 : cexpr.decl = decl;
12716 : : }
12717 : : else
12718 : 118745 : cexpr.decl = NULL_TREE;
12719 : :
12720 : 151370 : unsigned flags = u ();
12721 : :
12722 : 151370 : if (flags & 2)
12723 : : {
12724 : 151053 : pdata.start_locus = state->read_location (*this);
12725 : 151053 : pdata.end_locus = state->read_location (*this);
12726 : 151053 : pdata.returns_value = flags & 4;
12727 : 151053 : pdata.returns_null = flags & 8;
12728 : 151053 : pdata.returns_abnormally = flags & 16;
12729 : 151053 : pdata.infinite_loop = flags & 32;
12730 : : }
12731 : :
12732 : 151370 : if (get_overrun ())
12733 : : return NULL_TREE;
12734 : :
12735 : 151370 : if (installing)
12736 : : {
12737 : 70804 : DECL_NOT_REALLY_EXTERN (decl) = flags & 1;
12738 : 70804 : DECL_RESULT (decl) = result;
12739 : 70804 : DECL_INITIAL (decl) = initial;
12740 : 70804 : DECL_SAVED_TREE (decl) = saved;
12741 : :
12742 : 70804 : if (context)
12743 : 2786 : SET_DECL_FRIEND_CONTEXT (decl, context);
12744 : 70804 : if (cexpr.decl)
12745 : 21070 : register_constexpr_fundef (cexpr);
12746 : 70804 : post_process (pdata);
12747 : : }
12748 : : else if (maybe_dup)
12749 : : {
12750 : : // FIXME:QOI Check matching defn
12751 : : }
12752 : :
12753 : : return true;
12754 : : }
12755 : :
12756 : : /* Also for CONCEPT_DECLs. */
12757 : :
12758 : : void
12759 : 75672 : trees_out::write_var_def (tree decl)
12760 : : {
12761 : : /* The initializer of a non-inline variable or variable template is
12762 : : ignored for determining exposures. */
12763 : 75672 : auto ovr = make_temp_override (dep_hash->ignore_tu_local,
12764 : 75672 : VAR_P (decl) && !DECL_INLINE_VAR_P (decl));
12765 : :
12766 : 75672 : tree init = DECL_INITIAL (decl);
12767 : 75672 : tree_node (init);
12768 : 75672 : if (!init)
12769 : : {
12770 : 1210 : tree dyn_init = NULL_TREE;
12771 : :
12772 : : /* We only need to write initializers in header modules. */
12773 : 2248 : if (header_module_p () && DECL_NONTRIVIALLY_INITIALIZED_P (decl))
12774 : : {
12775 : 450 : dyn_init = value_member (decl,
12776 : 450 : CP_DECL_THREAD_LOCAL_P (decl)
12777 : : ? tls_aggregates : static_aggregates);
12778 : 450 : gcc_checking_assert (dyn_init);
12779 : : /* Mark it so write_inits knows this is needed. */
12780 : 450 : TREE_LANG_FLAG_0 (dyn_init) = true;
12781 : 450 : dyn_init = TREE_PURPOSE (dyn_init);
12782 : : }
12783 : 1210 : tree_node (dyn_init);
12784 : : }
12785 : 75672 : }
12786 : :
12787 : : void
12788 : 0 : trees_out::mark_var_def (tree)
12789 : : {
12790 : 0 : }
12791 : :
12792 : : bool
12793 : 30309 : trees_in::read_var_def (tree decl, tree maybe_template)
12794 : : {
12795 : : /* Do not mark the virtual table entries as used. */
12796 : 30309 : bool vtable = VAR_P (decl) && DECL_VTABLE_OR_VTT_P (decl);
12797 : 30309 : unused += vtable;
12798 : 30309 : tree init = tree_node ();
12799 : 30309 : tree dyn_init = init ? NULL_TREE : tree_node ();
12800 : 30309 : unused -= vtable;
12801 : :
12802 : 30309 : if (get_overrun ())
12803 : : return false;
12804 : :
12805 : 30309 : bool initialized = (VAR_P (decl) ? bool (DECL_INITIALIZED_P (decl))
12806 : 30309 : : bool (DECL_INITIAL (decl)));
12807 : 30309 : tree maybe_dup = odr_duplicate (maybe_template, initialized);
12808 : 30309 : bool installing = maybe_dup && !initialized;
12809 : 30309 : if (installing)
12810 : : {
12811 : 17542 : DECL_INITIAL (decl) = init;
12812 : 17542 : if (DECL_EXTERNAL (decl))
12813 : 2358 : DECL_NOT_REALLY_EXTERN (decl) = true;
12814 : 17542 : if (VAR_P (decl))
12815 : : {
12816 : 15400 : DECL_INITIALIZED_P (decl) = true;
12817 : 15400 : if (maybe_dup && DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (maybe_dup))
12818 : 15058 : DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl) = true;
12819 : 15400 : tentative_decl_linkage (decl);
12820 : 15400 : if (DECL_EXPLICIT_INSTANTIATION (decl)
12821 : 15400 : && !DECL_EXTERNAL (decl))
12822 : 9 : setup_explicit_instantiation_definition_linkage (decl);
12823 : 15400 : if (DECL_IMPLICIT_INSTANTIATION (decl)
12824 : 14670 : || (DECL_EXPLICIT_INSTANTIATION (decl)
12825 : 18 : && !DECL_EXTERNAL (decl))
12826 : 30061 : || (DECL_CLASS_SCOPE_P (decl)
12827 : 8543 : && !DECL_VTABLE_OR_VTT_P (decl)
12828 : 7303 : && !DECL_TEMPLATE_INFO (decl)))
12829 : 5212 : note_vague_linkage_variable (decl);
12830 : : }
12831 : 17542 : if (!dyn_init)
12832 : : ;
12833 : 216 : else if (CP_DECL_THREAD_LOCAL_P (decl))
12834 : 96 : tls_aggregates = tree_cons (dyn_init, decl, tls_aggregates);
12835 : : else
12836 : 120 : static_aggregates = tree_cons (dyn_init, decl, static_aggregates);
12837 : : }
12838 : : else if (maybe_dup)
12839 : : {
12840 : : // FIXME:QOI Check matching defn
12841 : : }
12842 : :
12843 : : return true;
12844 : : }
12845 : :
12846 : : /* If MEMBER doesn't have an independent life outside the class,
12847 : : return it (or its TEMPLATE_DECL). Otherwise NULL. */
12848 : :
12849 : : static tree
12850 : 185578 : member_owned_by_class (tree member)
12851 : : {
12852 : 185578 : gcc_assert (DECL_P (member));
12853 : :
12854 : : /* Clones are owned by their origin. */
12855 : 185578 : if (DECL_CLONED_FUNCTION_P (member))
12856 : : return NULL;
12857 : :
12858 : 185578 : if (TREE_CODE (member) == FIELD_DECL)
12859 : : /* FIELD_DECLS can have template info in some cases. We always
12860 : : want the FIELD_DECL though, as there's never a TEMPLATE_DECL
12861 : : wrapping them. */
12862 : : return member;
12863 : :
12864 : 87919 : int use_tpl = -1;
12865 : 87919 : if (tree ti = node_template_info (member, use_tpl))
12866 : : {
12867 : : // FIXME: Don't bail on things that CANNOT have their own
12868 : : // template header. No, make sure they're in the same cluster.
12869 : 0 : if (use_tpl > 0)
12870 : : return NULL_TREE;
12871 : :
12872 : 0 : if (DECL_TEMPLATE_RESULT (TI_TEMPLATE (ti)) == member)
12873 : 185578 : member = TI_TEMPLATE (ti);
12874 : : }
12875 : : return member;
12876 : : }
12877 : :
12878 : : void
12879 : 142304 : trees_out::write_class_def (tree defn)
12880 : : {
12881 : 142304 : gcc_assert (DECL_P (defn));
12882 : 142304 : if (streaming_p ())
12883 : 71402 : dump () && dump ("Writing class definition %N", defn);
12884 : :
12885 : 142304 : tree type = TREE_TYPE (defn);
12886 : 142304 : tree_node (TYPE_SIZE (type));
12887 : 142304 : tree_node (TYPE_SIZE_UNIT (type));
12888 : 142304 : tree_node (TYPE_VFIELD (type));
12889 : 142304 : tree_node (TYPE_BINFO (type));
12890 : :
12891 : 142304 : vec_chained_decls (TYPE_FIELDS (type));
12892 : :
12893 : : /* Every class but __as_base has a type-specific. */
12894 : 282424 : gcc_checking_assert (!TYPE_LANG_SPECIFIC (type) == IS_FAKE_BASE_TYPE (type));
12895 : :
12896 : 142304 : if (TYPE_LANG_SPECIFIC (type))
12897 : : {
12898 : 140120 : {
12899 : 140120 : vec<tree, va_gc> *v = CLASSTYPE_MEMBER_VEC (type);
12900 : 140120 : if (!v)
12901 : : {
12902 : 38644 : gcc_checking_assert (!streaming_p ());
12903 : : /* Force a class vector. */
12904 : 38644 : v = set_class_bindings (type, -1);
12905 : 38644 : gcc_checking_assert (v);
12906 : : }
12907 : :
12908 : 140120 : unsigned len = v->length ();
12909 : 140120 : if (streaming_p ())
12910 : 70040 : u (len);
12911 : 1058214 : for (unsigned ix = 0; ix != len; ix++)
12912 : : {
12913 : 918094 : tree m = (*v)[ix];
12914 : 918094 : if (TREE_CODE (m) == TYPE_DECL
12915 : 293791 : && DECL_ARTIFICIAL (m)
12916 : 1069283 : && TYPE_STUB_DECL (TREE_TYPE (m)) == m)
12917 : : /* This is a using-decl for a type, or an anonymous
12918 : : struct (maybe with a typedef name). Write the type. */
12919 : 10312 : m = TREE_TYPE (m);
12920 : 918094 : tree_node (m);
12921 : : }
12922 : : }
12923 : 140120 : tree_node (CLASSTYPE_LAMBDA_EXPR (type));
12924 : :
12925 : : /* TYPE_CONTAINS_VPTR_P looks at the vbase vector, which the
12926 : : reader won't know at this point. */
12927 : 140120 : int has_vptr = TYPE_CONTAINS_VPTR_P (type);
12928 : :
12929 : 140120 : if (streaming_p ())
12930 : : {
12931 : 70040 : unsigned nvbases = vec_safe_length (CLASSTYPE_VBASECLASSES (type));
12932 : 70040 : u (nvbases);
12933 : 70040 : i (has_vptr);
12934 : : }
12935 : :
12936 : 140120 : if (has_vptr)
12937 : : {
12938 : 4820 : tree_vec (CLASSTYPE_PURE_VIRTUALS (type));
12939 : 4820 : tree_pair_vec (CLASSTYPE_VCALL_INDICES (type));
12940 : 4820 : tree_node (CLASSTYPE_KEY_METHOD (type));
12941 : : }
12942 : : }
12943 : :
12944 : 142304 : if (TYPE_LANG_SPECIFIC (type))
12945 : : {
12946 : 140120 : tree_node (CLASSTYPE_PRIMARY_BINFO (type));
12947 : :
12948 : 140120 : tree as_base = CLASSTYPE_AS_BASE (type);
12949 : 140120 : if (as_base)
12950 : 70157 : as_base = TYPE_NAME (as_base);
12951 : 140120 : tree_node (as_base);
12952 : :
12953 : : /* Write the vtables. */
12954 : 140120 : tree vtables = CLASSTYPE_VTABLES (type);
12955 : 140120 : vec_chained_decls (vtables);
12956 : 285876 : for (; vtables; vtables = TREE_CHAIN (vtables))
12957 : 5636 : write_definition (vtables);
12958 : :
12959 : 140120 : {
12960 : : /* Friend declarations in class definitions are ignored when
12961 : : determining exposures. */
12962 : 140120 : auto ovr = make_temp_override (dep_hash->ignore_tu_local, true);
12963 : :
12964 : : /* Write the friend classes. */
12965 : 140120 : tree_list (CLASSTYPE_FRIEND_CLASSES (type), false);
12966 : :
12967 : : /* Write the friend functions. */
12968 : 140120 : for (tree friends = DECL_FRIENDLIST (defn);
12969 : 156222 : friends; friends = TREE_CHAIN (friends))
12970 : : {
12971 : 16102 : tree_node (FRIEND_NAME (friends));
12972 : 16102 : tree_list (FRIEND_DECLS (friends), false);
12973 : : }
12974 : : /* End of friend fns. */
12975 : 140120 : tree_node (NULL_TREE);
12976 : 140120 : }
12977 : :
12978 : : /* Write the decl list. We don't need to ignore exposures of friend
12979 : : decls here as any such decls should already have been added and
12980 : : ignored above. */
12981 : 140120 : tree_list (CLASSTYPE_DECL_LIST (type), true);
12982 : :
12983 : 140120 : if (TYPE_CONTAINS_VPTR_P (type))
12984 : : {
12985 : : /* Write the thunks. */
12986 : 4820 : for (tree decls = TYPE_FIELDS (type);
12987 : 136674 : decls; decls = DECL_CHAIN (decls))
12988 : 131854 : if (TREE_CODE (decls) == FUNCTION_DECL
12989 : 95480 : && DECL_VIRTUAL_P (decls)
12990 : 156608 : && DECL_THUNKS (decls))
12991 : : {
12992 : 852 : tree_node (decls);
12993 : : /* Thunks are always unique, so chaining is ok. */
12994 : 852 : chained_decls (DECL_THUNKS (decls));
12995 : : }
12996 : 4820 : tree_node (NULL_TREE);
12997 : : }
12998 : : }
12999 : 142304 : }
13000 : :
13001 : : void
13002 : 185578 : trees_out::mark_class_member (tree member, bool do_defn)
13003 : : {
13004 : 185578 : gcc_assert (DECL_P (member));
13005 : :
13006 : 185578 : member = member_owned_by_class (member);
13007 : 185578 : if (member)
13008 : 371156 : mark_declaration (member, do_defn && has_definition (member));
13009 : 185578 : }
13010 : :
13011 : : void
13012 : 142304 : trees_out::mark_class_def (tree defn)
13013 : : {
13014 : 142304 : gcc_assert (DECL_P (defn));
13015 : 142304 : tree type = TREE_TYPE (defn);
13016 : : /* Mark the class members that are not type-decls and cannot have
13017 : : independent definitions. */
13018 : 1438992 : for (tree member = TYPE_FIELDS (type); member; member = DECL_CHAIN (member))
13019 : 1296688 : if (TREE_CODE (member) == FIELD_DECL
13020 : 1296688 : || TREE_CODE (member) == USING_DECL
13021 : : /* A cloned enum-decl from 'using enum unrelated;' */
13022 : 1296688 : || (TREE_CODE (member) == CONST_DECL
13023 : 13798 : && DECL_CONTEXT (member) == type))
13024 : : {
13025 : 185578 : mark_class_member (member);
13026 : 185578 : if (TREE_CODE (member) == FIELD_DECL)
13027 : 97659 : if (tree repr = DECL_BIT_FIELD_REPRESENTATIVE (member))
13028 : : /* If we're marking a class template definition, then
13029 : : this'll contain the width (as set by grokbitfield)
13030 : : instead of a decl. */
13031 : 1952 : if (DECL_P (repr))
13032 : 1652 : mark_declaration (repr, false);
13033 : : }
13034 : :
13035 : : /* Mark the binfo hierarchy. */
13036 : 339312 : for (tree child = TYPE_BINFO (type); child; child = TREE_CHAIN (child))
13037 : 197008 : mark_by_value (child);
13038 : :
13039 : 142304 : if (TYPE_LANG_SPECIFIC (type))
13040 : : {
13041 : 140120 : for (tree vtable = CLASSTYPE_VTABLES (type);
13042 : 145756 : vtable; vtable = TREE_CHAIN (vtable))
13043 : 5636 : mark_declaration (vtable, true);
13044 : :
13045 : 140120 : if (TYPE_CONTAINS_VPTR_P (type))
13046 : : /* Mark the thunks, they belong to the class definition,
13047 : : /not/ the thunked-to function. */
13048 : 4820 : for (tree decls = TYPE_FIELDS (type);
13049 : 136674 : decls; decls = DECL_CHAIN (decls))
13050 : 131854 : if (TREE_CODE (decls) == FUNCTION_DECL)
13051 : 95480 : for (tree thunks = DECL_THUNKS (decls);
13052 : 96608 : thunks; thunks = DECL_CHAIN (thunks))
13053 : 1128 : mark_declaration (thunks, false);
13054 : : }
13055 : 142304 : }
13056 : :
13057 : : /* Nop sorting, needed for resorting the member vec. */
13058 : :
13059 : : static void
13060 : 5624978 : nop (void *, void *, void *)
13061 : : {
13062 : 5624978 : }
13063 : :
13064 : : bool
13065 : 51741 : trees_in::read_class_def (tree defn, tree maybe_template)
13066 : : {
13067 : 51741 : gcc_assert (DECL_P (defn));
13068 : 52243 : dump () && dump ("Reading class definition %N", defn);
13069 : 51741 : tree type = TREE_TYPE (defn);
13070 : 51741 : tree size = tree_node ();
13071 : 51741 : tree size_unit = tree_node ();
13072 : 51741 : tree vfield = tree_node ();
13073 : 51741 : tree binfo = tree_node ();
13074 : 51741 : vec<tree, va_gc> *vbase_vec = NULL;
13075 : 51741 : vec<tree, va_gc> *member_vec = NULL;
13076 : 51741 : vec<tree, va_gc> *pure_virts = NULL;
13077 : 51741 : vec<tree_pair_s, va_gc> *vcall_indices = NULL;
13078 : 51741 : tree key_method = NULL_TREE;
13079 : 51741 : tree lambda = NULL_TREE;
13080 : :
13081 : : /* Read the fields. */
13082 : 51741 : vec<tree, va_heap> *fields = vec_chained_decls ();
13083 : :
13084 : 51741 : if (TYPE_LANG_SPECIFIC (type))
13085 : : {
13086 : 50719 : if (unsigned len = u ())
13087 : : {
13088 : 50719 : vec_alloc (member_vec, len);
13089 : 425082 : for (unsigned ix = 0; ix != len; ix++)
13090 : : {
13091 : 374363 : tree m = tree_node ();
13092 : 374363 : if (get_overrun ())
13093 : : break;
13094 : 374363 : if (TYPE_P (m))
13095 : 3743 : m = TYPE_STUB_DECL (m);
13096 : 374363 : member_vec->quick_push (m);
13097 : : }
13098 : : }
13099 : 50719 : lambda = tree_node ();
13100 : :
13101 : 50719 : if (!get_overrun ())
13102 : : {
13103 : 50719 : unsigned nvbases = u ();
13104 : 50719 : if (nvbases)
13105 : : {
13106 : 246 : vec_alloc (vbase_vec, nvbases);
13107 : 1117 : for (tree child = binfo; child; child = TREE_CHAIN (child))
13108 : 871 : if (BINFO_VIRTUAL_P (child))
13109 : 246 : vbase_vec->quick_push (child);
13110 : : }
13111 : : }
13112 : :
13113 : 50719 : if (!get_overrun ())
13114 : : {
13115 : 50719 : int has_vptr = i ();
13116 : 50719 : if (has_vptr)
13117 : : {
13118 : 2078 : pure_virts = tree_vec ();
13119 : 2078 : vcall_indices = tree_pair_vec ();
13120 : 2078 : key_method = tree_node ();
13121 : : }
13122 : : }
13123 : : }
13124 : :
13125 : 51741 : tree maybe_dup = odr_duplicate (maybe_template, TYPE_SIZE (type));
13126 : 51741 : bool installing = maybe_dup && !TYPE_SIZE (type);
13127 : 22772 : if (installing)
13128 : : {
13129 : 22772 : if (maybe_dup != defn)
13130 : : {
13131 : : // FIXME: This is needed on other defns too, almost
13132 : : // duplicate-decl like? See is_matching_decl too.
13133 : : /* Copy flags from the duplicate. */
13134 : 235 : tree type_dup = TREE_TYPE (maybe_dup);
13135 : :
13136 : : /* Core pieces. */
13137 : 235 : TYPE_MODE_RAW (type) = TYPE_MODE_RAW (type_dup);
13138 : 235 : TYPE_ALIGN_RAW (type) = TYPE_ALIGN_RAW (type_dup);
13139 : 470 : TYPE_WARN_IF_NOT_ALIGN_RAW (type)
13140 : 235 : = TYPE_WARN_IF_NOT_ALIGN_RAW (type_dup);
13141 : 235 : TYPE_USER_ALIGN (type) = TYPE_USER_ALIGN (type_dup);
13142 : :
13143 : 235 : SET_DECL_MODE (defn, DECL_MODE (maybe_dup));
13144 : 235 : DECL_SIZE (defn) = DECL_SIZE (maybe_dup);
13145 : 235 : DECL_SIZE_UNIT (defn) = DECL_SIZE_UNIT (maybe_dup);
13146 : 235 : DECL_ALIGN_RAW (defn) = DECL_ALIGN_RAW (maybe_dup);
13147 : 470 : DECL_WARN_IF_NOT_ALIGN_RAW (defn)
13148 : 235 : = DECL_WARN_IF_NOT_ALIGN_RAW (maybe_dup);
13149 : 235 : DECL_USER_ALIGN (defn) = DECL_USER_ALIGN (maybe_dup);
13150 : :
13151 : 235 : TYPE_TYPELESS_STORAGE (type) = TYPE_TYPELESS_STORAGE (type_dup);
13152 : 235 : TYPE_CXX_ODR_P (type) = TYPE_CXX_ODR_P (type_dup);
13153 : 235 : TYPE_NO_FORCE_BLK (type) = TYPE_NO_FORCE_BLK (type_dup);
13154 : 235 : TYPE_TRANSPARENT_AGGR (type) = TYPE_TRANSPARENT_AGGR (type_dup);
13155 : 470 : TYPE_CONTAINS_PLACEHOLDER_INTERNAL (type)
13156 : 235 : = TYPE_CONTAINS_PLACEHOLDER_INTERNAL (type_dup);
13157 : :
13158 : 235 : TYPE_EMPTY_P (type) = TYPE_EMPTY_P (type_dup);
13159 : 235 : TREE_ADDRESSABLE (type) = TREE_ADDRESSABLE (type_dup);
13160 : :
13161 : : /* C++ pieces. */
13162 : 235 : TYPE_POLYMORPHIC_P (type) = TYPE_POLYMORPHIC_P (type_dup);
13163 : 235 : CLASSTYPE_FINAL (type) = CLASSTYPE_FINAL (type_dup);
13164 : :
13165 : 470 : TYPE_HAS_USER_CONSTRUCTOR (type)
13166 : 235 : = TYPE_HAS_USER_CONSTRUCTOR (type_dup);
13167 : 470 : TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type)
13168 : 235 : = TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type_dup);
13169 : 470 : TYPE_NEEDS_CONSTRUCTING (type)
13170 : 235 : = TYPE_NEEDS_CONSTRUCTING (type_dup);
13171 : :
13172 : 235 : if (auto ls = TYPE_LANG_SPECIFIC (type_dup))
13173 : : {
13174 : 235 : if (TYPE_LANG_SPECIFIC (type))
13175 : : {
13176 : 705 : CLASSTYPE_BEFRIENDING_CLASSES (type_dup)
13177 : 235 : = CLASSTYPE_BEFRIENDING_CLASSES (type);
13178 : 235 : if (!ANON_AGGR_TYPE_P (type))
13179 : 705 : CLASSTYPE_TYPEINFO_VAR (type_dup)
13180 : 235 : = CLASSTYPE_TYPEINFO_VAR (type);
13181 : : }
13182 : 1090 : for (tree v = type; v; v = TYPE_NEXT_VARIANT (v))
13183 : 855 : TYPE_LANG_SPECIFIC (v) = ls;
13184 : : }
13185 : : }
13186 : :
13187 : 22772 : TYPE_SIZE (type) = size;
13188 : 22772 : TYPE_SIZE_UNIT (type) = size_unit;
13189 : :
13190 : 22772 : if (fields)
13191 : : {
13192 : 22772 : tree *chain = &TYPE_FIELDS (type);
13193 : 22772 : unsigned len = fields->length ();
13194 : 255438 : for (unsigned ix = 0; ix != len; ix++)
13195 : : {
13196 : 232666 : tree decl = (*fields)[ix];
13197 : :
13198 : 232666 : if (!decl)
13199 : : {
13200 : : /* An anonymous struct with typedef name. */
13201 : 3 : tree tdef = (*fields)[ix+1];
13202 : 3 : decl = TYPE_STUB_DECL (TREE_TYPE (tdef));
13203 : 3 : gcc_checking_assert (IDENTIFIER_ANON_P (DECL_NAME (decl))
13204 : : && decl != tdef);
13205 : : }
13206 : :
13207 : 421601 : gcc_checking_assert (!*chain == !DECL_CLONED_FUNCTION_P (decl));
13208 : 232666 : *chain = decl;
13209 : 232666 : chain = &DECL_CHAIN (decl);
13210 : :
13211 : 232666 : if (TREE_CODE (decl) == FIELD_DECL
13212 : 232666 : && ANON_AGGR_TYPE_P (TREE_TYPE (decl)))
13213 : : {
13214 : 157 : tree anon_type = TYPE_MAIN_VARIANT (TREE_TYPE (decl));
13215 : 157 : if (DECL_NAME (defn) == as_base_identifier)
13216 : : /* ANON_AGGR_TYPE_FIELD should already point to the
13217 : : original FIELD_DECL; don't overwrite it to point
13218 : : to the as-base FIELD_DECL copy. */
13219 : 10 : gcc_checking_assert (ANON_AGGR_TYPE_FIELD (anon_type));
13220 : : else
13221 : 147 : ANON_AGGR_TYPE_FIELD (anon_type) = decl;
13222 : : }
13223 : :
13224 : 232666 : if (TREE_CODE (decl) == USING_DECL
13225 : 232666 : && TREE_CODE (USING_DECL_SCOPE (decl)) == RECORD_TYPE)
13226 : : {
13227 : : /* Reconstruct DECL_ACCESS. */
13228 : 10564 : tree decls = USING_DECL_DECLS (decl);
13229 : 10564 : tree access = declared_access (decl);
13230 : :
13231 : 12084 : for (ovl_iterator iter (decls); iter; ++iter)
13232 : : {
13233 : 970 : tree d = *iter;
13234 : :
13235 : 970 : retrofit_lang_decl (d);
13236 : 970 : tree list = DECL_ACCESS (d);
13237 : :
13238 : 970 : if (!purpose_member (type, list))
13239 : 1144 : DECL_ACCESS (d) = tree_cons (type, access, list);
13240 : : }
13241 : : }
13242 : : }
13243 : : }
13244 : :
13245 : 22772 : TYPE_VFIELD (type) = vfield;
13246 : 22772 : TYPE_BINFO (type) = binfo;
13247 : :
13248 : 22772 : if (TYPE_LANG_SPECIFIC (type))
13249 : : {
13250 : 22257 : CLASSTYPE_LAMBDA_EXPR (type) = lambda;
13251 : :
13252 : 22257 : CLASSTYPE_MEMBER_VEC (type) = member_vec;
13253 : 22257 : CLASSTYPE_PURE_VIRTUALS (type) = pure_virts;
13254 : 22257 : CLASSTYPE_VCALL_INDICES (type) = vcall_indices;
13255 : :
13256 : 22257 : CLASSTYPE_KEY_METHOD (type) = key_method;
13257 : :
13258 : 22257 : CLASSTYPE_VBASECLASSES (type) = vbase_vec;
13259 : :
13260 : : /* Resort the member vector. */
13261 : 22257 : resort_type_member_vec (member_vec, NULL, nop, NULL);
13262 : : }
13263 : : }
13264 : : else if (maybe_dup)
13265 : : {
13266 : : // FIXME:QOI Check matching defn
13267 : : }
13268 : :
13269 : 51741 : if (TYPE_LANG_SPECIFIC (type))
13270 : : {
13271 : 50719 : tree primary = tree_node ();
13272 : 50719 : tree as_base = tree_node ();
13273 : :
13274 : 50719 : if (as_base)
13275 : 25464 : as_base = TREE_TYPE (as_base);
13276 : :
13277 : : /* Read the vtables. */
13278 : 50719 : vec<tree, va_heap> *vtables = vec_chained_decls ();
13279 : 50719 : if (vtables)
13280 : : {
13281 : 2044 : unsigned len = vtables->length ();
13282 : 4503 : for (unsigned ix = 0; ix != len; ix++)
13283 : : {
13284 : 2459 : tree vtable = (*vtables)[ix];
13285 : 2459 : read_var_def (vtable, vtable);
13286 : : }
13287 : : }
13288 : :
13289 : 50719 : tree friend_classes = tree_list (false);
13290 : 50719 : tree friend_functions = NULL_TREE;
13291 : 50719 : for (tree *chain = &friend_functions;
13292 : 58149 : tree name = tree_node (); chain = &TREE_CHAIN (*chain))
13293 : : {
13294 : 7430 : tree val = tree_list (false);
13295 : 7430 : *chain = build_tree_list (name, val);
13296 : 7430 : }
13297 : 50719 : tree decl_list = tree_list (true);
13298 : :
13299 : 50719 : if (installing)
13300 : : {
13301 : 22257 : CLASSTYPE_PRIMARY_BINFO (type) = primary;
13302 : 22257 : CLASSTYPE_AS_BASE (type) = as_base;
13303 : :
13304 : 22257 : if (vtables)
13305 : : {
13306 : 1030 : if ((!CLASSTYPE_KEY_METHOD (type)
13307 : : /* Sneaky user may have defined it inline
13308 : : out-of-class. */
13309 : 341 : || DECL_DECLARED_INLINE_P (CLASSTYPE_KEY_METHOD (type)))
13310 : : /* An imported non-template class attached to a module
13311 : : doesn't need to have its vtables emitted here. */
13312 : 1036 : && (CLASSTYPE_USE_TEMPLATE (type)
13313 : 96 : || !DECL_MODULE_ATTACH_P (defn)))
13314 : 647 : vec_safe_push (keyed_classes, type);
13315 : 1030 : unsigned len = vtables->length ();
13316 : 1030 : tree *chain = &CLASSTYPE_VTABLES (type);
13317 : 2270 : for (unsigned ix = 0; ix != len; ix++)
13318 : : {
13319 : 1240 : tree vtable = (*vtables)[ix];
13320 : 1240 : gcc_checking_assert (!*chain);
13321 : 1240 : *chain = vtable;
13322 : 1240 : chain = &DECL_CHAIN (vtable);
13323 : : }
13324 : : }
13325 : 22257 : CLASSTYPE_FRIEND_CLASSES (type) = friend_classes;
13326 : 22257 : DECL_FRIENDLIST (defn) = friend_functions;
13327 : 22257 : CLASSTYPE_DECL_LIST (type) = decl_list;
13328 : :
13329 : 23797 : for (; friend_classes; friend_classes = TREE_CHAIN (friend_classes))
13330 : : {
13331 : 1540 : tree f = TREE_VALUE (friend_classes);
13332 : 1540 : if (TREE_CODE (f) == TEMPLATE_DECL)
13333 : 595 : f = TREE_TYPE (f);
13334 : :
13335 : 1540 : if (CLASS_TYPE_P (f))
13336 : : {
13337 : 1508 : CLASSTYPE_BEFRIENDING_CLASSES (f)
13338 : 3016 : = tree_cons (NULL_TREE, type,
13339 : 1508 : CLASSTYPE_BEFRIENDING_CLASSES (f));
13340 : 1546 : dump () && dump ("Class %N befriending %C:%N",
13341 : 6 : type, TREE_CODE (f), f);
13342 : : }
13343 : : }
13344 : :
13345 : 25568 : for (; friend_functions;
13346 : 3311 : friend_functions = TREE_CHAIN (friend_functions))
13347 : 3311 : for (tree friend_decls = TREE_VALUE (friend_functions);
13348 : 7452 : friend_decls; friend_decls = TREE_CHAIN (friend_decls))
13349 : : {
13350 : 4141 : tree f = TREE_VALUE (friend_decls);
13351 : 4141 : if (TREE_CODE (f) == TU_LOCAL_ENTITY)
13352 : 36 : continue;
13353 : :
13354 : 4105 : DECL_BEFRIENDING_CLASSES (f)
13355 : 4105 : = tree_cons (NULL_TREE, type, DECL_BEFRIENDING_CLASSES (f));
13356 : 4168 : dump () && dump ("Class %N befriending %C:%N",
13357 : 27 : type, TREE_CODE (f), f);
13358 : : }
13359 : : }
13360 : :
13361 : 50719 : if (TYPE_CONTAINS_VPTR_P (type))
13362 : : /* Read and install the thunks. */
13363 : 2454 : while (tree vfunc = tree_node ())
13364 : : {
13365 : 376 : tree thunks = chained_decls ();
13366 : 376 : if (installing)
13367 : 186 : SET_DECL_THUNKS (vfunc, thunks);
13368 : : }
13369 : :
13370 : 50719 : vec_free (vtables);
13371 : : }
13372 : :
13373 : : /* Propagate to all variants. */
13374 : 51741 : if (installing)
13375 : 22772 : fixup_type_variants (type);
13376 : :
13377 : : /* IS_FAKE_BASE_TYPE is inaccurate at this point, because if this is
13378 : : the fake base, we've not hooked it into the containing class's
13379 : : data structure yet. Fortunately it has a unique name. */
13380 : 22772 : if (installing
13381 : 22772 : && DECL_NAME (defn) != as_base_identifier
13382 : 22257 : && (!CLASSTYPE_TEMPLATE_INFO (type)
13383 : 18894 : || !uses_template_parms (TI_ARGS (CLASSTYPE_TEMPLATE_INFO (type)))))
13384 : : /* Emit debug info. It'd be nice to know if the interface TU
13385 : : already emitted this. */
13386 : 12449 : rest_of_type_compilation (type, !LOCAL_CLASS_P (type));
13387 : :
13388 : 51741 : vec_free (fields);
13389 : :
13390 : 51741 : return !get_overrun ();
13391 : : }
13392 : :
13393 : : void
13394 : 7625 : trees_out::write_enum_def (tree decl)
13395 : : {
13396 : 7625 : tree type = TREE_TYPE (decl);
13397 : :
13398 : 7625 : tree_node (TYPE_VALUES (type));
13399 : : /* Note that we stream TYPE_MIN/MAX_VALUE directly as part of the
13400 : : ENUMERAL_TYPE. */
13401 : 7625 : }
13402 : :
13403 : : void
13404 : 7625 : trees_out::mark_enum_def (tree decl)
13405 : : {
13406 : 7625 : tree type = TREE_TYPE (decl);
13407 : :
13408 : 36258 : for (tree values = TYPE_VALUES (type); values; values = TREE_CHAIN (values))
13409 : : {
13410 : 28633 : tree cst = TREE_VALUE (values);
13411 : 28633 : mark_by_value (cst);
13412 : : /* We must mark the init to avoid circularity in tt_enum_int. */
13413 : 28633 : if (tree init = DECL_INITIAL (cst))
13414 : 28425 : if (TREE_CODE (init) == INTEGER_CST)
13415 : 27865 : mark_by_value (init);
13416 : : }
13417 : 7625 : }
13418 : :
13419 : : bool
13420 : 2460 : trees_in::read_enum_def (tree defn, tree maybe_template)
13421 : : {
13422 : 2460 : tree type = TREE_TYPE (defn);
13423 : 2460 : tree values = tree_node ();
13424 : :
13425 : 2460 : if (get_overrun ())
13426 : : return false;
13427 : :
13428 : 2460 : tree maybe_dup = odr_duplicate (maybe_template, TYPE_VALUES (type));
13429 : 4920 : bool installing = maybe_dup && !TYPE_VALUES (type);
13430 : :
13431 : 2460 : if (installing)
13432 : : {
13433 : 1016 : TYPE_VALUES (type) = values;
13434 : : /* Note that we stream TYPE_MIN/MAX_VALUE directly as part of the
13435 : : ENUMERAL_TYPE. */
13436 : :
13437 : 1667 : rest_of_type_compilation (type, DECL_NAMESPACE_SCOPE_P (defn));
13438 : : }
13439 : 1444 : else if (maybe_dup)
13440 : : {
13441 : 1444 : tree known = TYPE_VALUES (type);
13442 : 8054 : for (; known && values;
13443 : 6610 : known = TREE_CHAIN (known), values = TREE_CHAIN (values))
13444 : : {
13445 : 6619 : tree known_decl = TREE_VALUE (known);
13446 : 6619 : tree new_decl = TREE_VALUE (values);
13447 : :
13448 : 6619 : if (DECL_NAME (known_decl) != DECL_NAME (new_decl))
13449 : : break;
13450 : :
13451 : 6613 : new_decl = maybe_duplicate (new_decl);
13452 : :
13453 : 6613 : if (!cp_tree_equal (DECL_INITIAL (known_decl),
13454 : 6613 : DECL_INITIAL (new_decl)))
13455 : : break;
13456 : : }
13457 : :
13458 : 1444 : if (known || values)
13459 : : {
13460 : 12 : auto_diagnostic_group d;
13461 : 12 : error_at (DECL_SOURCE_LOCATION (maybe_dup),
13462 : : "definition of %qD does not match", maybe_dup);
13463 : 12 : inform (DECL_SOURCE_LOCATION (defn),
13464 : : "existing definition %qD", defn);
13465 : :
13466 : 12 : tree known_decl = NULL_TREE, new_decl = NULL_TREE;
13467 : :
13468 : 12 : if (known)
13469 : 9 : known_decl = TREE_VALUE (known);
13470 : 12 : if (values)
13471 : 12 : new_decl = maybe_duplicate (TREE_VALUE (values));
13472 : :
13473 : 12 : if (known_decl && new_decl)
13474 : : {
13475 : 9 : inform (DECL_SOURCE_LOCATION (new_decl),
13476 : : "enumerator %qD does not match ...", new_decl);
13477 : 9 : inform (DECL_SOURCE_LOCATION (known_decl),
13478 : : "... this enumerator %qD", known_decl);
13479 : : }
13480 : 3 : else if (known_decl || new_decl)
13481 : : {
13482 : 3 : tree extra = known_decl ? known_decl : new_decl;
13483 : 3 : inform (DECL_SOURCE_LOCATION (extra),
13484 : : "additional enumerators beginning with %qD", extra);
13485 : : }
13486 : : else
13487 : 0 : inform (DECL_SOURCE_LOCATION (maybe_dup),
13488 : : "enumeration range differs");
13489 : :
13490 : : /* Mark it bad. */
13491 : 12 : unmatched_duplicate (maybe_template);
13492 : 12 : }
13493 : : }
13494 : :
13495 : : return true;
13496 : : }
13497 : :
13498 : : /* Write out the body of DECL. See above circularity note. */
13499 : :
13500 : : void
13501 : 572924 : trees_out::write_definition (tree decl, bool refs_tu_local)
13502 : : {
13503 : 572924 : auto ovr = make_temp_override (writing_local_entities,
13504 : 572924 : writing_local_entities || refs_tu_local);
13505 : :
13506 : 572924 : if (streaming_p ())
13507 : : {
13508 : 286385 : assert_definition (decl);
13509 : 286385 : dump ()
13510 : 573 : && dump ("Writing definition %C:%N", TREE_CODE (decl), decl);
13511 : : }
13512 : : else
13513 : 286539 : dump (dumper::DEPEND)
13514 : 75 : && dump ("Depending definition %C:%N", TREE_CODE (decl), decl);
13515 : :
13516 : 906882 : again:
13517 : 906882 : switch (TREE_CODE (decl))
13518 : : {
13519 : 0 : default:
13520 : 0 : gcc_unreachable ();
13521 : :
13522 : 333958 : case TEMPLATE_DECL:
13523 : 333958 : decl = DECL_TEMPLATE_RESULT (decl);
13524 : 333958 : goto again;
13525 : :
13526 : 347323 : case FUNCTION_DECL:
13527 : 347323 : write_function_def (decl);
13528 : 347323 : break;
13529 : :
13530 : 149929 : case TYPE_DECL:
13531 : 149929 : {
13532 : 149929 : tree type = TREE_TYPE (decl);
13533 : 149929 : gcc_assert (TYPE_MAIN_VARIANT (type) == type
13534 : : && TYPE_NAME (type) == decl);
13535 : 149929 : if (TREE_CODE (type) == ENUMERAL_TYPE)
13536 : 7625 : write_enum_def (decl);
13537 : : else
13538 : 142304 : write_class_def (decl);
13539 : : }
13540 : : break;
13541 : :
13542 : 75672 : case VAR_DECL:
13543 : 75672 : case CONCEPT_DECL:
13544 : 75672 : write_var_def (decl);
13545 : 75672 : break;
13546 : : }
13547 : 572924 : }
13548 : :
13549 : : /* Mark a declaration for by-value walking. If DO_DEFN is true, mark
13550 : : its body too. */
13551 : :
13552 : : void
13553 : 2487682 : trees_out::mark_declaration (tree decl, bool do_defn)
13554 : : {
13555 : 2487682 : mark_by_value (decl);
13556 : :
13557 : 2487682 : if (TREE_CODE (decl) == TEMPLATE_DECL)
13558 : 838032 : decl = DECL_TEMPLATE_RESULT (decl);
13559 : :
13560 : 2487682 : if (!do_defn)
13561 : : return;
13562 : :
13563 : 572924 : switch (TREE_CODE (decl))
13564 : : {
13565 : 0 : default:
13566 : 0 : gcc_unreachable ();
13567 : :
13568 : : case FUNCTION_DECL:
13569 : : mark_function_def (decl);
13570 : : break;
13571 : :
13572 : 149929 : case TYPE_DECL:
13573 : 149929 : {
13574 : 149929 : tree type = TREE_TYPE (decl);
13575 : 149929 : gcc_assert (TYPE_MAIN_VARIANT (type) == type
13576 : : && TYPE_NAME (type) == decl);
13577 : 149929 : if (TREE_CODE (type) == ENUMERAL_TYPE)
13578 : 7625 : mark_enum_def (decl);
13579 : : else
13580 : 142304 : mark_class_def (decl);
13581 : : }
13582 : : break;
13583 : :
13584 : : case VAR_DECL:
13585 : : case CONCEPT_DECL:
13586 : : mark_var_def (decl);
13587 : : break;
13588 : : }
13589 : : }
13590 : :
13591 : : /* Read in the body of DECL. See above circularity note. */
13592 : :
13593 : : bool
13594 : 233421 : trees_in::read_definition (tree decl)
13595 : : {
13596 : 234469 : dump () && dump ("Reading definition %C %N", TREE_CODE (decl), decl);
13597 : :
13598 : : tree maybe_template = decl;
13599 : :
13600 : 233421 : again:
13601 : 373140 : switch (TREE_CODE (decl))
13602 : : {
13603 : : default:
13604 : : break;
13605 : :
13606 : 139719 : case TEMPLATE_DECL:
13607 : 139719 : decl = DECL_TEMPLATE_RESULT (decl);
13608 : 139719 : goto again;
13609 : :
13610 : 151370 : case FUNCTION_DECL:
13611 : 151370 : return read_function_def (decl, maybe_template);
13612 : :
13613 : 54201 : case TYPE_DECL:
13614 : 54201 : {
13615 : 54201 : tree type = TREE_TYPE (decl);
13616 : 54201 : gcc_assert (TYPE_MAIN_VARIANT (type) == type
13617 : : && TYPE_NAME (type) == decl);
13618 : 54201 : if (TREE_CODE (type) == ENUMERAL_TYPE)
13619 : 2460 : return read_enum_def (decl, maybe_template);
13620 : : else
13621 : 51741 : return read_class_def (decl, maybe_template);
13622 : : }
13623 : 27850 : break;
13624 : :
13625 : 27850 : case VAR_DECL:
13626 : 27850 : case CONCEPT_DECL:
13627 : 27850 : return read_var_def (decl, maybe_template);
13628 : : }
13629 : :
13630 : : return false;
13631 : : }
13632 : :
13633 : : /* Lookup an maybe insert a slot for depset for KEY. */
13634 : :
13635 : : depset **
13636 : 11657601 : depset::hash::entity_slot (tree entity, bool insert)
13637 : : {
13638 : 11657601 : traits::compare_type key (entity, NULL);
13639 : 17692633 : depset **slot = find_slot_with_hash (key, traits::hash (key),
13640 : : insert ? INSERT : NO_INSERT);
13641 : :
13642 : 11657601 : return slot;
13643 : : }
13644 : :
13645 : : depset **
13646 : 149413 : depset::hash::binding_slot (tree ctx, tree name, bool insert)
13647 : : {
13648 : 149413 : traits::compare_type key (ctx, name);
13649 : 182618 : depset **slot = find_slot_with_hash (key, traits::hash (key),
13650 : : insert ? INSERT : NO_INSERT);
13651 : :
13652 : 149413 : return slot;
13653 : : }
13654 : :
13655 : : depset *
13656 : 5744988 : depset::hash::find_dependency (tree decl)
13657 : : {
13658 : 5744988 : depset **slot = entity_slot (decl, false);
13659 : :
13660 : 5744988 : return slot ? *slot : NULL;
13661 : : }
13662 : :
13663 : : depset *
13664 : 33205 : depset::hash::find_binding (tree ctx, tree name)
13665 : : {
13666 : 33205 : depset **slot = binding_slot (ctx, name, false);
13667 : :
13668 : 33205 : return slot ? *slot : NULL;
13669 : : }
13670 : :
13671 : : /* Returns true if DECL is a TU-local entity, as defined by [basic.link].
13672 : : If EXPLAIN is true, emit an informative note about why DECL is TU-local. */
13673 : :
13674 : : bool
13675 : 1064740 : depset::hash::is_tu_local_entity (tree decl, bool explain/*=false*/)
13676 : : {
13677 : 1064740 : gcc_checking_assert (DECL_P (decl));
13678 : 1064740 : location_t loc = DECL_SOURCE_LOCATION (decl);
13679 : 1064740 : tree type = TREE_TYPE (decl);
13680 : :
13681 : : /* Only types, functions, variables, and template (specialisations)
13682 : : can be TU-local. */
13683 : 1064740 : if (TREE_CODE (decl) != TYPE_DECL
13684 : 1064740 : && TREE_CODE (decl) != FUNCTION_DECL
13685 : 397227 : && TREE_CODE (decl) != VAR_DECL
13686 : 383304 : && TREE_CODE (decl) != TEMPLATE_DECL)
13687 : : return false;
13688 : :
13689 : : /* An explicit type alias is not an entity; we don't want to stream
13690 : : such aliases if they refer to TU-local entities, so propagate this
13691 : : from the original type. The built-in declarations of 'int' and such
13692 : : are never TU-local. */
13693 : 1062187 : if (TREE_CODE (decl) == TYPE_DECL
13694 : 573828 : && !DECL_SELF_REFERENCE_P (decl)
13695 : 1612711 : && !DECL_IMPLICIT_TYPEDEF_P (decl))
13696 : : {
13697 : 314049 : tree orig = DECL_ORIGINAL_TYPE (decl);
13698 : 314049 : if (orig && TYPE_NAME (orig))
13699 : : {
13700 : 62662 : if (explain)
13701 : 11 : inform (loc, "%qD is an alias of TU-local type %qT", decl, orig);
13702 : 62662 : return is_tu_local_entity (TYPE_NAME (orig), explain);
13703 : : }
13704 : : else
13705 : : return false;
13706 : : }
13707 : :
13708 : : /* Check specializations first for slightly better explanations. */
13709 : 748138 : int use_tpl = -1;
13710 : 748138 : tree ti = node_template_info (decl, use_tpl);
13711 : 1057999 : if (use_tpl > 0 && TREE_CODE (TI_TEMPLATE (ti)) == TEMPLATE_DECL)
13712 : : {
13713 : : /* A specialization of a TU-local template. */
13714 : 309848 : tree tmpl = TI_TEMPLATE (ti);
13715 : 309848 : if (is_tu_local_entity (tmpl))
13716 : : {
13717 : 42 : if (explain)
13718 : : {
13719 : 9 : inform (loc, "%qD is a specialization of TU-local template %qD",
13720 : : decl, tmpl);
13721 : 9 : is_tu_local_entity (tmpl, /*explain=*/true);
13722 : : }
13723 : 42 : return true;
13724 : : }
13725 : :
13726 : : /* A specialization of a template with any TU-local template argument. */
13727 : 309806 : if (has_tu_local_tmpl_arg (decl, TI_ARGS (ti), explain))
13728 : : return true;
13729 : :
13730 : : /* FIXME A specialization of a template whose (possibly instantiated)
13731 : : declaration is an exposure. This should always be covered by the
13732 : : above cases?? */
13733 : : }
13734 : :
13735 : : /* A type, function, variable, or template with internal linkage. */
13736 : 748069 : linkage_kind kind = decl_linkage (decl);
13737 : 748069 : if (kind == lk_internal
13738 : : /* But although weakrefs are marked static, don't consider them
13739 : : to be TU-local. */
13740 : 748069 : && !lookup_attribute ("weakref", DECL_ATTRIBUTES (decl)))
13741 : : {
13742 : 567 : if (explain)
13743 : 126 : inform (loc, "%qD declared with internal linkage", decl);
13744 : 567 : return true;
13745 : : }
13746 : :
13747 : : /* Does not have a name with linkage and is declared, or introduced by a
13748 : : lambda-expression, within the definition of a TU-local entity. */
13749 : 747502 : if (kind == lk_none)
13750 : : {
13751 : 16734 : tree ctx = CP_DECL_CONTEXT (decl);
13752 : 28897 : if (LAMBDA_TYPE_P (type))
13753 : 3856 : if (tree extra = LAMBDA_TYPE_EXTRA_SCOPE (type))
13754 : 16734 : ctx = extra;
13755 : :
13756 : 16734 : if (TREE_CODE (ctx) == NAMESPACE_DECL)
13757 : : {
13758 : 62 : if (!TREE_PUBLIC (ctx))
13759 : : {
13760 : 0 : if (explain)
13761 : 0 : inform (loc, "%qD has no linkage and is declared in an "
13762 : : "anonymous namespace", decl);
13763 : 0 : return true;
13764 : : }
13765 : : }
13766 : 16672 : else if (TYPE_P (ctx))
13767 : : {
13768 : 5784 : tree ctx_decl = TYPE_MAIN_DECL (ctx);
13769 : 5784 : if (is_tu_local_entity (ctx_decl))
13770 : : {
13771 : 0 : if (explain)
13772 : : {
13773 : 0 : inform (loc, "%qD has no linkage and is declared within "
13774 : : "TU-local entity %qT", decl, ctx);
13775 : 0 : is_tu_local_entity (ctx_decl, /*explain=*/true);
13776 : : }
13777 : 0 : return true;
13778 : : }
13779 : : }
13780 : 10888 : else if (is_tu_local_entity (ctx))
13781 : : {
13782 : 27 : if (explain)
13783 : : {
13784 : 9 : inform (loc, "%qD has no linkage and is declared within "
13785 : : "TU-local entity %qD", decl, ctx);
13786 : 9 : is_tu_local_entity (ctx, /*explain=*/true);
13787 : : }
13788 : 27 : return true;
13789 : : }
13790 : : }
13791 : :
13792 : : /* A type with no name that is defined outside a class-specifier, function
13793 : : body, or initializer; or is introduced by a defining-type-specifier that
13794 : : is used to declare only TU-local entities.
13795 : :
13796 : : We consider types with names for linkage purposes as having names, since
13797 : : these aren't really TU-local. */
13798 : 747475 : tree inner = STRIP_TEMPLATE (decl);
13799 : 380652 : if (inner
13800 : 747475 : && TREE_CODE (inner) == TYPE_DECL
13801 : 1029080 : && TYPE_ANON_P (type)
13802 : 5700 : && !DECL_SELF_REFERENCE_P (inner)
13803 : : /* An enum with an enumerator name for linkage. */
13804 : 385549 : && !(UNSCOPED_ENUM_P (type) && TYPE_VALUES (type)))
13805 : : {
13806 : 3561 : tree main_decl = TYPE_MAIN_DECL (type);
13807 : 6906 : if (LAMBDA_TYPE_P (type))
13808 : : {
13809 : : /* A lambda expression is, in practice, TU-local iff it has no
13810 : : mangling scope. This currently doesn't line up exactly with
13811 : : the standard's definition due to some ABI issues, but it's
13812 : : pretty close, and avoids other issues down the line. */
13813 : 3314 : if (!LAMBDA_TYPE_EXTRA_SCOPE (type))
13814 : : {
13815 : 14 : if (explain)
13816 : 6 : inform (loc, "%qT has no name and cannot be differentiated "
13817 : : "from similar lambdas in other TUs", type);
13818 : 14 : return true;
13819 : : }
13820 : : }
13821 : 494 : else if (!DECL_CLASS_SCOPE_P (main_decl)
13822 : 277 : && !decl_function_context (main_decl))
13823 : : {
13824 : 30 : if (explain)
13825 : 12 : inform (loc, "%qT has no name and is not defined within a class, "
13826 : : "function, or initializer", type);
13827 : 30 : return true;
13828 : : }
13829 : :
13830 : : // FIXME introduced by a defining-type-specifier only declaring TU-local
13831 : : // entities; does this refer to e.g. 'static struct {} a;"? I can't
13832 : : // think of any cases where this isn't covered by earlier cases. */
13833 : : }
13834 : :
13835 : : return false;
13836 : : }
13837 : :
13838 : : /* Helper for is_tu_local_entity. Returns true if one of the ARGS of
13839 : : DECL is TU-local. Emits an explanation if EXPLAIN is true. */
13840 : :
13841 : : bool
13842 : 347686 : depset::hash::has_tu_local_tmpl_arg (tree decl, tree args, bool explain)
13843 : : {
13844 : 347686 : if (!args || TREE_CODE (args) != TREE_VEC)
13845 : : return false;
13846 : :
13847 : 929320 : for (tree a : tree_vec_range (args))
13848 : : {
13849 : 581661 : if (TREE_CODE (a) == TREE_VEC)
13850 : : {
13851 : 37880 : if (has_tu_local_tmpl_arg (decl, a, explain))
13852 : 27 : return true;
13853 : : }
13854 : 543781 : else if (!WILDCARD_TYPE_P (a))
13855 : : {
13856 : 491411 : if (DECL_P (a) && is_tu_local_entity (a))
13857 : : {
13858 : 0 : if (explain)
13859 : : {
13860 : 0 : inform (DECL_SOURCE_LOCATION (decl),
13861 : : "%qD has TU-local template argument %qD",
13862 : : decl, a);
13863 : 0 : is_tu_local_entity (a, /*explain=*/true);
13864 : : }
13865 : 0 : return true;
13866 : : }
13867 : :
13868 : 491411 : if (TYPE_P (a) && TYPE_NAME (a) && is_tu_local_entity (TYPE_NAME (a)))
13869 : : {
13870 : 12 : if (explain)
13871 : : {
13872 : 0 : inform (DECL_SOURCE_LOCATION (decl),
13873 : : "%qD has TU-local template argument %qT",
13874 : : decl, a);
13875 : 0 : is_tu_local_entity (TYPE_NAME (a), /*explain=*/true);
13876 : : }
13877 : 12 : return true;
13878 : : }
13879 : :
13880 : 491399 : if (EXPR_P (a) && is_tu_local_value (decl, a, explain))
13881 : : return true;
13882 : : }
13883 : : }
13884 : :
13885 : 347659 : return false;
13886 : : }
13887 : :
13888 : : /* Returns true if EXPR (part of the initializer for DECL) is a TU-local value
13889 : : or object. Emits an explanation if EXPLAIN is true. */
13890 : :
13891 : : bool
13892 : 33611 : depset::hash::is_tu_local_value (tree decl, tree expr, bool explain)
13893 : : {
13894 : 33611 : if (!expr)
13895 : : return false;
13896 : :
13897 : 32605 : tree e = expr;
13898 : 32605 : STRIP_ANY_LOCATION_WRAPPER (e);
13899 : 32605 : STRIP_NOPS (e);
13900 : 32605 : if (TREE_CODE (e) == TARGET_EXPR)
13901 : 0 : e = TARGET_EXPR_INITIAL (e);
13902 : 0 : if (!e)
13903 : : return false;
13904 : :
13905 : : /* It is, or is a pointer to, a TU-local function or the object associated
13906 : : with a TU-local variable. */
13907 : 32605 : tree object = NULL_TREE;
13908 : 32605 : if (TREE_CODE (e) == ADDR_EXPR)
13909 : 1876 : object = TREE_OPERAND (e, 0);
13910 : 30729 : else if (TREE_CODE (e) == PTRMEM_CST)
13911 : 0 : object = PTRMEM_CST_MEMBER (e);
13912 : 30729 : else if (VAR_OR_FUNCTION_DECL_P (e))
13913 : : object = e;
13914 : :
13915 : 1876 : if (object
13916 : 2152 : && VAR_OR_FUNCTION_DECL_P (object)
13917 : 2236 : && is_tu_local_entity (object))
13918 : : {
13919 : 54 : if (explain)
13920 : : {
13921 : : /* We've lost a lot of location information by the time we get here,
13922 : : so let's just do our best effort. */
13923 : 18 : auto loc = cp_expr_loc_or_loc (expr, DECL_SOURCE_LOCATION (decl));
13924 : 18 : if (VAR_P (object))
13925 : 9 : inform (loc, "%qD refers to TU-local object %qD", decl, object);
13926 : : else
13927 : 9 : inform (loc, "%qD refers to TU-local function %qD", decl, object);
13928 : 18 : is_tu_local_entity (object, true);
13929 : : }
13930 : 54 : return true;
13931 : : }
13932 : :
13933 : : /* It is an object of class or array type and any of its subobjects or
13934 : : any of the objects or functions to which its non-static data members
13935 : : of reference type refer is TU-local and is usable in constant
13936 : : expressions. */
13937 : 32551 : if (TREE_CODE (e) == CONSTRUCTOR && AGGREGATE_TYPE_P (TREE_TYPE (e)))
13938 : 20218 : for (auto &f : CONSTRUCTOR_ELTS (e))
13939 : 13868 : if (is_tu_local_value (decl, f.value, explain))
13940 : : return true;
13941 : :
13942 : : return false;
13943 : : }
13944 : :
13945 : : /* DECL is a newly discovered dependency. Create the depset, if it
13946 : : doesn't already exist. Add it to the worklist if so.
13947 : :
13948 : : DECL will be an OVL_USING_P OVERLOAD, if it's from a binding that's
13949 : : a using decl.
13950 : :
13951 : : We do not have to worry about adding the same dependency more than
13952 : : once. First it's harmless, but secondly the TREE_VISITED marking
13953 : : prevents us wanting to do it anyway. */
13954 : :
13955 : : depset *
13956 : 4858196 : depset::hash::make_dependency (tree decl, entity_kind ek)
13957 : : {
13958 : : /* Make sure we're being told consistent information. */
13959 : 9006545 : gcc_checking_assert ((ek == EK_NAMESPACE)
13960 : : == (TREE_CODE (decl) == NAMESPACE_DECL
13961 : : && !DECL_NAMESPACE_ALIAS (decl)));
13962 : 4858196 : gcc_checking_assert (ek != EK_BINDING && ek != EK_REDIRECT);
13963 : 4858196 : gcc_checking_assert (TREE_CODE (decl) != FIELD_DECL
13964 : : && (TREE_CODE (decl) != USING_DECL
13965 : : || TREE_CODE (DECL_CONTEXT (decl)) == FUNCTION_DECL));
13966 : 4858196 : gcc_checking_assert (!is_key_order ());
13967 : 4858196 : if (ek == EK_USING)
13968 : 13450 : gcc_checking_assert (TREE_CODE (decl) == OVERLOAD);
13969 : :
13970 : 4858196 : if (TREE_CODE (decl) == TEMPLATE_DECL)
13971 : : /* The template should have copied these from its result decl. */
13972 : 1760348 : gcc_checking_assert (DECL_MODULE_EXPORT_P (decl)
13973 : : == DECL_MODULE_EXPORT_P (DECL_TEMPLATE_RESULT (decl)));
13974 : :
13975 : 4858196 : depset **slot = entity_slot (decl, true);
13976 : 4858196 : depset *dep = *slot;
13977 : 4858196 : bool for_binding = ek == EK_FOR_BINDING;
13978 : :
13979 : 4858196 : if (!dep)
13980 : : {
13981 : 415119 : if ((DECL_IMPLICIT_TYPEDEF_P (decl)
13982 : : /* ... not an enum, for instance. */
13983 : 214602 : && RECORD_OR_UNION_TYPE_P (TREE_TYPE (decl))
13984 : 210688 : && TYPE_LANG_SPECIFIC (TREE_TYPE (decl))
13985 : 190077 : && CLASSTYPE_USE_TEMPLATE (TREE_TYPE (decl)) == 2)
13986 : 1425358 : || (VAR_P (decl)
13987 : 43612 : && DECL_LANG_SPECIFIC (decl)
13988 : 43531 : && DECL_USE_TEMPLATE (decl) == 2))
13989 : : {
13990 : : /* A partial or explicit specialization. Partial
13991 : : specializations might not be in the hash table, because
13992 : : there can be multiple differently-constrained variants.
13993 : :
13994 : : template<typename T> class silly;
13995 : : template<typename T> requires true class silly {};
13996 : :
13997 : : We need to find them, insert their TEMPLATE_DECL in the
13998 : : dep_hash, and then convert the dep we just found into a
13999 : : redirect. */
14000 : :
14001 : 34105 : tree ti = get_template_info (decl);
14002 : 34105 : tree tmpl = TI_TEMPLATE (ti);
14003 : 34105 : tree partial = NULL_TREE;
14004 : 34105 : for (tree spec = DECL_TEMPLATE_SPECIALIZATIONS (tmpl);
14005 : 131165 : spec; spec = TREE_CHAIN (spec))
14006 : 116250 : if (DECL_TEMPLATE_RESULT (TREE_VALUE (spec)) == decl)
14007 : : {
14008 : : partial = TREE_VALUE (spec);
14009 : : break;
14010 : : }
14011 : :
14012 : 34105 : if (partial)
14013 : : {
14014 : : /* Eagerly create an empty redirect. The following
14015 : : make_dependency call could cause hash reallocation,
14016 : : and invalidate slot's value. */
14017 : 19190 : depset *redirect = make_entity (decl, EK_REDIRECT);
14018 : :
14019 : : /* Redirects are never reached -- always snap to their target. */
14020 : 19190 : redirect->set_flag_bit<DB_UNREACHED_BIT> ();
14021 : :
14022 : 19190 : *slot = redirect;
14023 : :
14024 : 19190 : depset *tmpl_dep = make_dependency (partial, EK_PARTIAL);
14025 : 19190 : gcc_checking_assert (tmpl_dep->get_entity_kind () == EK_PARTIAL);
14026 : :
14027 : 19190 : redirect->deps.safe_push (tmpl_dep);
14028 : :
14029 : 19190 : return redirect;
14030 : : }
14031 : : }
14032 : :
14033 : 1021012 : bool has_def = ek != EK_USING && has_definition (decl);
14034 : 1007562 : if (ek > EK_BINDING)
14035 : 131678 : ek = EK_DECL;
14036 : :
14037 : : /* The only OVERLOADS we should see are USING decls from
14038 : : bindings. */
14039 : 1021012 : *slot = dep = make_entity (decl, ek, has_def);
14040 : :
14041 : 1021012 : if (CHECKING_P && TREE_CODE (decl) == TEMPLATE_DECL)
14042 : : /* The template_result should otherwise not be in the
14043 : : table, or be an empty redirect (created above). */
14044 : 290044 : if (auto *eslot = entity_slot (DECL_TEMPLATE_RESULT (decl), false))
14045 : 19190 : gcc_checking_assert ((*eslot)->get_entity_kind () == EK_REDIRECT
14046 : : && !(*eslot)->deps.length ());
14047 : :
14048 : 1021012 : if (ek != EK_USING)
14049 : : {
14050 : 1007562 : tree not_tmpl = STRIP_TEMPLATE (decl);
14051 : 1007562 : bool imported_from_module_p = false;
14052 : :
14053 : 1007562 : if (DECL_LANG_SPECIFIC (not_tmpl)
14054 : 1954703 : && DECL_MODULE_IMPORT_P (not_tmpl))
14055 : : {
14056 : : /* Store the module number and index in cluster/section,
14057 : : so we don't have to look them up again. */
14058 : 2218 : unsigned index = import_entity_index (decl);
14059 : 2218 : module_state *from = import_entity_module (index);
14060 : : /* Remap will be zero for imports from partitions, which
14061 : : we want to treat as-if declared in this TU. */
14062 : 2218 : if (from->remap)
14063 : : {
14064 : 1769 : dep->cluster = index - from->entity_lwm;
14065 : 1769 : dep->section = from->remap;
14066 : 1769 : dep->set_flag_bit<DB_IMPORTED_BIT> ();
14067 : :
14068 : 1769 : if (!from->is_header ())
14069 : 1007562 : imported_from_module_p = true;
14070 : : }
14071 : : }
14072 : :
14073 : : /* Check for TU-local entities. This is unnecessary in header
14074 : : units because we can export internal-linkage decls, and
14075 : : no declarations are exposures. Similarly, if the decl was
14076 : : imported from a non-header module we know it cannot have
14077 : : been TU-local. */
14078 : 1007562 : if (!header_module_p () && !imported_from_module_p)
14079 : : {
14080 : 289666 : if (is_tu_local_entity (decl))
14081 : 198 : dep->set_flag_bit<DB_TU_LOCAL_BIT> ();
14082 : :
14083 : 289666 : if (VAR_P (decl)
14084 : 12688 : && decl_maybe_constant_var_p (decl)
14085 : 301711 : && is_tu_local_value (decl, DECL_INITIAL (decl)))
14086 : : {
14087 : : /* A potentially-constant variable initialized to a TU-local
14088 : : value is not usable in constant expressions within other
14089 : : translation units. We can achieve this by simply not
14090 : : streaming the definition in such cases. */
14091 : 24 : dep->clear_flag_bit<DB_DEFN_BIT> ();
14092 : :
14093 : 24 : if (DECL_DECLARED_CONSTEXPR_P (decl)
14094 : 39 : || DECL_INLINE_VAR_P (decl))
14095 : : /* A constexpr variable initialized to a TU-local value,
14096 : : or an inline value (PR c++/119996), is an exposure. */
14097 : 15 : dep->set_flag_bit<DB_EXPOSURE_BIT> ();
14098 : : }
14099 : : }
14100 : :
14101 : : /* A namespace-scope type may be declared in one module unit
14102 : : and defined in another; make sure that we're found when
14103 : : completing the class. */
14104 : 1007562 : if (ek == EK_DECL
14105 : 439481 : && !dep->is_import ()
14106 : 438992 : && dep->has_defn ()
14107 : 215752 : && DECL_NAMESPACE_SCOPE_P (not_tmpl)
14108 : 86917 : && DECL_IMPLICIT_TYPEDEF_P (not_tmpl)
14109 : : /* Anonymous types can't be forward-declared. */
14110 : 1032940 : && !IDENTIFIER_ANON_P (DECL_NAME (not_tmpl)))
14111 : 24972 : dep->set_flag_bit<DB_IS_PENDING_BIT> ();
14112 : : }
14113 : :
14114 : 1021012 : if (!dep->is_import ())
14115 : 1019243 : worklist.safe_push (dep);
14116 : : }
14117 : :
14118 : 4839006 : dump (dumper::DEPEND)
14119 : 33890 : && dump ("%s on %s %C:%N found",
14120 : : ek == EK_REDIRECT ? "Redirect"
14121 : 33890 : : for_binding ? "Binding" : "Dependency",
14122 : 33890 : dep->entity_kind_name (), TREE_CODE (decl), decl);
14123 : :
14124 : 4839006 : return dep;
14125 : : }
14126 : :
14127 : : /* Whether REF is an exposure of a member type of SOURCE.
14128 : :
14129 : : This comes up with exposures of class-scope lambdas, that we currently
14130 : : treat as TU-local due to ABI reasons. In such a case the type of the
14131 : : lambda will be exposed in two places, first by the class type it is in
14132 : : the TYPE_FIELDS list of, and second by the actual member declaring that
14133 : : lambda. We only want the second case to warn. */
14134 : :
14135 : : static bool
14136 : 167 : is_exposure_of_member_type (depset *source, depset *ref)
14137 : : {
14138 : 334 : gcc_checking_assert (source->refs_tu_local () && ref->is_tu_local ());
14139 : 167 : tree source_entity = STRIP_TEMPLATE (source->get_entity ());
14140 : 167 : tree ref_entity = STRIP_TEMPLATE (ref->get_entity ());
14141 : :
14142 : 167 : if (source_entity
14143 : 167 : && ref_entity
14144 : 167 : && DECL_IMPLICIT_TYPEDEF_P (source_entity)
14145 : 2 : && DECL_IMPLICIT_TYPEDEF_P (ref_entity)
14146 : 2 : && DECL_CLASS_SCOPE_P (ref_entity)
14147 : 169 : && DECL_CONTEXT (ref_entity) == TREE_TYPE (source_entity))
14148 : : {
14149 : 4 : gcc_checking_assert (LAMBDA_TYPE_P (TREE_TYPE (ref_entity)));
14150 : : return true;
14151 : : }
14152 : : else
14153 : : return false;
14154 : : }
14155 : :
14156 : : /* DEP is a newly discovered dependency. Append it to current's
14157 : : depset. */
14158 : :
14159 : : void
14160 : 3598317 : depset::hash::add_dependency (depset *dep)
14161 : : {
14162 : 3598317 : gcc_checking_assert (current && !is_key_order ());
14163 : 3598317 : current->deps.safe_push (dep);
14164 : :
14165 : 3598317 : if (dep->is_tu_local ())
14166 : : {
14167 : 257 : current->set_flag_bit<DB_REFS_TU_LOCAL_BIT> ();
14168 : 257 : if (!ignore_tu_local && !is_exposure_of_member_type (current, dep))
14169 : 84 : current->set_flag_bit<DB_EXPOSURE_BIT> ();
14170 : : }
14171 : :
14172 : 3598317 : if (current->get_entity_kind () == EK_USING
14173 : 13450 : && DECL_IMPLICIT_TYPEDEF_P (dep->get_entity ())
14174 : 3602145 : && TREE_CODE (TREE_TYPE (dep->get_entity ())) == ENUMERAL_TYPE)
14175 : : {
14176 : : /* CURRENT is an unwrapped using-decl and DECL is an enum's
14177 : : implicit typedef. Is CURRENT a member of the enum? */
14178 : 3705 : tree c_decl = OVL_FUNCTION (current->get_entity ());
14179 : :
14180 : 3705 : if (TREE_CODE (c_decl) == CONST_DECL
14181 : 7399 : && (current->deps[0]->get_entity ()
14182 : 3694 : == CP_DECL_CONTEXT (dep->get_entity ())))
14183 : : /* Make DECL depend on CURRENT. */
14184 : 3643 : dep->deps.safe_push (current);
14185 : : }
14186 : :
14187 : : /* If two dependencies recursively depend on each other existing within
14188 : : their own merge keys, we must ensure that the first dep we saw while
14189 : : walking is written first in this cluster. See sort_cluster for more
14190 : : details. */
14191 : 3598317 : if (writing_merge_key)
14192 : : {
14193 : 639128 : dep->set_flag_bit<DB_MAYBE_RECURSIVE_BIT> ();
14194 : 639128 : if (!current->is_maybe_recursive ())
14195 : 603609 : current->set_flag_bit<DB_ENTRY_BIT> ();
14196 : : }
14197 : :
14198 : 3598317 : if (dep->is_unreached ())
14199 : : {
14200 : : /* The dependency is reachable now. */
14201 : 279691 : reached_unreached = true;
14202 : 279691 : dep->clear_flag_bit<DB_UNREACHED_BIT> ();
14203 : 279691 : dump (dumper::DEPEND)
14204 : 24 : && dump ("Reaching unreached %s %C:%N", dep->entity_kind_name (),
14205 : 24 : TREE_CODE (dep->get_entity ()), dep->get_entity ());
14206 : : }
14207 : 3598317 : }
14208 : :
14209 : : depset *
14210 : 5413063 : depset::hash::add_dependency (tree decl, entity_kind ek)
14211 : : {
14212 : 5413063 : depset *dep;
14213 : :
14214 : 5413063 : if (is_key_order ())
14215 : : {
14216 : 1771303 : dep = find_dependency (decl);
14217 : 1771303 : if (dep)
14218 : : {
14219 : 860796 : current->deps.safe_push (dep);
14220 : 860796 : dump (dumper::MERGE)
14221 : 471 : && dump ("Key dependency on %s %C:%N found",
14222 : 471 : dep->entity_kind_name (), TREE_CODE (decl), decl);
14223 : : }
14224 : : else
14225 : : {
14226 : : /* It's not a mergeable decl, look for it in the original
14227 : : table. */
14228 : 910507 : dep = chain->find_dependency (decl);
14229 : 910507 : gcc_checking_assert (dep);
14230 : : }
14231 : : }
14232 : : else
14233 : : {
14234 : 3641760 : dep = make_dependency (decl, ek);
14235 : 3641760 : if (dep->get_entity_kind () != EK_REDIRECT)
14236 : 3598317 : add_dependency (dep);
14237 : : }
14238 : :
14239 : 5413063 : return dep;
14240 : : }
14241 : :
14242 : : void
14243 : 469083 : depset::hash::add_namespace_context (depset *dep, tree ns)
14244 : : {
14245 : 469083 : depset *ns_dep = make_dependency (ns, depset::EK_NAMESPACE);
14246 : 469083 : dep->deps.safe_push (ns_dep);
14247 : :
14248 : : /* Mark it as special if imported so we don't walk connect when
14249 : : SCCing. */
14250 : 469083 : if (!dep->is_binding () && ns_dep->is_import ())
14251 : 0 : dep->set_special ();
14252 : 469083 : }
14253 : :
14254 : : struct add_binding_data
14255 : : {
14256 : : tree ns;
14257 : : bitmap partitions;
14258 : : depset *binding;
14259 : : depset::hash *hash;
14260 : : bool met_namespace;
14261 : : };
14262 : :
14263 : : /* Return true if we are, or contain something that is exported. */
14264 : :
14265 : : bool
14266 : 4961908 : depset::hash::add_binding_entity (tree decl, WMB_Flags flags, void *data_)
14267 : : {
14268 : 4961908 : auto data = static_cast <add_binding_data *> (data_);
14269 : 4961908 : decl = strip_using_decl (decl);
14270 : :
14271 : 4961908 : if (!(TREE_CODE (decl) == NAMESPACE_DECL && !DECL_NAMESPACE_ALIAS (decl)))
14272 : : {
14273 : 4954946 : tree inner = decl;
14274 : :
14275 : 4954946 : if (TREE_CODE (inner) == CONST_DECL
14276 : 4816 : && TREE_CODE (DECL_CONTEXT (inner)) == ENUMERAL_TYPE
14277 : : /* A using-decl could make a CONST_DECL purview for a non-purview
14278 : : enumeration. */
14279 : 4959762 : && (!DECL_LANG_SPECIFIC (inner) || !DECL_MODULE_PURVIEW_P (inner)))
14280 : 4780 : inner = TYPE_NAME (DECL_CONTEXT (inner));
14281 : 4950166 : else if (TREE_CODE (inner) == TEMPLATE_DECL)
14282 : 93159 : inner = DECL_TEMPLATE_RESULT (inner);
14283 : :
14284 : 9757397 : if ((!DECL_LANG_SPECIFIC (inner) || !DECL_MODULE_PURVIEW_P (inner))
14285 : 9612263 : && !((flags & WMB_Using) && (flags & WMB_Purview)))
14286 : : /* Ignore entities not within the module purview. */
14287 : : return false;
14288 : :
14289 : 145230 : if (!header_module_p () && data->hash->is_tu_local_entity (decl))
14290 : : /* Ignore TU-local entitites. */
14291 : : return false;
14292 : :
14293 : 145021 : if ((TREE_CODE (decl) == VAR_DECL
14294 : 145021 : || TREE_CODE (decl) == TYPE_DECL)
14295 : 145021 : && DECL_TINFO_P (decl))
14296 : : /* Ignore TINFO things. */
14297 : : return false;
14298 : :
14299 : 145021 : if (TREE_CODE (decl) == VAR_DECL && DECL_NTTP_OBJECT_P (decl))
14300 : : /* Ignore NTTP objects. */
14301 : : return false;
14302 : :
14303 : 145021 : if (deduction_guide_p (decl))
14304 : : {
14305 : : /* Ignore deduction guides, bindings for them will be created within
14306 : : find_dependencies for their class template. But still build a dep
14307 : : for them so that we don't discard them. */
14308 : 1336 : data->hash->make_dependency (decl, EK_FOR_BINDING);
14309 : 1336 : return false;
14310 : : }
14311 : :
14312 : 143685 : if (!(flags & WMB_Using) && CP_DECL_CONTEXT (decl) != data->ns)
14313 : : {
14314 : : /* An unscoped enum constant implicitly brought into the containing
14315 : : namespace. We treat this like a using-decl. */
14316 : 3056 : gcc_checking_assert (TREE_CODE (decl) == CONST_DECL);
14317 : :
14318 : 3056 : flags = WMB_Flags (flags | WMB_Using);
14319 : 3056 : if (DECL_MODULE_EXPORT_P (TYPE_NAME (TREE_TYPE (decl)))
14320 : : /* A using-decl can make an enum constant exported for a
14321 : : non-exported enumeration. */
14322 : 3056 : || (DECL_LANG_SPECIFIC (decl) && DECL_MODULE_EXPORT_P (decl)))
14323 : 2827 : flags = WMB_Flags (flags | WMB_Export);
14324 : : }
14325 : :
14326 : 143685 : if (!data->binding)
14327 : : /* No binding to check. */;
14328 : 28075 : else if (flags & WMB_Using)
14329 : : {
14330 : : /* Look in the binding to see if we already have this
14331 : : using. */
14332 : 18280 : for (unsigned ix = data->binding->deps.length (); --ix;)
14333 : : {
14334 : 14262 : depset *d = data->binding->deps[ix];
14335 : 28524 : if (d->get_entity_kind () == EK_USING
14336 : 14262 : && OVL_FUNCTION (d->get_entity ()) == decl)
14337 : : {
14338 : 0 : if (!(flags & WMB_Hidden))
14339 : 0 : d->clear_hidden_binding ();
14340 : 0 : OVL_PURVIEW_P (d->get_entity ()) = true;
14341 : 0 : if (flags & WMB_Export)
14342 : 0 : OVL_EXPORT_P (d->get_entity ()) = true;
14343 : 0 : return bool (flags & WMB_Export);
14344 : : }
14345 : : }
14346 : : }
14347 : 26066 : else if (flags & WMB_Dups)
14348 : : {
14349 : : /* Look in the binding to see if we already have this decl. */
14350 : 78 : for (unsigned ix = data->binding->deps.length (); --ix;)
14351 : : {
14352 : 39 : depset *d = data->binding->deps[ix];
14353 : 39 : if (d->get_entity () == decl)
14354 : : {
14355 : 33 : if (!(flags & WMB_Hidden))
14356 : 30 : d->clear_hidden_binding ();
14357 : 33 : return false;
14358 : : }
14359 : : }
14360 : : }
14361 : :
14362 : : /* We're adding something. */
14363 : 143652 : if (!data->binding)
14364 : : {
14365 : 115610 : data->binding = make_binding (data->ns, DECL_NAME (decl));
14366 : 115610 : data->hash->add_namespace_context (data->binding, data->ns);
14367 : :
14368 : 115610 : depset **slot = data->hash->binding_slot (data->ns,
14369 : 115610 : DECL_NAME (decl), true);
14370 : 115610 : gcc_checking_assert (!*slot);
14371 : 115610 : *slot = data->binding;
14372 : : }
14373 : :
14374 : : /* Make sure nobody left a tree visited lying about. */
14375 : 143652 : gcc_checking_assert (!TREE_VISITED (decl));
14376 : :
14377 : 143652 : if (flags & WMB_Using)
14378 : : {
14379 : 13450 : decl = ovl_make (decl, NULL_TREE);
14380 : 13450 : OVL_USING_P (decl) = true;
14381 : 13450 : OVL_PURVIEW_P (decl) = true;
14382 : 13450 : if (flags & WMB_Export)
14383 : 12588 : OVL_EXPORT_P (decl) = true;
14384 : : }
14385 : :
14386 : 143652 : depset *dep = data->hash->make_dependency
14387 : 273854 : (decl, flags & WMB_Using ? EK_USING : EK_FOR_BINDING);
14388 : 143652 : if (flags & WMB_Hidden)
14389 : 3754 : dep->set_hidden_binding ();
14390 : 143652 : data->binding->deps.safe_push (dep);
14391 : : /* Binding and contents are mutually dependent. */
14392 : 143652 : dep->deps.safe_push (data->binding);
14393 : :
14394 : 143652 : return (flags & WMB_Using
14395 : 143652 : ? flags & WMB_Export : DECL_MODULE_EXPORT_P (decl));
14396 : : }
14397 : 6962 : else if (!data->met_namespace)
14398 : : {
14399 : : /* Namespace, walk exactly once. */
14400 : 6953 : data->met_namespace = true;
14401 : 6953 : if (data->hash->add_namespace_entities (decl, data->partitions))
14402 : : {
14403 : : /* It contains an exported thing, so it is exported. */
14404 : 1323 : gcc_checking_assert (DECL_MODULE_PURVIEW_P (decl));
14405 : 1323 : gcc_checking_assert (TREE_PUBLIC (decl) || header_module_p ());
14406 : 1323 : DECL_MODULE_EXPORT_P (decl) = true;
14407 : : }
14408 : :
14409 : 6953 : if (DECL_MODULE_PURVIEW_P (decl))
14410 : : {
14411 : 1611 : data->hash->make_dependency (decl, depset::EK_NAMESPACE);
14412 : :
14413 : 1611 : return DECL_MODULE_EXPORT_P (decl);
14414 : : }
14415 : : }
14416 : :
14417 : : return false;
14418 : : }
14419 : :
14420 : : /* Recursively find all the namespace bindings of NS. Add a depset
14421 : : for every binding that contains an export or module-linkage entity.
14422 : : Add a defining depset for every such decl that we need to write a
14423 : : definition. Such defining depsets depend on the binding depset.
14424 : : Returns true if we contain something exported. */
14425 : :
14426 : : bool
14427 : 9362 : depset::hash::add_namespace_entities (tree ns, bitmap partitions)
14428 : : {
14429 : 10316 : dump () && dump ("Looking for writables in %N", ns);
14430 : 9362 : dump.indent ();
14431 : :
14432 : 9362 : unsigned count = 0;
14433 : 9362 : add_binding_data data;
14434 : 9362 : data.ns = ns;
14435 : 9362 : data.partitions = partitions;
14436 : 9362 : data.hash = this;
14437 : :
14438 : 9362 : hash_table<named_decl_hash>::iterator end
14439 : 9362 : (DECL_NAMESPACE_BINDINGS (ns)->end ());
14440 : 6501529 : for (hash_table<named_decl_hash>::iterator iter
14441 : 13003058 : (DECL_NAMESPACE_BINDINGS (ns)->begin ()); iter != end; ++iter)
14442 : : {
14443 : 6492167 : data.binding = nullptr;
14444 : 6492167 : data.met_namespace = false;
14445 : 6492167 : if (walk_module_binding (*iter, partitions, add_binding_entity, &data))
14446 : 108306 : count++;
14447 : : }
14448 : :
14449 : 9362 : if (count)
14450 : 3329 : dump () && dump ("Found %u entries", count);
14451 : 9362 : dump.outdent ();
14452 : :
14453 : 9362 : return count != 0;
14454 : : }
14455 : :
14456 : : void
14457 : 194 : depset::hash::add_partial_entities (vec<tree, va_gc> *partial_classes)
14458 : : {
14459 : 17005 : for (unsigned ix = 0; ix != partial_classes->length (); ix++)
14460 : : {
14461 : 16811 : tree inner = (*partial_classes)[ix];
14462 : :
14463 : 16811 : depset *dep = make_dependency (inner, depset::EK_DECL);
14464 : :
14465 : 16811 : if (dep->get_entity_kind () == depset::EK_REDIRECT)
14466 : : {
14467 : 16811 : dep = dep->deps[0];
14468 : : /* We should have recorded the template as a partial
14469 : : specialization. */
14470 : 16811 : gcc_checking_assert (dep->get_entity_kind ()
14471 : : == depset::EK_PARTIAL);
14472 : :
14473 : : /* Only emit GM entities if reached. */
14474 : 16811 : if (!DECL_LANG_SPECIFIC (inner)
14475 : 28457 : || !DECL_MODULE_PURVIEW_P (inner))
14476 : 5207 : dep->set_flag_bit<DB_UNREACHED_BIT> ();
14477 : : }
14478 : : else
14479 : : {
14480 : : /* It was an explicit specialization, not a partial one.
14481 : : We should have already added this. */
14482 : 0 : gcc_checking_assert (dep->get_entity_kind ()
14483 : : == depset::EK_SPECIALIZATION);
14484 : 0 : gcc_checking_assert (dep->is_special ());
14485 : : }
14486 : : }
14487 : 194 : }
14488 : :
14489 : : /* Add the members of imported classes that we defined in this TU.
14490 : : This will also include lazily created implicit member function
14491 : : declarations. (All others will be definitions.) */
14492 : :
14493 : : void
14494 : 12 : depset::hash::add_class_entities (vec<tree, va_gc> *class_members)
14495 : : {
14496 : 24 : for (unsigned ix = 0; ix != class_members->length (); ix++)
14497 : : {
14498 : 12 : tree defn = (*class_members)[ix];
14499 : 12 : depset *dep = make_dependency (defn, EK_INNER_DECL);
14500 : :
14501 : 12 : if (dep->get_entity_kind () == EK_REDIRECT)
14502 : 0 : dep = dep->deps[0];
14503 : :
14504 : : /* Only non-instantiations need marking as pendings. */
14505 : 24 : if (dep->get_entity_kind () == EK_DECL)
14506 : 12 : dep->set_flag_bit <DB_IS_PENDING_BIT> ();
14507 : : }
14508 : 12 : }
14509 : :
14510 : : /* We add the partial & explicit specializations, and the explicit
14511 : : instantiations. */
14512 : :
14513 : : static void
14514 : 565359 : specialization_add (bool decl_p, spec_entry *entry, void *data_)
14515 : : {
14516 : 565359 : vec<spec_entry *> *data = reinterpret_cast <vec<spec_entry *> *> (data_);
14517 : :
14518 : 565359 : if (!decl_p)
14519 : : {
14520 : : /* We exclusively use decls to locate things. Make sure there's
14521 : : no mismatch between the two specialization tables we keep.
14522 : : pt.cc optimizes instantiation lookup using a complicated
14523 : : heuristic. We don't attempt to replicate that algorithm, but
14524 : : observe its behaviour and reproduce it upon read back. */
14525 : :
14526 : 181589 : gcc_checking_assert (TREE_CODE (entry->spec) == ENUMERAL_TYPE
14527 : : || DECL_CLASS_TEMPLATE_P (entry->tmpl));
14528 : :
14529 : 181589 : gcc_checking_assert (!match_mergeable_specialization (true, entry));
14530 : : }
14531 : 383770 : else if (VAR_OR_FUNCTION_DECL_P (entry->spec))
14532 : 192445 : gcc_checking_assert (!DECL_LOCAL_DECL_P (entry->spec));
14533 : :
14534 : 565359 : data->safe_push (entry);
14535 : 565359 : }
14536 : :
14537 : : /* Arbitrary stable comparison. */
14538 : :
14539 : : static int
14540 : 33255125 : specialization_cmp (const void *a_, const void *b_)
14541 : : {
14542 : 33255125 : const spec_entry *ea = *reinterpret_cast<const spec_entry *const *> (a_);
14543 : 33255125 : const spec_entry *eb = *reinterpret_cast<const spec_entry *const *> (b_);
14544 : :
14545 : 33255125 : if (ea == eb)
14546 : : return 0;
14547 : :
14548 : 33255125 : tree a = ea->spec;
14549 : 33255125 : tree b = eb->spec;
14550 : 33255125 : if (TYPE_P (a))
14551 : : {
14552 : 10152063 : a = TYPE_NAME (a);
14553 : 10152063 : b = TYPE_NAME (b);
14554 : : }
14555 : :
14556 : 33255125 : if (a == b)
14557 : : /* This can happen with friend specializations. Just order by
14558 : : entry address. See note in depset_cmp. */
14559 : 172 : return ea < eb ? -1 : +1;
14560 : :
14561 : 33255008 : return DECL_UID (a) < DECL_UID (b) ? -1 : +1;
14562 : : }
14563 : :
14564 : : /* We add all kinds of specialializations. Implicit specializations
14565 : : should only streamed and walked if they are reachable from
14566 : : elsewhere. Hence the UNREACHED flag. This is making the
14567 : : assumption that it is cheaper to reinstantiate them on demand
14568 : : elsewhere, rather than stream them in when we instantiate their
14569 : : general template. Also, if we do stream them, we can only do that
14570 : : if they are not internal (which they can become if they themselves
14571 : : touch an internal entity?). */
14572 : :
14573 : : void
14574 : 4818 : depset::hash::add_specializations (bool decl_p)
14575 : : {
14576 : 4818 : vec<spec_entry *> data;
14577 : 4818 : data.create (100);
14578 : 4818 : walk_specializations (decl_p, specialization_add, &data);
14579 : 4818 : data.qsort (specialization_cmp);
14580 : 570177 : while (data.length ())
14581 : : {
14582 : 565359 : spec_entry *entry = data.pop ();
14583 : 565359 : tree spec = entry->spec;
14584 : 565359 : int use_tpl = 0;
14585 : 565359 : bool is_friend = false;
14586 : :
14587 : 565359 : if (decl_p && DECL_UNINSTANTIATED_TEMPLATE_FRIEND_P (entry->tmpl))
14588 : : /* A friend of a template. This is keyed to the
14589 : : instantiation. */
14590 : : is_friend = true;
14591 : :
14592 : 565359 : if (decl_p)
14593 : : {
14594 : 383770 : if (tree ti = DECL_TEMPLATE_INFO (spec))
14595 : : {
14596 : 383770 : tree tmpl = TI_TEMPLATE (ti);
14597 : :
14598 : 383770 : use_tpl = DECL_USE_TEMPLATE (spec);
14599 : 383770 : if (spec == DECL_TEMPLATE_RESULT (tmpl))
14600 : : {
14601 : 2790 : spec = tmpl;
14602 : 2790 : gcc_checking_assert (DECL_USE_TEMPLATE (spec) == use_tpl);
14603 : : }
14604 : 380980 : else if (is_friend)
14605 : : {
14606 : 2082 : if (TI_TEMPLATE (ti) != entry->tmpl
14607 : 2082 : || !template_args_equal (TI_ARGS (ti), entry->tmpl))
14608 : 2082 : goto template_friend;
14609 : : }
14610 : : }
14611 : : else
14612 : : {
14613 : 0 : template_friend:;
14614 : 2082 : gcc_checking_assert (is_friend);
14615 : : /* This is a friend of a template class, but not the one
14616 : : that generated entry->spec itself (i.e. it's an
14617 : : equivalent clone). We do not need to record
14618 : : this. */
14619 : 2082 : continue;
14620 : : }
14621 : : }
14622 : : else
14623 : : {
14624 : 181589 : if (TREE_CODE (spec) == ENUMERAL_TYPE)
14625 : : {
14626 : 1009 : tree ctx = DECL_CONTEXT (TYPE_NAME (spec));
14627 : :
14628 : 1009 : if (TYPE_P (ctx))
14629 : 1003 : use_tpl = CLASSTYPE_USE_TEMPLATE (ctx);
14630 : : else
14631 : 6 : use_tpl = DECL_USE_TEMPLATE (ctx);
14632 : : }
14633 : : else
14634 : 180580 : use_tpl = CLASSTYPE_USE_TEMPLATE (spec);
14635 : :
14636 : 181589 : tree ti = TYPE_TEMPLATE_INFO (spec);
14637 : 181589 : tree tmpl = TI_TEMPLATE (ti);
14638 : :
14639 : 181589 : spec = TYPE_NAME (spec);
14640 : 181589 : if (spec == DECL_TEMPLATE_RESULT (tmpl))
14641 : : {
14642 : 770 : spec = tmpl;
14643 : 770 : use_tpl = DECL_USE_TEMPLATE (spec);
14644 : : }
14645 : : }
14646 : :
14647 : 563277 : bool needs_reaching = false;
14648 : 563277 : if (use_tpl == 1)
14649 : : /* Implicit instantiations only walked if we reach them. */
14650 : : needs_reaching = true;
14651 : 58302 : else if (!DECL_LANG_SPECIFIC (STRIP_TEMPLATE (spec))
14652 : 106453 : || !DECL_MODULE_PURVIEW_P (STRIP_TEMPLATE (spec)))
14653 : : /* Likewise, GMF explicit or partial specializations. */
14654 : : needs_reaching = true;
14655 : :
14656 : : #if false && CHECKING_P
14657 : : /* The instantiation isn't always on
14658 : : DECL_TEMPLATE_INSTANTIATIONS, */
14659 : : // FIXME: we probably need to remember this information?
14660 : : /* Verify the specialization is on the
14661 : : DECL_TEMPLATE_INSTANTIATIONS of the template. */
14662 : : for (tree cons = DECL_TEMPLATE_INSTANTIATIONS (entry->tmpl);
14663 : : cons; cons = TREE_CHAIN (cons))
14664 : : if (TREE_VALUE (cons) == entry->spec)
14665 : : {
14666 : : gcc_assert (entry->args == TREE_PURPOSE (cons));
14667 : : goto have_spec;
14668 : : }
14669 : : gcc_unreachable ();
14670 : : have_spec:;
14671 : : #endif
14672 : :
14673 : : /* Make sure nobody left a tree visited lying about. */
14674 : 563277 : gcc_checking_assert (!TREE_VISITED (spec));
14675 : 563277 : depset *dep = make_dependency (spec, depset::EK_SPECIALIZATION);
14676 : 563277 : if (dep->is_special ())
14677 : 0 : gcc_unreachable ();
14678 : : else
14679 : : {
14680 : 563277 : if (dep->get_entity_kind () == depset::EK_REDIRECT)
14681 : 18497 : dep = dep->deps[0];
14682 : 544780 : else if (dep->get_entity_kind () == depset::EK_SPECIALIZATION)
14683 : : {
14684 : 544780 : dep->set_special ();
14685 : 544780 : dep->deps.safe_push (reinterpret_cast<depset *> (entry));
14686 : 544780 : if (!decl_p)
14687 : 165471 : dep->set_flag_bit<DB_TYPE_SPEC_BIT> ();
14688 : : }
14689 : :
14690 : 563277 : if (needs_reaching)
14691 : 522419 : dep->set_flag_bit<DB_UNREACHED_BIT> ();
14692 : 563277 : if (is_friend)
14693 : 0 : dep->set_flag_bit<DB_FRIEND_SPEC_BIT> ();
14694 : : }
14695 : : }
14696 : 4818 : data.release ();
14697 : 4818 : }
14698 : :
14699 : : /* Add a depset into the mergeable hash. */
14700 : :
14701 : : void
14702 : 764373 : depset::hash::add_mergeable (depset *mergeable)
14703 : : {
14704 : 764373 : gcc_checking_assert (is_key_order ());
14705 : 764373 : entity_kind ek = mergeable->get_entity_kind ();
14706 : 764373 : tree decl = mergeable->get_entity ();
14707 : 764373 : gcc_checking_assert (ek < EK_DIRECT_HWM);
14708 : :
14709 : 764373 : depset **slot = entity_slot (decl, true);
14710 : 764373 : gcc_checking_assert (!*slot);
14711 : 764373 : depset *dep = make_entity (decl, ek);
14712 : 764373 : *slot = dep;
14713 : :
14714 : 764373 : worklist.safe_push (dep);
14715 : :
14716 : : /* So we can locate the mergeable depset this depset refers to,
14717 : : mark the first dep. */
14718 : 764373 : dep->set_special ();
14719 : 764373 : dep->deps.safe_push (mergeable);
14720 : 764373 : }
14721 : :
14722 : : /* Find the innermost-namespace scope of DECL, and that
14723 : : namespace-scope decl. */
14724 : :
14725 : : tree
14726 : 24635679 : find_pending_key (tree decl, tree *decl_p = nullptr)
14727 : : {
14728 : 24635679 : tree ns = decl;
14729 : 29499745 : do
14730 : : {
14731 : 29499745 : decl = ns;
14732 : 29499745 : ns = CP_DECL_CONTEXT (ns);
14733 : 29499745 : if (TYPE_P (ns))
14734 : 3070961 : ns = TYPE_NAME (ns);
14735 : : }
14736 : 29499745 : while (TREE_CODE (ns) != NAMESPACE_DECL);
14737 : :
14738 : 24635679 : if (decl_p)
14739 : 24284745 : *decl_p = decl;
14740 : :
14741 : 24635679 : return ns;
14742 : : }
14743 : :
14744 : : /* Creates bindings and dependencies for all deduction guides of
14745 : : the given class template DECL as needed. */
14746 : :
14747 : : void
14748 : 40933 : depset::hash::add_deduction_guides (tree decl)
14749 : : {
14750 : : /* Alias templates never have deduction guides. */
14751 : 40933 : if (DECL_ALIAS_TEMPLATE_P (decl))
14752 : 40329 : return;
14753 : :
14754 : : /* We don't need to do anything for class-scope deduction guides,
14755 : : as they will be added as members anyway. */
14756 : 40933 : if (!DECL_NAMESPACE_SCOPE_P (decl))
14757 : : return;
14758 : :
14759 : 33205 : tree ns = CP_DECL_CONTEXT (decl);
14760 : 33205 : tree name = dguide_name (decl);
14761 : :
14762 : : /* We always add all deduction guides with a given name at once,
14763 : : so if there's already a binding there's nothing to do. */
14764 : 33205 : if (find_binding (ns, name))
14765 : : return;
14766 : :
14767 : 31630 : tree guides = lookup_qualified_name (ns, name, LOOK_want::NORMAL,
14768 : : /*complain=*/false);
14769 : 31630 : if (guides == error_mark_node)
14770 : : return;
14771 : :
14772 : 604 : depset *binding = nullptr;
14773 : 2672 : for (tree t : lkp_range (guides))
14774 : : {
14775 : 1464 : gcc_checking_assert (!TREE_VISITED (t));
14776 : 1464 : depset *dep = make_dependency (t, EK_FOR_BINDING);
14777 : :
14778 : : /* We don't want to create bindings for imported deduction guides, as
14779 : : this would potentially cause name lookup to return duplicates. */
14780 : 1464 : if (dep->is_import ())
14781 : 6 : continue;
14782 : :
14783 : 1458 : if (!binding)
14784 : : {
14785 : : /* We have bindings to add. */
14786 : 598 : binding = make_binding (ns, name);
14787 : 598 : add_namespace_context (binding, ns);
14788 : :
14789 : 598 : depset **slot = binding_slot (ns, name, /*insert=*/true);
14790 : 598 : *slot = binding;
14791 : : }
14792 : :
14793 : 1458 : binding->deps.safe_push (dep);
14794 : 1458 : dep->deps.safe_push (binding);
14795 : : }
14796 : : }
14797 : :
14798 : : /* Iteratively find dependencies. During the walk we may find more
14799 : : entries on the same binding that need walking. */
14800 : :
14801 : : void
14802 : 223735 : depset::hash::find_dependencies (module_state *module)
14803 : : {
14804 : 223735 : trees_out walker (NULL, module, *this);
14805 : 223735 : vec<depset *> unreached;
14806 : 447470 : unreached.create (worklist.length ());
14807 : :
14808 : 996 : for (;;)
14809 : : {
14810 : 224731 : reached_unreached = false;
14811 : 3115576 : while (worklist.length ())
14812 : : {
14813 : 2890845 : depset *item = worklist.pop ();
14814 : :
14815 : 2890845 : gcc_checking_assert (!item->is_binding ());
14816 : 2890845 : if (item->is_unreached ())
14817 : 1343969 : unreached.quick_push (item);
14818 : : else
14819 : : {
14820 : 1546876 : current = item;
14821 : 1546876 : tree decl = current->get_entity ();
14822 : 1546876 : dump (is_key_order () ? dumper::MERGE : dumper::DEPEND)
14823 : 1547830 : && dump ("Dependencies of %s %C:%N",
14824 : 954 : is_key_order () ? "key-order"
14825 : 954 : : current->entity_kind_name (), TREE_CODE (decl), decl);
14826 : 1546876 : dump.indent ();
14827 : 1546876 : walker.begin ();
14828 : 1546876 : if (current->get_entity_kind () == EK_USING)
14829 : 13450 : walker.tree_node (OVL_FUNCTION (decl));
14830 : 1533426 : else if (TREE_VISITED (decl))
14831 : : /* A global tree. */;
14832 : 1531256 : else if (item->get_entity_kind () == EK_NAMESPACE)
14833 : : {
14834 : 1941 : module->note_location (DECL_SOURCE_LOCATION (decl));
14835 : 1941 : add_namespace_context (current, CP_DECL_CONTEXT (decl));
14836 : : }
14837 : : else
14838 : : {
14839 : 1529315 : walker.mark_declaration (decl, current->has_defn ());
14840 : :
14841 : 1529315 : if (!is_key_order ()
14842 : 1529315 : && item->is_pending_entity ())
14843 : : {
14844 : 350934 : tree ns = find_pending_key (decl, nullptr);
14845 : 350934 : add_namespace_context (item, ns);
14846 : : }
14847 : :
14848 : 1529315 : walker.decl_value (decl, current);
14849 : 1529315 : if (current->has_defn ())
14850 : 283721 : walker.write_definition (decl, current->refs_tu_local ());
14851 : : }
14852 : 1546876 : walker.end ();
14853 : :
14854 : : /* If we see either a class template or a deduction guide, make
14855 : : sure to add all visible deduction guides. We need to check
14856 : : both in case they have been added in separate modules, or
14857 : : one is in the GMF and would have otherwise been discarded. */
14858 : 1546876 : if (!is_key_order ()
14859 : 1546876 : && DECL_CLASS_TEMPLATE_P (decl))
14860 : 39469 : add_deduction_guides (decl);
14861 : 1546876 : if (!is_key_order ()
14862 : 1546876 : && deduction_guide_p (decl))
14863 : 1464 : add_deduction_guides (TYPE_NAME (TREE_TYPE (TREE_TYPE (decl))));
14864 : :
14865 : 1546876 : if (!is_key_order ()
14866 : 782503 : && TREE_CODE (decl) == TEMPLATE_DECL
14867 : 1826272 : && !DECL_UNINSTANTIATED_TEMPLATE_FRIEND_P (decl))
14868 : : {
14869 : : /* Mark all the explicit & partial specializations as
14870 : : reachable. We search both specialization lists as some
14871 : : constrained partial specializations for class types are
14872 : : only found in DECL_TEMPLATE_SPECIALIZATIONS. */
14873 : 753344 : auto mark_reached = [this](tree spec)
14874 : : {
14875 : 478640 : if (TYPE_P (spec))
14876 : 146952 : spec = TYPE_NAME (spec);
14877 : 478640 : int use_tpl;
14878 : 478640 : node_template_info (spec, use_tpl);
14879 : 478640 : if (use_tpl & 2)
14880 : : {
14881 : 61461 : depset *spec_dep = find_dependency (spec);
14882 : 61461 : if (spec_dep->get_entity_kind () == EK_REDIRECT)
14883 : 14486 : spec_dep = spec_dep->deps[0];
14884 : 61461 : if (spec_dep->is_unreached ())
14885 : : {
14886 : 5034 : reached_unreached = true;
14887 : 5034 : spec_dep->clear_flag_bit<DB_UNREACHED_BIT> ();
14888 : 5034 : dump (dumper::DEPEND)
14889 : 0 : && dump ("Reaching unreached specialization"
14890 : 0 : " %C:%N", TREE_CODE (spec), spec);
14891 : : }
14892 : : }
14893 : 753344 : };
14894 : :
14895 : 274704 : for (tree cons = DECL_TEMPLATE_INSTANTIATIONS (decl);
14896 : 738236 : cons; cons = TREE_CHAIN (cons))
14897 : 463532 : mark_reached (TREE_VALUE (cons));
14898 : 274704 : for (tree cons = DECL_TEMPLATE_SPECIALIZATIONS (decl);
14899 : 289812 : cons; cons = TREE_CHAIN (cons))
14900 : 15108 : mark_reached (TREE_VALUE (cons));
14901 : : }
14902 : :
14903 : 1546876 : dump.outdent ();
14904 : 1546876 : current = NULL;
14905 : : }
14906 : : }
14907 : :
14908 : 224731 : if (!reached_unreached)
14909 : : break;
14910 : :
14911 : : /* It's possible the we reached the unreached before we
14912 : : processed it in the above loop, so we'll be doing this an
14913 : : extra time. However, to avoid that we have to do some
14914 : : bit shuffling that also involves a scan of the list.
14915 : : Swings & roundabouts I guess. */
14916 : 996 : std::swap (worklist, unreached);
14917 : : }
14918 : :
14919 : 223735 : unreached.release ();
14920 : 223735 : }
14921 : :
14922 : : /* Compare two entries of a single binding. TYPE_DECL before
14923 : : non-exported before exported. */
14924 : :
14925 : : static int
14926 : 404159 : binding_cmp (const void *a_, const void *b_)
14927 : : {
14928 : 404159 : depset *a = *(depset *const *)a_;
14929 : 404159 : depset *b = *(depset *const *)b_;
14930 : :
14931 : 404159 : tree a_ent = a->get_entity ();
14932 : 404159 : tree b_ent = b->get_entity ();
14933 : 404159 : gcc_checking_assert (a_ent != b_ent
14934 : : && !a->is_binding ()
14935 : : && !b->is_binding ());
14936 : :
14937 : : /* Implicit typedefs come first. */
14938 : 404159 : bool a_implicit = DECL_IMPLICIT_TYPEDEF_P (a_ent);
14939 : 404159 : bool b_implicit = DECL_IMPLICIT_TYPEDEF_P (b_ent);
14940 : 404057 : if (a_implicit || b_implicit)
14941 : : {
14942 : : /* A binding with two implicit type decls? That's unpossible! */
14943 : 204 : gcc_checking_assert (!(a_implicit && b_implicit));
14944 : 306 : return a_implicit ? -1 : +1; /* Implicit first. */
14945 : : }
14946 : :
14947 : : /* Hidden before non-hidden. */
14948 : 403955 : bool a_hidden = a->is_hidden ();
14949 : 403955 : bool b_hidden = b->is_hidden ();
14950 : 403955 : if (a_hidden != b_hidden)
14951 : 41433 : return a_hidden ? -1 : +1;
14952 : :
14953 : 376775 : bool a_using = a->get_entity_kind () == depset::EK_USING;
14954 : 376775 : bool a_export;
14955 : 376775 : if (a_using)
14956 : : {
14957 : 26803 : a_export = OVL_EXPORT_P (a_ent);
14958 : 26803 : a_ent = OVL_FUNCTION (a_ent);
14959 : : }
14960 : 349972 : else if (TREE_CODE (a_ent) == CONST_DECL
14961 : 0 : && DECL_LANG_SPECIFIC (a_ent)
14962 : 349972 : && DECL_MODULE_EXPORT_P (a_ent))
14963 : : a_export = true;
14964 : : else
14965 : 349972 : a_export = DECL_MODULE_EXPORT_P (TREE_CODE (a_ent) == CONST_DECL
14966 : : ? TYPE_NAME (TREE_TYPE (a_ent))
14967 : : : STRIP_TEMPLATE (a_ent));
14968 : :
14969 : 376775 : bool b_using = b->get_entity_kind () == depset::EK_USING;
14970 : 376775 : bool b_export;
14971 : 376775 : if (b_using)
14972 : : {
14973 : 23221 : b_export = OVL_EXPORT_P (b_ent);
14974 : 23221 : b_ent = OVL_FUNCTION (b_ent);
14975 : : }
14976 : 353554 : else if (TREE_CODE (b_ent) == CONST_DECL
14977 : 0 : && DECL_LANG_SPECIFIC (b_ent)
14978 : 353554 : && DECL_MODULE_EXPORT_P (b_ent))
14979 : : b_export = true;
14980 : : else
14981 : 353554 : b_export = DECL_MODULE_EXPORT_P (TREE_CODE (b_ent) == CONST_DECL
14982 : : ? TYPE_NAME (TREE_TYPE (b_ent))
14983 : : : STRIP_TEMPLATE (b_ent));
14984 : :
14985 : : /* Non-exports before exports. */
14986 : 376775 : if (a_export != b_export)
14987 : 72 : return a_export ? +1 : -1;
14988 : :
14989 : : /* At this point we don't care, but want a stable sort. */
14990 : :
14991 : 376730 : if (a_using != b_using)
14992 : : /* using first. */
14993 : 19920 : return a_using? -1 : +1;
14994 : :
14995 : 362256 : return DECL_UID (a_ent) < DECL_UID (b_ent) ? -1 : +1;
14996 : : }
14997 : :
14998 : : /* True iff TMPL has an explicit instantiation definition.
14999 : :
15000 : : This is local to module.cc because register_specialization skips adding most
15001 : : instantiations unless module_maybe_has_cmi_p. */
15002 : :
15003 : : static bool
15004 : 54 : template_has_explicit_inst (tree tmpl)
15005 : : {
15006 : 60 : for (tree t = DECL_TEMPLATE_INSTANTIATIONS (tmpl); t; t = TREE_CHAIN (t))
15007 : : {
15008 : 12 : tree spec = TREE_VALUE (t);
15009 : 12 : if (DECL_EXPLICIT_INSTANTIATION (spec)
15010 : 12 : && !DECL_REALLY_EXTERN (spec))
15011 : : return true;
15012 : : }
15013 : : return false;
15014 : : }
15015 : :
15016 : : /* Sort the bindings, issue errors about bad internal refs. */
15017 : :
15018 : : bool
15019 : 2409 : depset::hash::finalize_dependencies ()
15020 : : {
15021 : 2409 : bool ok = true;
15022 : 2315229 : for (depset *dep : *this)
15023 : : {
15024 : 1156410 : if (dep->is_binding ())
15025 : : {
15026 : : /* Keep the containing namespace dep first. */
15027 : 116208 : gcc_checking_assert (dep->deps.length () > 1
15028 : : && (dep->deps[0]->get_entity_kind ()
15029 : : == EK_NAMESPACE)
15030 : : && (dep->deps[0]->get_entity ()
15031 : : == dep->get_entity ()));
15032 : 116208 : if (dep->deps.length () > 2)
15033 : 8448 : gcc_qsort (&dep->deps[1], dep->deps.length () - 1,
15034 : : sizeof (dep->deps[1]), binding_cmp);
15035 : :
15036 : : /* Bindings shouldn't refer to imported entities. */
15037 : 116208 : if (CHECKING_P)
15038 : 609942 : for (depset *entity : dep->deps)
15039 : 261318 : gcc_checking_assert (!entity->is_import ());
15040 : : }
15041 : 1040298 : else if (dep->is_exposure () && !dep->is_tu_local ())
15042 : : {
15043 : 96 : ok = false;
15044 : 96 : bool explained = false;
15045 : 96 : tree decl = dep->get_entity ();
15046 : :
15047 : 390 : for (depset *rdep : dep->deps)
15048 : 183 : if (!rdep->is_binding ()
15049 : 95 : && rdep->is_tu_local ()
15050 : 264 : && !is_exposure_of_member_type (dep, rdep))
15051 : : {
15052 : : // FIXME:QOI Better location information? We're
15053 : : // losing, so it doesn't matter about efficiency
15054 : 81 : tree exposed = rdep->get_entity ();
15055 : 81 : auto_diagnostic_group d;
15056 : 81 : error_at (DECL_SOURCE_LOCATION (decl),
15057 : : "%qD exposes TU-local entity %qD", decl, exposed);
15058 : 81 : bool informed = is_tu_local_entity (exposed, /*explain=*/true);
15059 : 81 : gcc_checking_assert (informed);
15060 : 81 : explained = true;
15061 : 81 : break;
15062 : 81 : }
15063 : :
15064 : 81 : if (!explained
15065 : 15 : && VAR_P (decl)
15066 : 15 : && (DECL_DECLARED_CONSTEXPR_P (decl)
15067 : 6 : || DECL_INLINE_VAR_P (decl)))
15068 : : {
15069 : 15 : auto_diagnostic_group d;
15070 : 15 : if (DECL_DECLARED_CONSTEXPR_P (decl))
15071 : 9 : error_at (DECL_SOURCE_LOCATION (decl),
15072 : : "%qD is declared %<constexpr%> and is initialized to "
15073 : : "a TU-local value", decl);
15074 : : else
15075 : : {
15076 : : /* This can only occur with references. */
15077 : 6 : gcc_checking_assert (TYPE_REF_P (TREE_TYPE (decl)));
15078 : 6 : error_at (DECL_SOURCE_LOCATION (decl),
15079 : : "%qD is a reference declared %<inline%> and is "
15080 : : "constant-initialized to a TU-local value", decl);
15081 : : }
15082 : 15 : bool informed = is_tu_local_value (decl, DECL_INITIAL (decl),
15083 : : /*explain=*/true);
15084 : 15 : gcc_checking_assert (informed);
15085 : 15 : explained = true;
15086 : 15 : }
15087 : :
15088 : : /* We should have emitted an error above. */
15089 : 96 : gcc_checking_assert (explained);
15090 : : }
15091 : 1040106 : else if (warn_template_names_tu_local
15092 : 1120759 : && dep->refs_tu_local () && !dep->is_tu_local ())
15093 : : {
15094 : 63 : tree decl = dep->get_entity ();
15095 : :
15096 : : /* Friend decls in a class body are ignored, but this is harmless:
15097 : : it should not impact any consumers. */
15098 : 63 : if (RECORD_OR_UNION_TYPE_P (TREE_TYPE (decl)))
15099 : 9 : continue;
15100 : :
15101 : : /* We should now only be warning about templates. */
15102 : 54 : gcc_checking_assert
15103 : : (TREE_CODE (decl) == TEMPLATE_DECL
15104 : : && VAR_OR_FUNCTION_DECL_P (DECL_TEMPLATE_RESULT (decl)));
15105 : :
15106 : : /* Don't warn if we've seen any explicit instantiation definitions,
15107 : : the intent might be for importers to only use those. */
15108 : 54 : if (template_has_explicit_inst (decl))
15109 : 6 : continue;
15110 : :
15111 : 195 : for (depset *rdep : dep->deps)
15112 : 153 : if (!rdep->is_binding () && rdep->is_tu_local ())
15113 : : {
15114 : 48 : tree ref = rdep->get_entity ();
15115 : 48 : auto_diagnostic_group d;
15116 : 48 : if (warning_at (DECL_SOURCE_LOCATION (decl),
15117 : 48 : OPT_Wtemplate_names_tu_local,
15118 : : "%qD refers to TU-local entity %qD and cannot "
15119 : : "be instantiated in other TUs", decl, ref))
15120 : 48 : is_tu_local_entity (ref, /*explain=*/true);
15121 : 48 : break;
15122 : 48 : }
15123 : : }
15124 : : }
15125 : :
15126 : 2409 : return ok;
15127 : : }
15128 : :
15129 : : /* Core of TARJAN's algorithm to find Strongly Connected Components
15130 : : within a graph. See https://en.wikipedia.org/wiki/
15131 : : Tarjan%27s_strongly_connected_components_algorithm for details.
15132 : :
15133 : : We use depset::section as lowlink. Completed nodes have
15134 : : depset::cluster containing the cluster number, with the top
15135 : : bit set.
15136 : :
15137 : : A useful property is that the output vector is a reverse
15138 : : topological sort of the resulting DAG. In our case that means
15139 : : dependent SCCs are found before their dependers. We make use of
15140 : : that property. */
15141 : :
15142 : : void
15143 : 1662268 : depset::tarjan::connect (depset *v)
15144 : : {
15145 : 1662268 : gcc_checking_assert (v->is_binding ()
15146 : : || !(v->is_tu_local ()
15147 : : || v->is_unreached ()
15148 : : || v->is_import ()));
15149 : :
15150 : 1662268 : v->cluster = v->section = ++index;
15151 : 1662268 : stack.safe_push (v);
15152 : :
15153 : : /* Walk all our dependencies, ignore a first marked slot */
15154 : 13759386 : for (unsigned ix = v->is_special (); ix != v->deps.length (); ix++)
15155 : : {
15156 : 5220821 : depset *dep = v->deps[ix];
15157 : :
15158 : 5220821 : if (dep->is_binding ()
15159 : 15371900 : || !(dep->is_import () || dep->is_tu_local ()))
15160 : : {
15161 : 5219929 : unsigned lwm = dep->cluster;
15162 : :
15163 : 5219929 : if (!dep->cluster)
15164 : : {
15165 : : /* A new node. Connect it. */
15166 : 918699 : connect (dep);
15167 : 918699 : lwm = dep->section;
15168 : : }
15169 : :
15170 : 5219929 : if (dep->section && v->section > lwm)
15171 : 818987 : v->section = lwm;
15172 : : }
15173 : : }
15174 : :
15175 : 1662268 : if (v->section == v->cluster)
15176 : : {
15177 : : /* Root of a new SCC. Push all the members onto the result list. */
15178 : : unsigned num = v->cluster;
15179 : 1662268 : depset *p;
15180 : 1662268 : do
15181 : : {
15182 : 1662268 : p = stack.pop ();
15183 : 1662268 : p->cluster = num;
15184 : 1662268 : p->section = 0;
15185 : 1662268 : result.quick_push (p);
15186 : : }
15187 : 1662268 : while (p != v);
15188 : : }
15189 : 1662268 : }
15190 : :
15191 : : /* Compare two depsets. The specific ordering is unimportant, we're
15192 : : just trying to get consistency. */
15193 : :
15194 : : static int
15195 : 77097267 : depset_cmp (const void *a_, const void *b_)
15196 : : {
15197 : 77097267 : depset *a = *(depset *const *)a_;
15198 : 77097267 : depset *b = *(depset *const *)b_;
15199 : :
15200 : 77097267 : depset::entity_kind a_kind = a->get_entity_kind ();
15201 : 77097267 : depset::entity_kind b_kind = b->get_entity_kind ();
15202 : :
15203 : 77097267 : if (a_kind != b_kind)
15204 : : /* Different entity kinds, order by that. */
15205 : 3998588 : return a_kind < b_kind ? -1 : +1;
15206 : :
15207 : 74254517 : tree a_decl = a->get_entity ();
15208 : 74254517 : tree b_decl = b->get_entity ();
15209 : 74254517 : if (a_kind == depset::EK_USING)
15210 : : {
15211 : : /* If one is a using, the other must be too. */
15212 : 735821 : a_decl = OVL_FUNCTION (a_decl);
15213 : 735821 : b_decl = OVL_FUNCTION (b_decl);
15214 : : }
15215 : :
15216 : 74254517 : if (a_decl != b_decl)
15217 : : /* Different entities, order by their UID. */
15218 : 68550864 : return DECL_UID (a_decl) < DECL_UID (b_decl) ? -1 : +1;
15219 : :
15220 : 5703653 : if (a_kind == depset::EK_BINDING)
15221 : : {
15222 : : /* Both are bindings. Order by identifier hash. */
15223 : 5700731 : gcc_checking_assert (a->get_name () != b->get_name ());
15224 : 5700731 : hashval_t ah = IDENTIFIER_HASH_VALUE (a->get_name ());
15225 : 5700731 : hashval_t bh = IDENTIFIER_HASH_VALUE (b->get_name ());
15226 : 8491682 : return (ah == bh ? 0 : ah < bh ? -1 : +1);
15227 : : }
15228 : :
15229 : : /* They are the same decl. This can happen with two using decls
15230 : : pointing to the same target. The best we can aim for is
15231 : : consistently telling qsort how to order them. Hopefully we'll
15232 : : never have to debug a case that depends on this. Oh, who am I
15233 : : kidding? Good luck. */
15234 : 2922 : gcc_checking_assert (a_kind == depset::EK_USING);
15235 : :
15236 : : /* Order by depset address. Not the best, but it is something. */
15237 : 2922 : return a < b ? -1 : +1;
15238 : : }
15239 : :
15240 : : /* Sort the clusters in SCC such that those that depend on one another
15241 : : are placed later. */
15242 : :
15243 : : // FIXME: I am not convinced this is needed and, if needed,
15244 : : // sufficient. We emit the decls in this order but that emission
15245 : : // could walk into later decls (from the body of the decl, or default
15246 : : // arg-like things). Why doesn't that walk do the right thing? And
15247 : : // if it DTRT why do we need to sort here -- won't things naturally
15248 : : // work? I think part of the issue is that when we're going to refer
15249 : : // to an entity by name, and that entity is in the same cluster as us,
15250 : : // we need to actually walk that entity, if we've not already walked
15251 : : // it.
15252 : : static void
15253 : 221326 : sort_cluster (depset::hash *original, depset *scc[], unsigned size)
15254 : : {
15255 : 221326 : depset::hash table (size, original);
15256 : :
15257 : 221326 : dump.indent ();
15258 : :
15259 : : /* Place bindings last, usings before that. It's not strictly
15260 : : necessary, but it does make things neater. Says Mr OCD. */
15261 : : unsigned bind_lwm = size;
15262 : : unsigned use_lwm = size;
15263 : 1115156 : for (unsigned ix = 0; ix != use_lwm;)
15264 : : {
15265 : 893830 : depset *dep = scc[ix];
15266 : 893830 : switch (dep->get_entity_kind ())
15267 : : {
15268 : 116010 : case depset::EK_BINDING:
15269 : : /* Move to end. No increment. Notice this could be moving
15270 : : a using decl, which we'll then move again. */
15271 : 116010 : if (--bind_lwm != ix)
15272 : : {
15273 : 60942 : scc[ix] = scc[bind_lwm];
15274 : 60942 : scc[bind_lwm] = dep;
15275 : : }
15276 : 116010 : if (use_lwm > bind_lwm)
15277 : : {
15278 : 98551 : use_lwm--;
15279 : 98551 : break;
15280 : : }
15281 : : /* We must have copied a using, so move it too. */
15282 : 17459 : dep = scc[ix];
15283 : 17459 : gcc_checking_assert (dep->get_entity_kind () == depset::EK_USING);
15284 : : /* FALLTHROUGH */
15285 : :
15286 : 30906 : case depset::EK_USING:
15287 : 30906 : if (--use_lwm != ix)
15288 : : {
15289 : 21751 : scc[ix] = scc[use_lwm];
15290 : 21751 : scc[use_lwm] = dep;
15291 : : }
15292 : : break;
15293 : :
15294 : 764373 : case depset::EK_DECL:
15295 : 764373 : case depset::EK_SPECIALIZATION:
15296 : 764373 : case depset::EK_PARTIAL:
15297 : 764373 : table.add_mergeable (dep);
15298 : 764373 : ix++;
15299 : 764373 : break;
15300 : :
15301 : 0 : default:
15302 : 0 : gcc_unreachable ();
15303 : : }
15304 : : }
15305 : :
15306 : 221326 : gcc_checking_assert (use_lwm <= bind_lwm);
15307 : 221590 : dump (dumper::MERGE) && dump ("Ordering %u/%u depsets", use_lwm, size);
15308 : :
15309 : 221326 : table.find_dependencies (nullptr);
15310 : :
15311 : 442652 : auto_vec<depset *> order = table.connect ();
15312 : 442652 : gcc_checking_assert (order.length () == use_lwm);
15313 : :
15314 : : /* Now rewrite entries [0,lwm), in the dependency order we
15315 : : discovered. Usually each entity is in its own cluster. Rarely,
15316 : : we can get multi-entity clusters, in which case all but one must
15317 : : only be reached from within the cluster. This happens for
15318 : : something like:
15319 : :
15320 : : template<typename T>
15321 : : auto Foo (const T &arg) -> TPL<decltype (arg)>;
15322 : :
15323 : : The instantiation of TPL will be in the specialization table, and
15324 : : refer to Foo via arg. But we can only get to that specialization
15325 : : from Foo's declaration, so we only need to treat Foo as mergable
15326 : : (We'll do structural comparison of TPL<decltype (arg)>).
15327 : :
15328 : : We approximate finding the single cluster entry dep by checking for
15329 : : entities recursively depending on a dep first seen when streaming
15330 : : its own merge key; the first dep we see in such a cluster should be
15331 : : the first one streamed. */
15332 : : unsigned entry_pos = ~0u;
15333 : : unsigned cluster = ~0u;
15334 : 1971398 : for (unsigned ix = 0; ix != order.length (); ix++)
15335 : : {
15336 : 764373 : gcc_checking_assert (order[ix]->is_special ());
15337 : 764373 : bool tight = order[ix]->cluster == cluster;
15338 : 764373 : depset *dep = order[ix]->deps[0];
15339 : 765096 : dump (dumper::MERGE)
15340 : 1443 : && dump ("Mergeable %u is %N%s%s", ix, dep->get_entity (),
15341 : 723 : tight ? " (tight)" : "", dep->is_entry () ? " (entry)" : "");
15342 : 764373 : scc[ix] = dep;
15343 : 764373 : if (tight)
15344 : : {
15345 : 93 : gcc_checking_assert (dep->is_maybe_recursive ());
15346 : 93 : if (dep->is_entry ())
15347 : : {
15348 : : /* There should only be one entry dep in a cluster. */
15349 : 6 : gcc_checking_assert (!scc[entry_pos]->is_entry ());
15350 : 6 : gcc_checking_assert (scc[entry_pos]->is_maybe_recursive ());
15351 : 6 : scc[ix] = scc[entry_pos];
15352 : 6 : scc[entry_pos] = dep;
15353 : : }
15354 : : }
15355 : : else
15356 : : entry_pos = ix;
15357 : 764373 : cluster = order[ix]->cluster;
15358 : : }
15359 : :
15360 : 221590 : dump (dumper::MERGE) && dump ("Ordered %u keys", order.length ());
15361 : 221326 : dump.outdent ();
15362 : 221326 : }
15363 : :
15364 : : /* Reduce graph to SCCS clusters. SCCS will be populated with the
15365 : : depsets in dependency order. Each depset's CLUSTER field contains
15366 : : its cluster number. Each SCC has a unique cluster number, and are
15367 : : contiguous in SCCS. Cluster numbers are otherwise arbitrary. */
15368 : :
15369 : : vec<depset *>
15370 : 223707 : depset::hash::connect ()
15371 : : {
15372 : 223707 : tarjan connector (size ());
15373 : 223707 : vec<depset *> deps;
15374 : 223707 : deps.create (size ());
15375 : 4063791 : for (depset *item : *this)
15376 : : {
15377 : 1920042 : entity_kind kind = item->get_entity_kind ();
15378 : 1804032 : if (kind == EK_BINDING
15379 : 1804032 : || !(kind == EK_REDIRECT
15380 : 1784842 : || item->is_tu_local ()
15381 : 1784749 : || item->is_unreached ()
15382 : 1546806 : || item->is_import ()))
15383 : 1662268 : deps.quick_push (item);
15384 : : }
15385 : :
15386 : : /* Iteration over the hash table is an unspecified ordering. While
15387 : : that has advantages, it causes 2 problems. Firstly repeatable
15388 : : builds are tricky. Secondly creating testcases that check
15389 : : dependencies are correct by making sure a bad ordering would
15390 : : happen if that was wrong. */
15391 : 1190983 : deps.qsort (depset_cmp);
15392 : :
15393 : 1885975 : while (deps.length ())
15394 : : {
15395 : 1662268 : depset *v = deps.pop ();
15396 : 1662268 : dump (dumper::CLUSTER) &&
15397 : 1599 : (v->is_binding ()
15398 : 195 : ? dump ("Connecting binding %P", v->get_entity (), v->get_name ())
15399 : 1404 : : dump ("Connecting %s %s %C:%N",
15400 : 1404 : is_key_order () ? "key-order"
15401 : 774 : : !v->has_defn () ? "declaration" : "definition",
15402 : 1404 : v->entity_kind_name (), TREE_CODE (v->get_entity ()),
15403 : : v->get_entity ()));
15404 : 1662268 : if (!v->cluster)
15405 : 743569 : connector.connect (v);
15406 : : }
15407 : :
15408 : 223707 : deps.release ();
15409 : 447414 : return connector.result;
15410 : 223707 : }
15411 : :
15412 : : /* Initialize location spans. */
15413 : :
15414 : : void
15415 : 4289 : loc_spans::init (const line_maps *lmaps, const line_map_ordinary *map)
15416 : : {
15417 : 4289 : gcc_checking_assert (!init_p ());
15418 : 4289 : spans = new vec<span> ();
15419 : 4289 : spans->reserve (20);
15420 : :
15421 : 4289 : span interval;
15422 : 4289 : interval.ordinary.first = 0;
15423 : 4289 : interval.macro.second = MAX_LOCATION_T + 1;
15424 : 4289 : interval.ordinary_delta = interval.macro_delta = 0;
15425 : :
15426 : : /* A span for reserved fixed locs. */
15427 : 4289 : interval.ordinary.second
15428 : 4289 : = MAP_START_LOCATION (LINEMAPS_ORDINARY_MAP_AT (line_table, 0));
15429 : 4289 : interval.macro.first = interval.macro.second;
15430 : 4289 : dump (dumper::LOCATION)
15431 : 42 : && dump ("Fixed span %u ordinary:[%K,%K) macro:[%K,%K)", spans->length (),
15432 : : interval.ordinary.first, interval.ordinary.second,
15433 : : interval.macro.first, interval.macro.second);
15434 : 4289 : spans->quick_push (interval);
15435 : :
15436 : : /* A span for command line & forced headers. */
15437 : 4289 : interval.ordinary.first = interval.ordinary.second;
15438 : 4289 : interval.macro.second = interval.macro.first;
15439 : 4289 : if (map)
15440 : : {
15441 : 4283 : interval.ordinary.second = map->start_location;
15442 : 4283 : interval.macro.first = LINEMAPS_MACRO_LOWEST_LOCATION (lmaps);
15443 : : }
15444 : 4289 : dump (dumper::LOCATION)
15445 : 21 : && dump ("Pre span %u ordinary:[%K,%K) macro:[%K,%K)", spans->length (),
15446 : : interval.ordinary.first, interval.ordinary.second,
15447 : : interval.macro.first, interval.macro.second);
15448 : 4289 : spans->quick_push (interval);
15449 : :
15450 : : /* Start an interval for the main file. */
15451 : 4289 : interval.ordinary.first = interval.ordinary.second;
15452 : 4289 : interval.macro.second = interval.macro.first;
15453 : 4289 : dump (dumper::LOCATION)
15454 : 21 : && dump ("Main span %u ordinary:[%K,*) macro:[*,%K)", spans->length (),
15455 : : interval.ordinary.first, interval.macro.second);
15456 : 4289 : spans->quick_push (interval);
15457 : 4289 : }
15458 : :
15459 : : /* Reopen the span, if we want the about-to-be-inserted set of maps to
15460 : : be propagated in our own location table. I.e. we are the primary
15461 : : interface and we're importing a partition. */
15462 : :
15463 : : bool
15464 : 2662 : loc_spans::maybe_propagate (module_state *import, location_t hwm)
15465 : : {
15466 : 2662 : bool opened = (module_interface_p () && !module_partition_p ()
15467 : 3016 : && import->is_partition ());
15468 : 147 : if (opened)
15469 : 147 : open (hwm);
15470 : 2662 : return opened;
15471 : : }
15472 : :
15473 : : /* Open a new linemap interval. The just-created ordinary map is the
15474 : : first map of the interval. */
15475 : :
15476 : : void
15477 : 998 : loc_spans::open (location_t hwm)
15478 : : {
15479 : 998 : span interval;
15480 : 998 : interval.ordinary.first = interval.ordinary.second = hwm;
15481 : 1996 : interval.macro.first = interval.macro.second
15482 : 998 : = LINEMAPS_MACRO_LOWEST_LOCATION (line_table);
15483 : 998 : interval.ordinary_delta = interval.macro_delta = 0;
15484 : 998 : dump (dumper::LOCATION)
15485 : 0 : && dump ("Opening span %u ordinary:[%K,... macro:...,%K)",
15486 : 0 : spans->length (), interval.ordinary.first,
15487 : : interval.macro.second);
15488 : 998 : if (spans->length ())
15489 : : {
15490 : : /* No overlapping! */
15491 : 998 : auto &last = spans->last ();
15492 : 998 : gcc_checking_assert (interval.ordinary.first >= last.ordinary.second);
15493 : 998 : gcc_checking_assert (interval.macro.second <= last.macro.first);
15494 : : }
15495 : 998 : spans->safe_push (interval);
15496 : 998 : }
15497 : :
15498 : : /* Close out the current linemap interval. The last maps are within
15499 : : the interval. */
15500 : :
15501 : : void
15502 : 5284 : loc_spans::close ()
15503 : : {
15504 : 5284 : span &interval = spans->last ();
15505 : :
15506 : 5284 : interval.ordinary.second
15507 : 5284 : = ((line_table->highest_location
15508 : 5284 : + (loc_one << line_table->default_range_bits))
15509 : 5284 : & ~((loc_one << line_table->default_range_bits) - 1));
15510 : 5284 : interval.macro.first = LINEMAPS_MACRO_LOWEST_LOCATION (line_table);
15511 : 5284 : dump (dumper::LOCATION)
15512 : 21 : && dump ("Closing span %u ordinary:[%K,%K) macro:[%K,%K)",
15513 : 21 : spans->length () - 1,
15514 : : interval.ordinary.first,interval.ordinary.second,
15515 : : interval.macro.first, interval.macro.second);
15516 : 5284 : }
15517 : :
15518 : : /* Given an ordinary location LOC, return the lmap_interval it resides
15519 : : in. NULL if it is not in an interval. */
15520 : :
15521 : : const loc_spans::span *
15522 : 23405698 : loc_spans::ordinary (location_t loc)
15523 : : {
15524 : 23405698 : unsigned len = spans->length ();
15525 : 46672531 : unsigned pos = 0;
15526 : 46679609 : while (len)
15527 : : {
15528 : 46672138 : unsigned half = len / 2;
15529 : 46672138 : const span &probe = (*spans)[pos + half];
15530 : 46672138 : if (loc < probe.ordinary.first)
15531 : : len = half;
15532 : 46665060 : else if (loc < probe.ordinary.second)
15533 : : return &probe;
15534 : : else
15535 : : {
15536 : 23266833 : pos += half + 1;
15537 : 23266833 : len = len - (half + 1);
15538 : : }
15539 : : }
15540 : : return NULL;
15541 : : }
15542 : :
15543 : : /* Likewise, given a macro location LOC, return the lmap interval it
15544 : : resides in. */
15545 : :
15546 : : const loc_spans::span *
15547 : 1923790 : loc_spans::macro (location_t loc)
15548 : : {
15549 : 1923790 : unsigned len = spans->length ();
15550 : 3842440 : unsigned pos = 0;
15551 : 3842440 : while (len)
15552 : : {
15553 : 3842428 : unsigned half = len / 2;
15554 : 3842428 : const span &probe = (*spans)[pos + half];
15555 : 3842428 : if (loc >= probe.macro.second)
15556 : : len = half;
15557 : 3842428 : else if (loc >= probe.macro.first)
15558 : : return &probe;
15559 : : else
15560 : : {
15561 : 1918650 : pos += half + 1;
15562 : 1918650 : len = len - (half + 1);
15563 : : }
15564 : : }
15565 : : return NULL;
15566 : : }
15567 : :
15568 : : /* Return the ordinary location closest to FROM. */
15569 : :
15570 : : static location_t
15571 : 6076 : ordinary_loc_of (line_maps *lmaps, location_t from)
15572 : : {
15573 : 12155 : while (!IS_ORDINARY_LOC (from))
15574 : : {
15575 : 3 : if (IS_ADHOC_LOC (from))
15576 : 3 : from = get_location_from_adhoc_loc (lmaps, from);
15577 : 3 : if (from >= LINEMAPS_MACRO_LOWEST_LOCATION (lmaps))
15578 : : {
15579 : : /* Find the ordinary location nearest FROM. */
15580 : 0 : const line_map *map = linemap_lookup (lmaps, from);
15581 : 0 : const line_map_macro *mac_map = linemap_check_macro (map);
15582 : 0 : from = mac_map->get_expansion_point_location ();
15583 : : }
15584 : : }
15585 : 6076 : return from;
15586 : : }
15587 : :
15588 : : static module_state **
15589 : 10690 : get_module_slot (tree name, module_state *parent, bool partition, bool insert)
15590 : : {
15591 : 10690 : module_state_hash::compare_type ct (name, uintptr_t (parent) | partition);
15592 : 10690 : hashval_t hv = module_state_hash::hash (ct);
15593 : :
15594 : 10690 : return modules_hash->find_slot_with_hash (ct, hv, insert ? INSERT : NO_INSERT);
15595 : : }
15596 : :
15597 : : static module_state *
15598 : 393 : get_primary (module_state *parent)
15599 : : {
15600 : 3751 : while (parent->is_partition ())
15601 : 345 : parent = parent->parent;
15602 : :
15603 : 3406 : if (!parent->name)
15604 : : // Implementation unit has null name
15605 : 126 : parent = parent->parent;
15606 : :
15607 : 357 : return parent;
15608 : : }
15609 : :
15610 : : /* Find or create module NAME & PARENT in the hash table. */
15611 : :
15612 : : module_state *
15613 : 10690 : get_module (tree name, module_state *parent, bool partition)
15614 : : {
15615 : : /* We might be given an empty NAME if preprocessing fails to handle
15616 : : a header-name token. */
15617 : 10690 : if (name && TREE_CODE (name) == STRING_CST
15618 : 13411 : && TREE_STRING_LENGTH (name) == 0)
15619 : : return nullptr;
15620 : :
15621 : 10690 : if (partition)
15622 : : {
15623 : 879 : if (!parent)
15624 : 186 : parent = get_primary ((*modules)[0]);
15625 : :
15626 : 879 : if (!parent->is_partition () && !parent->flatname)
15627 : 207 : parent->set_flatname ();
15628 : : }
15629 : :
15630 : 10690 : module_state **slot = get_module_slot (name, parent, partition, true);
15631 : 10690 : module_state *state = *slot;
15632 : 10690 : if (!state)
15633 : : {
15634 : 5839 : state = (new (ggc_alloc<module_state> ())
15635 : 5839 : module_state (name, parent, partition));
15636 : 5839 : *slot = state;
15637 : : }
15638 : : return state;
15639 : : }
15640 : :
15641 : : /* Process string name PTR into a module_state. */
15642 : :
15643 : : static module_state *
15644 : 366 : get_module (const char *ptr)
15645 : : {
15646 : : /* On DOS based file systems, there is an ambiguity with A:B which can be
15647 : : interpreted as a module Module:Partition or Drive:PATH. Interpret strings
15648 : : which clearly starts as pathnames as header-names and everything else is
15649 : : treated as a (possibly malformed) named moduled. */
15650 : 366 : if (IS_DIR_SEPARATOR (ptr[ptr[0] == '.']) // ./FOO or /FOO
15651 : : #if HAVE_DOS_BASED_FILE_SYSTEM
15652 : : || (HAS_DRIVE_SPEC (ptr) && IS_DIR_SEPARATOR (ptr[2])) // A:/FOO
15653 : : #endif
15654 : : || false)
15655 : : /* A header name. */
15656 : 107 : return get_module (build_string (strlen (ptr), ptr));
15657 : :
15658 : : bool partition = false;
15659 : : module_state *mod = NULL;
15660 : :
15661 : 1062 : for (const char *probe = ptr;; probe++)
15662 : 1321 : if (!*probe || *probe == '.' || *probe == ':')
15663 : : {
15664 : 331 : if (probe == ptr)
15665 : : return NULL;
15666 : :
15667 : 331 : mod = get_module (get_identifier_with_length (ptr, probe - ptr),
15668 : : mod, partition);
15669 : 331 : ptr = probe;
15670 : 331 : if (*ptr == ':')
15671 : : {
15672 : 69 : if (partition)
15673 : : return NULL;
15674 : : partition = true;
15675 : : }
15676 : :
15677 : 331 : if (!*ptr++)
15678 : : break;
15679 : : }
15680 : 990 : else if (!(ISALPHA (*probe) || *probe == '_'
15681 : 18 : || (probe != ptr && ISDIGIT (*probe))))
15682 : : return NULL;
15683 : :
15684 : : return mod;
15685 : : }
15686 : :
15687 : : /* Create a new mapper connecting to OPTION. */
15688 : :
15689 : : module_client *
15690 : 4289 : make_mapper (location_t loc, class mkdeps *deps)
15691 : : {
15692 : 4289 : timevar_start (TV_MODULE_MAPPER);
15693 : 4289 : const char *option = module_mapper_name;
15694 : 4289 : if (!option)
15695 : 4247 : option = getenv ("CXX_MODULE_MAPPER");
15696 : :
15697 : 8578 : mapper = module_client::open_module_client
15698 : 4289 : (loc, option, deps, &set_cmi_repo,
15699 : 4289 : (save_decoded_options[0].opt_index == OPT_SPECIAL_program_name)
15700 : 4289 : && save_decoded_options[0].arg != progname
15701 : : ? save_decoded_options[0].arg : nullptr);
15702 : :
15703 : 4289 : timevar_stop (TV_MODULE_MAPPER);
15704 : :
15705 : 4289 : return mapper;
15706 : : }
15707 : :
15708 : : static unsigned lazy_snum;
15709 : :
15710 : : static bool
15711 : 7267 : recursive_lazy (unsigned snum = ~0u)
15712 : : {
15713 : 7267 : if (lazy_snum)
15714 : : {
15715 : 0 : error_at (input_location, "recursive lazy load");
15716 : 0 : return true;
15717 : : }
15718 : :
15719 : 7267 : lazy_snum = snum;
15720 : 7267 : return false;
15721 : : }
15722 : :
15723 : : /* If THIS is the current purview, issue an import error and return false. */
15724 : :
15725 : : bool
15726 : 2525 : module_state::check_not_purview (location_t from)
15727 : : {
15728 : 2525 : module_state *imp = (*modules)[0];
15729 : 2525 : if (imp && !imp->name)
15730 : 1913 : imp = imp->parent;
15731 : 2525 : if (imp == this)
15732 : : {
15733 : : /* Cannot import the current module. */
15734 : 12 : auto_diagnostic_group d;
15735 : 12 : error_at (from, "cannot import module in its own purview");
15736 : 12 : inform (loc, "module %qs declared here", get_flatname ());
15737 : 12 : return false;
15738 : 12 : }
15739 : : return true;
15740 : : }
15741 : :
15742 : : /* Module name substitutions. */
15743 : : static vec<module_state *,va_heap> substs;
15744 : :
15745 : : void
15746 : 7184 : module_state::mangle (bool include_partition)
15747 : : {
15748 : 7184 : if (subst)
15749 : 170 : mangle_module_substitution (subst);
15750 : : else
15751 : : {
15752 : 7014 : if (parent)
15753 : 700 : parent->mangle (include_partition);
15754 : 7014 : if (include_partition || !is_partition ())
15755 : : {
15756 : : // Partitions are significant for global initializer
15757 : : // functions
15758 : 6932 : bool partition = is_partition () && !parent->is_partition ();
15759 : 6932 : subst = mangle_module_component (name, partition);
15760 : 6932 : substs.safe_push (this);
15761 : : }
15762 : : }
15763 : 7184 : }
15764 : :
15765 : : void
15766 : 6484 : mangle_module (int mod, bool include_partition)
15767 : : {
15768 : 6484 : module_state *imp = (*modules)[mod];
15769 : :
15770 : 6484 : gcc_checking_assert (!imp->is_header ());
15771 : :
15772 : 6484 : if (!imp->name)
15773 : : /* Set when importing the primary module interface. */
15774 : 222 : imp = imp->parent;
15775 : :
15776 : 6484 : imp->mangle (include_partition);
15777 : 6484 : }
15778 : :
15779 : : /* Clean up substitutions. */
15780 : : void
15781 : 6278 : mangle_module_fini ()
15782 : : {
15783 : 13210 : while (substs.length ())
15784 : 6932 : substs.pop ()->subst = 0;
15785 : 6278 : }
15786 : :
15787 : : /* Announce WHAT about the module. */
15788 : :
15789 : : void
15790 : 11080 : module_state::announce (const char *what) const
15791 : : {
15792 : 11080 : if (noisy_p ())
15793 : : {
15794 : 0 : fprintf (stderr, " %s:%s", what, get_flatname ());
15795 : 0 : fflush (stderr);
15796 : : }
15797 : 11080 : }
15798 : :
15799 : : /* A human-readable README section. The contents of this section to
15800 : : not contribute to the CRC, so the contents can change per
15801 : : compilation. That allows us to embed CWD, hostname, build time and
15802 : : what not. It is a STRTAB that may be extracted with:
15803 : : readelf -pgnu.c++.README $(module).gcm */
15804 : :
15805 : : void
15806 : 2381 : module_state::write_readme (elf_out *to, cpp_reader *reader, const char *dialect)
15807 : : {
15808 : 2381 : bytes_out readme (to);
15809 : :
15810 : 2381 : readme.begin (false);
15811 : :
15812 : 2381 : readme.printf ("GNU C++ %s",
15813 : 2381 : is_header () ? "header unit"
15814 : 1530 : : !is_partition () ? "primary interface"
15815 : 159 : : is_interface () ? "interface partition"
15816 : : : "internal partition");
15817 : :
15818 : : /* Compiler's version. */
15819 : 2381 : readme.printf ("compiler: %s", version_string);
15820 : :
15821 : : /* Module format version. */
15822 : 2381 : verstr_t string;
15823 : 2381 : version2string (MODULE_VERSION, string);
15824 : 2381 : readme.printf ("version: %s", string);
15825 : :
15826 : : /* Module information. */
15827 : 2381 : readme.printf ("module: %s", get_flatname ());
15828 : 2381 : readme.printf ("source: %s", main_input_filename);
15829 : 2381 : readme.printf ("dialect: %s", dialect);
15830 : 2381 : if (extensions)
15831 : 24 : readme.printf ("extensions: %s%s%s",
15832 : : extensions & SE_OPENMP ? "-fopenmp"
15833 : 6 : : extensions & SE_OPENMP_SIMD ? "-fopenmp-simd" : "",
15834 : : (extensions & SE_OPENACC)
15835 : 3 : && (extensions & (SE_OPENMP | SE_OPENMP_SIMD))
15836 : : ? " " : "",
15837 : 9 : extensions & SE_OPENACC ? "-fopenacc" : "");
15838 : :
15839 : : /* The following fields could be expected to change between
15840 : : otherwise identical compilations. Consider a distributed build
15841 : : system. We should have a way of overriding that. */
15842 : 2381 : if (char *cwd = getcwd (NULL, 0))
15843 : : {
15844 : 2381 : readme.printf ("cwd: %s", cwd);
15845 : 2381 : free (cwd);
15846 : : }
15847 : 4762 : readme.printf ("repository: %s", cmi_repo ? cmi_repo : ".");
15848 : : #if NETWORKING
15849 : : {
15850 : : char hostname[64];
15851 : : if (!gethostname (hostname, sizeof (hostname)))
15852 : : readme.printf ("host: %s", hostname);
15853 : : }
15854 : : #endif
15855 : 2381 : {
15856 : : /* This of course will change! */
15857 : 2381 : time_t stampy;
15858 : 2381 : auto kind = cpp_get_date (reader, &stampy);
15859 : 2381 : if (kind != CPP_time_kind::UNKNOWN)
15860 : : {
15861 : 2381 : struct tm *time;
15862 : :
15863 : 2381 : time = gmtime (&stampy);
15864 : 2381 : readme.print_time ("build", time, "UTC");
15865 : :
15866 : 2381 : if (kind == CPP_time_kind::DYNAMIC)
15867 : : {
15868 : 2381 : time = localtime (&stampy);
15869 : 2381 : readme.print_time ("local", time,
15870 : : #if defined (__USE_MISC) || defined (__USE_BSD) /* Is there a better way? */
15871 : : time->tm_zone
15872 : : #else
15873 : : ""
15874 : : #endif
15875 : : );
15876 : : }
15877 : : }
15878 : : }
15879 : :
15880 : : /* Its direct imports. */
15881 : 2941 : for (unsigned ix = 1; ix < modules->length (); ix++)
15882 : : {
15883 : 560 : module_state *state = (*modules)[ix];
15884 : :
15885 : 560 : if (state->is_direct ())
15886 : 829 : readme.printf ("%s: %s %s", state->exported_p ? "export" : "import",
15887 : : state->get_flatname (), state->filename);
15888 : : }
15889 : :
15890 : 2381 : readme.end (to, to->name (MOD_SNAME_PFX ".README"), NULL);
15891 : 2381 : }
15892 : :
15893 : : /* Sort environment var names in reverse order. */
15894 : :
15895 : : static int
15896 : 0 : env_var_cmp (const void *a_, const void *b_)
15897 : : {
15898 : 0 : const unsigned char *a = *(const unsigned char *const *)a_;
15899 : 0 : const unsigned char *b = *(const unsigned char *const *)b_;
15900 : :
15901 : 0 : for (unsigned ix = 0; ; ix++)
15902 : : {
15903 : 0 : bool a_end = !a[ix] || a[ix] == '=';
15904 : 0 : if (a[ix] == b[ix])
15905 : : {
15906 : 0 : if (a_end)
15907 : : break;
15908 : : }
15909 : : else
15910 : : {
15911 : 0 : bool b_end = !b[ix] || b[ix] == '=';
15912 : :
15913 : 0 : if (!a_end && !b_end)
15914 : 0 : return a[ix] < b[ix] ? +1 : -1;
15915 : 0 : if (a_end && b_end)
15916 : : break;
15917 : 0 : return a_end ? +1 : -1;
15918 : : }
15919 : 0 : }
15920 : :
15921 : : return 0;
15922 : : }
15923 : :
15924 : : /* Write the environment. It is a STRTAB that may be extracted with:
15925 : : readelf -pgnu.c++.ENV $(module).gcm */
15926 : :
15927 : : void
15928 : 0 : module_state::write_env (elf_out *to)
15929 : : {
15930 : 0 : vec<const char *> vars;
15931 : 0 : vars.create (20);
15932 : :
15933 : 0 : extern char **environ;
15934 : 0 : while (const char *var = environ[vars.length ()])
15935 : 0 : vars.safe_push (var);
15936 : 0 : vars.qsort (env_var_cmp);
15937 : :
15938 : 0 : bytes_out env (to);
15939 : 0 : env.begin (false);
15940 : 0 : while (vars.length ())
15941 : 0 : env.printf ("%s", vars.pop ());
15942 : 0 : env.end (to, to->name (MOD_SNAME_PFX ".ENV"), NULL);
15943 : :
15944 : 0 : vars.release ();
15945 : 0 : }
15946 : :
15947 : : /* Write the direct or indirect imports.
15948 : : u:N
15949 : : {
15950 : : u:index
15951 : : s:name
15952 : : u32:crc
15953 : : s:filename (direct)
15954 : : u:exported (direct)
15955 : : } imports[N]
15956 : : */
15957 : :
15958 : : void
15959 : 754 : module_state::write_imports (bytes_out &sec, bool direct)
15960 : : {
15961 : 754 : unsigned count = 0;
15962 : :
15963 : 1586 : for (unsigned ix = 1; ix < modules->length (); ix++)
15964 : : {
15965 : 832 : module_state *imp = (*modules)[ix];
15966 : :
15967 : 832 : if (imp->remap && imp->is_direct () == direct)
15968 : 401 : count++;
15969 : : }
15970 : :
15971 : 754 : gcc_assert (!direct || count);
15972 : :
15973 : 754 : sec.u (count);
15974 : 1586 : for (unsigned ix = 1; ix < modules->length (); ix++)
15975 : : {
15976 : 832 : module_state *imp = (*modules)[ix];
15977 : :
15978 : 832 : if (imp->remap && imp->is_direct () == direct)
15979 : : {
15980 : 539 : dump () && dump ("Writing %simport:%u->%u %M (crc=%x)",
15981 : : !direct ? "indirect "
15982 : 69 : : imp->exported_p ? "exported " : "",
15983 : : ix, imp->remap, imp, imp->crc);
15984 : 401 : sec.u (imp->remap);
15985 : 401 : sec.str (imp->get_flatname ());
15986 : 401 : sec.u32 (imp->crc);
15987 : 401 : if (direct)
15988 : : {
15989 : 396 : write_location (sec, imp->imported_from ());
15990 : 396 : sec.str (imp->filename);
15991 : 396 : int exportedness = 0;
15992 : 396 : if (imp->exported_p)
15993 : : exportedness = +1;
15994 : 236 : else if (!imp->is_purview_direct ())
15995 : 12 : exportedness = -1;
15996 : 396 : sec.i (exportedness);
15997 : : }
15998 : : }
15999 : : }
16000 : 754 : }
16001 : :
16002 : : /* READER, LMAPS != NULL == direct imports,
16003 : : == NUL == indirect imports. */
16004 : :
16005 : : unsigned
16006 : 636 : module_state::read_imports (bytes_in &sec, cpp_reader *reader, line_maps *lmaps)
16007 : : {
16008 : 636 : unsigned count = sec.u ();
16009 : 636 : unsigned loaded = 0;
16010 : :
16011 : 1605 : while (count--)
16012 : : {
16013 : 333 : unsigned ix = sec.u ();
16014 : 333 : if (ix >= slurp->remap->length () || !ix || (*slurp->remap)[ix])
16015 : : {
16016 : 0 : sec.set_overrun ();
16017 : 0 : break;
16018 : : }
16019 : :
16020 : 333 : const char *name = sec.str (NULL);
16021 : 333 : module_state *imp = get_module (name);
16022 : 333 : unsigned crc = sec.u32 ();
16023 : 333 : int exportedness = 0;
16024 : :
16025 : : /* If the import is a partition, it must be the same primary
16026 : : module as this TU. */
16027 : 333 : if (imp && imp->is_partition () &&
16028 : : (!named_module_p ()
16029 : 126 : || (get_primary ((*modules)[0]) != get_primary (imp))))
16030 : : imp = NULL;
16031 : :
16032 : 333 : if (!imp)
16033 : 0 : sec.set_overrun ();
16034 : 333 : if (sec.get_overrun ())
16035 : : break;
16036 : :
16037 : 333 : if (lmaps)
16038 : : {
16039 : : /* A direct import, maybe load it. */
16040 : 333 : location_t floc = read_location (sec);
16041 : 333 : const char *fname = sec.str (NULL);
16042 : 333 : exportedness = sec.i ();
16043 : :
16044 : 333 : if (sec.get_overrun ())
16045 : : break;
16046 : :
16047 : 333 : if (!imp->check_not_purview (loc))
16048 : 3 : continue;
16049 : :
16050 : 330 : if (imp->loadedness == ML_NONE)
16051 : : {
16052 : 262 : imp->loc = floc;
16053 : 262 : imp->crc = crc;
16054 : 262 : if (!imp->get_flatname ())
16055 : 223 : imp->set_flatname ();
16056 : :
16057 : 262 : unsigned n = dump.push (imp);
16058 : :
16059 : 262 : if (!imp->filename && fname)
16060 : 220 : imp->filename = xstrdup (fname);
16061 : :
16062 : 262 : if (imp->is_partition ())
16063 : 30 : dump () && dump ("Importing elided partition %M", imp);
16064 : :
16065 : 262 : if (!imp->do_import (reader, false))
16066 : 3 : imp = NULL;
16067 : 262 : dump.pop (n);
16068 : 262 : if (!imp)
16069 : 3 : continue;
16070 : : }
16071 : :
16072 : 327 : if (is_partition ())
16073 : : {
16074 : 54 : if (!imp->is_direct ())
16075 : 27 : imp->directness = MD_PARTITION_DIRECT;
16076 : 54 : if (exportedness > 0)
16077 : 3 : imp->exported_p = true;
16078 : : }
16079 : : }
16080 : : else
16081 : : {
16082 : : /* An indirect import, find it, it should already be here. */
16083 : 0 : if (imp->loadedness == ML_NONE)
16084 : : {
16085 : 0 : error_at (loc, "indirect import %qs is not already loaded", name);
16086 : 0 : continue;
16087 : : }
16088 : : }
16089 : :
16090 : 327 : if (imp->crc != crc)
16091 : 0 : error_at (loc, "import %qs has CRC mismatch", imp->get_flatname ());
16092 : :
16093 : 327 : (*slurp->remap)[ix] = (imp->mod << 1) | (lmaps != NULL);
16094 : :
16095 : 327 : if (lmaps && exportedness >= 0)
16096 : 315 : set_import (imp, bool (exportedness));
16097 : 477 : dump () && dump ("Found %simport:%u %M->%u", !lmaps ? "indirect "
16098 : 75 : : exportedness > 0 ? "exported "
16099 : 42 : : exportedness < 0 ? "gmf" : "", ix, imp,
16100 : : imp->mod);
16101 : 327 : loaded++;
16102 : : }
16103 : :
16104 : 636 : return loaded;
16105 : : }
16106 : :
16107 : : /* Write the import table to MOD_SNAME_PFX.imp. */
16108 : :
16109 : : void
16110 : 377 : module_state::write_imports (elf_out *to, unsigned *crc_ptr)
16111 : : {
16112 : 443 : dump () && dump ("Writing imports");
16113 : 377 : dump.indent ();
16114 : :
16115 : 377 : bytes_out sec (to);
16116 : 377 : sec.begin ();
16117 : :
16118 : 377 : write_imports (sec, true);
16119 : 377 : write_imports (sec, false);
16120 : :
16121 : 377 : sec.end (to, to->name (MOD_SNAME_PFX ".imp"), crc_ptr);
16122 : 377 : dump.outdent ();
16123 : 377 : }
16124 : :
16125 : : bool
16126 : 318 : module_state::read_imports (cpp_reader *reader, line_maps *lmaps)
16127 : : {
16128 : 318 : bytes_in sec;
16129 : :
16130 : 318 : if (!sec.begin (loc, from (), MOD_SNAME_PFX ".imp"))
16131 : : return false;
16132 : :
16133 : 390 : dump () && dump ("Reading %u imports", slurp->remap->length () - 1);
16134 : 318 : dump.indent ();
16135 : :
16136 : : /* Read the imports. */
16137 : 318 : unsigned direct = read_imports (sec, reader, lmaps);
16138 : 318 : unsigned indirect = read_imports (sec, NULL, NULL);
16139 : 318 : if (direct + indirect + 1 != slurp->remap->length ())
16140 : 6 : from ()->set_error (elf::E_BAD_IMPORT);
16141 : :
16142 : 318 : dump.outdent ();
16143 : 318 : if (!sec.end (from ()))
16144 : : return false;
16145 : : return true;
16146 : 318 : }
16147 : :
16148 : : /* We're the primary module interface, but have partitions. Document
16149 : : them so that non-partition module implementation units know which
16150 : : have already been loaded. */
16151 : :
16152 : : void
16153 : 111 : module_state::write_partitions (elf_out *to, unsigned count, unsigned *crc_ptr)
16154 : : {
16155 : 129 : dump () && dump ("Writing %u elided partitions", count);
16156 : 111 : dump.indent ();
16157 : :
16158 : 111 : bytes_out sec (to);
16159 : 111 : sec.begin ();
16160 : :
16161 : 285 : for (unsigned ix = 1; ix != modules->length (); ix++)
16162 : : {
16163 : 174 : module_state *imp = (*modules)[ix];
16164 : 174 : if (imp->is_partition ())
16165 : : {
16166 : 189 : dump () && dump ("Writing elided partition %M (crc=%x)",
16167 : : imp, imp->crc);
16168 : 159 : sec.str (imp->get_flatname ());
16169 : 159 : sec.u32 (imp->crc);
16170 : 309 : write_location (sec, imp->is_direct ()
16171 : 150 : ? imp->imported_from () : UNKNOWN_LOCATION);
16172 : 159 : sec.str (imp->filename);
16173 : : }
16174 : : }
16175 : :
16176 : 111 : sec.end (to, to->name (MOD_SNAME_PFX ".prt"), crc_ptr);
16177 : 111 : dump.outdent ();
16178 : 111 : }
16179 : :
16180 : : bool
16181 : 15 : module_state::read_partitions (unsigned count)
16182 : : {
16183 : 15 : bytes_in sec;
16184 : 15 : if (!sec.begin (loc, from (), MOD_SNAME_PFX ".prt"))
16185 : : return false;
16186 : :
16187 : 18 : dump () && dump ("Reading %u elided partitions", count);
16188 : 15 : dump.indent ();
16189 : :
16190 : 42 : while (count--)
16191 : : {
16192 : 27 : const char *name = sec.str (NULL);
16193 : 27 : unsigned crc = sec.u32 ();
16194 : 27 : location_t floc = read_location (sec);
16195 : 27 : const char *fname = sec.str (NULL);
16196 : :
16197 : 27 : if (sec.get_overrun ())
16198 : : break;
16199 : :
16200 : 33 : dump () && dump ("Reading elided partition %s (crc=%x)", name, crc);
16201 : :
16202 : 27 : module_state *imp = get_module (name);
16203 : 27 : if (!imp /* Partition should be ... */
16204 : 27 : || !imp->is_partition () /* a partition ... */
16205 : 27 : || imp->loadedness != ML_NONE /* that is not yet loaded ... */
16206 : 54 : || get_primary (imp) != this) /* whose primary is this. */
16207 : : {
16208 : 0 : sec.set_overrun ();
16209 : 0 : break;
16210 : : }
16211 : :
16212 : 27 : if (!imp->has_location ())
16213 : 24 : imp->loc = floc;
16214 : 27 : imp->crc = crc;
16215 : 27 : if (!imp->filename && fname[0])
16216 : 24 : imp->filename = xstrdup (fname);
16217 : : }
16218 : :
16219 : 15 : dump.outdent ();
16220 : 15 : if (!sec.end (from ()))
16221 : : return false;
16222 : : return true;
16223 : 15 : }
16224 : :
16225 : : /* Data for config reading and writing. */
16226 : : struct module_state_config {
16227 : : const char *dialect_str = get_dialect ();
16228 : : line_map_uint_t ordinary_locs = 0;
16229 : : line_map_uint_t macro_locs = 0;
16230 : : unsigned num_imports = 0;
16231 : : unsigned num_partitions = 0;
16232 : : unsigned num_entities = 0;
16233 : : unsigned loc_range_bits = 0;
16234 : : unsigned active_init = 0;
16235 : :
16236 : 93543 : static void release ()
16237 : : {
16238 : 93543 : XDELETEVEC (dialect);
16239 : 93543 : dialect = NULL;
16240 : : }
16241 : :
16242 : : private:
16243 : : static const char *get_dialect ();
16244 : : static char *dialect;
16245 : : };
16246 : :
16247 : : char *module_state_config::dialect;
16248 : :
16249 : : /* Generate a string of the significant compilation options.
16250 : : Generally assume the user knows what they're doing, in the same way
16251 : : that object files can be mixed. */
16252 : :
16253 : : const char *
16254 : 5130 : module_state_config::get_dialect ()
16255 : : {
16256 : 5130 : if (!dialect)
16257 : 8242 : dialect = concat (get_cxx_dialect_name (cxx_dialect),
16258 : : /* C++ implies these, only show if disabled. */
16259 : 4121 : flag_exceptions ? "" : "/no-exceptions",
16260 : 4121 : flag_rtti ? "" : "/no-rtti",
16261 : 4121 : flag_new_inheriting_ctors ? "" : "/old-inheriting-ctors",
16262 : : /* C++ 20 implies concepts and coroutines. */
16263 : 1320 : cxx_dialect < cxx20 && flag_concepts ? "/concepts" : "",
16264 : 4121 : (cxx_dialect < cxx20 && flag_coroutines
16265 : : ? "/coroutines" : ""),
16266 : 4121 : flag_module_implicit_inline ? "/implicit-inline" : "",
16267 : 4121 : flag_contracts ? "/contracts" : "",
16268 : : NULL);
16269 : :
16270 : 5130 : return dialect;
16271 : : }
16272 : :
16273 : : /* Contents of a cluster. */
16274 : : enum cluster_tag {
16275 : : ct_decl, /* A decl. */
16276 : : ct_defn, /* A definition. */
16277 : : ct_bind, /* A binding. */
16278 : : ct_hwm
16279 : : };
16280 : :
16281 : : /* Binding modifiers. */
16282 : : enum ct_bind_flags
16283 : : {
16284 : : cbf_export = 0x1, /* An exported decl. */
16285 : : cbf_hidden = 0x2, /* A hidden (friend) decl. */
16286 : : cbf_using = 0x4, /* A using decl. */
16287 : : };
16288 : :
16289 : : /* DEP belongs to a different cluster, seed it to prevent
16290 : : unfortunately timed duplicate import. */
16291 : : // FIXME: QOI For inter-cluster references we could just only pick
16292 : : // one entity from an earlier cluster. Even better track
16293 : : // dependencies between earlier clusters
16294 : :
16295 : : void
16296 : 4941558 : module_state::intercluster_seed (trees_out &sec, unsigned index_hwm, depset *dep)
16297 : : {
16298 : 4941558 : if (dep->is_tu_local ())
16299 : : /* We only stream placeholders for TU-local entities anyway. */;
16300 : 4941405 : else if (dep->is_import () || dep->cluster < index_hwm)
16301 : : {
16302 : 2219364 : tree ent = dep->get_entity ();
16303 : 2219364 : if (!TREE_VISITED (ent))
16304 : : {
16305 : 977007 : sec.tree_node (ent);
16306 : 977397 : dump (dumper::CLUSTER)
16307 : 390 : && dump ("Seeded %s %N",
16308 : 390 : dep->is_import () ? "import" : "intercluster", ent);
16309 : : }
16310 : : }
16311 : 4941558 : }
16312 : :
16313 : : /* Write the cluster of depsets in SCC[0-SIZE).
16314 : : dep->section -> section number
16315 : : dep->cluster -> entity number
16316 : : */
16317 : :
16318 : : unsigned
16319 : 221326 : module_state::write_cluster (elf_out *to, depset *scc[], unsigned size,
16320 : : depset::hash &table, unsigned *counts,
16321 : : unsigned *crc_ptr)
16322 : : {
16323 : 222568 : dump () && dump ("Writing section:%u %u depsets", table.section, size);
16324 : 221326 : dump.indent ();
16325 : :
16326 : 221326 : trees_out sec (to, this, table, table.section);
16327 : 221326 : sec.begin ();
16328 : 221326 : unsigned index_lwm = counts[MSC_entities];
16329 : :
16330 : : /* Determine entity numbers, mark for writing. */
16331 : 221608 : dump (dumper::CLUSTER) && dump ("Cluster members:") && (dump.indent (), true);
16332 : 1115156 : for (unsigned ix = 0; ix != size; ix++)
16333 : : {
16334 : 893830 : depset *b = scc[ix];
16335 : :
16336 : 893830 : switch (b->get_entity_kind ())
16337 : : {
16338 : 0 : default:
16339 : 0 : gcc_unreachable ();
16340 : :
16341 : 116010 : case depset::EK_BINDING:
16342 : 116010 : {
16343 : 116010 : dump (dumper::CLUSTER)
16344 : 195 : && dump ("[%u]=%s %P", ix, b->entity_kind_name (),
16345 : : b->get_entity (), b->get_name ());
16346 : 116010 : depset *ns_dep = b->deps[0];
16347 : 116010 : gcc_checking_assert (ns_dep->get_entity_kind ()
16348 : : == depset::EK_NAMESPACE
16349 : : && ns_dep->get_entity () == b->get_entity ());
16350 : 260922 : for (unsigned jx = b->deps.length (); --jx;)
16351 : : {
16352 : 144912 : depset *dep = b->deps[jx];
16353 : : // We could be declaring something that is also a
16354 : : // (merged) import
16355 : 158359 : gcc_checking_assert (dep->is_import ()
16356 : : || TREE_VISITED (dep->get_entity ())
16357 : : || (dep->get_entity_kind ()
16358 : : == depset::EK_USING));
16359 : : }
16360 : : }
16361 : : break;
16362 : :
16363 : 764373 : case depset::EK_DECL:
16364 : 764373 : case depset::EK_SPECIALIZATION:
16365 : 764373 : case depset::EK_PARTIAL:
16366 : 764373 : b->cluster = counts[MSC_entities]++;
16367 : 764373 : sec.mark_declaration (b->get_entity (), b->has_defn ());
16368 : : /* FALLTHROUGH */
16369 : :
16370 : 777820 : case depset::EK_USING:
16371 : 1555640 : gcc_checking_assert (!b->is_import ()
16372 : : && !b->is_unreached ());
16373 : 895975 : dump (dumper::CLUSTER)
16374 : 1014 : && dump ("[%u]=%s %s %N", ix, b->entity_kind_name (),
16375 : 639 : b->has_defn () ? "definition" : "declaration",
16376 : : b->get_entity ());
16377 : : break;
16378 : : }
16379 : : }
16380 : 221608 : dump (dumper::CLUSTER) && (dump.outdent (), true);
16381 : :
16382 : : /* Ensure every out-of-cluster decl is referenced before we start
16383 : : streaming. We must do both imports *and* earlier clusters,
16384 : : because the latter could reach into the former and cause a
16385 : : duplicate loop. */
16386 : 221326 : sec.set_importing (+1);
16387 : 1115156 : for (unsigned ix = 0; ix != size; ix++)
16388 : : {
16389 : 893830 : depset *b = scc[ix];
16390 : 10499214 : for (unsigned jx = b->is_special (); jx != b->deps.length (); jx++)
16391 : : {
16392 : 4358102 : depset *dep = b->deps[jx];
16393 : :
16394 : 4358102 : if (dep->is_binding ())
16395 : : {
16396 : 873280 : for (unsigned ix = dep->deps.length (); --ix;)
16397 : : {
16398 : 583456 : depset *bind = dep->deps[ix];
16399 : 583456 : if (bind->get_entity_kind () == depset::EK_USING)
16400 : 38332 : bind = bind->deps[1];
16401 : :
16402 : 583456 : intercluster_seed (sec, index_lwm, bind);
16403 : : }
16404 : : /* Also check the namespace itself. */
16405 : 144912 : dep = dep->deps[0];
16406 : : }
16407 : :
16408 : 4358102 : intercluster_seed (sec, index_lwm, dep);
16409 : : }
16410 : : }
16411 : 221326 : sec.tree_node (NULL_TREE);
16412 : : /* We're done importing now. */
16413 : 221326 : sec.set_importing (-1);
16414 : :
16415 : : /* Write non-definitions. */
16416 : 1115156 : for (unsigned ix = 0; ix != size; ix++)
16417 : : {
16418 : 893830 : depset *b = scc[ix];
16419 : 893830 : tree decl = b->get_entity ();
16420 : 893830 : switch (b->get_entity_kind ())
16421 : : {
16422 : 0 : default:
16423 : 0 : gcc_unreachable ();
16424 : 116010 : break;
16425 : :
16426 : 116010 : case depset::EK_BINDING:
16427 : 116010 : {
16428 : 116010 : gcc_assert (TREE_CODE (decl) == NAMESPACE_DECL);
16429 : 117030 : dump () && dump ("Depset:%u binding %C:%P", ix, TREE_CODE (decl),
16430 : : decl, b->get_name ());
16431 : 116010 : sec.u (ct_bind);
16432 : 116010 : sec.tree_node (decl);
16433 : 116010 : sec.tree_node (b->get_name ());
16434 : :
16435 : : /* Write in reverse order, so reading will see the exports
16436 : : first, thus building the overload chain will be
16437 : : optimized. */
16438 : 376932 : for (unsigned jx = b->deps.length (); --jx;)
16439 : : {
16440 : 144912 : depset *dep = b->deps[jx];
16441 : 144912 : tree bound = dep->get_entity ();
16442 : 144912 : unsigned flags = 0;
16443 : 144912 : if (dep->get_entity_kind () == depset::EK_USING)
16444 : : {
16445 : 13447 : tree ovl = bound;
16446 : 13447 : bound = OVL_FUNCTION (bound);
16447 : 13447 : if (!(TREE_CODE (bound) == CONST_DECL
16448 : 3691 : && UNSCOPED_ENUM_P (TREE_TYPE (bound))
16449 : 3086 : && decl == TYPE_NAME (TREE_TYPE (bound))))
16450 : : /* An unscoped enumerator in its enumeration's
16451 : : scope is not a using. */
16452 : : flags |= cbf_using;
16453 : 13447 : if (OVL_EXPORT_P (ovl))
16454 : 12588 : flags |= cbf_export;
16455 : : }
16456 : : else
16457 : : {
16458 : : /* An implicit typedef must be at one. */
16459 : 131465 : gcc_assert (!DECL_IMPLICIT_TYPEDEF_P (bound) || jx == 1);
16460 : 131465 : if (dep->is_hidden ())
16461 : : flags |= cbf_hidden;
16462 : 127711 : else if (DECL_MODULE_EXPORT_P (STRIP_TEMPLATE (bound)))
16463 : 117150 : flags |= cbf_export;
16464 : : }
16465 : :
16466 : 144912 : gcc_checking_assert (DECL_P (bound));
16467 : :
16468 : 144912 : sec.i (flags);
16469 : 144912 : sec.tree_node (bound);
16470 : : }
16471 : :
16472 : : /* Terminate the list. */
16473 : 116010 : sec.i (-1);
16474 : : }
16475 : 116010 : break;
16476 : :
16477 : 13447 : case depset::EK_USING:
16478 : 13471 : dump () && dump ("Depset:%u %s %C:%N", ix, b->entity_kind_name (),
16479 : 24 : TREE_CODE (decl), decl);
16480 : : break;
16481 : :
16482 : 764373 : case depset::EK_SPECIALIZATION:
16483 : 764373 : case depset::EK_PARTIAL:
16484 : 764373 : case depset::EK_DECL:
16485 : 766494 : dump () && dump ("Depset:%u %s entity:%u %C:%N", ix,
16486 : : b->entity_kind_name (), b->cluster,
16487 : 2121 : TREE_CODE (decl), decl);
16488 : :
16489 : 764373 : sec.u (ct_decl);
16490 : 764373 : sec.tree_node (decl);
16491 : :
16492 : 895951 : dump () && dump ("Wrote declaration entity:%u %C:%N",
16493 : 2121 : b->cluster, TREE_CODE (decl), decl);
16494 : : break;
16495 : : }
16496 : : }
16497 : :
16498 : : depset *namer = NULL;
16499 : :
16500 : : /* Write out definitions */
16501 : 1115156 : for (unsigned ix = 0; ix != size; ix++)
16502 : : {
16503 : 893830 : depset *b = scc[ix];
16504 : 893830 : tree decl = b->get_entity ();
16505 : 893830 : switch (b->get_entity_kind ())
16506 : : {
16507 : : default:
16508 : : break;
16509 : :
16510 : 764373 : case depset::EK_SPECIALIZATION:
16511 : 764373 : case depset::EK_PARTIAL:
16512 : 764373 : case depset::EK_DECL:
16513 : 764373 : if (!namer)
16514 : 213565 : namer = b;
16515 : :
16516 : 764373 : if (b->has_defn ())
16517 : : {
16518 : 283567 : sec.u (ct_defn);
16519 : 283567 : sec.tree_node (decl);
16520 : 284131 : dump () && dump ("Writing definition %N", decl);
16521 : 283567 : sec.write_definition (decl, b->refs_tu_local ());
16522 : :
16523 : 283567 : if (!namer->has_defn ())
16524 : 893830 : namer = b;
16525 : : }
16526 : : break;
16527 : : }
16528 : : }
16529 : :
16530 : : /* We don't find the section by name. Use depset's decl's name for
16531 : : human friendliness. */
16532 : 221326 : unsigned name = 0;
16533 : 221326 : tree naming_decl = NULL_TREE;
16534 : 221326 : if (namer)
16535 : : {
16536 : 213565 : naming_decl = namer->get_entity ();
16537 : 213565 : if (namer->get_entity_kind () == depset::EK_USING)
16538 : : /* This unfortunately names the section from the target of the
16539 : : using decl. But the name is only a guide, so Do Not Care. */
16540 : 0 : naming_decl = OVL_FUNCTION (naming_decl);
16541 : 213565 : if (DECL_IMPLICIT_TYPEDEF_P (naming_decl))
16542 : : /* Lose any anonymousness. */
16543 : 72674 : naming_decl = TYPE_NAME (TREE_TYPE (naming_decl));
16544 : 213565 : name = to->qualified_name (naming_decl, namer->has_defn ());
16545 : : }
16546 : :
16547 : 221326 : unsigned bytes = sec.pos;
16548 : 221326 : unsigned snum = sec.end (to, name, crc_ptr);
16549 : :
16550 : 1115156 : for (unsigned ix = size; ix--;)
16551 : 893830 : gcc_checking_assert (scc[ix]->section == snum);
16552 : :
16553 : 221326 : dump.outdent ();
16554 : 222568 : dump () && dump ("Wrote section:%u named-by:%N", table.section, naming_decl);
16555 : :
16556 : 221326 : return bytes;
16557 : 221326 : }
16558 : :
16559 : : /* Read a cluster from section SNUM. */
16560 : :
16561 : : bool
16562 : 163869 : module_state::read_cluster (unsigned snum)
16563 : : {
16564 : 163869 : trees_in sec (this);
16565 : :
16566 : 163869 : if (!sec.begin (loc, from (), snum))
16567 : : return false;
16568 : :
16569 : 164979 : dump () && dump ("Reading section:%u", snum);
16570 : 163869 : dump.indent ();
16571 : :
16572 : : /* We care about structural equality. */
16573 : 163869 : comparing_dependent_aliases++;
16574 : :
16575 : : /* First seed the imports. */
16576 : 936339 : while (tree import = sec.tree_node ())
16577 : 1708974 : dump (dumper::CLUSTER) && dump ("Seeded import %N", import);
16578 : :
16579 : 1103996 : while (!sec.get_overrun () && sec.more_p ())
16580 : : {
16581 : 940127 : unsigned ct = sec.u ();
16582 : 940127 : switch (ct)
16583 : : {
16584 : 0 : default:
16585 : 0 : sec.set_overrun ();
16586 : 0 : break;
16587 : :
16588 : 86122 : case ct_bind:
16589 : : /* A set of namespace bindings. */
16590 : 86122 : {
16591 : 86122 : tree ns = sec.tree_node ();
16592 : 86122 : tree name = sec.tree_node ();
16593 : 86122 : tree decls = NULL_TREE;
16594 : 86122 : tree visible = NULL_TREE;
16595 : 86122 : tree type = NULL_TREE;
16596 : 86122 : bool dedup = false;
16597 : 86122 : bool global_p = is_header ();
16598 : :
16599 : : /* We rely on the bindings being in the reverse order of
16600 : : the resulting overload set. */
16601 : 196854 : for (;;)
16602 : : {
16603 : 196854 : int flags = sec.i ();
16604 : 196854 : if (flags < 0)
16605 : : break;
16606 : :
16607 : 110732 : if ((flags & cbf_hidden)
16608 : 3156 : && (flags & (cbf_using | cbf_export)))
16609 : 0 : sec.set_overrun ();
16610 : :
16611 : 110732 : tree decl = sec.tree_node ();
16612 : 110732 : if (sec.get_overrun ())
16613 : : break;
16614 : :
16615 : 110732 : if (!global_p)
16616 : : {
16617 : : /* Check if the decl could require GM merging. */
16618 : 2619 : tree orig = get_originating_module_decl (decl);
16619 : 2619 : tree inner = STRIP_TEMPLATE (orig);
16620 : 2619 : if (!DECL_LANG_SPECIFIC (inner)
16621 : 5238 : || !DECL_MODULE_ATTACH_P (inner))
16622 : : global_p = true;
16623 : : }
16624 : :
16625 : 110732 : if (decls && TREE_CODE (decl) == TYPE_DECL)
16626 : : {
16627 : : /* Stat hack. */
16628 : 45 : if (type || !DECL_IMPLICIT_TYPEDEF_P (decl))
16629 : 0 : sec.set_overrun ();
16630 : :
16631 : 45 : if (flags & cbf_using)
16632 : : {
16633 : 3 : type = build_lang_decl_loc (UNKNOWN_LOCATION,
16634 : : USING_DECL,
16635 : 3 : DECL_NAME (decl),
16636 : : NULL_TREE);
16637 : 3 : USING_DECL_DECLS (type) = decl;
16638 : 3 : USING_DECL_SCOPE (type) = CP_DECL_CONTEXT (decl);
16639 : 3 : DECL_CONTEXT (type) = ns;
16640 : :
16641 : 3 : DECL_MODULE_PURVIEW_P (type) = true;
16642 : 3 : if (flags & cbf_export)
16643 : 3 : DECL_MODULE_EXPORT_P (type) = true;
16644 : : }
16645 : : else
16646 : : type = decl;
16647 : : }
16648 : : else
16649 : : {
16650 : 110687 : if ((flags & cbf_using) &&
16651 : 9741 : !DECL_DECLARES_FUNCTION_P (decl))
16652 : : {
16653 : : /* We should only see a single non-function using-decl
16654 : : for a binding; more than that would clash. */
16655 : 3780 : if (decls)
16656 : 0 : sec.set_overrun ();
16657 : :
16658 : : /* FIXME: Propagate the location of the using-decl
16659 : : for use in diagnostics. */
16660 : 3780 : decls = build_lang_decl_loc (UNKNOWN_LOCATION,
16661 : : USING_DECL,
16662 : 3780 : DECL_NAME (decl),
16663 : : NULL_TREE);
16664 : 3780 : USING_DECL_DECLS (decls) = decl;
16665 : : /* We don't currently record the actual scope of the
16666 : : using-declaration, but this approximation should
16667 : : generally be good enough. */
16668 : 3780 : USING_DECL_SCOPE (decls) = CP_DECL_CONTEXT (decl);
16669 : 3780 : DECL_CONTEXT (decls) = ns;
16670 : :
16671 : 3780 : DECL_MODULE_PURVIEW_P (decls) = true;
16672 : 3780 : if (flags & cbf_export)
16673 : 3774 : DECL_MODULE_EXPORT_P (decls) = true;
16674 : : }
16675 : 106907 : else if (decls
16676 : 82342 : || (flags & (cbf_hidden | cbf_using))
16677 : 184436 : || DECL_FUNCTION_TEMPLATE_P (decl))
16678 : : {
16679 : 40865 : decls = ovl_make (decl, decls);
16680 : 40865 : if (flags & cbf_using)
16681 : : {
16682 : 5961 : dedup = true;
16683 : 5961 : OVL_USING_P (decls) = true;
16684 : 5961 : OVL_PURVIEW_P (decls) = true;
16685 : 5961 : if (flags & cbf_export)
16686 : 5946 : OVL_EXPORT_P (decls) = true;
16687 : : }
16688 : :
16689 : 40865 : if (flags & cbf_hidden)
16690 : 3156 : OVL_HIDDEN_P (decls) = true;
16691 : 37709 : else if (dedup)
16692 : 5961 : OVL_DEDUP_P (decls) = true;
16693 : : }
16694 : : else
16695 : : decls = decl;
16696 : :
16697 : 110681 : if (flags & cbf_export
16698 : 110687 : || (!(flags & cbf_hidden)
16699 : 714 : && (is_module () || is_partition ())))
16700 : : visible = decls;
16701 : : }
16702 : : }
16703 : :
16704 : 86122 : if (!decls)
16705 : 0 : sec.set_overrun ();
16706 : :
16707 : 86122 : if (sec.get_overrun ())
16708 : : break; /* Bail. */
16709 : :
16710 : 86930 : dump () && dump ("Binding of %P", ns, name);
16711 : 86122 : if (!set_module_binding (ns, name, mod, global_p,
16712 : 86122 : is_module () || is_partition (),
16713 : : decls, type, visible))
16714 : 0 : sec.set_overrun ();
16715 : : }
16716 : : break;
16717 : :
16718 : 620584 : case ct_decl:
16719 : : /* A decl. */
16720 : 620584 : {
16721 : 620584 : tree decl = sec.tree_node ();
16722 : 1727435 : dump () && dump ("Read declaration of %N", decl);
16723 : : }
16724 : : break;
16725 : :
16726 : 233421 : case ct_defn:
16727 : 233421 : {
16728 : 233421 : tree decl = sec.tree_node ();
16729 : 234469 : dump () && dump ("Reading definition of %N", decl);
16730 : 233421 : sec.read_definition (decl);
16731 : : }
16732 : 233421 : break;
16733 : : }
16734 : : }
16735 : :
16736 : : /* When lazy loading is in effect, we can be in the middle of
16737 : : parsing or instantiating a function. Save it away.
16738 : : push_function_context does too much work. */
16739 : 163869 : tree old_cfd = current_function_decl;
16740 : 163869 : struct function *old_cfun = cfun;
16741 : 263525 : for (const post_process_data& pdata : sec.post_process ())
16742 : : {
16743 : 70804 : tree decl = pdata.decl;
16744 : :
16745 : 70804 : bool abstract = false;
16746 : 70804 : if (TREE_CODE (decl) == TEMPLATE_DECL)
16747 : : {
16748 : 39268 : abstract = true;
16749 : 39268 : decl = DECL_TEMPLATE_RESULT (decl);
16750 : : }
16751 : :
16752 : 70804 : current_function_decl = decl;
16753 : 70804 : allocate_struct_function (decl, abstract);
16754 : 70804 : cfun->language = ggc_cleared_alloc<language_function> ();
16755 : 70804 : cfun->language->base.x_stmt_tree.stmts_are_full_exprs_p = 1;
16756 : 70804 : cfun->function_start_locus = pdata.start_locus;
16757 : 70804 : cfun->function_end_locus = pdata.end_locus;
16758 : 70804 : cfun->language->returns_value = pdata.returns_value;
16759 : 70804 : cfun->language->returns_null = pdata.returns_null;
16760 : 70804 : cfun->language->returns_abnormally = pdata.returns_abnormally;
16761 : 70804 : cfun->language->infinite_loop = pdata.infinite_loop;
16762 : :
16763 : : /* Make sure we emit explicit instantiations.
16764 : : FIXME do we want to do this in expand_or_defer_fn instead? */
16765 : 70804 : if (DECL_EXPLICIT_INSTANTIATION (decl)
16766 : 70804 : && !DECL_EXTERNAL (decl))
16767 : 18 : setup_explicit_instantiation_definition_linkage (decl);
16768 : :
16769 : 70804 : if (abstract)
16770 : : ;
16771 : 31536 : else if (DECL_MAYBE_IN_CHARGE_CDTOR_P (decl))
16772 : 5392 : vec_safe_push (post_load_decls, decl);
16773 : : else
16774 : : {
16775 : 26144 : bool aggr = aggregate_value_p (DECL_RESULT (decl), decl);
16776 : : #ifdef PCC_STATIC_STRUCT_RETURN
16777 : : cfun->returns_pcc_struct = aggr;
16778 : : #endif
16779 : 26144 : cfun->returns_struct = aggr;
16780 : 26144 : expand_or_defer_fn (decl);
16781 : :
16782 : : /* If we first see this function after at_eof, it doesn't get
16783 : : note_vague_linkage_fn from tentative_decl_linkage, so the loop in
16784 : : c_parse_final_cleanups won't consider it. But with DECL_COMDAT we
16785 : : can just clear DECL_EXTERNAL and let cgraph decide.
16786 : : FIXME handle this outside module.cc after GCC 15. */
16787 : 2794 : if (at_eof && DECL_COMDAT (decl) && DECL_EXTERNAL (decl)
16788 : 26856 : && DECL_NOT_REALLY_EXTERN (decl))
16789 : 680 : DECL_EXTERNAL (decl) = false;
16790 : : }
16791 : :
16792 : : }
16793 : : /* Look, function.cc's interface to cfun does too much for us, we
16794 : : just need to restore the old value. I do not want to go
16795 : : redesigning that API right now. */
16796 : : #undef cfun
16797 : 163869 : cfun = old_cfun;
16798 : 163869 : current_function_decl = old_cfd;
16799 : 163869 : comparing_dependent_aliases--;
16800 : :
16801 : 163869 : dump.outdent ();
16802 : 164979 : dump () && dump ("Read section:%u", snum);
16803 : :
16804 : 163869 : loaded_clusters++;
16805 : :
16806 : 163869 : if (!sec.end (from ()))
16807 : : return false;
16808 : :
16809 : : return true;
16810 : 163869 : }
16811 : :
16812 : : void
16813 : 117933 : module_state::write_namespace (bytes_out &sec, depset *dep)
16814 : : {
16815 : 117933 : unsigned ns_num = dep->cluster;
16816 : 117933 : unsigned ns_import = 0;
16817 : :
16818 : 117933 : if (dep->is_import ())
16819 : 0 : ns_import = dep->section;
16820 : 117933 : else if (dep->get_entity () != global_namespace)
16821 : 73649 : ns_num++;
16822 : :
16823 : 117933 : sec.u (ns_import);
16824 : 117933 : sec.u (ns_num);
16825 : 117933 : }
16826 : :
16827 : : tree
16828 : 142110 : module_state::read_namespace (bytes_in &sec)
16829 : : {
16830 : 142110 : unsigned ns_import = sec.u ();
16831 : 142110 : unsigned ns_num = sec.u ();
16832 : 142110 : tree ns = NULL_TREE;
16833 : :
16834 : 142110 : if (ns_import || ns_num)
16835 : : {
16836 : 87699 : if (!ns_import)
16837 : 87699 : ns_num--;
16838 : :
16839 : 87699 : if (unsigned origin = slurp->remap_module (ns_import))
16840 : : {
16841 : 87699 : module_state *from = (*modules)[origin];
16842 : 87699 : if (ns_num < from->entity_num)
16843 : : {
16844 : 87699 : binding_slot &slot = (*entity_ary)[from->entity_lwm + ns_num];
16845 : :
16846 : 87699 : if (!slot.is_lazy ())
16847 : 87699 : ns = slot;
16848 : : }
16849 : : }
16850 : : else
16851 : 0 : sec.set_overrun ();
16852 : : }
16853 : : else
16854 : 54411 : ns = global_namespace;
16855 : :
16856 : 142110 : return ns;
16857 : : }
16858 : :
16859 : : /* SPACES is a sorted vector of namespaces. Write out the namespaces
16860 : : to MOD_SNAME_PFX.nms section. */
16861 : :
16862 : : void
16863 : 470 : module_state::write_namespaces (elf_out *to, vec<depset *> spaces,
16864 : : unsigned num, unsigned *crc_p)
16865 : : {
16866 : 506 : dump () && dump ("Writing namespaces");
16867 : 470 : dump.indent ();
16868 : :
16869 : 470 : bytes_out sec (to);
16870 : 470 : sec.begin ();
16871 : :
16872 : 470 : hash_map<tree, unsigned> ns_map;
16873 : :
16874 : 2393 : for (unsigned ix = 0; ix != num; ix++)
16875 : : {
16876 : 1923 : depset *b = spaces[ix];
16877 : 1923 : tree ns = b->get_entity ();
16878 : :
16879 : 1923 : ns_map.put (ns, ix);
16880 : :
16881 : : /* This could be an anonymous namespace even for a named module,
16882 : : since we can still emit no-linkage decls. */
16883 : 1923 : gcc_checking_assert (TREE_CODE (ns) == NAMESPACE_DECL);
16884 : :
16885 : 1923 : unsigned flags = 0;
16886 : 1923 : if (TREE_PUBLIC (ns))
16887 : 1877 : flags |= 1;
16888 : 1923 : if (DECL_NAMESPACE_INLINE_P (ns))
16889 : 352 : flags |= 2;
16890 : 1923 : if (DECL_MODULE_PURVIEW_P (ns))
16891 : 1593 : flags |= 4;
16892 : 1923 : if (DECL_MODULE_EXPORT_P (ns))
16893 : 1445 : flags |= 8;
16894 : :
16895 : 1989 : dump () && dump ("Writing namespace:%u %N%s%s%s%s",
16896 : : b->cluster, ns,
16897 : 66 : flags & 1 ? ", public" : "",
16898 : 66 : flags & 2 ? ", inline" : "",
16899 : 66 : flags & 4 ? ", purview" : "",
16900 : 66 : flags & 8 ? ", export" : "");
16901 : 1923 : sec.u (b->cluster);
16902 : 1923 : sec.u (to->name (DECL_NAME (ns)));
16903 : 1923 : write_namespace (sec, b->deps[0]);
16904 : :
16905 : 1923 : sec.u (flags);
16906 : 1923 : write_location (sec, DECL_SOURCE_LOCATION (ns));
16907 : :
16908 : 1923 : if (DECL_NAMESPACE_INLINE_P (ns))
16909 : : {
16910 : 352 : if (tree attr = lookup_attribute ("abi_tag", DECL_ATTRIBUTES (ns)))
16911 : : {
16912 : 108 : tree tags = TREE_VALUE (attr);
16913 : 108 : sec.u (list_length (tags));
16914 : 219 : for (tree tag = tags; tag; tag = TREE_CHAIN (tag))
16915 : 111 : sec.str (TREE_STRING_POINTER (TREE_VALUE (tag)));
16916 : : }
16917 : : else
16918 : 244 : sec.u (0);
16919 : : }
16920 : : }
16921 : :
16922 : : /* Now write exported using-directives, as a sequence of 1-origin indices in
16923 : : the spaces array (not entity indices): First the using namespace, then the
16924 : : used namespaces. And then a zero terminating the list. :: is
16925 : : represented as index -1. */
16926 : 2863 : auto emit_one_ns = [&](unsigned ix, tree ns) {
16927 : 2741 : for (auto udir: NAMESPACE_LEVEL (ns)->using_directives)
16928 : : {
16929 : 116 : if (TREE_CODE (udir) != USING_DECL || !DECL_MODULE_EXPORT_P (udir))
16930 : 40 : continue;
16931 : 76 : tree ns2 = USING_DECL_DECLS (udir);
16932 : 76 : dump() && dump ("Writing using-directive in %N for %N",
16933 : : ns, ns2);
16934 : 76 : sec.u (ix);
16935 : 76 : sec.u (*ns_map.get (ns2) + 1);
16936 : : }
16937 : 2863 : };
16938 : 470 : emit_one_ns (-1, global_namespace);
16939 : 2393 : for (unsigned ix = 0; ix != num; ix++)
16940 : : {
16941 : 1923 : depset *b = spaces[ix];
16942 : 1923 : tree ns = b->get_entity ();
16943 : 1923 : emit_one_ns (ix + 1, ns);
16944 : : }
16945 : 470 : sec.u (0);
16946 : :
16947 : 470 : sec.end (to, to->name (MOD_SNAME_PFX ".nms"), crc_p);
16948 : 470 : dump.outdent ();
16949 : 470 : }
16950 : :
16951 : : /* Read the namespace hierarchy from MOD_SNAME_PFX.namespace. Fill in
16952 : : SPACES from that data. */
16953 : :
16954 : : bool
16955 : 484 : module_state::read_namespaces (unsigned num)
16956 : : {
16957 : 484 : bytes_in sec;
16958 : :
16959 : 484 : if (!sec.begin (loc, from (), MOD_SNAME_PFX ".nms"))
16960 : : return false;
16961 : :
16962 : 538 : dump () && dump ("Reading namespaces");
16963 : 484 : dump.indent ();
16964 : :
16965 : 484 : tree *ns_map = XALLOCAVEC (tree, num);
16966 : :
16967 : 2673 : for (unsigned ix = 0; ix != num; ix++)
16968 : : {
16969 : 2189 : unsigned entity_index = sec.u ();
16970 : 2189 : unsigned name = sec.u ();
16971 : :
16972 : 2189 : tree parent = read_namespace (sec);
16973 : :
16974 : : /* See comment in write_namespace about why not bits. */
16975 : 2189 : unsigned flags = sec.u ();
16976 : 2189 : location_t src_loc = read_location (sec);
16977 : 2189 : unsigned tags_count = (flags & 2) ? sec.u () : 0;
16978 : :
16979 : 2189 : if (entity_index >= entity_num
16980 : 2189 : || !parent
16981 : 2189 : || (flags & 0xc) == 0x8)
16982 : 0 : sec.set_overrun ();
16983 : :
16984 : : tree tags = NULL_TREE;
16985 : 2308 : while (tags_count--)
16986 : : {
16987 : 119 : size_t len;
16988 : 119 : const char *str = sec.str (&len);
16989 : 119 : tags = tree_cons (NULL_TREE, build_string (len + 1, str), tags);
16990 : 119 : tags = nreverse (tags);
16991 : : }
16992 : :
16993 : 2189 : if (sec.get_overrun ())
16994 : : break;
16995 : :
16996 : 4332 : tree id = name ? get_identifier (from ()->name (name)) : NULL_TREE;
16997 : :
16998 : 2321 : dump () && dump ("Read namespace:%u %P%s%s%s%s",
16999 : : entity_index, parent, id,
17000 : 66 : flags & 1 ? ", public" : "",
17001 : : flags & 2 ? ", inline" : "",
17002 : 66 : flags & 4 ? ", purview" : "",
17003 : 66 : flags & 8 ? ", export" : "");
17004 : 2189 : bool visible_p = ((flags & 8)
17005 : 2189 : || ((flags & 1)
17006 : 458 : && (flags & 4)
17007 : 44 : && (is_partition () || is_module ())));
17008 : 2189 : tree inner = add_imported_namespace (parent, id, src_loc, mod,
17009 : : bool (flags & 2), visible_p);
17010 : 2189 : if (!inner)
17011 : : {
17012 : 0 : sec.set_overrun ();
17013 : 0 : break;
17014 : : }
17015 : :
17016 : 2189 : if (is_partition ())
17017 : : {
17018 : 30 : if (flags & 4)
17019 : 24 : DECL_MODULE_PURVIEW_P (inner) = true;
17020 : 30 : if (flags & 8)
17021 : 24 : DECL_MODULE_EXPORT_P (inner) = true;
17022 : : }
17023 : :
17024 : 2189 : if (tags)
17025 : 116 : DECL_ATTRIBUTES (inner)
17026 : 232 : = tree_cons (get_identifier ("abi_tag"), tags, DECL_ATTRIBUTES (inner));
17027 : :
17028 : 2189 : ns_map[ix] = inner;
17029 : :
17030 : : /* Install the namespace. */
17031 : 2189 : (*entity_ary)[entity_lwm + entity_index] = inner;
17032 : 2189 : if (DECL_MODULE_IMPORT_P (inner))
17033 : : {
17034 : 0 : bool existed;
17035 : 0 : unsigned *slot = &entity_map->get_or_insert
17036 : 0 : (DECL_UID (inner), &existed);
17037 : 0 : if (existed)
17038 : : /* If it existed, it should match. */
17039 : 0 : gcc_checking_assert (inner == (*entity_ary)[*slot]);
17040 : : else
17041 : 0 : *slot = entity_lwm + entity_index;
17042 : : }
17043 : : }
17044 : :
17045 : : /* Read the exported using-directives. */
17046 : 575 : while (unsigned ix = sec.u ())
17047 : : {
17048 : 91 : tree ns;
17049 : 91 : if (ix == (unsigned)-1)
17050 : 39 : ns = global_namespace;
17051 : : else
17052 : : {
17053 : 52 : if (--ix >= num)
17054 : : {
17055 : 0 : sec.set_overrun ();
17056 : 0 : break;
17057 : : }
17058 : 52 : ns = ns_map [ix];
17059 : : }
17060 : 91 : unsigned ix2 = sec.u ();
17061 : 91 : if (--ix2 >= num)
17062 : : {
17063 : 0 : sec.set_overrun ();
17064 : 0 : break;
17065 : : }
17066 : 91 : tree ns2 = ns_map [ix2];
17067 : 91 : if (directness)
17068 : : {
17069 : 79 : dump() && dump ("Reading using-directive in %N for %N",
17070 : : ns, ns2);
17071 : : /* In an export import this will mark the using-directive as
17072 : : exported, so it will be emitted again. */
17073 : 79 : add_using_namespace (ns, ns2);
17074 : : }
17075 : : else
17076 : : /* Ignore using-directives from indirect imports, we only want them
17077 : : from our own imports. */
17078 : 587 : dump() && dump ("Ignoring using-directive in %N for %N",
17079 : : ns, ns2);
17080 : : }
17081 : :
17082 : 484 : dump.outdent ();
17083 : 484 : if (!sec.end (from ()))
17084 : : return false;
17085 : : return true;
17086 : 484 : }
17087 : :
17088 : : /* Write the binding TABLE to MOD_SNAME_PFX.bnd */
17089 : :
17090 : : unsigned
17091 : 2381 : module_state::write_bindings (elf_out *to, vec<depset *> sccs, unsigned *crc_p)
17092 : : {
17093 : 2632 : dump () && dump ("Writing binding table");
17094 : 2381 : dump.indent ();
17095 : :
17096 : 2381 : unsigned num = 0;
17097 : 2381 : bytes_out sec (to);
17098 : 2381 : sec.begin ();
17099 : :
17100 : 1800552 : for (unsigned ix = 0; ix != sccs.length (); ix++)
17101 : : {
17102 : 897895 : depset *b = sccs[ix];
17103 : 897895 : if (b->is_binding ())
17104 : : {
17105 : 116010 : tree ns = b->get_entity ();
17106 : 117030 : dump () && dump ("Bindings %P section:%u", ns, b->get_name (),
17107 : : b->section);
17108 : 116010 : sec.u (to->name (b->get_name ()));
17109 : 116010 : write_namespace (sec, b->deps[0]);
17110 : 116010 : sec.u (b->section);
17111 : 116010 : num++;
17112 : : }
17113 : : }
17114 : :
17115 : 2381 : sec.end (to, to->name (MOD_SNAME_PFX ".bnd"), crc_p);
17116 : 2381 : dump.outdent ();
17117 : :
17118 : 2381 : return num;
17119 : 2381 : }
17120 : :
17121 : : /* Read the binding table from MOD_SNAME_PFX.bind. */
17122 : :
17123 : : bool
17124 : 2548 : module_state::read_bindings (unsigned num, unsigned lwm, unsigned hwm)
17125 : : {
17126 : 2548 : bytes_in sec;
17127 : :
17128 : 2548 : if (!sec.begin (loc, from (), MOD_SNAME_PFX ".bnd"))
17129 : : return false;
17130 : :
17131 : 3019 : dump () && dump ("Reading binding table");
17132 : 2548 : dump.indent ();
17133 : 142469 : for (; !sec.get_overrun () && num--;)
17134 : : {
17135 : 139921 : const char *name = from ()->name (sec.u ());
17136 : 139921 : tree ns = read_namespace (sec);
17137 : 139921 : unsigned snum = sec.u ();
17138 : :
17139 : 139921 : if (!ns || !name || (snum - lwm) >= (hwm - lwm))
17140 : 0 : sec.set_overrun ();
17141 : 139921 : if (!sec.get_overrun ())
17142 : : {
17143 : 139921 : tree id = get_identifier (name);
17144 : 141338 : dump () && dump ("Bindings %P section:%u", ns, id, snum);
17145 : 139921 : if (mod && !import_module_binding (ns, id, mod, snum))
17146 : : break;
17147 : : }
17148 : : }
17149 : :
17150 : 2548 : dump.outdent ();
17151 : 2548 : if (!sec.end (from ()))
17152 : : return false;
17153 : : return true;
17154 : 2548 : }
17155 : :
17156 : : /* Write the entity table to MOD_SNAME_PFX.ent
17157 : :
17158 : : Each entry is a section number. */
17159 : :
17160 : : void
17161 : 2139 : module_state::write_entities (elf_out *to, vec<depset *> depsets,
17162 : : unsigned count, unsigned *crc_p)
17163 : : {
17164 : 2343 : dump () && dump ("Writing entities");
17165 : 2139 : dump.indent ();
17166 : :
17167 : 2139 : bytes_out sec (to);
17168 : 2139 : sec.begin ();
17169 : :
17170 : 2139 : unsigned current = 0;
17171 : 900019 : for (unsigned ix = 0; ix < depsets.length (); ix++)
17172 : : {
17173 : 897880 : depset *d = depsets[ix];
17174 : :
17175 : 1666315 : switch (d->get_entity_kind ())
17176 : : {
17177 : : default:
17178 : : break;
17179 : :
17180 : 4062 : case depset::EK_NAMESPACE:
17181 : 4062 : if (!d->is_import () && d->get_entity () != global_namespace)
17182 : : {
17183 : 1923 : gcc_checking_assert (d->cluster == current);
17184 : 1923 : current++;
17185 : 1923 : sec.u (0);
17186 : : }
17187 : : break;
17188 : :
17189 : 764373 : case depset::EK_DECL:
17190 : 764373 : case depset::EK_SPECIALIZATION:
17191 : 764373 : case depset::EK_PARTIAL:
17192 : 1528746 : gcc_checking_assert (!d->is_unreached ()
17193 : : && !d->is_import ()
17194 : : && d->cluster == current
17195 : : && d->section);
17196 : 764373 : current++;
17197 : 764373 : sec.u (d->section);
17198 : 764373 : break;
17199 : : }
17200 : : }
17201 : 2139 : gcc_assert (count == current);
17202 : 2139 : sec.end (to, to->name (MOD_SNAME_PFX ".ent"), crc_p);
17203 : 2139 : dump.outdent ();
17204 : 2139 : }
17205 : :
17206 : : bool
17207 : 2343 : module_state::read_entities (unsigned count, unsigned lwm, unsigned hwm)
17208 : : {
17209 : 2343 : trees_in sec (this);
17210 : :
17211 : 2343 : if (!sec.begin (loc, from (), MOD_SNAME_PFX ".ent"))
17212 : : return false;
17213 : :
17214 : 2763 : dump () && dump ("Reading entities");
17215 : 2343 : dump.indent ();
17216 : :
17217 : 924732 : for (binding_slot *slot = entity_ary->begin () + entity_lwm; count--; slot++)
17218 : : {
17219 : 922389 : unsigned snum = sec.u ();
17220 : 922389 : if (snum && (snum - lwm) >= (hwm - lwm))
17221 : 0 : sec.set_overrun ();
17222 : 922389 : if (sec.get_overrun ())
17223 : : break;
17224 : :
17225 : 922389 : if (snum)
17226 : 920200 : slot->set_lazy (snum << 2);
17227 : : }
17228 : :
17229 : 2343 : dump.outdent ();
17230 : 2343 : if (!sec.end (from ()))
17231 : : return false;
17232 : : return true;
17233 : 2343 : }
17234 : :
17235 : : /* Write the pending table to MOD_SNAME_PFX.pnd
17236 : :
17237 : : The pending table holds information about clusters that need to be
17238 : : loaded because they contain information about something that is not
17239 : : found by namespace-scope lookup.
17240 : :
17241 : : The three cases are:
17242 : :
17243 : : (a) Template (maybe-partial) specializations that we have
17244 : : instantiated or defined. When an importer needs to instantiate
17245 : : that template, they /must have/ the partial, explicit & extern
17246 : : specializations available. If they have the other specializations
17247 : : available, they'll have less work to do. Thus, when we're about to
17248 : : instantiate FOO, we have to be able to ask 'are there any
17249 : : specialization of FOO in our imports?'.
17250 : :
17251 : : (b) (Maybe-implicit) member functions definitions. A class could
17252 : : be defined in one header, and an inline member defined in a
17253 : : different header (this occurs in the STL). Similarly, like the
17254 : : specialization case, an implicit member function could have been
17255 : : 'instantiated' in one module, and it'd be nice to not have to
17256 : : reinstantiate it in another.
17257 : :
17258 : : (c) Classes completed elsewhere. A class could be declared in one
17259 : : header and defined in another. We need to know to load the class
17260 : : definition before looking in it. It does highlight an issue --
17261 : : there could be an intermediate import between the outermost containing
17262 : : namespace-scope class and the innermost being-defined class. This is
17263 : : actually possible with all of these cases, so be aware -- we're not
17264 : : just talking of one level of import to get to the innermost namespace.
17265 : :
17266 : : This gets complicated fast, it took me multiple attempts to even
17267 : : get something remotely working. Partially because I focussed on
17268 : : optimizing what I think turns out to be a smaller problem, given
17269 : : the known need to do the more general case *anyway*. I document
17270 : : the smaller problem, because it does appear to be the natural way
17271 : : to do it. It's trap!
17272 : :
17273 : : **** THE TRAP
17274 : :
17275 : : Let's refer to the primary template or the containing class as the
17276 : : KEY. And the specialization or member as the PENDING-ENTITY. (To
17277 : : avoid having to say those mouthfuls all the time.)
17278 : :
17279 : : In either case, we have an entity and we need some way of mapping
17280 : : that to a set of entities that need to be loaded before we can
17281 : : proceed with whatever processing of the entity we were going to do.
17282 : :
17283 : : We need to link the key to the pending-entity in some way. Given a
17284 : : key, tell me the pending-entities I need to have loaded. However
17285 : : we tie the key to the pending-entity must not rely on the key being
17286 : : loaded -- that'd defeat the lazy loading scheme.
17287 : :
17288 : : As the key will be an import in we know its entity number (either
17289 : : because we imported it, or we're writing it out too). Thus we can
17290 : : generate a map of key-indices to pending-entities. The
17291 : : pending-entity indices will be into our span of the entity table,
17292 : : and thus allow them to be lazily loaded. The key index will be
17293 : : into another slot of the entity table. Notice that this checking
17294 : : could be expensive, we don't want to iterate over a bunch of
17295 : : pending-entity indices (across multiple imports), every time we're
17296 : : about do to the thing with the key. We need to quickly determine
17297 : : 'definitely nothing needed'.
17298 : :
17299 : : That's almost good enough, except that key indices are not unique
17300 : : in a couple of cases :( Specifically the Global Module or a module
17301 : : partition can result in multiple modules assigning an entity index
17302 : : for the key. The decl-merging on loading will detect that so we
17303 : : only have one Key loaded, and in the entity hash it'll indicate the
17304 : : entity index of first load. Which might be different to how we
17305 : : know it. Notice this is restricted to GM entities or this-module
17306 : : entities. Foreign imports cannot have this.
17307 : :
17308 : : We can simply resolve this in the direction of how this module
17309 : : referred to the key to how the importer knows it. Look in the
17310 : : entity table slot that we nominate, maybe lazy load it, and then
17311 : : lookup the resultant entity in the entity hash to learn how the
17312 : : importer knows it.
17313 : :
17314 : : But we need to go in the other direction :( Given the key, find all
17315 : : the index-aliases of that key. We can partially solve that by
17316 : : adding an alias hash table. Whenever we load a merged decl, add or
17317 : : augment a mapping from the entity (or its entity-index) to the
17318 : : newly-discovered index. Then when we look for pending entities of
17319 : : a key, we also iterate over this aliases this mapping provides.
17320 : :
17321 : : But that requires the alias to be loaded. And that's not
17322 : : necessarily true.
17323 : :
17324 : : *** THE SIMPLER WAY
17325 : :
17326 : : The remaining fixed thing we have is the innermost namespace
17327 : : containing the ultimate namespace-scope container of the key and
17328 : : the name of that container (which might be the key itself). I.e. a
17329 : : namespace-decl/identifier/module tuple. Let's call this the
17330 : : top-key. We'll discover that the module is not important here,
17331 : : because of cross-module possibilities mentioned in case #c above.
17332 : : We can't markup namespace-binding slots. The best we can do is
17333 : : mark the binding vector with 'there's something here', and have
17334 : : another map from namespace/identifier pairs to a vector of pending
17335 : : entity indices.
17336 : :
17337 : : Maintain a pending-entity map. This is keyed by top-key, and
17338 : : maps to a vector of pending-entity indices. On the binding vector
17339 : : have flags saying whether the pending-name-entity map has contents.
17340 : : (We might want to further extend the key to be GM-vs-Partition and
17341 : : specialization-vs-member, but let's not get ahead of ourselves.)
17342 : :
17343 : : For every key-like entity, find the outermost namespace-scope
17344 : : name. Use that to lookup in the pending-entity map and then make
17345 : : sure the specified entities are loaded.
17346 : :
17347 : : An optimization might be to have a flag in each key-entity saying
17348 : : that its top key might be in the entity table. It's not clear to
17349 : : me how to set that flag cheaply -- cheaper than just looking.
17350 : :
17351 : : FIXME: It'd be nice to have a bit in decls to tell us whether to
17352 : : even try this. We can have a 'already done' flag, that we set when
17353 : : we've done KLASS's lazy pendings. When we import a module that
17354 : : registers pendings on the same top-key as KLASS we need to clear
17355 : : the flag. A recursive walk of the top-key clearing the bit will
17356 : : suffice. Plus we only need to recurse on classes that have the bit
17357 : : set. (That means we need to set the bit on parents of KLASS here,
17358 : : don't forget.) However, first: correctness, second: efficiency. */
17359 : :
17360 : : unsigned
17361 : 2381 : module_state::write_pendings (elf_out *to, vec<depset *> depsets,
17362 : : depset::hash &table, unsigned *crc_p)
17363 : : {
17364 : 2632 : dump () && dump ("Writing pending-entities");
17365 : 2381 : dump.indent ();
17366 : :
17367 : 2381 : trees_out sec (to, this, table);
17368 : 2381 : sec.begin ();
17369 : :
17370 : 2381 : unsigned count = 0;
17371 : 2381 : tree cache_ns = NULL_TREE;
17372 : 2381 : tree cache_id = NULL_TREE;
17373 : 2381 : unsigned cache_section = ~0;
17374 : 900276 : for (unsigned ix = 0; ix < depsets.length (); ix++)
17375 : : {
17376 : 897895 : depset *d = depsets[ix];
17377 : :
17378 : 897895 : if (d->is_binding ())
17379 : 547024 : continue;
17380 : :
17381 : 781885 : if (d->is_import ())
17382 : 0 : continue;
17383 : :
17384 : 781885 : if (!d->is_pending_entity ())
17385 : 431014 : continue;
17386 : :
17387 : 350871 : tree key_decl = nullptr;
17388 : 350871 : tree key_ns = find_pending_key (d->get_entity (), &key_decl);
17389 : 350871 : tree key_name = DECL_NAME (key_decl);
17390 : :
17391 : 350871 : if (IDENTIFIER_ANON_P (key_name))
17392 : : {
17393 : 6 : gcc_checking_assert (IDENTIFIER_LAMBDA_P (key_name));
17394 : 6 : if (tree attached = LAMBDA_TYPE_EXTRA_SCOPE (TREE_TYPE (key_decl)))
17395 : 6 : key_name = DECL_NAME (attached);
17396 : : else
17397 : : {
17398 : : /* There's nothing to attach it to. Must
17399 : : always reinstantiate. */
17400 : 0 : dump ()
17401 : 0 : && dump ("Unattached lambda %N[%u] section:%u",
17402 : 0 : d->get_entity_kind () == depset::EK_DECL
17403 : : ? "Member" : "Specialization", d->get_entity (),
17404 : : d->cluster, d->section);
17405 : 0 : continue;
17406 : : }
17407 : : }
17408 : :
17409 : 350871 : char const *also = "";
17410 : 350871 : if (d->section == cache_section
17411 : 221871 : && key_ns == cache_ns
17412 : 221871 : && key_name == cache_id)
17413 : : /* Same section & key as previous, no need to repeat ourselves. */
17414 : : also = "also ";
17415 : : else
17416 : : {
17417 : 167297 : cache_ns = key_ns;
17418 : 167297 : cache_id = key_name;
17419 : 167297 : cache_section = d->section;
17420 : 167297 : gcc_checking_assert (table.find_dependency (cache_ns));
17421 : 167297 : sec.tree_node (cache_ns);
17422 : 167297 : sec.tree_node (cache_id);
17423 : 167297 : sec.u (d->cluster);
17424 : 167297 : count++;
17425 : : }
17426 : 351933 : dump () && dump ("Pending %s %N entity:%u section:%u %skeyed to %P",
17427 : 531 : d->get_entity_kind () == depset::EK_DECL
17428 : : ? "member" : "specialization", d->get_entity (),
17429 : : d->cluster, cache_section, also, cache_ns, cache_id);
17430 : : }
17431 : 2381 : sec.end (to, to->name (MOD_SNAME_PFX ".pnd"), crc_p);
17432 : 2381 : dump.outdent ();
17433 : :
17434 : 2381 : return count;
17435 : 2381 : }
17436 : :
17437 : : bool
17438 : 1343 : module_state::read_pendings (unsigned count)
17439 : : {
17440 : 1343 : trees_in sec (this);
17441 : :
17442 : 1343 : if (!sec.begin (loc, from (), MOD_SNAME_PFX ".pnd"))
17443 : : return false;
17444 : :
17445 : 1631 : dump () && dump ("Reading %u pendings", count);
17446 : 1343 : dump.indent ();
17447 : :
17448 : 200240 : for (unsigned ix = 0; ix != count; ix++)
17449 : : {
17450 : 198897 : pending_key key;
17451 : 198897 : unsigned index;
17452 : :
17453 : 198897 : key.ns = sec.tree_node ();
17454 : 198897 : key.id = sec.tree_node ();
17455 : 198897 : index = sec.u ();
17456 : :
17457 : 198897 : if (!key.ns || !key.id
17458 : 198897 : || !(TREE_CODE (key.ns) == NAMESPACE_DECL
17459 : 198897 : && !DECL_NAMESPACE_ALIAS (key.ns))
17460 : 198897 : || !identifier_p (key.id)
17461 : 397794 : || index >= entity_num)
17462 : 0 : sec.set_overrun ();
17463 : :
17464 : 198897 : if (sec.get_overrun ())
17465 : : break;
17466 : :
17467 : 199602 : dump () && dump ("Pending:%u keyed to %P", index, key.ns, key.id);
17468 : :
17469 : 198897 : index += entity_lwm;
17470 : 198897 : auto &vec = pending_table->get_or_insert (key);
17471 : 198897 : vec.safe_push (index);
17472 : : }
17473 : :
17474 : 1343 : dump.outdent ();
17475 : 1343 : if (!sec.end (from ()))
17476 : : return false;
17477 : : return true;
17478 : 1343 : }
17479 : :
17480 : : /* Read & write locations. */
17481 : : enum loc_kind {
17482 : : LK_ORDINARY,
17483 : : LK_MACRO,
17484 : : LK_IMPORT_ORDINARY,
17485 : : LK_IMPORT_MACRO,
17486 : : LK_ADHOC,
17487 : : LK_RESERVED,
17488 : : };
17489 : :
17490 : : static const module_state *
17491 : 3734 : module_for_ordinary_loc (location_t loc)
17492 : : {
17493 : 3734 : unsigned pos = 0;
17494 : 7468 : unsigned len = ool->length () - pos;
17495 : :
17496 : 3737 : while (len)
17497 : : {
17498 : 3737 : unsigned half = len / 2;
17499 : 3737 : module_state *probe = (*ool)[pos + half];
17500 : 3737 : if (loc < probe->ordinary_locs.first)
17501 : : len = half;
17502 : 3734 : else if (loc < probe->ordinary_locs.first + probe->ordinary_locs.second)
17503 : : return probe;
17504 : : else
17505 : : {
17506 : 0 : pos += half + 1;
17507 : 0 : len = len - (half + 1);
17508 : : }
17509 : : }
17510 : :
17511 : : return nullptr;
17512 : : }
17513 : :
17514 : : static const module_state *
17515 : 6 : module_for_macro_loc (location_t loc)
17516 : : {
17517 : 6 : unsigned pos = 1;
17518 : 6 : unsigned len = modules->length () - pos;
17519 : :
17520 : 6 : while (len)
17521 : : {
17522 : 6 : unsigned half = len / 2;
17523 : 6 : module_state *probe = (*modules)[pos + half];
17524 : 6 : if (loc < probe->macro_locs.first)
17525 : : {
17526 : 0 : pos += half + 1;
17527 : 0 : len = len - (half + 1);
17528 : : }
17529 : 6 : else if (loc >= probe->macro_locs.first + probe->macro_locs.second)
17530 : : len = half;
17531 : : else
17532 : : return probe;
17533 : : }
17534 : :
17535 : : return NULL;
17536 : : }
17537 : :
17538 : : location_t
17539 : 1095 : module_state::imported_from () const
17540 : : {
17541 : 1095 : location_t from = loc;
17542 : 1095 : line_map_ordinary const *fmap
17543 : 1095 : = linemap_check_ordinary (linemap_lookup (line_table, from));
17544 : :
17545 : 1095 : if (MAP_MODULE_P (fmap))
17546 : 1095 : from = linemap_included_from (fmap);
17547 : :
17548 : 1095 : return from;
17549 : : }
17550 : :
17551 : : /* Note that LOC will need writing. This allows us to prune locations
17552 : : that are not needed. */
17553 : :
17554 : : bool
17555 : 16204966 : module_state::note_location (location_t loc)
17556 : : {
17557 : 16204966 : bool added = false;
17558 : 16204966 : if (!macro_loc_table && !ord_loc_table)
17559 : : ;
17560 : 16204966 : else if (loc < RESERVED_LOCATION_COUNT)
17561 : : ;
17562 : 14747648 : else if (IS_ADHOC_LOC (loc))
17563 : : {
17564 : 1803395 : location_t locus = get_location_from_adhoc_loc (line_table, loc);
17565 : 1803395 : note_location (locus);
17566 : 1803395 : source_range range = get_range_from_loc (line_table, loc);
17567 : 1803395 : if (range.m_start != locus)
17568 : 1743705 : note_location (range.m_start);
17569 : 1803395 : note_location (range.m_finish);
17570 : : }
17571 : 12944253 : else if (loc >= LINEMAPS_MACRO_LOWEST_LOCATION (line_table))
17572 : : {
17573 : 967874 : if (spans.macro (loc))
17574 : : {
17575 : 967868 : const line_map *map = linemap_lookup (line_table, loc);
17576 : 967868 : const line_map_macro *mac_map = linemap_check_macro (map);
17577 : 967868 : hashval_t hv = macro_loc_traits::hash (mac_map);
17578 : 967868 : macro_loc_info *slot
17579 : 967868 : = macro_loc_table->find_slot_with_hash (mac_map, hv, INSERT);
17580 : 967868 : if (!slot->src)
17581 : : {
17582 : 113509 : slot->src = mac_map;
17583 : 113509 : slot->remap = 0;
17584 : : // Expansion locations could themselves be from a
17585 : : // macro, we need to note them all.
17586 : 113509 : note_location (mac_map->m_expansion);
17587 : 113509 : gcc_checking_assert (mac_map->n_tokens);
17588 : 113509 : location_t tloc = UNKNOWN_LOCATION;
17589 : 3961739 : for (unsigned ix = mac_map->n_tokens * 2; ix--;)
17590 : 3848230 : if (mac_map->macro_locations[ix] != tloc)
17591 : : {
17592 : 2030547 : tloc = mac_map->macro_locations[ix];
17593 : 2030547 : note_location (tloc);
17594 : : }
17595 : : added = true;
17596 : : }
17597 : : }
17598 : : }
17599 : 11976379 : else if (IS_ORDINARY_LOC (loc))
17600 : : {
17601 : 11976379 : if (spans.ordinary (loc))
17602 : : {
17603 : 11972642 : const line_map *map = linemap_lookup (line_table, loc);
17604 : 11972642 : const line_map_ordinary *ord_map = linemap_check_ordinary (map);
17605 : 11972642 : ord_loc_info lkup;
17606 : 11972642 : lkup.src = ord_map;
17607 : 11972642 : lkup.span = loc_one << ord_map->m_column_and_range_bits;
17608 : 11972642 : lkup.offset = (loc - MAP_START_LOCATION (ord_map)) & ~(lkup.span - 1);
17609 : 11972642 : lkup.remap = 0;
17610 : 11972642 : ord_loc_info *slot = (ord_loc_table->find_slot_with_hash
17611 : 11972642 : (lkup, ord_loc_traits::hash (lkup), INSERT));
17612 : 11972642 : if (!slot->src)
17613 : : {
17614 : 1454783 : *slot = lkup;
17615 : 1454783 : added = true;
17616 : : }
17617 : : }
17618 : : }
17619 : : else
17620 : 0 : gcc_unreachable ();
17621 : 16204966 : return added;
17622 : : }
17623 : :
17624 : : /* If we're not streaming, record that we need location LOC.
17625 : : Otherwise stream it. */
17626 : :
17627 : : void
17628 : 24110433 : module_state::write_location (bytes_out &sec, location_t loc)
17629 : : {
17630 : 24110433 : if (!sec.streaming_p ())
17631 : : {
17632 : 8467022 : note_location (loc);
17633 : 8467022 : return;
17634 : : }
17635 : :
17636 : 15643411 : if (loc < RESERVED_LOCATION_COUNT)
17637 : : {
17638 : 1495834 : dump (dumper::LOCATION) && dump ("Reserved location %K", loc);
17639 : 1495816 : sec.loc (LK_RESERVED + loc);
17640 : : }
17641 : 14147595 : else if (IS_ADHOC_LOC (loc))
17642 : : {
17643 : 1762363 : dump (dumper::LOCATION) && dump ("Adhoc location");
17644 : 1762360 : sec.u (LK_ADHOC);
17645 : 1762360 : location_t locus = get_location_from_adhoc_loc (line_table, loc);
17646 : 1762360 : write_location (sec, locus);
17647 : 1762360 : source_range range = get_range_from_loc (line_table, loc);
17648 : 1762360 : if (range.m_start == locus)
17649 : : /* Compress. */
17650 : 56290 : range.m_start = UNKNOWN_LOCATION;
17651 : 1762360 : write_location (sec, range.m_start);
17652 : 1762360 : write_location (sec, range.m_finish);
17653 : 1762360 : unsigned discriminator = get_discriminator_from_adhoc_loc (line_table, loc);
17654 : 1762360 : sec.u (discriminator);
17655 : : }
17656 : 12385235 : else if (loc >= LINEMAPS_MACRO_LOWEST_LOCATION (line_table))
17657 : : {
17658 : 955916 : const macro_loc_info *info = nullptr;
17659 : 955916 : line_map_uint_t offset = 0;
17660 : 955916 : if (unsigned hwm = macro_loc_remap->length ())
17661 : : {
17662 : 955910 : info = macro_loc_remap->begin ();
17663 : 14069195 : while (hwm != 1)
17664 : : {
17665 : 12157375 : unsigned mid = hwm / 2;
17666 : 12157375 : if (MAP_START_LOCATION (info[mid].src) <= loc)
17667 : : {
17668 : 6203150 : info += mid;
17669 : 6203150 : hwm -= mid;
17670 : : }
17671 : : else
17672 : : hwm = mid;
17673 : : }
17674 : 955910 : offset = loc - MAP_START_LOCATION (info->src);
17675 : 955910 : if (offset > info->src->n_tokens)
17676 : 0 : info = nullptr;
17677 : : }
17678 : :
17679 : 955916 : gcc_checking_assert (bool (info) == bool (spans.macro (loc)));
17680 : :
17681 : 955916 : if (info)
17682 : : {
17683 : 955910 : offset += info->remap;
17684 : 955910 : sec.u (LK_MACRO);
17685 : 955910 : sec.loc (offset);
17686 : 955910 : dump (dumper::LOCATION)
17687 : 9 : && dump ("Macro location %K output %K", loc, offset);
17688 : : }
17689 : 6 : else if (const module_state *import = module_for_macro_loc (loc))
17690 : : {
17691 : 6 : auto off = loc - import->macro_locs.first;
17692 : 6 : sec.u (LK_IMPORT_MACRO);
17693 : 6 : sec.u (import->remap);
17694 : 6 : sec.loc (off);
17695 : 6 : dump (dumper::LOCATION)
17696 : 0 : && dump ("Imported macro location %K output %u:%K",
17697 : 0 : loc, import->remap, off);
17698 : : }
17699 : : else
17700 : 0 : gcc_unreachable ();
17701 : : }
17702 : 11429319 : else if (IS_ORDINARY_LOC (loc))
17703 : : {
17704 : : /* If we ran out of locations for imported decls, this location could
17705 : : be a module unit's location. In that case, remap the location
17706 : : to be where we imported the module from. */
17707 : 11429319 : if (spans.locations_exhausted_p () || CHECKING_P)
17708 : : {
17709 : 11429319 : const line_map_ordinary *map
17710 : 11429319 : = linemap_check_ordinary (linemap_lookup (line_table, loc));
17711 : 11429319 : if (MAP_MODULE_P (map) && loc == MAP_START_LOCATION (map))
17712 : : {
17713 : 0 : gcc_checking_assert (spans.locations_exhausted_p ());
17714 : 0 : write_location (sec, linemap_included_from (map));
17715 : 0 : return;
17716 : : }
17717 : : }
17718 : :
17719 : 11429319 : const ord_loc_info *info = nullptr;
17720 : 11429319 : line_map_uint_t offset = 0;
17721 : 11429319 : if (line_map_uint_t hwm = ord_loc_remap->length ())
17722 : : {
17723 : 11429319 : info = ord_loc_remap->begin ();
17724 : 165627335 : while (hwm != 1)
17725 : : {
17726 : 142768697 : auto mid = hwm / 2;
17727 : 142768697 : if (MAP_START_LOCATION (info[mid].src) + info[mid].offset <= loc)
17728 : : {
17729 : 75957087 : info += mid;
17730 : 75957087 : hwm -= mid;
17731 : : }
17732 : : else
17733 : : hwm = mid;
17734 : : }
17735 : 11429319 : offset = loc - MAP_START_LOCATION (info->src) - info->offset;
17736 : 11429319 : if (offset > info->span)
17737 : 3734 : info = nullptr;
17738 : : }
17739 : :
17740 : 11429319 : gcc_checking_assert (bool (info) == bool (spans.ordinary (loc)));
17741 : :
17742 : 11429319 : if (info)
17743 : : {
17744 : 11425585 : offset += info->remap;
17745 : 11425585 : sec.u (LK_ORDINARY);
17746 : 11425585 : sec.loc (offset);
17747 : :
17748 : 11425585 : dump (dumper::LOCATION)
17749 : 78 : && dump ("Ordinary location %K output %K", loc, offset);
17750 : : }
17751 : 3734 : else if (const module_state *import = module_for_ordinary_loc (loc))
17752 : : {
17753 : 3734 : auto off = loc - import->ordinary_locs.first;
17754 : 3734 : sec.u (LK_IMPORT_ORDINARY);
17755 : 3734 : sec.u (import->remap);
17756 : 3734 : sec.loc (off);
17757 : 3734 : dump (dumper::LOCATION)
17758 : 0 : && dump ("Imported ordinary location %K output %u:%K",
17759 : 0 : loc, import->remap, off);
17760 : : }
17761 : : else
17762 : 0 : gcc_unreachable ();
17763 : : }
17764 : : else
17765 : 0 : gcc_unreachable ();
17766 : : }
17767 : :
17768 : : location_t
17769 : 14214369 : module_state::read_location (bytes_in &sec) const
17770 : : {
17771 : 14214369 : location_t locus = UNKNOWN_LOCATION;
17772 : 14214369 : unsigned kind = sec.u ();
17773 : 14214369 : switch (kind)
17774 : : {
17775 : 1332088 : default:
17776 : 1332088 : {
17777 : 1332088 : if (kind < LK_RESERVED + RESERVED_LOCATION_COUNT)
17778 : 1332088 : locus = location_t (kind - LK_RESERVED);
17779 : : else
17780 : 0 : sec.set_overrun ();
17781 : 1332088 : dump (dumper::LOCATION)
17782 : 0 : && dump ("Reserved location %K", locus);
17783 : : }
17784 : : break;
17785 : :
17786 : 1534949 : case LK_ADHOC:
17787 : 1534949 : {
17788 : 1534949 : dump (dumper::LOCATION) && dump ("Adhoc location");
17789 : 1534949 : locus = read_location (sec);
17790 : 1534949 : source_range range;
17791 : 1534949 : range.m_start = read_location (sec);
17792 : 1534949 : if (range.m_start == UNKNOWN_LOCATION)
17793 : 45695 : range.m_start = locus;
17794 : 1534949 : range.m_finish = read_location (sec);
17795 : 1534949 : unsigned discriminator = sec.u ();
17796 : 1534949 : if (locus != loc && range.m_start != loc && range.m_finish != loc)
17797 : 1534949 : locus = line_table->get_or_create_combined_loc (locus, range,
17798 : : nullptr, discriminator);
17799 : : }
17800 : : break;
17801 : :
17802 : 1032324 : case LK_MACRO:
17803 : 1032324 : {
17804 : 1032324 : auto off = sec.loc ();
17805 : :
17806 : 1032324 : if (macro_locs.second)
17807 : : {
17808 : 1032324 : if (off < macro_locs.second)
17809 : 1032324 : locus = off + macro_locs.first;
17810 : : else
17811 : 0 : sec.set_overrun ();
17812 : : }
17813 : : else
17814 : 0 : locus = loc;
17815 : 1032324 : dump (dumper::LOCATION)
17816 : 0 : && dump ("Macro %K becoming %K", off, locus);
17817 : : }
17818 : : break;
17819 : :
17820 : 10313973 : case LK_ORDINARY:
17821 : 10313973 : {
17822 : 10313973 : auto off = sec.loc ();
17823 : 10313973 : if (ordinary_locs.second)
17824 : : {
17825 : 10313973 : if (off < ordinary_locs.second)
17826 : 10313973 : locus = off + ordinary_locs.first;
17827 : : else
17828 : 0 : sec.set_overrun ();
17829 : : }
17830 : : else
17831 : 0 : locus = loc;
17832 : :
17833 : 10313973 : dump (dumper::LOCATION)
17834 : 0 : && dump ("Ordinary location %K becoming %K", off, locus);
17835 : : }
17836 : : break;
17837 : :
17838 : 1035 : case LK_IMPORT_MACRO:
17839 : 1035 : case LK_IMPORT_ORDINARY:
17840 : 1035 : {
17841 : 1035 : unsigned mod = sec.u ();
17842 : 1035 : location_t off = sec.loc ();
17843 : 1035 : const module_state *import = NULL;
17844 : :
17845 : 1035 : if (!mod && !slurp->remap)
17846 : : /* This is an early read of a partition location during the
17847 : : read of our ordinary location map. */
17848 : : import = this;
17849 : : else
17850 : : {
17851 : 1035 : mod = slurp->remap_module (mod);
17852 : 1035 : if (!mod)
17853 : 0 : sec.set_overrun ();
17854 : : else
17855 : 1035 : import = (*modules)[mod];
17856 : : }
17857 : :
17858 : 1035 : if (import)
17859 : : {
17860 : 1035 : if (kind == LK_IMPORT_MACRO)
17861 : : {
17862 : 6 : if (!import->macro_locs.second)
17863 : 0 : locus = import->loc;
17864 : 6 : else if (off < import->macro_locs.second)
17865 : 6 : locus = off + import->macro_locs.first;
17866 : : else
17867 : 0 : sec.set_overrun ();
17868 : : }
17869 : : else
17870 : : {
17871 : 1029 : if (!import->ordinary_locs.second)
17872 : 0 : locus = import->loc;
17873 : 1029 : else if (off < import->ordinary_locs.second)
17874 : 1029 : locus = import->ordinary_locs.first + off;
17875 : : else
17876 : 0 : sec.set_overrun ();
17877 : : }
17878 : : }
17879 : : }
17880 : : break;
17881 : : }
17882 : :
17883 : 14214369 : return locus;
17884 : : }
17885 : :
17886 : : /* Allocate hash tables to record needed locations. */
17887 : :
17888 : : void
17889 : 2409 : module_state::write_init_maps ()
17890 : : {
17891 : 2409 : macro_loc_table = new hash_table<macro_loc_traits> (EXPERIMENT (1, 400));
17892 : 2409 : ord_loc_table = new hash_table<ord_loc_traits> (EXPERIMENT (1, 400));
17893 : 2409 : }
17894 : :
17895 : : /* Prepare the span adjustments. We prune unneeded locations -- at
17896 : : this point every needed location must have been seen by
17897 : : note_location. */
17898 : :
17899 : : range_t
17900 : 2381 : module_state::write_prepare_maps (module_state_config *cfg, bool has_partitions)
17901 : : {
17902 : 2632 : dump () && dump ("Preparing locations");
17903 : 2381 : dump.indent ();
17904 : :
17905 : 2632 : dump () && dump ("Reserved locations [%K,%K) macro [%K,%K)",
17906 : 251 : spans[loc_spans::SPAN_RESERVED].ordinary.first,
17907 : 251 : spans[loc_spans::SPAN_RESERVED].ordinary.second,
17908 : 251 : spans[loc_spans::SPAN_RESERVED].macro.first,
17909 : 251 : spans[loc_spans::SPAN_RESERVED].macro.second);
17910 : :
17911 : 2381 : range_t info {0, 0};
17912 : :
17913 : : // Sort the noted lines.
17914 : 2381 : vec_alloc (ord_loc_remap, ord_loc_table->size ());
17915 : 2381 : for (auto iter = ord_loc_table->begin (), end = ord_loc_table->end ();
17916 : 2890851 : iter != end; ++iter)
17917 : 1444235 : ord_loc_remap->quick_push (*iter);
17918 : 2381 : ord_loc_remap->qsort (&ord_loc_info::compare);
17919 : :
17920 : : // Note included-from maps.
17921 : 2381 : bool added = false;
17922 : 2381 : const line_map_ordinary *current = nullptr;
17923 : 1451378 : for (auto iter = ord_loc_remap->begin (), end = ord_loc_remap->end ();
17924 : 1446616 : iter != end; ++iter)
17925 : 1444235 : if (iter->src != current)
17926 : : {
17927 : 25993 : current = iter->src;
17928 : 10127 : for (auto probe = current;
17929 : 25993 : auto from = linemap_included_from (probe);
17930 : 10127 : probe = linemap_check_ordinary (linemap_lookup (line_table, from)))
17931 : : {
17932 : 23346 : if (has_partitions)
17933 : : {
17934 : : // Partition locations need to elide their module map
17935 : : // entry.
17936 : 180 : probe
17937 : 180 : = linemap_check_ordinary (linemap_lookup (line_table, from));
17938 : 180 : if (MAP_MODULE_P (probe))
17939 : 150 : from = linemap_included_from (probe);
17940 : : }
17941 : :
17942 : 23346 : if (!note_location (from))
17943 : : break;
17944 : 10127 : added = true;
17945 : 10127 : }
17946 : : }
17947 : 2381 : if (added)
17948 : : {
17949 : : // Reconstruct the line array as we added items to the hash table.
17950 : 447 : vec_free (ord_loc_remap);
17951 : 447 : vec_alloc (ord_loc_remap, ord_loc_table->size ());
17952 : 447 : for (auto iter = ord_loc_table->begin (), end = ord_loc_table->end ();
17953 : 2890173 : iter != end; ++iter)
17954 : 1444863 : ord_loc_remap->quick_push (*iter);
17955 : 447 : ord_loc_remap->qsort (&ord_loc_info::compare);
17956 : : }
17957 : 2381 : delete ord_loc_table;
17958 : 2381 : ord_loc_table = nullptr;
17959 : :
17960 : : // Merge (sufficiently) adjacent spans, and calculate remapping.
17961 : 2381 : constexpr line_map_uint_t adjacency = 2; // Allow 2 missing lines.
17962 : 4762 : auto begin = ord_loc_remap->begin (), end = ord_loc_remap->end ();
17963 : 2381 : auto dst = begin;
17964 : 2381 : line_map_uint_t offset = 0;
17965 : 2381 : unsigned range_bits = 0;
17966 : 2381 : ord_loc_info *base = nullptr;
17967 : 1456743 : for (auto iter = begin; iter != end; ++iter)
17968 : : {
17969 : 1454362 : if (base && iter->src == base->src)
17970 : : {
17971 : 2702604 : if (base->offset + base->span +
17972 : 1432418 : ((adjacency << base->src->m_column_and_range_bits)
17973 : : // If there are few c&r bits, allow further separation.
17974 : 1432418 : | (adjacency << 4))
17975 : 1432418 : >= iter->offset)
17976 : : {
17977 : : // Merge.
17978 : 1270186 : offset -= base->span;
17979 : 1270186 : base->span = iter->offset + iter->span - base->offset;
17980 : 1270186 : offset += base->span;
17981 : 1270186 : continue;
17982 : : }
17983 : : }
17984 : 21944 : else if (range_bits < iter->src->m_range_bits)
17985 : 2285 : range_bits = iter->src->m_range_bits;
17986 : :
17987 : 184176 : offset += ((loc_one << iter->src->m_range_bits) - 1);
17988 : 184176 : offset &= ~((loc_one << iter->src->m_range_bits) - 1);
17989 : 184176 : iter->remap = offset;
17990 : 184176 : offset += iter->span;
17991 : 184176 : base = dst;
17992 : 184176 : *dst++ = *iter;
17993 : : }
17994 : 2381 : ord_loc_remap->truncate (dst - begin);
17995 : :
17996 : 2381 : info.first = ord_loc_remap->length ();
17997 : 2381 : cfg->ordinary_locs = offset;
17998 : 2381 : cfg->loc_range_bits = range_bits;
17999 : 2632 : dump () && dump ("Ordinary maps:%K locs:%K range_bits:%u",
18000 : : info.first,
18001 : : cfg->ordinary_locs,
18002 : : cfg->loc_range_bits);
18003 : :
18004 : : // Remap the macro locations.
18005 : 2381 : vec_alloc (macro_loc_remap, macro_loc_table->size ());
18006 : 2381 : for (auto iter = macro_loc_table->begin (), end = macro_loc_table->end ();
18007 : 229399 : iter != end; ++iter)
18008 : 113509 : macro_loc_remap->quick_push (*iter);
18009 : 2381 : delete macro_loc_table;
18010 : 2381 : macro_loc_table = nullptr;
18011 : :
18012 : 2381 : macro_loc_remap->qsort (¯o_loc_info::compare);
18013 : 2381 : offset = 0;
18014 : 7143 : for (auto iter = macro_loc_remap->begin (), end = macro_loc_remap->end ();
18015 : 115890 : iter != end; ++iter)
18016 : : {
18017 : 113509 : auto mac = iter->src;
18018 : 113509 : iter->remap = offset;
18019 : 113509 : offset += mac->n_tokens;
18020 : : }
18021 : 2381 : info.second = macro_loc_remap->length ();
18022 : 2381 : cfg->macro_locs = offset;
18023 : :
18024 : 2632 : dump () && dump ("Macro maps:%K locs:%K", info.second, cfg->macro_locs);
18025 : :
18026 : 2381 : dump.outdent ();
18027 : :
18028 : : // If we have no ordinary locs, we must also have no macro locs.
18029 : 2381 : gcc_checking_assert (cfg->ordinary_locs || !cfg->macro_locs);
18030 : :
18031 : 2381 : return info;
18032 : : }
18033 : :
18034 : : bool
18035 : 2599 : module_state::read_prepare_maps (const module_state_config *cfg)
18036 : : {
18037 : 2599 : location_t ordinary = line_table->highest_location + 1;
18038 : 2599 : ordinary += cfg->ordinary_locs;
18039 : :
18040 : 2599 : location_t macro = LINEMAPS_MACRO_LOWEST_LOCATION (line_table);
18041 : 2599 : macro -= cfg->macro_locs;
18042 : :
18043 : 2599 : if (ordinary < LINE_MAP_MAX_LOCATION_WITH_COLS
18044 : 2599 : && macro >= LINE_MAP_MAX_LOCATION)
18045 : : /* OK, we have enough locations. */
18046 : : return true;
18047 : :
18048 : 0 : ordinary_locs.first = ordinary_locs.second = 0;
18049 : 0 : macro_locs.first = macro_locs.second = 0;
18050 : :
18051 : 0 : spans.report_location_exhaustion (loc);
18052 : :
18053 : : return false;
18054 : : }
18055 : :
18056 : : /* Write & read the location maps. Not called if there are no
18057 : : locations. */
18058 : :
18059 : : void
18060 : 2285 : module_state::write_ordinary_maps (elf_out *to, range_t &info,
18061 : : bool has_partitions, unsigned *crc_p)
18062 : : {
18063 : 2517 : dump () && dump ("Writing ordinary location maps");
18064 : 2285 : dump.indent ();
18065 : :
18066 : 2285 : vec<const char *> filenames;
18067 : 2285 : filenames.create (20);
18068 : :
18069 : : /* Determine the unique filenames. */
18070 : 2285 : const line_map_ordinary *current = nullptr;
18071 : 191031 : for (auto iter = ord_loc_remap->begin (), end = ord_loc_remap->end ();
18072 : 186461 : iter != end; ++iter)
18073 : 184176 : if (iter->src != current)
18074 : : {
18075 : 21944 : current = iter->src;
18076 : 21944 : const char *fname = ORDINARY_MAP_FILE_NAME (iter->src);
18077 : :
18078 : : /* We should never find a module linemap in an interval. */
18079 : 21944 : gcc_checking_assert (!MAP_MODULE_P (iter->src));
18080 : :
18081 : : /* We expect very few filenames, so just an array.
18082 : : (Not true when headers are still in play :() */
18083 : 1301819 : for (unsigned jx = filenames.length (); jx--;)
18084 : : {
18085 : 1268148 : const char *name = filenames[jx];
18086 : 1268148 : if (0 == strcmp (name, fname))
18087 : : {
18088 : : /* Reset the linemap's name, because for things like
18089 : : preprocessed input we could have multiple instances
18090 : : of the same name, and we'd rather not percolate
18091 : : that. */
18092 : 10217 : const_cast<line_map_ordinary *> (iter->src)->to_file = name;
18093 : 10217 : fname = NULL;
18094 : 10217 : break;
18095 : : }
18096 : : }
18097 : 21944 : if (fname)
18098 : 11727 : filenames.safe_push (fname);
18099 : : }
18100 : :
18101 : 2285 : bytes_out sec (to);
18102 : 2285 : sec.begin ();
18103 : :
18104 : : /* Write the filenames. */
18105 : 2285 : unsigned len = filenames.length ();
18106 : 2285 : sec.u (len);
18107 : 2517 : dump () && dump ("%u source file names", len);
18108 : 14012 : for (unsigned ix = 0; ix != len; ix++)
18109 : : {
18110 : 11727 : const char *fname = filenames[ix];
18111 : 11742 : dump (dumper::LOCATION) && dump ("Source file[%u]=%s", ix, fname);
18112 : 11727 : sec.str (fname);
18113 : : }
18114 : :
18115 : 2285 : sec.loc (info.first); /* Num maps. */
18116 : 2285 : const ord_loc_info *base = nullptr;
18117 : 191031 : for (auto iter = ord_loc_remap->begin (), end = ord_loc_remap->end ();
18118 : 186461 : iter != end; ++iter)
18119 : : {
18120 : 184176 : dump (dumper::LOCATION)
18121 : 36 : && dump ("Span:%K ordinary [%K+%K,+%K)->[%K,+%K)",
18122 : 36 : (location_t) (iter - ord_loc_remap->begin ()),
18123 : 18 : MAP_START_LOCATION (iter->src),
18124 : : iter->offset, iter->span, iter->remap,
18125 : : iter->span);
18126 : :
18127 : 184176 : if (!base || iter->src != base->src)
18128 : 21944 : base = iter;
18129 : 184176 : sec.loc (iter->offset - base->offset);
18130 : 184176 : if (base == iter)
18131 : : {
18132 : 21944 : sec.u (iter->src->sysp);
18133 : 21944 : sec.u (iter->src->m_range_bits);
18134 : 21944 : sec.u (iter->src->m_column_and_range_bits - iter->src->m_range_bits);
18135 : :
18136 : 21944 : const char *fname = ORDINARY_MAP_FILE_NAME (iter->src);
18137 : 4041208 : for (unsigned ix = 0; ix != filenames.length (); ix++)
18138 : 2020604 : if (filenames[ix] == fname)
18139 : : {
18140 : 21944 : sec.u (ix);
18141 : 21944 : break;
18142 : : }
18143 : 21944 : unsigned line = ORDINARY_MAP_STARTING_LINE_NUMBER (iter->src);
18144 : 21944 : line += iter->offset >> iter->src->m_column_and_range_bits;
18145 : 21944 : sec.u (line);
18146 : : }
18147 : 184176 : sec.loc (iter->remap);
18148 : 184176 : if (base == iter)
18149 : : {
18150 : : /* Write the included from location, which means reading it
18151 : : while reading in the ordinary maps. So we'd better not
18152 : : be getting ahead of ourselves. */
18153 : 21944 : location_t from = linemap_included_from (iter->src);
18154 : 21944 : gcc_checking_assert (from < MAP_START_LOCATION (iter->src));
18155 : 21944 : if (from != UNKNOWN_LOCATION && has_partitions)
18156 : : {
18157 : : /* A partition's span will have a from pointing at a
18158 : : MODULE_INC. Find that map's from. */
18159 : 174 : line_map_ordinary const *fmap
18160 : 174 : = linemap_check_ordinary (linemap_lookup (line_table, from));
18161 : 174 : if (MAP_MODULE_P (fmap))
18162 : 144 : from = linemap_included_from (fmap);
18163 : : }
18164 : 21944 : write_location (sec, from);
18165 : : }
18166 : : }
18167 : :
18168 : 2285 : filenames.release ();
18169 : :
18170 : 2285 : sec.end (to, to->name (MOD_SNAME_PFX ".olm"), crc_p);
18171 : 2285 : dump.outdent ();
18172 : 2285 : }
18173 : :
18174 : : /* Return the prefix to use for dumping a #pragma diagnostic change to DK. */
18175 : :
18176 : : static const char *
18177 : 800 : dk_string (diagnostic_t dk)
18178 : : {
18179 : 800 : gcc_assert (dk > DK_UNSPECIFIED && dk < DK_LAST_DIAGNOSTIC_KIND);
18180 : 800 : if (dk == DK_IGNORED)
18181 : : /* diagnostic.def has an empty string for ignored. */
18182 : : return "ignored: ";
18183 : : else
18184 : 0 : return get_diagnostic_kind_text (dk);
18185 : : }
18186 : :
18187 : : /* Dump one #pragma GCC diagnostic entry. */
18188 : :
18189 : : static bool
18190 : 1646 : dump_dc_change (unsigned index, unsigned opt, diagnostic_t dk)
18191 : : {
18192 : 1646 : if (dk == DK_POP)
18193 : 846 : return dump (" Index %u: pop from %d", index, opt);
18194 : : else
18195 : 800 : return dump (" Index %u: %s%s", index, dk_string (dk),
18196 : 1600 : cl_options[opt].opt_text);
18197 : : }
18198 : :
18199 : : /* Write out any #pragma GCC diagnostic info to the .dgc section. */
18200 : :
18201 : : void
18202 : 4666 : module_state::write_diagnostic_classification (elf_out *to,
18203 : : diagnostic_context *dc,
18204 : : unsigned *crc_p)
18205 : : {
18206 : 4666 : auto &changes = dc->get_classification_history ();
18207 : :
18208 : 4666 : bytes_out sec (to);
18209 : 4666 : if (sec.streaming_p ())
18210 : : {
18211 : 2285 : sec.begin ();
18212 : 2517 : dump () && dump ("Writing diagnostic change locations");
18213 : 2285 : dump.indent ();
18214 : : }
18215 : :
18216 : 4666 : unsigned len = changes.length ();
18217 : :
18218 : : /* We don't want to write out any entries that came from one of our imports.
18219 : : But then we need to adjust the total, and change DK_POP targets to match
18220 : : the index in our actual output. So remember how many lines we had skipped
18221 : : at each step, where -1 means this line itself is skipped. */
18222 : 4666 : int skips = 0;
18223 : 4666 : auto_vec<int> skips_at (len);
18224 : 4666 : skips_at.safe_grow (len);
18225 : :
18226 : 41992 : for (unsigned i = 0; i < len; ++i)
18227 : : {
18228 : 37326 : const auto &c = changes[i];
18229 : 37326 : skips_at[i] = skips;
18230 : 37326 : if (linemap_location_from_module_p (line_table, c.location))
18231 : : {
18232 : 5970 : ++skips;
18233 : 5970 : skips_at[i] = -1;
18234 : 5970 : continue;
18235 : : }
18236 : : }
18237 : :
18238 : 4666 : if (sec.streaming_p ())
18239 : : {
18240 : 2285 : sec.u (len - skips);
18241 : 2517 : dump () && dump ("Diagnostic changes: %u", len - skips);
18242 : : }
18243 : :
18244 : 41992 : for (unsigned i = 0; i < len; ++i)
18245 : : {
18246 : 37326 : if (skips_at[i] == -1)
18247 : 5970 : continue;
18248 : :
18249 : 31356 : const auto &c = changes[i];
18250 : 31356 : write_location (sec, c.location);
18251 : 31356 : if (sec.streaming_p ())
18252 : : {
18253 : 15678 : unsigned opt = c.option;
18254 : 15678 : if (c.kind == DK_POP)
18255 : 8035 : opt -= skips_at[opt];
18256 : 15678 : sec.u (opt);
18257 : 15678 : sec.u (c.kind);
18258 : 38924 : dump () && dump_dc_change (i - skips_at[i], opt, c.kind);
18259 : : }
18260 : : }
18261 : :
18262 : 4666 : if (sec.streaming_p ())
18263 : : {
18264 : 2285 : sec.end (to, to->name (MOD_SNAME_PFX ".dgc"), crc_p);
18265 : 2285 : dump.outdent ();
18266 : : }
18267 : 4666 : }
18268 : :
18269 : : /* Read any #pragma GCC diagnostic info from the .dgc section. */
18270 : :
18271 : : bool
18272 : 2535 : module_state::read_diagnostic_classification (diagnostic_context *dc)
18273 : : {
18274 : 2535 : bytes_in sec;
18275 : :
18276 : 2535 : if (!sec.begin (loc, from (), MOD_SNAME_PFX ".dgc"))
18277 : : return false;
18278 : :
18279 : 2994 : dump () && dump ("Reading diagnostic change locations");
18280 : 2535 : dump.indent ();
18281 : :
18282 : 2535 : unsigned len = sec.u ();
18283 : 2994 : dump () && dump ("Diagnostic changes: %u", len);
18284 : :
18285 : 2535 : auto &changes = dc->get_classification_history ();
18286 : 2535 : int offset = changes.length ();
18287 : 2535 : changes.reserve (len + 1);
18288 : 20909 : for (unsigned i = 0; i < len; ++i)
18289 : : {
18290 : 18374 : location_t loc = read_location (sec);
18291 : 18374 : int opt = sec.u ();
18292 : 18374 : diagnostic_t kind = (diagnostic_t) sec.u ();
18293 : 18374 : if (kind == DK_POP)
18294 : : /* For a pop, opt is the 'changes' index to return to. */
18295 : 9412 : opt += offset;
18296 : 18374 : changes.quick_push ({ loc, opt, kind });
18297 : 18422 : dump () && dump_dc_change (changes.length () - 1, opt, kind);
18298 : : }
18299 : :
18300 : : /* Did the import pop all its diagnostic changes? */
18301 : 2535 : bool last_was_reset = (len == 0);
18302 : 2535 : if (len)
18303 : 204 : for (int i = changes.length () - 1; ; --i)
18304 : : {
18305 : 8159 : gcc_checking_assert (i >= offset);
18306 : :
18307 : 8159 : const auto &c = changes[i];
18308 : 8159 : if (c.kind != DK_POP)
18309 : : break;
18310 : 8150 : else if (c.option == offset)
18311 : : {
18312 : : last_was_reset = true;
18313 : : break;
18314 : : }
18315 : : else
18316 : : /* As in update_effective_level_from_pragmas, the loop will decrement
18317 : : i so we actually jump to c.option - 1. */
18318 : 8057 : i = c.option;
18319 : 8057 : }
18320 : 2535 : if (!last_was_reset)
18321 : : {
18322 : : /* It didn't, so add a pop at its last location to avoid affecting later
18323 : : imports. */
18324 : 9 : location_t last_loc = ordinary_locs.first + ordinary_locs.second - 1;
18325 : 9 : changes.quick_push ({ last_loc, offset, DK_POP });
18326 : 15 : dump () && dump (" Adding final pop from index %d", offset);
18327 : : }
18328 : :
18329 : 2535 : dump.outdent ();
18330 : 2535 : if (!sec.end (from ()))
18331 : : return false;
18332 : :
18333 : : return true;
18334 : 2535 : }
18335 : :
18336 : : void
18337 : 117 : module_state::write_macro_maps (elf_out *to, range_t &info, unsigned *crc_p)
18338 : : {
18339 : 129 : dump () && dump ("Writing macro location maps");
18340 : 117 : dump.indent ();
18341 : :
18342 : 117 : bytes_out sec (to);
18343 : 117 : sec.begin ();
18344 : :
18345 : 129 : dump () && dump ("Macro maps:%K", info.second);
18346 : 117 : sec.loc (info.second);
18347 : :
18348 : 117 : line_map_uint_t macro_num = 0;
18349 : 234 : for (auto iter = macro_loc_remap->end (), begin = macro_loc_remap->begin ();
18350 : 113626 : iter-- != begin;)
18351 : : {
18352 : 113509 : auto mac = iter->src;
18353 : 113509 : sec.loc (iter->remap);
18354 : 113509 : sec.u (mac->n_tokens);
18355 : 113509 : sec.cpp_node (mac->macro);
18356 : 113509 : write_location (sec, mac->m_expansion);
18357 : 113509 : const location_t *locs = mac->macro_locations;
18358 : : /* There are lots of identical runs. */
18359 : 113509 : location_t prev = UNKNOWN_LOCATION;
18360 : 113509 : unsigned count = 0;
18361 : 113509 : unsigned runs = 0;
18362 : 3961739 : for (unsigned jx = mac->n_tokens * 2; jx--;)
18363 : : {
18364 : 3848230 : location_t tok_loc = locs[jx];
18365 : 3848230 : if (tok_loc == prev)
18366 : : {
18367 : 1817683 : count++;
18368 : 1817683 : continue;
18369 : : }
18370 : 2030547 : runs++;
18371 : 2030547 : sec.u (count);
18372 : 2030547 : count = 1;
18373 : 2030547 : prev = tok_loc;
18374 : 2030547 : write_location (sec, tok_loc);
18375 : : }
18376 : 113509 : sec.u (count);
18377 : 113509 : dump (dumper::LOCATION)
18378 : 9 : && dump ("Macro:%K %I %u/%u*2 locations [%K,%K)->%K",
18379 : 9 : macro_num, identifier (mac->macro),
18380 : : runs, mac->n_tokens,
18381 : : MAP_START_LOCATION (mac),
18382 : 9 : MAP_START_LOCATION (mac) + mac->n_tokens,
18383 : : iter->remap);
18384 : 113509 : macro_num++;
18385 : : }
18386 : 117 : gcc_assert (macro_num == info.second);
18387 : :
18388 : 117 : sec.end (to, to->name (MOD_SNAME_PFX ".mlm"), crc_p);
18389 : 117 : dump.outdent ();
18390 : 117 : }
18391 : :
18392 : : bool
18393 : 2535 : module_state::read_ordinary_maps (line_map_uint_t num_ord_locs,
18394 : : unsigned range_bits)
18395 : : {
18396 : 2535 : bytes_in sec;
18397 : :
18398 : 2535 : if (!sec.begin (loc, from (), MOD_SNAME_PFX ".olm"))
18399 : : return false;
18400 : 2994 : dump () && dump ("Reading ordinary location maps");
18401 : 2535 : dump.indent ();
18402 : :
18403 : : /* Read the filename table. */
18404 : 2535 : unsigned len = sec.u ();
18405 : 2994 : dump () && dump ("%u source file names", len);
18406 : 2535 : vec<const char *> filenames;
18407 : 2535 : filenames.create (len);
18408 : 16226 : for (unsigned ix = 0; ix != len; ix++)
18409 : : {
18410 : 13691 : size_t l;
18411 : 13691 : const char *buf = sec.str (&l);
18412 : 13691 : char *fname = XNEWVEC (char, l + 1);
18413 : 13691 : memcpy (fname, buf, l + 1);
18414 : 13691 : dump (dumper::LOCATION) && dump ("Source file[%u]=%s", ix, fname);
18415 : : /* We leak these names into the line-map table. But it
18416 : : doesn't own them. */
18417 : 13691 : filenames.quick_push (fname);
18418 : : }
18419 : :
18420 : 2535 : line_map_uint_t num_ordinary = sec.loc ();
18421 : 2994 : dump () && dump ("Ordinary maps:%K, range_bits:%u",
18422 : : num_ordinary, range_bits);
18423 : :
18424 : 2535 : location_t offset = line_table->highest_location + 1;
18425 : 2535 : offset += ((loc_one << range_bits) - 1);
18426 : 2535 : offset &= ~((loc_one << range_bits) - 1);
18427 : 2535 : ordinary_locs.first = offset;
18428 : :
18429 : 2535 : bool propagated = spans.maybe_propagate (this, offset);
18430 : 2535 : line_map_ordinary *maps = static_cast<line_map_ordinary *>
18431 : 2535 : (line_map_new_raw (line_table, false, num_ordinary));
18432 : :
18433 : 2535 : const line_map_ordinary *base = nullptr;
18434 : 227080 : for (line_map_uint_t ix = 0; ix != num_ordinary && !sec.get_overrun (); ix++)
18435 : : {
18436 : 224545 : line_map_ordinary *map = &maps[ix];
18437 : :
18438 : 224545 : location_t offset = sec.loc ();
18439 : 224545 : if (!offset)
18440 : : {
18441 : 25976 : map->reason = LC_RENAME;
18442 : 25976 : map->sysp = sec.u ();
18443 : 25976 : map->m_range_bits = sec.u ();
18444 : 25976 : map->m_column_and_range_bits = sec.u () + map->m_range_bits;
18445 : 25976 : unsigned fnum = sec.u ();
18446 : 51952 : map->to_file = (fnum < filenames.length () ? filenames[fnum] : "");
18447 : 25976 : map->to_line = sec.u ();
18448 : 25976 : base = map;
18449 : : }
18450 : : else
18451 : : {
18452 : 198569 : *map = *base;
18453 : 198569 : map->to_line += offset >> map->m_column_and_range_bits;
18454 : : }
18455 : 224545 : location_t remap = sec.loc ();
18456 : 224545 : map->start_location = remap + ordinary_locs.first;
18457 : 224545 : if (base == map)
18458 : : {
18459 : : /* Root the outermost map at our location. */
18460 : 25976 : ordinary_locs.second = remap;
18461 : 25976 : location_t from = read_location (sec);
18462 : 25976 : map->included_from = from != UNKNOWN_LOCATION ? from : loc;
18463 : : }
18464 : : }
18465 : :
18466 : 2535 : ordinary_locs.second = num_ord_locs;
18467 : : /* highest_location is the one handed out, not the next one to
18468 : : hand out. */
18469 : 2535 : line_table->highest_location = ordinary_locs.first + ordinary_locs.second - 1;
18470 : :
18471 : 2535 : if (line_table->highest_location >= LINE_MAP_MAX_LOCATION_WITH_COLS)
18472 : : /* We shouldn't run out of locations, as we checked before
18473 : : starting. */
18474 : 0 : sec.set_overrun ();
18475 : 2994 : dump () && dump ("Ordinary location [%K,+%K)",
18476 : : ordinary_locs.first, ordinary_locs.second);
18477 : :
18478 : 2535 : if (propagated)
18479 : 144 : spans.close ();
18480 : :
18481 : 2535 : filenames.release ();
18482 : :
18483 : 2535 : dump.outdent ();
18484 : 2535 : if (!sec.end (from ()))
18485 : : return false;
18486 : :
18487 : : return true;
18488 : 2535 : }
18489 : :
18490 : : bool
18491 : 127 : module_state::read_macro_maps (line_map_uint_t num_macro_locs)
18492 : : {
18493 : 127 : bytes_in sec;
18494 : :
18495 : 127 : if (!sec.begin (loc, from (), MOD_SNAME_PFX ".mlm"))
18496 : : return false;
18497 : 133 : dump () && dump ("Reading macro location maps");
18498 : 127 : dump.indent ();
18499 : :
18500 : 127 : line_map_uint_t num_macros = sec.loc ();
18501 : 133 : dump () && dump ("Macro maps:%K locs:%K",
18502 : : num_macros, num_macro_locs);
18503 : :
18504 : 254 : bool propagated = spans.maybe_propagate (this,
18505 : 127 : line_table->highest_location + 1);
18506 : :
18507 : 127 : location_t offset = LINEMAPS_MACRO_LOWEST_LOCATION (line_table);
18508 : 127 : macro_locs.second = num_macro_locs;
18509 : 127 : macro_locs.first = offset - num_macro_locs;
18510 : :
18511 : 133 : dump () && dump ("Macro loc delta %K", offset);
18512 : 133 : dump () && dump ("Macro locations [%K,%K)",
18513 : : macro_locs.first, macro_locs.second);
18514 : :
18515 : 143258 : for (line_map_uint_t ix = 0; ix != num_macros && !sec.get_overrun (); ix++)
18516 : : {
18517 : 143131 : location_t offset = sec.loc ();
18518 : 143131 : unsigned n_tokens = sec.u ();
18519 : 143131 : cpp_hashnode *node = sec.cpp_node ();
18520 : 143131 : location_t exp_loc = read_location (sec);
18521 : :
18522 : 143131 : const line_map_macro *macro
18523 : 143131 : = linemap_enter_macro (line_table, node, exp_loc, n_tokens);
18524 : 143131 : if (!macro)
18525 : : /* We shouldn't run out of locations, as we checked that we
18526 : : had enough before starting. */
18527 : : break;
18528 : 143131 : gcc_checking_assert (MAP_START_LOCATION (macro)
18529 : : == offset + macro_locs.first);
18530 : :
18531 : 143131 : location_t *locs = macro->macro_locations;
18532 : 143131 : location_t tok_loc = UNKNOWN_LOCATION;
18533 : 143131 : unsigned count = sec.u ();
18534 : 143131 : unsigned runs = 0;
18535 : 4910085 : for (unsigned jx = macro->n_tokens * 2; jx-- && !sec.get_overrun ();)
18536 : : {
18537 : 7284758 : while (!count-- && !sec.get_overrun ())
18538 : : {
18539 : 2517804 : runs++;
18540 : 2517804 : tok_loc = read_location (sec);
18541 : 2517804 : count = sec.u ();
18542 : : }
18543 : 4766954 : locs[jx] = tok_loc;
18544 : : }
18545 : 143131 : if (count)
18546 : 0 : sec.set_overrun ();
18547 : 143158 : dump (dumper::LOCATION)
18548 : 0 : && dump ("Macro:%K %I %u/%u*2 locations [%K,%K)",
18549 : : ix, identifier (node), runs, n_tokens,
18550 : : MAP_START_LOCATION (macro),
18551 : 0 : MAP_START_LOCATION (macro) + n_tokens);
18552 : : }
18553 : :
18554 : 133 : dump () && dump ("Macro location lwm:%K", macro_locs.first);
18555 : 127 : if (propagated)
18556 : 3 : spans.close ();
18557 : :
18558 : 127 : dump.outdent ();
18559 : 127 : if (!sec.end (from ()))
18560 : : return false;
18561 : :
18562 : : return true;
18563 : 127 : }
18564 : :
18565 : : /* Serialize the definition of MACRO. */
18566 : :
18567 : : void
18568 : 66099 : module_state::write_define (bytes_out &sec, const cpp_macro *macro)
18569 : : {
18570 : 66099 : sec.u (macro->count);
18571 : :
18572 : 66099 : bytes_out::bits_out bits = sec.stream_bits ();
18573 : 66099 : bits.b (macro->fun_like);
18574 : 66099 : bits.b (macro->variadic);
18575 : 66099 : bits.b (macro->syshdr);
18576 : 66099 : bits.bflush ();
18577 : :
18578 : 66099 : write_location (sec, macro->line);
18579 : 66099 : if (macro->fun_like)
18580 : : {
18581 : 8872 : sec.u (macro->paramc);
18582 : 8872 : const cpp_hashnode *const *parms = macro->parm.params;
18583 : 22338 : for (unsigned ix = 0; ix != macro->paramc; ix++)
18584 : 13466 : sec.cpp_node (parms[ix]);
18585 : : }
18586 : :
18587 : : unsigned len = 0;
18588 : 217108 : for (unsigned ix = 0; ix != macro->count; ix++)
18589 : : {
18590 : 151009 : const cpp_token *token = ¯o->exp.tokens[ix];
18591 : 151009 : write_location (sec, token->src_loc);
18592 : 151009 : sec.u (token->type);
18593 : 151009 : sec.u (token->flags);
18594 : 151009 : switch (cpp_token_val_index (token))
18595 : : {
18596 : 0 : default:
18597 : 0 : gcc_unreachable ();
18598 : :
18599 : 11699 : case CPP_TOKEN_FLD_ARG_NO:
18600 : : /* An argument reference. */
18601 : 11699 : sec.u (token->val.macro_arg.arg_no);
18602 : 11699 : sec.cpp_node (token->val.macro_arg.spelling);
18603 : 11699 : break;
18604 : :
18605 : 29782 : case CPP_TOKEN_FLD_NODE:
18606 : : /* An identifier. */
18607 : 29782 : sec.cpp_node (token->val.node.node);
18608 : 29782 : if (token->val.node.spelling == token->val.node.node)
18609 : : /* The spelling will usually be the same. so optimize
18610 : : that. */
18611 : 29782 : sec.str (NULL, 0);
18612 : : else
18613 : 0 : sec.cpp_node (token->val.node.spelling);
18614 : : break;
18615 : :
18616 : : case CPP_TOKEN_FLD_NONE:
18617 : : break;
18618 : :
18619 : 46989 : case CPP_TOKEN_FLD_STR:
18620 : : /* A string, number or comment. Not always NUL terminated,
18621 : : we stream out in a single contatenation with embedded
18622 : : NULs as that's a safe default. */
18623 : 46989 : len += token->val.str.len + 1;
18624 : 46989 : sec.u (token->val.str.len);
18625 : 46989 : break;
18626 : :
18627 : 0 : case CPP_TOKEN_FLD_SOURCE:
18628 : 0 : case CPP_TOKEN_FLD_TOKEN_NO:
18629 : 0 : case CPP_TOKEN_FLD_PRAGMA:
18630 : : /* These do not occur inside a macro itself. */
18631 : 0 : gcc_unreachable ();
18632 : : }
18633 : : }
18634 : :
18635 : 66099 : if (len)
18636 : : {
18637 : 43625 : char *ptr = reinterpret_cast<char *> (sec.buf (len));
18638 : 43625 : len = 0;
18639 : 133072 : for (unsigned ix = 0; ix != macro->count; ix++)
18640 : : {
18641 : 89447 : const cpp_token *token = ¯o->exp.tokens[ix];
18642 : 89447 : if (cpp_token_val_index (token) == CPP_TOKEN_FLD_STR)
18643 : : {
18644 : 46989 : memcpy (ptr + len, token->val.str.text,
18645 : 46989 : token->val.str.len);
18646 : 46989 : len += token->val.str.len;
18647 : 46989 : ptr[len++] = 0;
18648 : : }
18649 : : }
18650 : : }
18651 : 66099 : }
18652 : :
18653 : : /* Read a macro definition. */
18654 : :
18655 : : cpp_macro *
18656 : 316 : module_state::read_define (bytes_in &sec, cpp_reader *reader) const
18657 : : {
18658 : 316 : unsigned count = sec.u ();
18659 : : /* We rely on knowing cpp_reader's hash table is ident_hash, and
18660 : : its subobject allocator is stringpool_ggc_alloc and that is just
18661 : : a wrapper for ggc_alloc_atomic. */
18662 : 316 : cpp_macro *macro
18663 : 632 : = (cpp_macro *)ggc_alloc_atomic (sizeof (cpp_macro)
18664 : 316 : + sizeof (cpp_token) * (count - !!count));
18665 : 316 : memset (macro, 0, sizeof (cpp_macro) + sizeof (cpp_token) * (count - !!count));
18666 : :
18667 : 316 : macro->count = count;
18668 : 316 : macro->kind = cmk_macro;
18669 : 316 : macro->imported_p = true;
18670 : :
18671 : 316 : bytes_in::bits_in bits = sec.stream_bits ();
18672 : 316 : macro->fun_like = bits.b ();
18673 : 316 : macro->variadic = bits.b ();
18674 : 316 : macro->syshdr = bits.b ();
18675 : 316 : bits.bflush ();
18676 : :
18677 : 316 : macro->line = read_location (sec);
18678 : :
18679 : 316 : if (macro->fun_like)
18680 : : {
18681 : 66 : unsigned paramc = sec.u ();
18682 : 66 : cpp_hashnode **params
18683 : 66 : = (cpp_hashnode **)ggc_alloc_atomic (sizeof (cpp_hashnode *) * paramc);
18684 : 66 : macro->paramc = paramc;
18685 : 66 : macro->parm.params = params;
18686 : 141 : for (unsigned ix = 0; ix != paramc; ix++)
18687 : 75 : params[ix] = sec.cpp_node ();
18688 : : }
18689 : :
18690 : : unsigned len = 0;
18691 : 1114 : for (unsigned ix = 0; ix != count && !sec.get_overrun (); ix++)
18692 : : {
18693 : 798 : cpp_token *token = ¯o->exp.tokens[ix];
18694 : 798 : token->src_loc = read_location (sec);
18695 : 798 : token->type = cpp_ttype (sec.u ());
18696 : 798 : token->flags = sec.u ();
18697 : 798 : switch (cpp_token_val_index (token))
18698 : : {
18699 : 0 : default:
18700 : 0 : sec.set_overrun ();
18701 : 0 : break;
18702 : :
18703 : 62 : case CPP_TOKEN_FLD_ARG_NO:
18704 : : /* An argument reference. */
18705 : 62 : {
18706 : 62 : unsigned arg_no = sec.u ();
18707 : 62 : if (arg_no - 1 >= macro->paramc)
18708 : 0 : sec.set_overrun ();
18709 : 62 : token->val.macro_arg.arg_no = arg_no;
18710 : 62 : token->val.macro_arg.spelling = sec.cpp_node ();
18711 : : }
18712 : 62 : break;
18713 : :
18714 : 208 : case CPP_TOKEN_FLD_NODE:
18715 : : /* An identifier. */
18716 : 208 : token->val.node.node = sec.cpp_node ();
18717 : 208 : token->val.node.spelling = sec.cpp_node ();
18718 : 208 : if (!token->val.node.spelling)
18719 : 208 : token->val.node.spelling = token->val.node.node;
18720 : : break;
18721 : :
18722 : : case CPP_TOKEN_FLD_NONE:
18723 : : break;
18724 : :
18725 : 176 : case CPP_TOKEN_FLD_STR:
18726 : : /* A string, number or comment. */
18727 : 176 : token->val.str.len = sec.u ();
18728 : 176 : len += token->val.str.len + 1;
18729 : 176 : break;
18730 : : }
18731 : : }
18732 : :
18733 : 316 : if (len)
18734 : 175 : if (const char *ptr = reinterpret_cast<const char *> (sec.buf (len)))
18735 : : {
18736 : : /* There should be a final NUL. */
18737 : 175 : if (ptr[len-1])
18738 : 0 : sec.set_overrun ();
18739 : : /* cpp_alloc_token_string will add a final NUL. */
18740 : 175 : const unsigned char *buf
18741 : 175 : = cpp_alloc_token_string (reader, (const unsigned char *)ptr, len - 1);
18742 : 175 : len = 0;
18743 : 653 : for (unsigned ix = 0; ix != count && !sec.get_overrun (); ix++)
18744 : : {
18745 : 478 : cpp_token *token = ¯o->exp.tokens[ix];
18746 : 478 : if (cpp_token_val_index (token) == CPP_TOKEN_FLD_STR)
18747 : : {
18748 : 176 : token->val.str.text = buf + len;
18749 : 176 : len += token->val.str.len;
18750 : 176 : if (buf[len++])
18751 : 0 : sec.set_overrun ();
18752 : : }
18753 : : }
18754 : : }
18755 : :
18756 : 316 : if (sec.get_overrun ())
18757 : 0 : return NULL;
18758 : : return macro;
18759 : 316 : }
18760 : :
18761 : : /* Exported macro data. */
18762 : : struct GTY(()) macro_export {
18763 : : cpp_macro *def;
18764 : : location_t undef_loc;
18765 : :
18766 : 94444 : macro_export ()
18767 : 94444 : :def (NULL), undef_loc (UNKNOWN_LOCATION)
18768 : : {
18769 : : }
18770 : : };
18771 : :
18772 : : /* Imported macro data. */
18773 : : class macro_import {
18774 : : public:
18775 : : struct slot {
18776 : : #if defined (WORDS_BIGENDIAN) && SIZEOF_VOID_P == 8
18777 : : int offset;
18778 : : #endif
18779 : : /* We need to ensure we don't use the LSB for representation, as
18780 : : that's the union discriminator below. */
18781 : : unsigned bits;
18782 : :
18783 : : #if !(defined (WORDS_BIGENDIAN) && SIZEOF_VOID_P == 8)
18784 : : int offset;
18785 : : #endif
18786 : :
18787 : : public:
18788 : : enum Layout {
18789 : : L_DEF = 1,
18790 : : L_UNDEF = 2,
18791 : : L_BOTH = 3,
18792 : : L_MODULE_SHIFT = 2
18793 : : };
18794 : :
18795 : : public:
18796 : : /* Not a regular ctor, because we put it in a union, and that's
18797 : : not allowed in C++ 98. */
18798 : 163895 : static slot ctor (unsigned module, unsigned defness)
18799 : : {
18800 : 163895 : gcc_checking_assert (defness);
18801 : 163895 : slot s;
18802 : 163895 : s.bits = defness | (module << L_MODULE_SHIFT);
18803 : 163895 : s.offset = -1;
18804 : 163895 : return s;
18805 : : }
18806 : :
18807 : : public:
18808 : 133709 : unsigned get_defness () const
18809 : : {
18810 : 133709 : return bits & L_BOTH;
18811 : : }
18812 : 96175 : unsigned get_module () const
18813 : : {
18814 : 96175 : return bits >> L_MODULE_SHIFT;
18815 : : }
18816 : 12 : void become_undef ()
18817 : : {
18818 : 12 : bits &= ~unsigned (L_DEF);
18819 : 12 : bits |= unsigned (L_UNDEF);
18820 : : }
18821 : : };
18822 : :
18823 : : private:
18824 : : typedef vec<slot, va_heap, vl_embed> ary_t;
18825 : : union either {
18826 : : /* Discriminated by bits 0|1 != 0. The expected case is that
18827 : : there will be exactly one slot per macro, hence the effort of
18828 : : packing that. */
18829 : : ary_t *ary;
18830 : : slot single;
18831 : : } u;
18832 : :
18833 : : public:
18834 : 131151 : macro_import ()
18835 : 131151 : {
18836 : 131151 : u.ary = NULL;
18837 : : }
18838 : :
18839 : : private:
18840 : 7247212 : bool single_p () const
18841 : : {
18842 : 7247212 : return u.single.bits & slot::L_BOTH;
18843 : : }
18844 : 7378369 : bool occupied_p () const
18845 : : {
18846 : 7378369 : return u.ary != NULL;
18847 : : }
18848 : :
18849 : : public:
18850 : 906 : unsigned length () const
18851 : : {
18852 : 906 : gcc_checking_assert (occupied_p ());
18853 : 906 : return single_p () ? 1 : u.ary->length ();
18854 : : }
18855 : 7119440 : slot &operator[] (unsigned ix)
18856 : : {
18857 : 7119440 : gcc_checking_assert (occupied_p ());
18858 : 7119440 : if (single_p ())
18859 : : {
18860 : 7034422 : gcc_checking_assert (!ix);
18861 : 7034422 : return u.single;
18862 : : }
18863 : : else
18864 : 85018 : return (*u.ary)[ix];
18865 : : }
18866 : :
18867 : : public:
18868 : : slot &exported ();
18869 : : slot &append (unsigned module, unsigned defness);
18870 : : };
18871 : :
18872 : : /* O is a new import to append to the list for. If we're an empty
18873 : : set, initialize us. */
18874 : :
18875 : : macro_import::slot &
18876 : 163895 : macro_import::append (unsigned module, unsigned defness)
18877 : : {
18878 : 163895 : if (!occupied_p ())
18879 : : {
18880 : 131151 : u.single = slot::ctor (module, defness);
18881 : 131151 : return u.single;
18882 : : }
18883 : : else
18884 : : {
18885 : 32744 : bool single = single_p ();
18886 : 32744 : ary_t *m = single ? NULL : u.ary;
18887 : 32744 : vec_safe_reserve (m, 1 + single);
18888 : 32744 : if (single)
18889 : 32741 : m->quick_push (u.single);
18890 : 32744 : u.ary = m;
18891 : 32744 : return *u.ary->quick_push (slot::ctor (module, defness));
18892 : : }
18893 : : }
18894 : :
18895 : : /* We're going to export something. Make sure the first import slot
18896 : : is us. */
18897 : :
18898 : : macro_import::slot &
18899 : 94128 : macro_import::exported ()
18900 : : {
18901 : 94128 : if (occupied_p () && !(*this)[0].get_module ())
18902 : : {
18903 : 6 : slot &res = (*this)[0];
18904 : 6 : res.bits |= slot::L_DEF;
18905 : 6 : return res;
18906 : : }
18907 : :
18908 : 94122 : slot *a = &append (0, slot::L_DEF);
18909 : 94122 : if (!single_p ())
18910 : : {
18911 : 28572 : slot &f = (*this)[0];
18912 : 28572 : std::swap (f, *a);
18913 : 28572 : a = &f;
18914 : : }
18915 : : return *a;
18916 : : }
18917 : :
18918 : : /* The import (&exported) macros. cpp_hasnode's deferred field
18919 : : indexes this array (offset by 1, so zero means 'not present'. */
18920 : :
18921 : : static vec<macro_import, va_heap, vl_embed> *macro_imports;
18922 : :
18923 : : /* The exported macros. A macro_import slot's zeroth element's offset
18924 : : indexes this array. If the zeroth slot is not for module zero,
18925 : : there is no export. */
18926 : :
18927 : : static GTY(()) vec<macro_export, va_gc> *macro_exports;
18928 : :
18929 : : /* The reachable set of header imports from this TU. */
18930 : :
18931 : : static GTY(()) bitmap headers;
18932 : :
18933 : : /* Get the (possibly empty) macro imports for NODE. */
18934 : :
18935 : : static macro_import &
18936 : 135329 : get_macro_imports (cpp_hashnode *node)
18937 : : {
18938 : 135329 : if (node->deferred)
18939 : 4178 : return (*macro_imports)[node->deferred - 1];
18940 : :
18941 : 131151 : vec_safe_reserve (macro_imports, 1);
18942 : 131151 : node->deferred = macro_imports->length () + 1;
18943 : 131151 : return *vec_safe_push (macro_imports, macro_import ());
18944 : : }
18945 : :
18946 : : /* Get the macro export for export EXP of NODE. */
18947 : :
18948 : : static macro_export &
18949 : 94128 : get_macro_export (macro_import::slot &slot)
18950 : : {
18951 : 94128 : if (slot.offset >= 0)
18952 : 6 : return (*macro_exports)[slot.offset];
18953 : :
18954 : 94122 : vec_safe_reserve (macro_exports, 1);
18955 : 94122 : slot.offset = macro_exports->length ();
18956 : 94122 : return *macro_exports->quick_push (macro_export ());
18957 : : }
18958 : :
18959 : : /* If NODE is an exportable macro, add it to the export set. */
18960 : :
18961 : : static int
18962 : 3672428 : maybe_add_macro (cpp_reader *, cpp_hashnode *node, void *data_)
18963 : : {
18964 : 3672428 : bool exporting = false;
18965 : :
18966 : 3672428 : if (cpp_user_macro_p (node))
18967 : 471956 : if (cpp_macro *macro = node->value.macro)
18968 : : /* Ignore imported, builtins, command line and forced header macros. */
18969 : 471514 : if (!macro->imported_p
18970 : 471514 : && !macro->lazy && macro->line >= spans.main_start ())
18971 : : {
18972 : 65556 : gcc_checking_assert (macro->kind == cmk_macro);
18973 : : /* I don't want to deal with this corner case, that I suspect is
18974 : : a devil's advocate reading of the standard. */
18975 : 65556 : gcc_checking_assert (!macro->extra_tokens);
18976 : :
18977 : 65556 : macro_import::slot &slot = get_macro_imports (node).exported ();
18978 : 65556 : macro_export &exp = get_macro_export (slot);
18979 : 65556 : exp.def = macro;
18980 : 65556 : exporting = true;
18981 : : }
18982 : :
18983 : 3606872 : if (!exporting && node->deferred)
18984 : : {
18985 : 581 : macro_import &imports = (*macro_imports)[node->deferred - 1];
18986 : 581 : macro_import::slot &slot = imports[0];
18987 : 581 : if (!slot.get_module ())
18988 : : {
18989 : 550 : gcc_checking_assert (slot.get_defness ());
18990 : : exporting = true;
18991 : : }
18992 : : }
18993 : :
18994 : 65556 : if (exporting)
18995 : 66106 : static_cast<vec<cpp_hashnode *> *> (data_)->safe_push (node);
18996 : :
18997 : 3672428 : return 1; /* Don't stop. */
18998 : : }
18999 : :
19000 : : /* Order cpp_hashnodes A_ and B_ by their exported macro locations. */
19001 : :
19002 : : static int
19003 : 3431095 : macro_loc_cmp (const void *a_, const void *b_)
19004 : : {
19005 : 3431095 : const cpp_hashnode *node_a = *(const cpp_hashnode *const *)a_;
19006 : 3431095 : macro_import &import_a = (*macro_imports)[node_a->deferred - 1];
19007 : 3431095 : const macro_export &export_a = (*macro_exports)[import_a[0].offset];
19008 : 3431095 : location_t loc_a = export_a.def ? export_a.def->line : export_a.undef_loc;
19009 : :
19010 : 3431095 : const cpp_hashnode *node_b = *(const cpp_hashnode *const *)b_;
19011 : 3431095 : macro_import &import_b = (*macro_imports)[node_b->deferred - 1];
19012 : 3431095 : const macro_export &export_b = (*macro_exports)[import_b[0].offset];
19013 : 3431095 : location_t loc_b = export_b.def ? export_b.def->line : export_b.undef_loc;
19014 : :
19015 : 3431095 : if (loc_a < loc_b)
19016 : : return +1;
19017 : 1762812 : else if (loc_a > loc_b)
19018 : : return -1;
19019 : : else
19020 : 0 : return 0;
19021 : : }
19022 : :
19023 : : /* Gather the macro definitions and undefinitions that we will need to
19024 : : write out. */
19025 : :
19026 : : vec<cpp_hashnode *> *
19027 : 851 : module_state::prepare_macros (cpp_reader *reader)
19028 : : {
19029 : 851 : vec<cpp_hashnode *> *macros;
19030 : 851 : vec_alloc (macros, 100);
19031 : :
19032 : 851 : cpp_forall_identifiers (reader, maybe_add_macro, macros);
19033 : :
19034 : 875 : dump (dumper::MACRO) && dump ("No more than %u macros", macros->length ());
19035 : :
19036 : 851 : macros->qsort (macro_loc_cmp);
19037 : :
19038 : : // Note the locations.
19039 : 67808 : for (unsigned ix = macros->length (); ix--;)
19040 : : {
19041 : 66106 : cpp_hashnode *node = (*macros)[ix];
19042 : 66106 : macro_import::slot &slot = (*macro_imports)[node->deferred - 1][0];
19043 : 66106 : macro_export &mac = (*macro_exports)[slot.offset];
19044 : :
19045 : 66106 : if (IDENTIFIER_KEYWORD_P (identifier (node)))
19046 : 1 : continue;
19047 : :
19048 : 66105 : if (mac.undef_loc != UNKNOWN_LOCATION)
19049 : 12 : note_location (mac.undef_loc);
19050 : 66105 : if (mac.def)
19051 : : {
19052 : 66099 : note_location (mac.def->line);
19053 : 217108 : for (unsigned ix = 0; ix != mac.def->count; ix++)
19054 : 151009 : note_location (mac.def->exp.tokens[ix].src_loc);
19055 : : }
19056 : : }
19057 : :
19058 : 851 : return macros;
19059 : : }
19060 : :
19061 : : /* Write out the exported defines. This is two sections, one
19062 : : containing the definitions, the other a table of node names. */
19063 : :
19064 : : unsigned
19065 : 851 : module_state::write_macros (elf_out *to, vec<cpp_hashnode *> *macros,
19066 : : unsigned *crc_p)
19067 : : {
19068 : 917 : dump () && dump ("Writing macros");
19069 : 851 : dump.indent ();
19070 : :
19071 : : /* Write the defs */
19072 : 851 : bytes_out sec (to);
19073 : 851 : sec.begin ();
19074 : :
19075 : 851 : unsigned count = 0;
19076 : 67808 : for (unsigned ix = macros->length (); ix--;)
19077 : : {
19078 : 66106 : cpp_hashnode *node = (*macros)[ix];
19079 : 66106 : macro_import::slot &slot = (*macro_imports)[node->deferred - 1][0];
19080 : 66106 : gcc_assert (!slot.get_module () && slot.get_defness ());
19081 : :
19082 : 66106 : macro_export &mac = (*macro_exports)[slot.offset];
19083 : 66106 : gcc_assert (!!(slot.get_defness () & macro_import::slot::L_UNDEF)
19084 : : == (mac.undef_loc != UNKNOWN_LOCATION)
19085 : : && !!(slot.get_defness () & macro_import::slot::L_DEF)
19086 : : == (mac.def != NULL));
19087 : :
19088 : 66106 : if (IDENTIFIER_KEYWORD_P (identifier (node)))
19089 : : {
19090 : 1 : warning_at (mac.def->line, 0,
19091 : : "not exporting %<#define %E%> as it is a keyword",
19092 : : identifier (node));
19093 : 1 : slot.offset = 0;
19094 : 1 : continue;
19095 : : }
19096 : :
19097 : 66105 : count++;
19098 : 66105 : slot.offset = sec.pos;
19099 : 66105 : dump (dumper::MACRO)
19100 : 24 : && dump ("Writing macro %s%s%s %I at %u",
19101 : 24 : slot.get_defness () & macro_import::slot::L_UNDEF
19102 : : ? "#undef" : "",
19103 : 24 : slot.get_defness () == macro_import::slot::L_BOTH
19104 : : ? " & " : "",
19105 : 24 : slot.get_defness () & macro_import::slot::L_DEF
19106 : : ? "#define" : "",
19107 : : identifier (node), slot.offset);
19108 : 66105 : if (mac.undef_loc != UNKNOWN_LOCATION)
19109 : 12 : write_location (sec, mac.undef_loc);
19110 : 66105 : if (mac.def)
19111 : 66099 : write_define (sec, mac.def);
19112 : : }
19113 : 851 : if (count)
19114 : : // We may have ended on a tokenless macro with a very short
19115 : : // location, that will cause problems reading its bit flags.
19116 : 141 : sec.u (0);
19117 : 851 : sec.end (to, to->name (MOD_SNAME_PFX ".def"), crc_p);
19118 : :
19119 : 851 : if (count)
19120 : : {
19121 : : /* Write the table. */
19122 : 141 : bytes_out sec (to);
19123 : 141 : sec.begin ();
19124 : 141 : sec.u (count);
19125 : :
19126 : 66387 : for (unsigned ix = macros->length (); ix--;)
19127 : : {
19128 : 66105 : const cpp_hashnode *node = (*macros)[ix];
19129 : 66105 : macro_import::slot &slot = (*macro_imports)[node->deferred - 1][0];
19130 : :
19131 : 66105 : if (slot.offset)
19132 : : {
19133 : 66105 : sec.cpp_node (node);
19134 : 66105 : sec.u (slot.get_defness ());
19135 : 66105 : sec.u (slot.offset);
19136 : : }
19137 : : }
19138 : 141 : sec.end (to, to->name (MOD_SNAME_PFX ".mac"), crc_p);
19139 : 141 : }
19140 : :
19141 : 851 : dump.outdent ();
19142 : 851 : return count;
19143 : 851 : }
19144 : :
19145 : : bool
19146 : 884 : module_state::read_macros ()
19147 : : {
19148 : : /* Get the def section. */
19149 : 884 : if (!slurp->macro_defs.begin (loc, from (), MOD_SNAME_PFX ".def"))
19150 : : return false;
19151 : :
19152 : : /* Get the tbl section, if there are defs. */
19153 : 884 : if (slurp->macro_defs.more_p ()
19154 : 884 : && !slurp->macro_tbl.begin (loc, from (), MOD_SNAME_PFX ".mac"))
19155 : : return false;
19156 : :
19157 : : return true;
19158 : : }
19159 : :
19160 : : /* Install the macro name table. */
19161 : :
19162 : : void
19163 : 890 : module_state::install_macros ()
19164 : : {
19165 : 890 : bytes_in &sec = slurp->macro_tbl;
19166 : 890 : if (!sec.size)
19167 : : return;
19168 : :
19169 : 197 : dump () && dump ("Reading macro table %M", this);
19170 : 176 : dump.indent ();
19171 : :
19172 : 176 : unsigned count = sec.u ();
19173 : 197 : dump () && dump ("%u macros", count);
19174 : 69949 : while (count--)
19175 : : {
19176 : 69773 : cpp_hashnode *node = sec.cpp_node ();
19177 : 69773 : macro_import &imp = get_macro_imports (node);
19178 : 69773 : unsigned flags = sec.u () & macro_import::slot::L_BOTH;
19179 : 69773 : if (!flags)
19180 : 0 : sec.set_overrun ();
19181 : :
19182 : 69773 : if (sec.get_overrun ())
19183 : : break;
19184 : :
19185 : 69773 : macro_import::slot &slot = imp.append (mod, flags);
19186 : 69773 : slot.offset = sec.u ();
19187 : :
19188 : 69773 : dump (dumper::MACRO)
19189 : 84 : && dump ("Read %s macro %s%s%s %I at %u",
19190 : 30 : imp.length () > 1 ? "add" : "new",
19191 : 27 : flags & macro_import::slot::L_UNDEF ? "#undef" : "",
19192 : : flags == macro_import::slot::L_BOTH ? " & " : "",
19193 : 30 : flags & macro_import::slot::L_DEF ? "#define" : "",
19194 : : identifier (node), slot.offset);
19195 : :
19196 : : /* We'll leak an imported definition's TOKEN_FLD_STR's data
19197 : : here. But that only happens when we've had to resolve the
19198 : : deferred macro before this import -- why are you doing
19199 : : that? */
19200 : 69773 : if (cpp_macro *cur = cpp_set_deferred_macro (node))
19201 : 28560 : if (!cur->imported_p)
19202 : : {
19203 : 28560 : macro_import::slot &slot = imp.exported ();
19204 : 28560 : macro_export &exp = get_macro_export (slot);
19205 : 28560 : exp.def = cur;
19206 : 98509 : dump (dumper::MACRO)
19207 : 0 : && dump ("Saving current #define %I", identifier (node));
19208 : : }
19209 : : }
19210 : :
19211 : : /* We're now done with the table. */
19212 : 176 : elf_in::release (slurp->from, sec);
19213 : :
19214 : 176 : dump.outdent ();
19215 : : }
19216 : :
19217 : : /* Import the transitive macros. */
19218 : :
19219 : : void
19220 : 848 : module_state::import_macros ()
19221 : : {
19222 : 848 : bitmap_ior_into (headers, slurp->headers);
19223 : :
19224 : 848 : bitmap_iterator bititer;
19225 : 848 : unsigned bitnum;
19226 : 1738 : EXECUTE_IF_SET_IN_BITMAP (slurp->headers, 0, bitnum, bititer)
19227 : 890 : (*modules)[bitnum]->install_macros ();
19228 : 848 : }
19229 : :
19230 : : /* NODE is being undefined at LOC. Record it in the export table, if
19231 : : necessary. */
19232 : :
19233 : : void
19234 : 187915 : module_state::undef_macro (cpp_reader *, location_t loc, cpp_hashnode *node)
19235 : : {
19236 : 187915 : if (!node->deferred)
19237 : : /* The macro is not imported, so our undef is irrelevant. */
19238 : : return;
19239 : :
19240 : 12 : unsigned n = dump.push (NULL);
19241 : :
19242 : 12 : macro_import::slot &slot = (*macro_imports)[node->deferred - 1].exported ();
19243 : 12 : macro_export &exp = get_macro_export (slot);
19244 : :
19245 : 12 : exp.undef_loc = loc;
19246 : 12 : slot.become_undef ();
19247 : 12 : exp.def = NULL;
19248 : :
19249 : 18 : dump (dumper::MACRO) && dump ("Recording macro #undef %I", identifier (node));
19250 : :
19251 : 12 : dump.pop (n);
19252 : : }
19253 : :
19254 : : /* NODE is a deferred macro node. Determine the definition and return
19255 : : it, with NULL if undefined. May issue diagnostics.
19256 : :
19257 : : This can leak memory, when merging declarations -- the string
19258 : : contents (TOKEN_FLD_STR) of each definition are allocated in
19259 : : unreclaimable cpp objstack. Only one will win. However, I do not
19260 : : expect this to be common -- mostly macros have a single point of
19261 : : definition. Perhaps we could restore the objstack to its position
19262 : : after the first imported definition (if that wins)? The macros
19263 : : themselves are GC'd. */
19264 : :
19265 : : cpp_macro *
19266 : 292 : module_state::deferred_macro (cpp_reader *reader, location_t loc,
19267 : : cpp_hashnode *node)
19268 : : {
19269 : 292 : macro_import &imports = (*macro_imports)[node->deferred - 1];
19270 : :
19271 : 292 : unsigned n = dump.push (NULL);
19272 : 298 : dump (dumper::MACRO) && dump ("Deferred macro %I", identifier (node));
19273 : :
19274 : 292 : bitmap visible (BITMAP_GGC_ALLOC ());
19275 : :
19276 : 292 : if (!((imports[0].get_defness () & macro_import::slot::L_UNDEF)
19277 : 0 : && !imports[0].get_module ()))
19278 : : {
19279 : : /* Calculate the set of visible header imports. */
19280 : 292 : bitmap_copy (visible, headers);
19281 : 747 : for (unsigned ix = imports.length (); ix--;)
19282 : : {
19283 : 455 : const macro_import::slot &slot = imports[ix];
19284 : 455 : unsigned mod = slot.get_module ();
19285 : 455 : if ((slot.get_defness () & macro_import::slot::L_UNDEF)
19286 : 455 : && bitmap_bit_p (visible, mod))
19287 : : {
19288 : 12 : bitmap arg = mod ? (*modules)[mod]->slurp->headers : headers;
19289 : 12 : bitmap_and_compl_into (visible, arg);
19290 : 12 : bitmap_set_bit (visible, mod);
19291 : : }
19292 : : }
19293 : : }
19294 : 292 : bitmap_set_bit (visible, 0);
19295 : :
19296 : : /* Now find the macros that are still visible. */
19297 : 292 : bool failed = false;
19298 : 292 : cpp_macro *def = NULL;
19299 : 292 : vec<macro_export> defs;
19300 : 292 : defs.create (imports.length ());
19301 : 747 : for (unsigned ix = imports.length (); ix--;)
19302 : : {
19303 : 455 : const macro_import::slot &slot = imports[ix];
19304 : 455 : unsigned mod = slot.get_module ();
19305 : 455 : if (bitmap_bit_p (visible, mod))
19306 : : {
19307 : 443 : macro_export *pushed = NULL;
19308 : 443 : if (mod)
19309 : : {
19310 : 322 : const module_state *imp = (*modules)[mod];
19311 : 322 : bytes_in &sec = imp->slurp->macro_defs;
19312 : 322 : if (!sec.get_overrun ())
19313 : : {
19314 : 322 : dump (dumper::MACRO)
19315 : 6 : && dump ("Reading macro %s%s%s %I module %M at %u",
19316 : 6 : slot.get_defness () & macro_import::slot::L_UNDEF
19317 : : ? "#undef" : "",
19318 : 6 : slot.get_defness () == macro_import::slot::L_BOTH
19319 : : ? " & " : "",
19320 : 6 : slot.get_defness () & macro_import::slot::L_DEF
19321 : : ? "#define" : "",
19322 : 6 : identifier (node), imp, slot.offset);
19323 : 322 : sec.random_access (slot.offset);
19324 : :
19325 : 322 : macro_export exp;
19326 : 322 : if (slot.get_defness () & macro_import::slot::L_UNDEF)
19327 : 12 : exp.undef_loc = imp->read_location (sec);
19328 : 322 : if (slot.get_defness () & macro_import::slot::L_DEF)
19329 : 316 : exp.def = imp->read_define (sec, reader);
19330 : 322 : if (sec.get_overrun ())
19331 : 0 : error_at (loc, "macro definitions of %qE corrupted",
19332 : 0 : imp->name);
19333 : : else
19334 : 322 : pushed = defs.quick_push (exp);
19335 : : }
19336 : : }
19337 : : else
19338 : 121 : pushed = defs.quick_push ((*macro_exports)[slot.offset]);
19339 : 443 : if (pushed && pushed->def)
19340 : : {
19341 : 437 : if (!def)
19342 : : def = pushed->def;
19343 : 148 : else if (cpp_compare_macros (def, pushed->def))
19344 : 455 : failed = true;
19345 : : }
19346 : : }
19347 : : }
19348 : :
19349 : 292 : if (failed)
19350 : : {
19351 : : /* If LOC is the first loc, this is the end of file check, which
19352 : : is a warning. */
19353 : 15 : auto_diagnostic_group d;
19354 : 15 : if (loc == MAP_START_LOCATION (LINEMAPS_ORDINARY_MAP_AT (line_table, 0)))
19355 : 9 : warning_at (loc, OPT_Winvalid_imported_macros,
19356 : : "inconsistent imported macro definition %qE",
19357 : : identifier (node));
19358 : : else
19359 : 6 : error_at (loc, "inconsistent imported macro definition %qE",
19360 : : identifier (node));
19361 : 60 : for (unsigned ix = defs.length (); ix--;)
19362 : : {
19363 : 30 : macro_export &exp = defs[ix];
19364 : 30 : if (exp.undef_loc)
19365 : 0 : inform (exp.undef_loc, "%<#undef %E%>", identifier (node));
19366 : 30 : if (exp.def)
19367 : 30 : inform (exp.def->line, "%<#define %s%>",
19368 : : cpp_macro_definition (reader, node, exp.def));
19369 : : }
19370 : 15 : def = NULL;
19371 : 15 : }
19372 : :
19373 : 292 : defs.release ();
19374 : :
19375 : 292 : dump.pop (n);
19376 : :
19377 : 292 : return def;
19378 : : }
19379 : :
19380 : : /* Stream the static aggregates. Sadly some headers (ahem:
19381 : : iostream) contain static vars, and rely on them to run global
19382 : : ctors. */
19383 : : unsigned
19384 : 851 : module_state::write_inits (elf_out *to, depset::hash &table, unsigned *crc_ptr)
19385 : : {
19386 : 851 : if (!static_aggregates && !tls_aggregates)
19387 : : return 0;
19388 : :
19389 : 45 : dump () && dump ("Writing initializers");
19390 : 45 : dump.indent ();
19391 : :
19392 : 45 : static_aggregates = nreverse (static_aggregates);
19393 : 45 : tls_aggregates = nreverse (tls_aggregates);
19394 : :
19395 : 45 : unsigned count = 0;
19396 : 45 : trees_out sec (to, this, table, ~0u);
19397 : 45 : sec.begin ();
19398 : :
19399 : 45 : tree list = static_aggregates;
19400 : 135 : for (int passes = 0; passes != 2; passes++)
19401 : : {
19402 : 258 : for (tree init = list; init; init = TREE_CHAIN (init))
19403 : 168 : if (TREE_LANG_FLAG_0 (init))
19404 : : {
19405 : 144 : if (STATIC_INIT_DECOMP_BASE_P (init))
19406 : : {
19407 : : /* Ensure that in the returned result chain if the
19408 : : STATIC_INIT_DECOMP_*BASE_P flags are set, there is
19409 : : always one or more STATIC_INIT_DECOMP_BASE_P TREE_LIST
19410 : : followed by one or more STATIC_INIT_DECOMP_NONBASE_P. */
19411 : 21 : int phase = 0;
19412 : 21 : tree last = NULL_TREE;
19413 : 21 : for (tree init2 = TREE_CHAIN (init);
19414 : 126 : init2; init2 = TREE_CHAIN (init2))
19415 : : {
19416 : 147 : if (phase == 0 && STATIC_INIT_DECOMP_BASE_P (init2))
19417 : : ;
19418 : 126 : else if (phase == 0
19419 : 147 : && STATIC_INIT_DECOMP_NONBASE_P (init2))
19420 : : {
19421 : 120 : phase = TREE_LANG_FLAG_0 (init2) ? 2 : 1;
19422 : : last = init2;
19423 : : }
19424 : 105 : else if (IN_RANGE (phase, 1, 2)
19425 : 210 : && STATIC_INIT_DECOMP_NONBASE_P (init2))
19426 : : {
19427 : 84 : if (TREE_LANG_FLAG_0 (init2))
19428 : 81 : phase = 2;
19429 : : last = init2;
19430 : : }
19431 : : else
19432 : : break;
19433 : : }
19434 : 21 : if (phase == 2)
19435 : : {
19436 : : /* In that case, add markers about it so that the
19437 : : STATIC_INIT_DECOMP_BASE_P and
19438 : : STATIC_INIT_DECOMP_NONBASE_P flags can be restored. */
19439 : 21 : sec.tree_node (build_int_cst (integer_type_node,
19440 : 21 : 2 * passes + 1));
19441 : 21 : phase = 1;
19442 : 147 : for (tree init2 = init; init2 != TREE_CHAIN (last);
19443 : 126 : init2 = TREE_CHAIN (init2))
19444 : 126 : if (TREE_LANG_FLAG_0 (init2))
19445 : : {
19446 : 102 : tree decl = TREE_VALUE (init2);
19447 : 102 : if (phase == 1
19448 : 102 : && STATIC_INIT_DECOMP_NONBASE_P (init2))
19449 : : {
19450 : 21 : sec.tree_node (build_int_cst (integer_type_node,
19451 : 21 : 2 * passes + 2));
19452 : 21 : phase = 2;
19453 : : }
19454 : 102 : dump ("Initializer:%u for %N", count, decl);
19455 : 102 : sec.tree_node (decl);
19456 : 102 : ++count;
19457 : : }
19458 : 21 : sec.tree_node (integer_zero_node);
19459 : 21 : init = last;
19460 : 21 : continue;
19461 : 21 : }
19462 : : }
19463 : :
19464 : 123 : tree decl = TREE_VALUE (init);
19465 : :
19466 : 123 : dump ("Initializer:%u for %N", count, decl);
19467 : 123 : sec.tree_node (decl);
19468 : 123 : ++count;
19469 : : }
19470 : :
19471 : 90 : list = tls_aggregates;
19472 : : }
19473 : :
19474 : 45 : sec.end (to, to->name (MOD_SNAME_PFX ".ini"), crc_ptr);
19475 : 45 : dump.outdent ();
19476 : :
19477 : 45 : return count;
19478 : 45 : }
19479 : :
19480 : : /* We have to defer some post-load processing until we've completed
19481 : : reading, because they can cause more reading. */
19482 : :
19483 : : static void
19484 : 7787 : post_load_processing ()
19485 : : {
19486 : : /* We mustn't cause a GC, our caller should have arranged for that
19487 : : not to happen. */
19488 : 7787 : gcc_checking_assert (function_depth);
19489 : :
19490 : 7787 : if (!post_load_decls)
19491 : : return;
19492 : :
19493 : 3933 : tree old_cfd = current_function_decl;
19494 : 3933 : struct function *old_cfun = cfun;
19495 : 9325 : while (post_load_decls->length ())
19496 : : {
19497 : 5392 : tree decl = post_load_decls->pop ();
19498 : :
19499 : 5437 : dump () && dump ("Post-load processing of %N", decl);
19500 : :
19501 : 5392 : gcc_checking_assert (DECL_MAYBE_IN_CHARGE_CDTOR_P (decl));
19502 : 5392 : expand_or_defer_fn (decl);
19503 : : /* As in module_state::read_cluster. */
19504 : 528 : if (at_eof && DECL_COMDAT (decl) && DECL_EXTERNAL (decl)
19505 : 5422 : && DECL_NOT_REALLY_EXTERN (decl))
19506 : 8 : DECL_EXTERNAL (decl) = false;
19507 : : }
19508 : :
19509 : 3933 : cfun = old_cfun;
19510 : 3933 : current_function_decl = old_cfd;
19511 : : }
19512 : :
19513 : : bool
19514 : 45 : module_state::read_inits (unsigned count)
19515 : : {
19516 : 45 : trees_in sec (this);
19517 : 45 : if (!sec.begin (loc, from (), from ()->find (MOD_SNAME_PFX ".ini")))
19518 : : return false;
19519 : 57 : dump () && dump ("Reading %u initializers", count);
19520 : 45 : dump.indent ();
19521 : :
19522 : 45 : lazy_snum = ~0u;
19523 : 45 : int decomp_phase = 0;
19524 : 45 : tree *aggrp = NULL;
19525 : 270 : for (unsigned ix = 0; ix != count; ix++)
19526 : : {
19527 : 225 : tree last = NULL_TREE;
19528 : 225 : if (decomp_phase)
19529 : 102 : last = *aggrp;
19530 : : /* Merely referencing the decl causes its initializer to be read
19531 : : and added to the correct list. */
19532 : 225 : tree decl = sec.tree_node ();
19533 : : /* module_state::write_inits can add special INTEGER_CST markers in
19534 : : between the decls. 1 means STATIC_INIT_DECOMP_BASE_P entries
19535 : : follow in static_aggregates, 2 means STATIC_INIT_DECOMP_NONBASE_P
19536 : : entries follow in static_aggregates, 3 means
19537 : : STATIC_INIT_DECOMP_BASE_P entries follow in tls_aggregates,
19538 : : 4 means STATIC_INIT_DECOMP_NONBASE_P follow in tls_aggregates,
19539 : : 0 means end of STATIC_INIT_DECOMP_{,NON}BASE_P sequence. */
19540 : 225 : if (tree_fits_shwi_p (decl))
19541 : : {
19542 : 63 : if (sec.get_overrun ())
19543 : : break;
19544 : 63 : decomp_phase = tree_to_shwi (decl);
19545 : 63 : if (decomp_phase)
19546 : : {
19547 : 42 : aggrp = decomp_phase > 2 ? &tls_aggregates : &static_aggregates;
19548 : : last = *aggrp;
19549 : : }
19550 : 63 : decl = sec.tree_node ();
19551 : : }
19552 : :
19553 : 225 : if (sec.get_overrun ())
19554 : : break;
19555 : 225 : if (decl)
19556 : 225 : dump ("Initializer:%u for %N", ix, decl);
19557 : 225 : if (decomp_phase)
19558 : : {
19559 : 102 : tree init = *aggrp;
19560 : 102 : gcc_assert (TREE_VALUE (init) == decl && TREE_CHAIN (init) == last);
19561 : 102 : if ((decomp_phase & 1) != 0)
19562 : 21 : STATIC_INIT_DECOMP_BASE_P (init) = 1;
19563 : : else
19564 : 81 : STATIC_INIT_DECOMP_NONBASE_P (init) = 1;
19565 : : }
19566 : : }
19567 : 45 : if (decomp_phase && !sec.get_overrun ())
19568 : : {
19569 : 0 : tree decl = sec.tree_node ();
19570 : 0 : gcc_assert (integer_zerop (decl));
19571 : : }
19572 : 45 : lazy_snum = 0;
19573 : 45 : post_load_processing ();
19574 : 45 : dump.outdent ();
19575 : 45 : if (!sec.end (from ()))
19576 : : return false;
19577 : : return true;
19578 : 45 : }
19579 : :
19580 : : void
19581 : 2381 : module_state::write_counts (elf_out *to, unsigned counts[MSC_HWM],
19582 : : unsigned *crc_ptr)
19583 : : {
19584 : 2381 : bytes_out cfg (to);
19585 : :
19586 : 2381 : cfg.begin ();
19587 : :
19588 : 21429 : for (unsigned ix = MSC_HWM; ix--;)
19589 : 19048 : cfg.u (counts[ix]);
19590 : :
19591 : 2381 : if (dump ())
19592 : : {
19593 : 251 : dump ("Cluster sections are [%u,%u)",
19594 : : counts[MSC_sec_lwm], counts[MSC_sec_hwm]);
19595 : 251 : dump ("Bindings %u", counts[MSC_bindings]);
19596 : 251 : dump ("Pendings %u", counts[MSC_pendings]);
19597 : 251 : dump ("Entities %u", counts[MSC_entities]);
19598 : 251 : dump ("Namespaces %u", counts[MSC_namespaces]);
19599 : 251 : dump ("Macros %u", counts[MSC_macros]);
19600 : 251 : dump ("Initializers %u", counts[MSC_inits]);
19601 : : }
19602 : :
19603 : 2381 : cfg.end (to, to->name (MOD_SNAME_PFX ".cnt"), crc_ptr);
19604 : 2381 : }
19605 : :
19606 : : bool
19607 : 2548 : module_state::read_counts (unsigned counts[MSC_HWM])
19608 : : {
19609 : 2548 : bytes_in cfg;
19610 : :
19611 : 2548 : if (!cfg.begin (loc, from (), MOD_SNAME_PFX ".cnt"))
19612 : : return false;
19613 : :
19614 : 22932 : for (unsigned ix = MSC_HWM; ix--;)
19615 : 20384 : counts[ix] = cfg.u ();
19616 : :
19617 : 2548 : if (dump ())
19618 : : {
19619 : 471 : dump ("Declaration sections are [%u,%u)",
19620 : : counts[MSC_sec_lwm], counts[MSC_sec_hwm]);
19621 : 471 : dump ("Bindings %u", counts[MSC_bindings]);
19622 : 471 : dump ("Pendings %u", counts[MSC_pendings]);
19623 : 471 : dump ("Entities %u", counts[MSC_entities]);
19624 : 471 : dump ("Namespaces %u", counts[MSC_namespaces]);
19625 : 471 : dump ("Macros %u", counts[MSC_macros]);
19626 : 471 : dump ("Initializers %u", counts[MSC_inits]);
19627 : : }
19628 : :
19629 : 2548 : return cfg.end (from ());
19630 : 2548 : }
19631 : :
19632 : : /* Tool configuration: MOD_SNAME_PFX .config
19633 : :
19634 : : This is data that confirms current state (or fails). */
19635 : :
19636 : : void
19637 : 2381 : module_state::write_config (elf_out *to, module_state_config &config,
19638 : : unsigned inner_crc)
19639 : : {
19640 : 2381 : bytes_out cfg (to);
19641 : :
19642 : 2381 : cfg.begin ();
19643 : :
19644 : : /* Write version and inner crc as u32 values, for easier
19645 : : debug inspection. */
19646 : 2632 : dump () && dump ("Writing version=%V, inner_crc=%x",
19647 : : MODULE_VERSION, inner_crc);
19648 : 2381 : cfg.u32 (unsigned (MODULE_VERSION));
19649 : 2381 : cfg.u32 (inner_crc);
19650 : :
19651 : 2381 : cfg.u (to->name (is_header () ? "" : get_flatname ()));
19652 : :
19653 : : /* Configuration. */
19654 : 2632 : dump () && dump ("Writing target='%s', host='%s'",
19655 : : TARGET_MACHINE, HOST_MACHINE);
19656 : 2381 : unsigned target = to->name (TARGET_MACHINE);
19657 : 2381 : unsigned host = (!strcmp (TARGET_MACHINE, HOST_MACHINE)
19658 : : ? target : to->name (HOST_MACHINE));
19659 : 2381 : cfg.u (target);
19660 : 2381 : cfg.u (host);
19661 : :
19662 : 2381 : cfg.str (config.dialect_str);
19663 : 2381 : cfg.u (extensions);
19664 : :
19665 : : /* Global tree information. We write the globals crc separately,
19666 : : rather than mix it directly into the overall crc, as it is used
19667 : : to ensure data match between instances of the compiler, not
19668 : : integrity of the file. */
19669 : 2632 : dump () && dump ("Writing globals=%u, crc=%x",
19670 : : fixed_trees->length (), global_crc);
19671 : 2381 : cfg.u (fixed_trees->length ());
19672 : 2381 : cfg.u32 (global_crc);
19673 : :
19674 : 2381 : if (is_partition ())
19675 : 159 : cfg.u (is_interface ());
19676 : :
19677 : 2381 : cfg.u (config.num_imports);
19678 : 2381 : cfg.u (config.num_partitions);
19679 : 2381 : cfg.u (config.num_entities);
19680 : :
19681 : 2381 : cfg.loc (config.ordinary_locs);
19682 : 2381 : cfg.loc (config.macro_locs);
19683 : 2381 : cfg.u (config.loc_range_bits);
19684 : :
19685 : 2381 : cfg.u (config.active_init);
19686 : :
19687 : : /* Now generate CRC, we'll have incorporated the inner CRC because
19688 : : of its serialization above. */
19689 : 2381 : cfg.end (to, to->name (MOD_SNAME_PFX ".cfg"), &crc);
19690 : 2632 : dump () && dump ("Writing CRC=%x", crc);
19691 : 2381 : }
19692 : :
19693 : : void
19694 : 37 : module_state::note_cmi_name ()
19695 : : {
19696 : 37 : if (!cmi_noted_p && filename)
19697 : : {
19698 : 37 : cmi_noted_p = true;
19699 : 37 : inform (loc, "compiled module file is %qs",
19700 : : maybe_add_cmi_prefix (filename));
19701 : : }
19702 : 37 : }
19703 : :
19704 : : bool
19705 : 2612 : module_state::read_config (module_state_config &config)
19706 : : {
19707 : 2612 : bytes_in cfg;
19708 : :
19709 : 2612 : if (!cfg.begin (loc, from (), MOD_SNAME_PFX ".cfg"))
19710 : : return false;
19711 : :
19712 : : /* Check version. */
19713 : 2612 : unsigned my_ver = MODULE_VERSION;
19714 : 2612 : unsigned their_ver = cfg.u32 ();
19715 : 3083 : dump () && dump (my_ver == their_ver ? "Version %V"
19716 : : : "Expecting %V found %V", my_ver, their_ver);
19717 : 2612 : if (their_ver != my_ver)
19718 : : {
19719 : : /* The compiler versions differ. Close enough? */
19720 : 0 : verstr_t my_string, their_string;
19721 : :
19722 : 0 : version2string (my_ver, my_string);
19723 : 0 : version2string (their_ver, their_string);
19724 : :
19725 : : /* Reject when either is non-experimental or when experimental
19726 : : major versions differ. */
19727 : 0 : auto_diagnostic_group d;
19728 : 0 : bool reject_p = ((!IS_EXPERIMENTAL (my_ver)
19729 : : || !IS_EXPERIMENTAL (their_ver)
19730 : 0 : || MODULE_MAJOR (my_ver) != MODULE_MAJOR (their_ver))
19731 : : /* The 'I know what I'm doing' switch. */
19732 : 0 : && !flag_module_version_ignore);
19733 : 0 : bool inform_p = true;
19734 : 0 : if (reject_p)
19735 : : {
19736 : 0 : cfg.set_overrun ();
19737 : 0 : error_at (loc, "compiled module is %sversion %s",
19738 : : IS_EXPERIMENTAL (their_ver) ? "experimental " : "",
19739 : : their_string);
19740 : : }
19741 : : else
19742 : 0 : inform_p = warning_at (loc, 0, "compiled module is %sversion %s",
19743 : : IS_EXPERIMENTAL (their_ver) ? "experimental " : "",
19744 : : their_string);
19745 : :
19746 : 0 : if (inform_p)
19747 : : {
19748 : 0 : inform (loc, "compiler is %sversion %s%s%s",
19749 : : IS_EXPERIMENTAL (my_ver) ? "experimental " : "",
19750 : : my_string,
19751 : 0 : reject_p ? "" : flag_module_version_ignore
19752 : 0 : ? ", be it on your own head!" : ", close enough?",
19753 : : reject_p ? "" : " \xc2\xaf\\_(\xe3\x83\x84)_/\xc2\xaf");
19754 : 0 : note_cmi_name ();
19755 : : }
19756 : :
19757 : 0 : if (reject_p)
19758 : 0 : goto done;
19759 : 0 : }
19760 : :
19761 : : /* We wrote the inner crc merely to merge it, so simply read it
19762 : : back and forget it. */
19763 : 2612 : cfg.u32 ();
19764 : :
19765 : : /* Check module name. */
19766 : 2612 : {
19767 : 2612 : const char *their_name = from ()->name (cfg.u ());
19768 : 2612 : const char *our_name = "";
19769 : :
19770 : 2612 : if (!is_header ())
19771 : 1674 : our_name = get_flatname ();
19772 : :
19773 : : /* Header units can be aliased, so name checking is
19774 : : inappropriate. */
19775 : 2612 : if (0 != strcmp (their_name, our_name))
19776 : : {
19777 : 0 : error_at (loc,
19778 : 0 : their_name[0] && our_name[0] ? G_("module %qs found")
19779 : : : their_name[0]
19780 : : ? G_("header module expected, module %qs found")
19781 : : : G_("module %qs expected, header module found"),
19782 : 0 : their_name[0] ? their_name : our_name);
19783 : 0 : cfg.set_overrun ();
19784 : 0 : goto done;
19785 : : }
19786 : : }
19787 : :
19788 : : /* Check the CRC after the above sanity checks, so that the user is
19789 : : clued in. */
19790 : 2612 : {
19791 : 2612 : unsigned e_crc = crc;
19792 : 2612 : crc = cfg.get_crc ();
19793 : 3083 : dump () && dump ("Reading CRC=%x", crc);
19794 : 2612 : if (!is_direct () && crc != e_crc)
19795 : : {
19796 : 3 : error_at (loc, "module %qs CRC mismatch", get_flatname ());
19797 : 3 : cfg.set_overrun ();
19798 : 3 : goto done;
19799 : : }
19800 : : }
19801 : :
19802 : : /* Check target & host. */
19803 : 2609 : {
19804 : 2609 : const char *their_target = from ()->name (cfg.u ());
19805 : 2609 : const char *their_host = from ()->name (cfg.u ());
19806 : 3080 : dump () && dump ("Read target='%s', host='%s'", their_target, their_host);
19807 : 2609 : if (strcmp (their_target, TARGET_MACHINE)
19808 : 2609 : || strcmp (their_host, HOST_MACHINE))
19809 : : {
19810 : 0 : error_at (loc, "target & host is %qs:%qs, expected %qs:%qs",
19811 : : their_target, TARGET_MACHINE, their_host, HOST_MACHINE);
19812 : 0 : cfg.set_overrun ();
19813 : 0 : goto done;
19814 : : }
19815 : : }
19816 : :
19817 : : /* Check compilation dialect. This must match. */
19818 : 2609 : {
19819 : 2609 : const char *their_dialect = cfg.str ();
19820 : 2609 : if (strcmp (their_dialect, config.dialect_str))
19821 : : {
19822 : 1 : error_at (loc, "language dialect differs %qs, expected %qs",
19823 : : their_dialect, config.dialect_str);
19824 : 1 : cfg.set_overrun ();
19825 : 1 : goto done;
19826 : : }
19827 : : }
19828 : :
19829 : : /* Check for extensions. If they set any, we must have them set
19830 : : too. */
19831 : 2608 : {
19832 : 2608 : unsigned ext = cfg.u ();
19833 : 2608 : unsigned allowed = (flag_openmp ? SE_OPENMP | SE_OPENMP_SIMD : 0);
19834 : 2608 : if (flag_openmp_simd)
19835 : 3 : allowed |= SE_OPENMP_SIMD;
19836 : 2608 : if (flag_openacc)
19837 : 3 : allowed |= SE_OPENACC;
19838 : :
19839 : 2608 : if (unsigned bad = ext & ~allowed)
19840 : : {
19841 : 9 : if (bad & SE_OPENMP)
19842 : 3 : error_at (loc, "module contains OpenMP, use %<-fopenmp%> to enable");
19843 : 6 : else if (bad & SE_OPENMP_SIMD)
19844 : 3 : error_at (loc, "module contains OpenMP, use %<-fopenmp%> or "
19845 : : "%<-fopenmp-simd%> to enable");
19846 : 9 : if (bad & SE_OPENACC)
19847 : 3 : error_at (loc, "module contains OpenACC, use %<-fopenacc%> to "
19848 : : "enable");
19849 : 9 : cfg.set_overrun ();
19850 : 9 : goto done;
19851 : : }
19852 : 2599 : extensions = ext;
19853 : : }
19854 : :
19855 : : /* Check global trees. */
19856 : 2599 : {
19857 : 2599 : unsigned their_fixed_length = cfg.u ();
19858 : 2599 : unsigned their_fixed_crc = cfg.u32 ();
19859 : 3070 : dump () && dump ("Read globals=%u, crc=%x",
19860 : : their_fixed_length, their_fixed_crc);
19861 : 2599 : if (!flag_preprocess_only
19862 : 2599 : && (their_fixed_length != fixed_trees->length ()
19863 : 2560 : || their_fixed_crc != global_crc))
19864 : : {
19865 : 0 : error_at (loc, "fixed tree mismatch");
19866 : 0 : cfg.set_overrun ();
19867 : 0 : goto done;
19868 : : }
19869 : : }
19870 : :
19871 : : /* All non-partitions are interfaces. */
19872 : 2599 : interface_p = !is_partition () || cfg.u ();
19873 : :
19874 : 2599 : config.num_imports = cfg.u ();
19875 : 2599 : config.num_partitions = cfg.u ();
19876 : 2599 : config.num_entities = cfg.u ();
19877 : :
19878 : 2599 : config.ordinary_locs = cfg.loc ();
19879 : 2599 : config.macro_locs = cfg.loc ();
19880 : 2599 : config.loc_range_bits = cfg.u ();
19881 : :
19882 : 2599 : config.active_init = cfg.u ();
19883 : :
19884 : 2612 : done:
19885 : 2612 : return cfg.end (from ());
19886 : 2612 : }
19887 : :
19888 : : /* Comparator for ordering the Ordered Ordinary Location array. */
19889 : :
19890 : : static int
19891 : 96 : ool_cmp (const void *a_, const void *b_)
19892 : : {
19893 : 96 : auto *a = *static_cast<const module_state *const *> (a_);
19894 : 96 : auto *b = *static_cast<const module_state *const *> (b_);
19895 : 96 : if (a == b)
19896 : : return 0;
19897 : 96 : else if (a->ordinary_locs.first < b->ordinary_locs.first)
19898 : : return -1;
19899 : : else
19900 : 38 : return +1;
19901 : : }
19902 : :
19903 : : /* Use ELROND format to record the following sections:
19904 : : qualified-names : binding value(s)
19905 : : MOD_SNAME_PFX.README : human readable, strings
19906 : : MOD_SNAME_PFX.ENV : environment strings, strings
19907 : : MOD_SNAME_PFX.nms : namespace hierarchy
19908 : : MOD_SNAME_PFX.bnd : binding table
19909 : : MOD_SNAME_PFX.spc : specialization table
19910 : : MOD_SNAME_PFX.imp : import table
19911 : : MOD_SNAME_PFX.ent : entity table
19912 : : MOD_SNAME_PFX.prt : partitions table
19913 : : MOD_SNAME_PFX.olm : ordinary line maps
19914 : : MOD_SNAME_PFX.mlm : macro line maps
19915 : : MOD_SNAME_PFX.def : macro definitions
19916 : : MOD_SNAME_PFX.mac : macro index
19917 : : MOD_SNAME_PFX.ini : inits
19918 : : MOD_SNAME_PFX.cnt : counts
19919 : : MOD_SNAME_PFX.cfg : config data
19920 : : */
19921 : :
19922 : : bool
19923 : 2409 : module_state::write_begin (elf_out *to, cpp_reader *reader,
19924 : : module_state_config &config, unsigned &crc)
19925 : : {
19926 : : /* Figure out remapped module numbers, which might elide
19927 : : partitions. */
19928 : 2409 : bitmap partitions = NULL;
19929 : 2409 : if (!is_header () && !is_partition ())
19930 : 1399 : partitions = BITMAP_GGC_ALLOC ();
19931 : 2409 : write_init_maps ();
19932 : :
19933 : 2409 : unsigned mod_hwm = 1;
19934 : 2972 : for (unsigned ix = 1; ix != modules->length (); ix++)
19935 : : {
19936 : 563 : module_state *imp = (*modules)[ix];
19937 : :
19938 : : /* Promote any non-partition direct import from a partition, unless
19939 : : we're a partition. */
19940 : 521 : if (!is_partition () && !imp->is_partition ()
19941 : 925 : && imp->is_partition_direct ())
19942 : 9 : imp->directness = MD_PURVIEW_DIRECT;
19943 : :
19944 : : /* Write any import that is not a partition, unless we're a
19945 : : partition. */
19946 : 563 : if (!partitions || !imp->is_partition ())
19947 : 404 : imp->remap = mod_hwm++;
19948 : : else
19949 : : {
19950 : 189 : dump () && dump ("Partition %M %u", imp, ix);
19951 : 159 : bitmap_set_bit (partitions, ix);
19952 : 159 : imp->remap = 0;
19953 : : /* All interface partitions must be exported. */
19954 : 159 : if (imp->is_interface () && !bitmap_bit_p (exports, imp->mod))
19955 : : {
19956 : 3 : error_at (imp->loc, "interface partition is not exported");
19957 : 3 : bitmap_set_bit (exports, imp->mod);
19958 : : }
19959 : :
19960 : : /* All the partition entities should have been loaded when
19961 : : loading the partition. */
19962 : : if (CHECKING_P)
19963 : 879 : for (unsigned jx = 0; jx != imp->entity_num; jx++)
19964 : : {
19965 : 720 : binding_slot *slot = &(*entity_ary)[imp->entity_lwm + jx];
19966 : 720 : gcc_checking_assert (!slot->is_lazy ());
19967 : : }
19968 : : }
19969 : :
19970 : 563 : if (imp->is_direct () && (imp->remap || imp->is_partition ()))
19971 : 549 : note_location (imp->imported_from ());
19972 : : }
19973 : :
19974 : 2409 : if (partitions && bitmap_empty_p (partitions))
19975 : : /* No partitions present. */
19976 : : partitions = nullptr;
19977 : :
19978 : : /* Find the set of decls we must write out. */
19979 : 2409 : depset::hash table (DECL_NAMESPACE_BINDINGS (global_namespace)->size () * 8);
19980 : : /* Add the specializations before the writables, so that we can
19981 : : detect injected friend specializations. */
19982 : 2409 : table.add_specializations (true);
19983 : 2409 : table.add_specializations (false);
19984 : 2409 : if (partial_specializations)
19985 : : {
19986 : 194 : table.add_partial_entities (partial_specializations);
19987 : 194 : partial_specializations = NULL;
19988 : : }
19989 : 2409 : table.add_namespace_entities (global_namespace, partitions);
19990 : 2409 : if (class_members)
19991 : : {
19992 : 12 : table.add_class_entities (class_members);
19993 : 12 : class_members = NULL;
19994 : : }
19995 : :
19996 : : /* Now join everything up. */
19997 : 2409 : table.find_dependencies (this);
19998 : :
19999 : 2409 : if (!table.finalize_dependencies ())
20000 : : return false;
20001 : :
20002 : : #if CHECKING_P
20003 : : /* We're done verifying at-most once reading, reset to verify
20004 : : at-most once writing. */
20005 : 2381 : note_defs = note_defs_table_t::create_ggc (1000);
20006 : : #endif
20007 : :
20008 : : /* Determine Strongly Connected Components. This will also strip any
20009 : : unnecessary dependencies on imported or TU-local entities. */
20010 : 2381 : vec<depset *> sccs = table.connect ();
20011 : :
20012 : 2381 : vec_alloc (ool, modules->length ());
20013 : 2941 : for (unsigned ix = modules->length (); --ix;)
20014 : : {
20015 : 560 : auto *import = (*modules)[ix];
20016 : 560 : if (import->loadedness > ML_NONE
20017 : 560 : && !(partitions && bitmap_bit_p (partitions, import->mod)))
20018 : 401 : ool->quick_push (import);
20019 : : }
20020 : 2381 : ool->qsort (ool_cmp);
20021 : :
20022 : 2381 : write_diagnostic_classification (nullptr, global_dc, nullptr);
20023 : :
20024 : 2381 : vec<cpp_hashnode *> *macros = nullptr;
20025 : 2381 : if (is_header ())
20026 : 851 : macros = prepare_macros (reader);
20027 : :
20028 : 2381 : config.num_imports = mod_hwm;
20029 : 2381 : config.num_partitions = modules->length () - mod_hwm;
20030 : 2381 : auto map_info = write_prepare_maps (&config, bool (config.num_partitions));
20031 : 2381 : unsigned counts[MSC_HWM];
20032 : 2381 : memset (counts, 0, sizeof (counts));
20033 : :
20034 : : /* depset::cluster is the cluster number,
20035 : : depset::section is unspecified scratch value.
20036 : :
20037 : : The following loops make use of the tarjan property that
20038 : : dependencies will be earlier in the SCCS array. */
20039 : :
20040 : : /* This first loop determines the number of depsets in each SCC, and
20041 : : also the number of namespaces we're dealing with. During the
20042 : : loop, the meaning of a couple of depset fields now change:
20043 : :
20044 : : depset::cluster -> size_of cluster, if first of cluster & !namespace
20045 : : depset::section -> section number of cluster (if !namespace). */
20046 : :
20047 : 2381 : unsigned n_spaces = 0;
20048 : 2381 : counts[MSC_sec_lwm] = counts[MSC_sec_hwm] = to->get_section_limit ();
20049 : 227772 : for (unsigned size, ix = 0; ix < sccs.length (); ix += size)
20050 : : {
20051 : 225391 : depset **base = &sccs[ix];
20052 : :
20053 : 425927 : if (base[0]->get_entity_kind () == depset::EK_NAMESPACE)
20054 : : {
20055 : 4065 : n_spaces++;
20056 : 4065 : size = 1;
20057 : : }
20058 : : else
20059 : : {
20060 : : /* Count the members in this cluster. */
20061 : 893830 : for (size = 1; ix + size < sccs.length (); size++)
20062 : 891743 : if (base[size]->cluster != base[0]->cluster)
20063 : : break;
20064 : :
20065 : 1115156 : for (unsigned jx = 0; jx != size; jx++)
20066 : : {
20067 : : /* Set the section number. */
20068 : 893830 : base[jx]->cluster = ~(~0u >> 1); /* A bad value. */
20069 : 893830 : base[jx]->section = counts[MSC_sec_hwm];
20070 : : }
20071 : :
20072 : : /* Save the size in the first member's cluster slot. */
20073 : 221326 : base[0]->cluster = size;
20074 : :
20075 : 221326 : counts[MSC_sec_hwm]++;
20076 : : }
20077 : : }
20078 : :
20079 : : /* Write the clusters. Namespace decls are put in the spaces array.
20080 : : The meaning of depset::cluster changes to provide the
20081 : : unnamed-decl count of the depset's decl (and remains zero for
20082 : : non-decls and non-unnamed). */
20083 : 2381 : unsigned bytes = 0;
20084 : 2381 : vec<depset *> spaces;
20085 : 2381 : spaces.create (n_spaces);
20086 : :
20087 : 227772 : for (unsigned size, ix = 0; ix < sccs.length (); ix += size)
20088 : : {
20089 : 225391 : depset **base = &sccs[ix];
20090 : :
20091 : 225391 : if (base[0]->get_entity_kind () == depset::EK_NAMESPACE)
20092 : : {
20093 : 4065 : tree decl = base[0]->get_entity ();
20094 : 4065 : if (decl == global_namespace)
20095 : 2142 : base[0]->cluster = 0;
20096 : 1923 : else if (!base[0]->is_import ())
20097 : : {
20098 : 1923 : base[0]->cluster = counts[MSC_entities]++;
20099 : 1923 : spaces.quick_push (base[0]);
20100 : 1923 : counts[MSC_namespaces]++;
20101 : 1923 : if (CHECKING_P)
20102 : : {
20103 : : /* Add it to the entity map, such that we can tell it is
20104 : : part of us. */
20105 : 1923 : bool existed;
20106 : 1923 : unsigned *slot = &entity_map->get_or_insert
20107 : 1923 : (DECL_UID (decl), &existed);
20108 : 1923 : if (existed)
20109 : : /* It must have come from a partition. */
20110 : 0 : gcc_checking_assert
20111 : : (import_entity_module (*slot)->is_partition ());
20112 : 1923 : *slot = ~base[0]->cluster;
20113 : : }
20114 : 227344 : dump (dumper::CLUSTER) && dump ("Cluster namespace %N", decl);
20115 : : }
20116 : : size = 1;
20117 : : }
20118 : : else
20119 : : {
20120 : 221326 : size = base[0]->cluster;
20121 : :
20122 : : /* Cluster is now used to number entities. */
20123 : 221326 : base[0]->cluster = ~(~0u >> 1); /* A bad value. */
20124 : :
20125 : 221326 : sort_cluster (&table, base, size);
20126 : :
20127 : : /* Record the section for consistency checking during stream
20128 : : out -- we don't want to start writing decls in different
20129 : : sections. */
20130 : 221326 : table.section = base[0]->section;
20131 : 221326 : bytes += write_cluster (to, base, size, table, counts, &crc);
20132 : 221326 : table.section = 0;
20133 : : }
20134 : : }
20135 : :
20136 : : /* depset::cluster - entity number (on entities)
20137 : : depset::section - cluster number */
20138 : : /* We'd better have written as many sections and found as many
20139 : : namespaces as we predicted. */
20140 : 4762 : gcc_assert (counts[MSC_sec_hwm] == to->get_section_limit ()
20141 : : && spaces.length () == counts[MSC_namespaces]);
20142 : :
20143 : : /* Write the entitites. None happens if we contain namespaces or
20144 : : nothing. */
20145 : 2381 : config.num_entities = counts[MSC_entities];
20146 : 2381 : if (counts[MSC_entities])
20147 : 2139 : write_entities (to, sccs, counts[MSC_entities], &crc);
20148 : :
20149 : : /* Write the namespaces. */
20150 : 2381 : if (counts[MSC_namespaces])
20151 : 470 : write_namespaces (to, spaces, counts[MSC_namespaces], &crc);
20152 : :
20153 : : /* Write the bindings themselves. */
20154 : 2381 : counts[MSC_bindings] = write_bindings (to, sccs, &crc);
20155 : :
20156 : : /* Write the unnamed. */
20157 : 2381 : counts[MSC_pendings] = write_pendings (to, sccs, table, &crc);
20158 : :
20159 : : /* Write the import table. */
20160 : 2381 : if (config.num_imports > 1)
20161 : 377 : write_imports (to, &crc);
20162 : :
20163 : : /* Write elided partition table. */
20164 : 2381 : if (config.num_partitions)
20165 : 111 : write_partitions (to, config.num_partitions, &crc);
20166 : :
20167 : : /* Write the line maps. */
20168 : 2381 : if (config.ordinary_locs)
20169 : : {
20170 : 2285 : write_ordinary_maps (to, map_info, bool (config.num_partitions), &crc);
20171 : 2285 : write_diagnostic_classification (to, global_dc, &crc);
20172 : : }
20173 : 2381 : if (config.macro_locs)
20174 : 117 : write_macro_maps (to, map_info, &crc);
20175 : :
20176 : 2381 : if (is_header ())
20177 : : {
20178 : 851 : counts[MSC_macros] = write_macros (to, macros, &crc);
20179 : 851 : counts[MSC_inits] = write_inits (to, table, &crc);
20180 : 851 : vec_free (macros);
20181 : : }
20182 : :
20183 : 2381 : unsigned clusters = counts[MSC_sec_hwm] - counts[MSC_sec_lwm];
20184 : 2381 : dump () && dump ("Wrote %u clusters, average %u bytes/cluster",
20185 : 251 : clusters, (bytes + clusters / 2) / (clusters + !clusters));
20186 : 2381 : trees_out::instrument ();
20187 : :
20188 : 2381 : write_counts (to, counts, &crc);
20189 : :
20190 : 2381 : spaces.release ();
20191 : 2381 : sccs.release ();
20192 : :
20193 : 2381 : vec_free (macro_loc_remap);
20194 : 2381 : vec_free (ord_loc_remap);
20195 : 2381 : vec_free (ool);
20196 : :
20197 : : // FIXME:QOI: Have a command line switch to control more detailed
20198 : : // information (which might leak data you do not want to leak).
20199 : : // Perhaps (some of) the write_readme contents should also be
20200 : : // so-controlled.
20201 : 2381 : if (false)
20202 : : write_env (to);
20203 : :
20204 : 2381 : return true;
20205 : 2409 : }
20206 : :
20207 : : // Finish module writing after we've emitted all dynamic initializers.
20208 : :
20209 : : void
20210 : 2381 : module_state::write_end (elf_out *to, cpp_reader *reader,
20211 : : module_state_config &config, unsigned &crc)
20212 : : {
20213 : : /* And finish up. */
20214 : 2381 : write_config (to, config, crc);
20215 : :
20216 : : /* Human-readable info. */
20217 : 2381 : write_readme (to, reader, config.dialect_str);
20218 : :
20219 : 2632 : dump () && dump ("Wrote %u sections", to->get_section_limit ());
20220 : 2381 : }
20221 : :
20222 : : /* Initial read of a CMI. Checks config, loads up imports and line
20223 : : maps. */
20224 : :
20225 : : bool
20226 : 2612 : module_state::read_initial (cpp_reader *reader)
20227 : : {
20228 : 2612 : module_state_config config;
20229 : 2612 : bool ok = true;
20230 : :
20231 : 2612 : if (ok && !from ()->begin (loc))
20232 : : ok = false;
20233 : :
20234 : 2612 : if (ok && !read_config (config))
20235 : : ok = false;
20236 : :
20237 : 2599 : bool have_locs = ok && read_prepare_maps (&config);
20238 : :
20239 : : /* Ordinary maps before the imports. */
20240 : 2599 : if (!(have_locs && config.ordinary_locs))
20241 : 77 : ordinary_locs.first = line_table->highest_location + 1;
20242 : 2535 : else if (!read_ordinary_maps (config.ordinary_locs, config.loc_range_bits))
20243 : : ok = false;
20244 : :
20245 : 2599 : if (ok && have_locs && config.ordinary_locs
20246 : 5147 : && !read_diagnostic_classification (global_dc))
20247 : : ok = false;
20248 : :
20249 : : /* Allocate the REMAP vector. */
20250 : 2612 : slurp->alloc_remap (config.num_imports);
20251 : :
20252 : 2612 : if (ok)
20253 : : {
20254 : : /* Read the import table. Decrement current to stop this CMI
20255 : : from being evicted during the import. */
20256 : 2599 : slurp->current--;
20257 : 2599 : if (config.num_imports > 1 && !read_imports (reader, line_table))
20258 : : ok = false;
20259 : 2599 : slurp->current++;
20260 : : }
20261 : :
20262 : : /* Read the elided partition table, if we're the primary partition. */
20263 : 2599 : if (ok && config.num_partitions && is_module ()
20264 : 2614 : && !read_partitions (config.num_partitions))
20265 : : ok = false;
20266 : :
20267 : : /* Determine the module's number. */
20268 : 2612 : gcc_checking_assert (mod == MODULE_UNKNOWN);
20269 : 2612 : gcc_checking_assert (this != (*modules)[0]);
20270 : :
20271 : 2612 : {
20272 : : /* Allocate space in the entities array now -- that array must be
20273 : : monotonically in step with the modules array. */
20274 : 2612 : entity_lwm = vec_safe_length (entity_ary);
20275 : 2612 : entity_num = config.num_entities;
20276 : 2612 : gcc_checking_assert (modules->length () == 1
20277 : : || modules->last ()->entity_lwm <= entity_lwm);
20278 : 2612 : vec_safe_reserve (entity_ary, config.num_entities);
20279 : :
20280 : 2612 : binding_slot slot;
20281 : 2612 : slot.u.binding = NULL_TREE;
20282 : 925022 : for (unsigned count = config.num_entities; count--;)
20283 : 922410 : entity_ary->quick_push (slot);
20284 : : }
20285 : :
20286 : : /* We'll run out of other resources before we run out of module
20287 : : indices. */
20288 : 2612 : mod = modules->length ();
20289 : 2612 : vec_safe_push (modules, this);
20290 : :
20291 : : /* We always import and export ourselves. */
20292 : 2612 : bitmap_set_bit (imports, mod);
20293 : 2612 : bitmap_set_bit (exports, mod);
20294 : :
20295 : 2612 : if (ok)
20296 : 2599 : (*slurp->remap)[0] = mod << 1;
20297 : 3083 : dump () && dump ("Assigning %M module number %u", this, mod);
20298 : :
20299 : : /* We should not have been frozen during the importing done by
20300 : : read_config. */
20301 : 2612 : gcc_assert (!from ()->is_frozen ());
20302 : :
20303 : : /* Macro maps after the imports. */
20304 : 2612 : if (!(ok && have_locs && config.macro_locs))
20305 : 2485 : macro_locs.first = LINEMAPS_MACRO_LOWEST_LOCATION (line_table);
20306 : 127 : else if (!read_macro_maps (config.macro_locs))
20307 : : ok = false;
20308 : :
20309 : : /* Note whether there's an active initializer. */
20310 : 2612 : active_init_p = !is_header () && bool (config.active_init);
20311 : :
20312 : 2612 : gcc_assert (slurp->current == ~0u);
20313 : 2612 : return ok;
20314 : : }
20315 : :
20316 : : /* Read a preprocessor state. */
20317 : :
20318 : : bool
20319 : 890 : module_state::read_preprocessor (bool outermost)
20320 : : {
20321 : 890 : gcc_checking_assert (is_header () && slurp
20322 : : && slurp->remap_module (0) == mod);
20323 : :
20324 : 890 : if (loadedness == ML_PREPROCESSOR)
20325 : 6 : return !(from () && from ()->get_error ());
20326 : :
20327 : 884 : bool ok = true;
20328 : :
20329 : : /* Read direct header imports. */
20330 : 884 : unsigned len = slurp->remap->length ();
20331 : 926 : for (unsigned ix = 1; ok && ix != len; ix++)
20332 : : {
20333 : 42 : unsigned map = (*slurp->remap)[ix];
20334 : 42 : if (map & 1)
20335 : : {
20336 : 42 : module_state *import = (*modules)[map >> 1];
20337 : 42 : if (import->is_header ())
20338 : : {
20339 : 42 : ok = import->read_preprocessor (false);
20340 : 42 : bitmap_ior_into (slurp->headers, import->slurp->headers);
20341 : : }
20342 : : }
20343 : : }
20344 : :
20345 : : /* Record as a direct header. */
20346 : 884 : if (ok)
20347 : 884 : bitmap_set_bit (slurp->headers, mod);
20348 : :
20349 : 884 : if (ok && !read_macros ())
20350 : : ok = false;
20351 : :
20352 : 884 : loadedness = ML_PREPROCESSOR;
20353 : 884 : announce ("macros");
20354 : :
20355 : 884 : if (flag_preprocess_only)
20356 : : /* We're done with the string table. */
20357 : 39 : from ()->release ();
20358 : :
20359 : 884 : return check_read (outermost, ok);
20360 : : }
20361 : :
20362 : : /* Read language state. */
20363 : :
20364 : : bool
20365 : 2616 : module_state::read_language (bool outermost)
20366 : : {
20367 : 2616 : gcc_checking_assert (!lazy_snum);
20368 : :
20369 : 2616 : if (loadedness == ML_LANGUAGE)
20370 : 68 : return !(slurp && from () && from ()->get_error ());
20371 : :
20372 : 2548 : gcc_checking_assert (slurp && slurp->current == ~0u
20373 : : && slurp->remap_module (0) == mod);
20374 : :
20375 : 2548 : bool ok = true;
20376 : :
20377 : : /* Read direct imports. */
20378 : 2548 : unsigned len = slurp->remap->length ();
20379 : 2875 : for (unsigned ix = 1; ok && ix != len; ix++)
20380 : : {
20381 : 327 : unsigned map = (*slurp->remap)[ix];
20382 : 327 : if (map & 1)
20383 : : {
20384 : 327 : module_state *import = (*modules)[map >> 1];
20385 : 327 : if (!import->read_language (false))
20386 : 327 : ok = false;
20387 : : }
20388 : : }
20389 : :
20390 : 2548 : unsigned counts[MSC_HWM];
20391 : :
20392 : 2548 : if (ok && !read_counts (counts))
20393 : : ok = false;
20394 : :
20395 : 2548 : function_depth++; /* Prevent unexpected GCs. */
20396 : :
20397 : 2548 : if (ok && counts[MSC_entities] != entity_num)
20398 : : ok = false;
20399 : 2548 : if (ok && counts[MSC_entities]
20400 : 2343 : && !read_entities (counts[MSC_entities],
20401 : : counts[MSC_sec_lwm], counts[MSC_sec_hwm]))
20402 : : ok = false;
20403 : :
20404 : : /* Read the namespace hierarchy. */
20405 : 2548 : if (ok && counts[MSC_namespaces]
20406 : 3032 : && !read_namespaces (counts[MSC_namespaces]))
20407 : : ok = false;
20408 : :
20409 : 2548 : if (ok && !read_bindings (counts[MSC_bindings],
20410 : : counts[MSC_sec_lwm], counts[MSC_sec_hwm]))
20411 : : ok = false;
20412 : :
20413 : : /* And unnamed. */
20414 : 2548 : if (ok && counts[MSC_pendings] && !read_pendings (counts[MSC_pendings]))
20415 : : ok = false;
20416 : :
20417 : 2548 : if (ok)
20418 : : {
20419 : 2548 : slurp->remaining = counts[MSC_sec_hwm] - counts[MSC_sec_lwm];
20420 : 2548 : available_clusters += counts[MSC_sec_hwm] - counts[MSC_sec_lwm];
20421 : : }
20422 : :
20423 : 2548 : if (!flag_module_lazy
20424 : 2548 : || (is_partition ()
20425 : 198 : && module_interface_p ()
20426 : 183 : && !module_partition_p ()))
20427 : : {
20428 : : /* Read the sections in forward order, so that dependencies are read
20429 : : first. See note about tarjan_connect. */
20430 : 475 : ggc_collect ();
20431 : :
20432 : 475 : lazy_snum = ~0u;
20433 : :
20434 : 475 : unsigned hwm = counts[MSC_sec_hwm];
20435 : 134887 : for (unsigned ix = counts[MSC_sec_lwm]; ok && ix != hwm; ix++)
20436 : 134412 : if (!load_section (ix, NULL))
20437 : : {
20438 : : ok = false;
20439 : : break;
20440 : : }
20441 : 475 : lazy_snum = 0;
20442 : 475 : post_load_processing ();
20443 : :
20444 : 475 : ggc_collect ();
20445 : :
20446 : 475 : if (ok && CHECKING_P)
20447 : 487057 : for (unsigned ix = 0; ix != entity_num; ix++)
20448 : 486582 : gcc_assert (!(*entity_ary)[ix + entity_lwm].is_lazy ());
20449 : : }
20450 : :
20451 : : // If the import is a header-unit, we need to register initializers
20452 : : // of any static objects it contains (looking at you _Ioinit).
20453 : : // Notice, the ordering of these initializers will be that of a
20454 : : // dynamic initializer at this point in the current TU. (Other
20455 : : // instances of these objects in other TUs will be initialized as
20456 : : // part of that TU's global initializers.)
20457 : 2548 : if (ok && counts[MSC_inits] && !read_inits (counts[MSC_inits]))
20458 : : ok = false;
20459 : :
20460 : 2548 : function_depth--;
20461 : :
20462 : 2864 : announce (flag_module_lazy ? "lazy" : "imported");
20463 : 2548 : loadedness = ML_LANGUAGE;
20464 : :
20465 : 2548 : gcc_assert (slurp->current == ~0u);
20466 : :
20467 : : /* We're done with the string table. */
20468 : 2548 : from ()->release ();
20469 : :
20470 : 2548 : return check_read (outermost, ok);
20471 : : }
20472 : :
20473 : : bool
20474 : 163869 : module_state::maybe_defrost ()
20475 : : {
20476 : 163869 : bool ok = true;
20477 : 163869 : if (from ()->is_frozen ())
20478 : : {
20479 : 9 : if (lazy_open >= lazy_limit)
20480 : 3 : freeze_an_elf ();
20481 : 18 : dump () && dump ("Defrosting '%s'", filename);
20482 : 9 : ok = from ()->defrost (maybe_add_cmi_prefix (filename));
20483 : 9 : lazy_open++;
20484 : : }
20485 : :
20486 : 163869 : return ok;
20487 : : }
20488 : :
20489 : : /* Load section SNUM, dealing with laziness. It doesn't matter if we
20490 : : have multiple concurrent loads, because we do not use TREE_VISITED
20491 : : when reading back in. */
20492 : :
20493 : : bool
20494 : 163869 : module_state::load_section (unsigned snum, binding_slot *mslot)
20495 : : {
20496 : 163869 : if (from ()->get_error ())
20497 : : return false;
20498 : :
20499 : 163869 : if (snum >= slurp->current)
20500 : 0 : from ()->set_error (elf::E_BAD_LAZY);
20501 : 163869 : else if (maybe_defrost ())
20502 : : {
20503 : 163869 : unsigned old_current = slurp->current;
20504 : 163869 : slurp->current = snum;
20505 : 163869 : slurp->lru = 0; /* Do not swap out. */
20506 : 163869 : slurp->remaining--;
20507 : 163869 : read_cluster (snum);
20508 : 163869 : slurp->lru = ++lazy_lru;
20509 : 163869 : slurp->current = old_current;
20510 : : }
20511 : :
20512 : 163869 : if (mslot && mslot->is_lazy ())
20513 : : {
20514 : : /* Oops, the section didn't set this slot. */
20515 : 0 : from ()->set_error (elf::E_BAD_DATA);
20516 : 0 : *mslot = NULL_TREE;
20517 : : }
20518 : :
20519 : 163869 : bool ok = !from ()->get_error ();
20520 : 163869 : if (!ok)
20521 : : {
20522 : 0 : error_at (loc, "failed to read compiled module cluster %u: %s",
20523 : 0 : snum, from ()->get_error (filename));
20524 : 0 : note_cmi_name ();
20525 : : }
20526 : :
20527 : 163869 : maybe_completed_reading ();
20528 : :
20529 : 163869 : return ok;
20530 : : }
20531 : :
20532 : : void
20533 : 169897 : module_state::maybe_completed_reading ()
20534 : : {
20535 : 169897 : if (loadedness == ML_LANGUAGE && slurp->current == ~0u && !slurp->remaining)
20536 : : {
20537 : 2118 : lazy_open--;
20538 : : /* We no longer need the macros, all tokenizing has been done. */
20539 : 2118 : slurp->release_macros ();
20540 : :
20541 : 2118 : from ()->end ();
20542 : 2118 : slurp->close ();
20543 : 2118 : slurped ();
20544 : : }
20545 : 169897 : }
20546 : :
20547 : : /* After a reading operation, make sure things are still ok. If not,
20548 : : emit an error and clean up. */
20549 : :
20550 : : bool
20551 : 6062 : module_state::check_read (bool outermost, bool ok)
20552 : : {
20553 : 6062 : gcc_checking_assert (!outermost || slurp->current == ~0u);
20554 : :
20555 : 6062 : if (!ok)
20556 : 13 : from ()->set_error ();
20557 : :
20558 : 6062 : if (int e = from ()->get_error ())
20559 : : {
20560 : 37 : auto_diagnostic_group d;
20561 : 37 : error_at (loc, "failed to read compiled module: %s",
20562 : 37 : from ()->get_error (filename));
20563 : 37 : note_cmi_name ();
20564 : :
20565 : 37 : if (e == EMFILE
20566 : 37 : || e == ENFILE
20567 : : #if MAPPED_READING
20568 : 37 : || e == ENOMEM
20569 : : #endif
20570 : : || false)
20571 : 0 : inform (loc, "consider using %<-fno-module-lazy%>,"
20572 : : " increasing %<-param-lazy-modules=%u%> value,"
20573 : : " or increasing the per-process file descriptor limit",
20574 : : param_lazy_modules);
20575 : 37 : else if (e == ENOENT)
20576 : 18 : inform (loc, "imports must be built before being imported");
20577 : :
20578 : 37 : if (outermost)
20579 : 34 : fatal_error (loc, "returning to the gate for a mechanical issue");
20580 : :
20581 : 3 : ok = false;
20582 : 3 : }
20583 : :
20584 : 6028 : maybe_completed_reading ();
20585 : :
20586 : 6028 : return ok;
20587 : : }
20588 : :
20589 : : /* Return the IDENTIFIER_NODE naming module IX. This is the name
20590 : : including dots. */
20591 : :
20592 : : char const *
20593 : 270 : module_name (unsigned ix, bool header_ok)
20594 : : {
20595 : 270 : if (modules)
20596 : : {
20597 : 270 : module_state *imp = (*modules)[ix];
20598 : :
20599 : 270 : if (ix && !imp->name)
20600 : 0 : imp = imp->parent;
20601 : :
20602 : 270 : if (header_ok || !imp->is_header ())
20603 : 270 : return imp->get_flatname ();
20604 : : }
20605 : :
20606 : : return NULL;
20607 : : }
20608 : :
20609 : : /* Return the bitmap describing what modules are imported. Remember,
20610 : : we always import ourselves. */
20611 : :
20612 : : bitmap
20613 : 70049 : get_import_bitmap ()
20614 : : {
20615 : 70049 : return (*modules)[0]->imports;
20616 : : }
20617 : :
20618 : : /* Return the visible imports and path of instantiation for an
20619 : : instantiation at TINST. If TINST is nullptr, we're not in an
20620 : : instantiation, and thus will return the visible imports of the
20621 : : current TU (and NULL *PATH_MAP_P). We cache the information on
20622 : : the tinst level itself. */
20623 : :
20624 : : static bitmap
20625 : 90251 : path_of_instantiation (tinst_level *tinst, bitmap *path_map_p)
20626 : : {
20627 : 90251 : gcc_checking_assert (modules_p ());
20628 : :
20629 : 90251 : if (!tinst)
20630 : : {
20631 : : /* Not inside an instantiation, just the regular case. */
20632 : 42106 : *path_map_p = nullptr;
20633 : 42106 : return get_import_bitmap ();
20634 : : }
20635 : :
20636 : 48145 : if (!tinst->path)
20637 : : {
20638 : : /* Calculate. */
20639 : 13576 : bitmap visible = path_of_instantiation (tinst->next, path_map_p);
20640 : 13576 : bitmap path_map = *path_map_p;
20641 : :
20642 : 13576 : if (!path_map)
20643 : : {
20644 : 2004 : path_map = BITMAP_GGC_ALLOC ();
20645 : 2004 : bitmap_set_bit (path_map, 0);
20646 : : }
20647 : :
20648 : 13576 : tree decl = tinst->tldcl;
20649 : 13576 : if (TREE_CODE (decl) == TREE_LIST)
20650 : 0 : decl = TREE_PURPOSE (decl);
20651 : 13576 : if (TYPE_P (decl))
20652 : 1629 : decl = TYPE_NAME (decl);
20653 : :
20654 : 13576 : if (unsigned mod = get_originating_module (decl))
20655 : 1708 : if (!bitmap_bit_p (path_map, mod))
20656 : : {
20657 : : /* This is brand new information! */
20658 : 73 : bitmap new_path = BITMAP_GGC_ALLOC ();
20659 : 73 : bitmap_copy (new_path, path_map);
20660 : 73 : bitmap_set_bit (new_path, mod);
20661 : 73 : path_map = new_path;
20662 : :
20663 : 73 : bitmap imports = (*modules)[mod]->imports;
20664 : 73 : if (bitmap_intersect_compl_p (imports, visible))
20665 : : {
20666 : : /* IMPORTS contains additional modules to VISIBLE. */
20667 : 6 : bitmap new_visible = BITMAP_GGC_ALLOC ();
20668 : :
20669 : 6 : bitmap_ior (new_visible, visible, imports);
20670 : 6 : visible = new_visible;
20671 : : }
20672 : : }
20673 : :
20674 : 13576 : tinst->path = path_map;
20675 : 13576 : tinst->visible = visible;
20676 : : }
20677 : :
20678 : 48145 : *path_map_p = tinst->path;
20679 : 48145 : return tinst->visible;
20680 : : }
20681 : :
20682 : : /* Return the bitmap describing what modules are visible along the
20683 : : path of instantiation. If we're not an instantiation, this will be
20684 : : the visible imports of the TU. *PATH_MAP_P is filled in with the
20685 : : modules owning the instantiation path -- we see the module-linkage
20686 : : entities of those modules. */
20687 : :
20688 : : bitmap
20689 : 20006045 : visible_instantiation_path (bitmap *path_map_p)
20690 : : {
20691 : 20006045 : if (!modules_p ())
20692 : : return NULL;
20693 : :
20694 : 76675 : return path_of_instantiation (current_instantiation (), path_map_p);
20695 : : }
20696 : :
20697 : : /* We've just directly imported IMPORT. Update our import/export
20698 : : bitmaps. IS_EXPORT is true if we're reexporting the OTHER. */
20699 : :
20700 : : void
20701 : 2680 : module_state::set_import (module_state const *import, bool is_export)
20702 : : {
20703 : 2680 : gcc_checking_assert (this != import);
20704 : :
20705 : : /* We see IMPORT's exports (which includes IMPORT). If IMPORT is
20706 : : the primary interface or a partition we'll see its imports. */
20707 : 2680 : bitmap_ior_into (imports, import->is_module () || import->is_partition ()
20708 : : ? import->imports : import->exports);
20709 : :
20710 : 2680 : if (is_export)
20711 : : /* We'll export OTHER's exports. */
20712 : 392 : bitmap_ior_into (exports, import->exports);
20713 : 2680 : }
20714 : :
20715 : : /* Return the declaring entity of DECL. That is the decl determining
20716 : : how to decorate DECL with module information. Returns NULL_TREE if
20717 : : it's the global module. */
20718 : :
20719 : : tree
20720 : 157887218 : get_originating_module_decl (tree decl)
20721 : : {
20722 : : /* An enumeration constant. */
20723 : 157887218 : if (TREE_CODE (decl) == CONST_DECL
20724 : 4799 : && DECL_CONTEXT (decl)
20725 : 157892017 : && (TREE_CODE (DECL_CONTEXT (decl)) == ENUMERAL_TYPE))
20726 : 4280 : decl = TYPE_NAME (DECL_CONTEXT (decl));
20727 : 157882938 : else if (TREE_CODE (decl) == FIELD_DECL
20728 : 157874553 : || TREE_CODE (decl) == USING_DECL
20729 : 315750072 : || CONST_DECL_USING_P (decl))
20730 : : {
20731 : 16323 : decl = DECL_CONTEXT (decl);
20732 : 16323 : if (TREE_CODE (decl) != FUNCTION_DECL)
20733 : 16323 : decl = TYPE_NAME (decl);
20734 : : }
20735 : :
20736 : 157887218 : gcc_checking_assert (TREE_CODE (decl) == TEMPLATE_DECL
20737 : : || TREE_CODE (decl) == FUNCTION_DECL
20738 : : || TREE_CODE (decl) == TYPE_DECL
20739 : : || TREE_CODE (decl) == VAR_DECL
20740 : : || TREE_CODE (decl) == CONCEPT_DECL
20741 : : || TREE_CODE (decl) == NAMESPACE_DECL);
20742 : :
20743 : 158812788 : for (;;)
20744 : : {
20745 : : /* Uninstantiated template friends are owned by the befriending
20746 : : class -- not their context. */
20747 : 158350003 : if (TREE_CODE (decl) == TEMPLATE_DECL
20748 : 158350003 : && DECL_UNINSTANTIATED_TEMPLATE_FRIEND_P (decl))
20749 : 5172 : decl = TYPE_NAME (DECL_CHAIN (decl));
20750 : :
20751 : : /* An imported temploid friend is attached to the same module the
20752 : : befriending class was. */
20753 : 158350003 : if (imported_temploid_friends)
20754 : 1848170 : if (tree *slot = imported_temploid_friends->get (decl))
20755 : 279 : decl = *slot;
20756 : :
20757 : 158350003 : int use;
20758 : 158350003 : if (tree ti = node_template_info (decl, use))
20759 : : {
20760 : 379064 : decl = TI_TEMPLATE (ti);
20761 : 379064 : if (TREE_CODE (decl) != TEMPLATE_DECL)
20762 : : {
20763 : : /* A friend template specialization. */
20764 : 7 : gcc_checking_assert (OVL_P (decl));
20765 : 7 : return global_namespace;
20766 : : }
20767 : : }
20768 : : else
20769 : : {
20770 : 157970939 : tree ctx = CP_DECL_CONTEXT (decl);
20771 : 157970939 : if (TREE_CODE (ctx) == NAMESPACE_DECL)
20772 : : break;
20773 : :
20774 : 83728 : if (TYPE_P (ctx))
20775 : : {
20776 : 77661 : ctx = TYPE_NAME (ctx);
20777 : 77661 : if (!ctx)
20778 : : {
20779 : : /* Some kind of internal type. */
20780 : 0 : gcc_checking_assert (DECL_ARTIFICIAL (decl));
20781 : 0 : return global_namespace;
20782 : : }
20783 : : }
20784 : 83728 : decl = ctx;
20785 : : }
20786 : 462785 : }
20787 : :
20788 : 157887211 : return decl;
20789 : : }
20790 : :
20791 : : /* If DECL is imported, return which module imported it, or 0 for the current
20792 : : module. Except that if GLOBAL_M1, return -1 for decls attached to the
20793 : : global module. */
20794 : :
20795 : : int
20796 : 646196 : get_originating_module (tree decl, bool global_m1)
20797 : : {
20798 : 646196 : tree owner = get_originating_module_decl (decl);
20799 : 646196 : tree not_tmpl = STRIP_TEMPLATE (owner);
20800 : :
20801 : 646196 : if (!DECL_LANG_SPECIFIC (not_tmpl))
20802 : 146830 : return global_m1 ? -1 : 0;
20803 : :
20804 : 966518 : if (global_m1 && !DECL_MODULE_ATTACH_P (not_tmpl))
20805 : : return -1;
20806 : :
20807 : 84836 : int mod = !DECL_MODULE_IMPORT_P (not_tmpl) ? 0 : get_importing_module (owner);
20808 : 84836 : gcc_checking_assert (!global_m1 || !(*modules)[mod]->is_header ());
20809 : : return mod;
20810 : : }
20811 : :
20812 : : /* DECL is imported, return which module imported it.
20813 : : If FLEXIBLE, return -1 if not found, otherwise checking ICE. */
20814 : :
20815 : : unsigned
20816 : 15135 : get_importing_module (tree decl, bool flexible)
20817 : : {
20818 : 15135 : unsigned index = import_entity_index (decl, flexible);
20819 : 15135 : if (index == ~(~0u >> 1))
20820 : : return -1;
20821 : 15135 : module_state *module = import_entity_module (index);
20822 : :
20823 : 15135 : return module->mod;
20824 : : }
20825 : :
20826 : : /* Is it permissible to redeclare OLDDECL with NEWDECL.
20827 : :
20828 : : If NEWDECL is NULL, assumes that OLDDECL will be redeclared using
20829 : : the current scope's module and attachment. */
20830 : :
20831 : : bool
20832 : 137066 : module_may_redeclare (tree olddecl, tree newdecl)
20833 : : {
20834 : 137066 : tree decl = olddecl;
20835 : 152876 : for (;;)
20836 : : {
20837 : 144971 : tree ctx = CP_DECL_CONTEXT (decl);
20838 : 144971 : if (TREE_CODE (ctx) == NAMESPACE_DECL)
20839 : : // Found the namespace-scope decl.
20840 : : break;
20841 : 14849 : if (!CLASS_TYPE_P (ctx))
20842 : : // We've met a non-class scope. Such a thing is not
20843 : : // reopenable, so we must be ok.
20844 : : return true;
20845 : 7905 : decl = TYPE_NAME (ctx);
20846 : 7905 : }
20847 : :
20848 : 130122 : int use_tpl = 0;
20849 : 130122 : if (node_template_info (STRIP_TEMPLATE (decl), use_tpl) && use_tpl)
20850 : : // Specializations of any kind can be redeclared anywhere.
20851 : : // FIXME: Should we be checking this in more places on the scope chain?
20852 : : return true;
20853 : :
20854 : 87154 : module_state *old_mod = (*modules)[0];
20855 : 87154 : module_state *new_mod = old_mod;
20856 : :
20857 : 87154 : tree old_origin = get_originating_module_decl (decl);
20858 : 87154 : tree old_inner = STRIP_TEMPLATE (old_origin);
20859 : 87154 : bool olddecl_attached_p = (DECL_LANG_SPECIFIC (old_inner)
20860 : 139603 : && DECL_MODULE_ATTACH_P (old_inner));
20861 : 139603 : if (DECL_LANG_SPECIFIC (old_inner) && DECL_MODULE_IMPORT_P (old_inner))
20862 : : {
20863 : 394 : unsigned index = import_entity_index (old_origin);
20864 : 394 : old_mod = import_entity_module (index);
20865 : : }
20866 : :
20867 : 87154 : bool newdecl_attached_p = module_attach_p ();
20868 : 87154 : if (newdecl)
20869 : : {
20870 : 23932 : tree new_origin = get_originating_module_decl (newdecl);
20871 : 23932 : tree new_inner = STRIP_TEMPLATE (new_origin);
20872 : 23932 : newdecl_attached_p = (DECL_LANG_SPECIFIC (new_inner)
20873 : 46666 : && DECL_MODULE_ATTACH_P (new_inner));
20874 : 46666 : if (DECL_LANG_SPECIFIC (new_inner) && DECL_MODULE_IMPORT_P (new_inner))
20875 : : {
20876 : 116 : unsigned index = import_entity_index (new_origin);
20877 : 116 : new_mod = import_entity_module (index);
20878 : : }
20879 : : }
20880 : :
20881 : : /* Module attachment needs to match. */
20882 : 87154 : if (olddecl_attached_p == newdecl_attached_p)
20883 : : {
20884 : 87112 : if (!olddecl_attached_p)
20885 : : /* Both are GM entities, OK. */
20886 : : return true;
20887 : :
20888 : 1097 : if (new_mod == old_mod
20889 : 1343 : || (new_mod && get_primary (new_mod) == get_primary (old_mod)))
20890 : : /* Both attached to same named module, OK. */
20891 : : return true;
20892 : : }
20893 : :
20894 : : /* Attached to different modules, error. */
20895 : 45 : decl = newdecl ? newdecl : olddecl;
20896 : 45 : location_t loc = newdecl ? DECL_SOURCE_LOCATION (newdecl) : input_location;
20897 : 45 : if (DECL_IS_UNDECLARED_BUILTIN (olddecl))
20898 : : {
20899 : 3 : if (newdecl_attached_p)
20900 : 3 : error_at (loc, "declaring %qD in module %qs conflicts with builtin "
20901 : : "in global module", decl, new_mod->get_flatname ());
20902 : : else
20903 : 0 : error_at (loc, "declaration %qD conflicts with builtin", decl);
20904 : : }
20905 : 81 : else if (DECL_LANG_SPECIFIC (old_inner) && DECL_MODULE_IMPORT_P (old_inner))
20906 : : {
20907 : 27 : auto_diagnostic_group d;
20908 : 27 : if (newdecl_attached_p)
20909 : 3 : error_at (loc, "redeclaring %qD in module %qs conflicts with import",
20910 : : decl, new_mod->get_flatname ());
20911 : : else
20912 : 24 : error_at (loc, "redeclaring %qD in global module conflicts with import",
20913 : : decl);
20914 : :
20915 : 27 : if (olddecl_attached_p)
20916 : 27 : inform (DECL_SOURCE_LOCATION (olddecl),
20917 : : "import declared attached to module %qs",
20918 : : old_mod->get_flatname ());
20919 : : else
20920 : 0 : inform (DECL_SOURCE_LOCATION (olddecl),
20921 : : "import declared in global module");
20922 : 27 : }
20923 : : else
20924 : : {
20925 : 15 : auto_diagnostic_group d;
20926 : 15 : if (newdecl_attached_p)
20927 : 15 : error_at (loc, "conflicting declaration of %qD in module %qs",
20928 : : decl, new_mod->get_flatname ());
20929 : : else
20930 : 0 : error_at (loc, "conflicting declaration of %qD in global module",
20931 : : decl);
20932 : :
20933 : 15 : if (olddecl_attached_p)
20934 : 0 : inform (DECL_SOURCE_LOCATION (olddecl),
20935 : : "previously declared in module %qs",
20936 : : old_mod->get_flatname ());
20937 : : else
20938 : 15 : inform (DECL_SOURCE_LOCATION (olddecl),
20939 : : "previously declared in global module");
20940 : 15 : }
20941 : : return false;
20942 : : }
20943 : :
20944 : : /* DECL is being created by this TU. Record it came from here. We
20945 : : record module purview, so we can see if partial or explicit
20946 : : specialization needs to be written out, even though its purviewness
20947 : : comes from the most general template. */
20948 : :
20949 : : void
20950 : 748253558 : set_instantiating_module (tree decl)
20951 : : {
20952 : 748253558 : gcc_assert (TREE_CODE (decl) == FUNCTION_DECL
20953 : : || VAR_P (decl)
20954 : : || TREE_CODE (decl) == TYPE_DECL
20955 : : || TREE_CODE (decl) == CONCEPT_DECL
20956 : : || TREE_CODE (decl) == TEMPLATE_DECL
20957 : : || TREE_CODE (decl) == CONST_DECL
20958 : : || (TREE_CODE (decl) == NAMESPACE_DECL
20959 : : && DECL_NAMESPACE_ALIAS (decl)));
20960 : :
20961 : 748253558 : if (!modules_p ())
20962 : : return;
20963 : :
20964 : 2382114 : decl = STRIP_TEMPLATE (decl);
20965 : :
20966 : 2382114 : if (!DECL_LANG_SPECIFIC (decl) && module_purview_p ())
20967 : 302081 : retrofit_lang_decl (decl);
20968 : :
20969 : 2382114 : if (DECL_LANG_SPECIFIC (decl))
20970 : : {
20971 : 1886158 : DECL_MODULE_PURVIEW_P (decl) = module_purview_p ();
20972 : : /* If this was imported, we'll still be in the entity_hash. */
20973 : 1886158 : DECL_MODULE_IMPORT_P (decl) = false;
20974 : : }
20975 : : }
20976 : :
20977 : : /* If DECL is a class member, whose class is not defined in this TU
20978 : : (it was imported), remember this decl. */
20979 : :
20980 : : void
20981 : 117390 : set_defining_module (tree decl)
20982 : : {
20983 : 117390 : gcc_checking_assert (!DECL_LANG_SPECIFIC (decl)
20984 : : || !DECL_MODULE_IMPORT_P (decl));
20985 : :
20986 : 117390 : if (module_maybe_has_cmi_p ())
20987 : : {
20988 : : /* We need to track all declarations within a module, not just those
20989 : : in the module purview, because we don't necessarily know yet if
20990 : : this module will require a CMI while in the global fragment. */
20991 : 78819 : tree ctx = DECL_CONTEXT (decl);
20992 : 78819 : if (ctx
20993 : 78819 : && (TREE_CODE (ctx) == RECORD_TYPE || TREE_CODE (ctx) == UNION_TYPE)
20994 : 13587 : && DECL_LANG_SPECIFIC (TYPE_NAME (ctx))
20995 : 88990 : && DECL_MODULE_IMPORT_P (TYPE_NAME (ctx)))
20996 : : {
20997 : : /* This entity's context is from an import. We may need to
20998 : : record this entity to make sure we emit it in the CMI.
20999 : : Template specializations are in the template hash tables,
21000 : : so we don't need to record them here as well. */
21001 : 12 : int use_tpl = -1;
21002 : 12 : tree ti = node_template_info (decl, use_tpl);
21003 : 12 : if (use_tpl <= 0)
21004 : : {
21005 : 12 : if (ti)
21006 : : {
21007 : 3 : gcc_checking_assert (!use_tpl);
21008 : : /* Get to the TEMPLATE_DECL. */
21009 : 3 : decl = TI_TEMPLATE (ti);
21010 : : }
21011 : :
21012 : : /* Record it on the class_members list. */
21013 : 12 : vec_safe_push (class_members, decl);
21014 : : }
21015 : : }
21016 : : }
21017 : 117390 : }
21018 : :
21019 : : /* Also remember DECL if it's a newly declared class template partial
21020 : : specialization, because these are not necessarily added to the
21021 : : instantiation tables. */
21022 : :
21023 : : void
21024 : 6997758 : set_defining_module_for_partial_spec (tree decl)
21025 : : {
21026 : 6997758 : if (module_maybe_has_cmi_p ()
21027 : 19217 : && DECL_IMPLICIT_TYPEDEF_P (decl)
21028 : 7014596 : && CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (decl)))
21029 : 16838 : vec_safe_push (partial_specializations, decl);
21030 : 6997758 : }
21031 : :
21032 : : void
21033 : 263557552 : set_originating_module (tree decl, bool friend_p ATTRIBUTE_UNUSED)
21034 : : {
21035 : 263557552 : set_instantiating_module (decl);
21036 : :
21037 : 263557552 : if (!DECL_NAMESPACE_SCOPE_P (decl))
21038 : : return;
21039 : :
21040 : 160830266 : gcc_checking_assert (friend_p || decl == get_originating_module_decl (decl));
21041 : :
21042 : 160830266 : if (module_attach_p ())
21043 : : {
21044 : 3795 : retrofit_lang_decl (decl);
21045 : 3795 : DECL_MODULE_ATTACH_P (decl) = true;
21046 : : }
21047 : :
21048 : : /* It is ill-formed to export a declaration with internal linkage. However,
21049 : : at the point this function is called we don't yet always know whether this
21050 : : declaration has internal linkage; instead we defer this check for callers
21051 : : to do once visibility has been determined. */
21052 : 160830266 : if (module_exporting_p ())
21053 : 194344 : DECL_MODULE_EXPORT_P (decl) = true;
21054 : : }
21055 : :
21056 : : /* Checks whether DECL within a module unit has valid linkage for its kind.
21057 : : Must be called after visibility for DECL has been finalised. */
21058 : :
21059 : : void
21060 : 324377454 : check_module_decl_linkage (tree decl)
21061 : : {
21062 : 324377454 : if (!module_has_cmi_p ())
21063 : : return;
21064 : :
21065 : : /* A header unit shall not contain a definition of a non-inline function
21066 : : or variable (not template) whose name has external linkage. */
21067 : 436995 : if (header_module_p ()
21068 : 389751 : && !processing_template_decl
21069 : 120213 : && ((TREE_CODE (decl) == FUNCTION_DECL
21070 : 72403 : && !DECL_DECLARED_INLINE_P (decl))
21071 : 89576 : || (TREE_CODE (decl) == VAR_DECL
21072 : 23039 : && !DECL_INLINE_VAR_P (decl)))
21073 : 38710 : && decl_defined_p (decl)
21074 : 3760 : && !(DECL_LANG_SPECIFIC (decl)
21075 : 3760 : && DECL_TEMPLATE_INSTANTIATION (decl))
21076 : 440755 : && decl_linkage (decl) == lk_external)
21077 : 60 : error_at (DECL_SOURCE_LOCATION (decl),
21078 : : "external linkage definition of %qD in header module must "
21079 : : "be declared %<inline%>", decl);
21080 : :
21081 : : /* An internal-linkage declaration cannot be generally be exported.
21082 : : But it's OK to export any declaration from a header unit, including
21083 : : internal linkage declarations. */
21084 : 436995 : if (!header_module_p () && DECL_MODULE_EXPORT_P (decl))
21085 : : {
21086 : : /* Let's additionally treat any exported declaration within an
21087 : : internal namespace as exporting a declaration with internal
21088 : : linkage, as this would also implicitly export the internal
21089 : : linkage namespace. */
21090 : 1762 : if (decl_internal_context_p (decl))
21091 : : {
21092 : 50 : error_at (DECL_SOURCE_LOCATION (decl),
21093 : : "exporting declaration %qD declared in unnamed namespace",
21094 : : decl);
21095 : 50 : DECL_MODULE_EXPORT_P (decl) = false;
21096 : : }
21097 : 1712 : else if (decl_linkage (decl) == lk_internal)
21098 : : {
21099 : 17 : error_at (DECL_SOURCE_LOCATION (decl),
21100 : : "exporting declaration %qD with internal linkage", decl);
21101 : 17 : DECL_MODULE_EXPORT_P (decl) = false;
21102 : : }
21103 : : }
21104 : : }
21105 : :
21106 : : /* DECL is keyed to CTX for odr purposes. */
21107 : :
21108 : : void
21109 : 900022 : maybe_key_decl (tree ctx, tree decl)
21110 : : {
21111 : 900022 : if (!modules_p ())
21112 : : return;
21113 : :
21114 : : /* We only need to deal with lambdas attached to var, field,
21115 : : parm, type, or concept decls. */
21116 : 6328 : if (TREE_CODE (ctx) != VAR_DECL
21117 : 6328 : && TREE_CODE (ctx) != FIELD_DECL
21118 : 6037 : && TREE_CODE (ctx) != PARM_DECL
21119 : 6025 : && TREE_CODE (ctx) != TYPE_DECL
21120 : 5997 : && TREE_CODE (ctx) != CONCEPT_DECL)
21121 : : return;
21122 : :
21123 : : /* For fields, key it to the containing type to handle deduplication
21124 : : correctly. */
21125 : 368 : if (TREE_CODE (ctx) == FIELD_DECL)
21126 : 6 : ctx = TYPE_NAME (DECL_CONTEXT (ctx));
21127 : :
21128 : 368 : if (!keyed_table)
21129 : 76 : keyed_table = new keyed_map_t (EXPERIMENT (1, 400));
21130 : :
21131 : 368 : auto &vec = keyed_table->get_or_insert (ctx);
21132 : 368 : if (!vec.length ())
21133 : : {
21134 : 354 : retrofit_lang_decl (ctx);
21135 : 354 : DECL_MODULE_KEYED_DECLS_P (ctx) = true;
21136 : : }
21137 : 368 : vec.safe_push (decl);
21138 : : }
21139 : :
21140 : : /* DECL is an instantiated friend that should be attached to the same
21141 : : module that ORIG is. */
21142 : :
21143 : : void
21144 : 1783892 : propagate_defining_module (tree decl, tree orig)
21145 : : {
21146 : 1783892 : if (!modules_p ())
21147 : : return;
21148 : :
21149 : 3422 : tree not_tmpl = STRIP_TEMPLATE (orig);
21150 : 6823 : if (DECL_LANG_SPECIFIC (not_tmpl) && DECL_MODULE_ATTACH_P (not_tmpl))
21151 : : {
21152 : 99 : tree inner = STRIP_TEMPLATE (decl);
21153 : 99 : retrofit_lang_decl (inner);
21154 : 99 : DECL_MODULE_ATTACH_P (inner) = true;
21155 : : }
21156 : :
21157 : 6823 : if (DECL_LANG_SPECIFIC (not_tmpl) && DECL_MODULE_IMPORT_P (not_tmpl))
21158 : : {
21159 : 145 : bool exists = imported_temploid_friends->put (decl, orig);
21160 : :
21161 : : /* We should only be called if lookup for an existing decl
21162 : : failed, in which case there shouldn't already be an entry
21163 : : in the map. */
21164 : 145 : gcc_assert (!exists);
21165 : : }
21166 : : }
21167 : :
21168 : : /* DECL is being freed, clear data we don't need anymore. */
21169 : :
21170 : : void
21171 : 28423 : remove_defining_module (tree decl)
21172 : : {
21173 : 28423 : if (!modules_p ())
21174 : : return;
21175 : :
21176 : 28423 : if (imported_temploid_friends)
21177 : 28423 : imported_temploid_friends->remove (decl);
21178 : : }
21179 : :
21180 : : /* Create the flat name string. It is simplest to have it handy. */
21181 : :
21182 : : void
21183 : 5541 : module_state::set_flatname ()
21184 : : {
21185 : 5541 : gcc_checking_assert (!flatname);
21186 : 5541 : if (parent)
21187 : : {
21188 : 584 : auto_vec<tree,5> ids;
21189 : 584 : size_t len = 0;
21190 : 584 : char const *primary = NULL;
21191 : 584 : size_t pfx_len = 0;
21192 : :
21193 : 584 : for (module_state *probe = this;
21194 : 1461 : probe;
21195 : 877 : probe = probe->parent)
21196 : 1300 : if (is_partition () && !probe->is_partition ())
21197 : : {
21198 : 423 : primary = probe->get_flatname ();
21199 : 423 : pfx_len = strlen (primary);
21200 : 423 : break;
21201 : : }
21202 : : else
21203 : : {
21204 : 877 : ids.safe_push (probe->name);
21205 : 877 : len += IDENTIFIER_LENGTH (probe->name) + 1;
21206 : : }
21207 : :
21208 : 584 : char *flat = XNEWVEC (char, pfx_len + len + is_partition ());
21209 : 584 : flatname = flat;
21210 : :
21211 : 584 : if (primary)
21212 : : {
21213 : 423 : memcpy (flat, primary, pfx_len);
21214 : 423 : flat += pfx_len;
21215 : 423 : *flat++ = ':';
21216 : : }
21217 : :
21218 : 1461 : for (unsigned len = 0; ids.length ();)
21219 : : {
21220 : 877 : if (len)
21221 : 293 : flat[len++] = '.';
21222 : 877 : tree elt = ids.pop ();
21223 : 877 : unsigned l = IDENTIFIER_LENGTH (elt);
21224 : 877 : memcpy (flat + len, IDENTIFIER_POINTER (elt), l + 1);
21225 : 877 : len += l;
21226 : : }
21227 : 584 : }
21228 : 4957 : else if (is_header ())
21229 : 1813 : flatname = TREE_STRING_POINTER (name);
21230 : : else
21231 : 3144 : flatname = IDENTIFIER_POINTER (name);
21232 : 5541 : }
21233 : :
21234 : : /* Read the CMI file for a module. */
21235 : :
21236 : : bool
21237 : 2630 : module_state::do_import (cpp_reader *reader, bool outermost)
21238 : : {
21239 : 2630 : gcc_assert (global_namespace == current_scope () && loadedness == ML_NONE);
21240 : :
21241 : 2630 : loc = linemap_module_loc (line_table, loc, get_flatname ());
21242 : :
21243 : 2630 : if (lazy_open >= lazy_limit)
21244 : 9 : freeze_an_elf ();
21245 : :
21246 : 2630 : int fd = -1;
21247 : 2630 : int e = ENOENT;
21248 : 2630 : if (filename)
21249 : : {
21250 : 2630 : const char *file = maybe_add_cmi_prefix (filename);
21251 : 3101 : dump () && dump ("CMI is %s", file);
21252 : 2630 : if (note_module_cmi_yes || inform_cmi_p)
21253 : 12 : inform (loc, "reading CMI %qs", file);
21254 : : /* Add the CMI file to the dependency tracking. */
21255 : 2630 : if (cpp_get_deps (reader))
21256 : 12 : deps_add_dep (cpp_get_deps (reader), file);
21257 : 2630 : fd = open (file, O_RDONLY | O_CLOEXEC | O_BINARY);
21258 : 2630 : e = errno;
21259 : : }
21260 : :
21261 : 2630 : gcc_checking_assert (!slurp);
21262 : 5242 : slurp = new slurping (new elf_in (fd, e));
21263 : :
21264 : 2630 : bool ok = true;
21265 : 2630 : if (!from ()->get_error ())
21266 : : {
21267 : 2612 : announce ("importing");
21268 : 2612 : loadedness = ML_CONFIG;
21269 : 2612 : lazy_open++;
21270 : 2612 : ok = read_initial (reader);
21271 : 2612 : slurp->lru = ++lazy_lru;
21272 : : }
21273 : :
21274 : 2630 : gcc_assert (slurp->current == ~0u);
21275 : :
21276 : 2630 : return check_read (outermost, ok);
21277 : : }
21278 : :
21279 : : /* Attempt to increase the file descriptor limit. */
21280 : :
21281 : : static bool
21282 : 4295 : try_increase_lazy (unsigned want)
21283 : : {
21284 : 4295 : gcc_checking_assert (lazy_open >= lazy_limit);
21285 : :
21286 : : /* If we're increasing, saturate at hard limit. */
21287 : 4295 : if (want > lazy_hard_limit && lazy_limit < lazy_hard_limit)
21288 : 4295 : want = lazy_hard_limit;
21289 : :
21290 : : #if HAVE_SETRLIMIT
21291 : 4295 : if ((!lazy_limit || !param_lazy_modules)
21292 : 4283 : && lazy_hard_limit
21293 : 4283 : && want <= lazy_hard_limit)
21294 : : {
21295 : 4283 : struct rlimit rlimit;
21296 : 4283 : rlimit.rlim_cur = want + LAZY_HEADROOM;
21297 : 4283 : rlimit.rlim_max = lazy_hard_limit + LAZY_HEADROOM;
21298 : 4283 : if (!setrlimit (RLIMIT_NOFILE, &rlimit))
21299 : 4283 : lazy_limit = want;
21300 : : }
21301 : : #endif
21302 : :
21303 : 4295 : return lazy_open < lazy_limit;
21304 : : }
21305 : :
21306 : : /* Pick a victim module to freeze its reader. */
21307 : :
21308 : : void
21309 : 12 : module_state::freeze_an_elf ()
21310 : : {
21311 : 12 : if (try_increase_lazy (lazy_open * 2))
21312 : : return;
21313 : :
21314 : 12 : module_state *victim = NULL;
21315 : 12 : for (unsigned ix = modules->length (); ix--;)
21316 : : {
21317 : 30 : module_state *candidate = (*modules)[ix];
21318 : 30 : if (candidate && candidate->slurp && candidate->slurp->lru
21319 : 60 : && candidate->from ()->is_freezable ()
21320 : 39 : && (!victim || victim->slurp->lru > candidate->slurp->lru))
21321 : : victim = candidate;
21322 : : }
21323 : :
21324 : 12 : if (victim)
21325 : : {
21326 : 18 : dump () && dump ("Freezing '%s'", victim->filename);
21327 : 9 : if (victim->slurp->macro_defs.size)
21328 : : /* Save the macro definitions to a buffer. */
21329 : 0 : victim->from ()->preserve (victim->slurp->macro_defs);
21330 : 9 : if (victim->slurp->macro_tbl.size)
21331 : : /* Save the macro definitions to a buffer. */
21332 : 0 : victim->from ()->preserve (victim->slurp->macro_tbl);
21333 : 9 : victim->from ()->freeze ();
21334 : 9 : lazy_open--;
21335 : : }
21336 : : else
21337 : 3 : dump () && dump ("No module available for freezing");
21338 : : }
21339 : :
21340 : : /* Load the lazy slot *MSLOT, INDEX'th slot of the module. */
21341 : :
21342 : : bool
21343 : 26932 : module_state::lazy_load (unsigned index, binding_slot *mslot)
21344 : : {
21345 : 26932 : unsigned n = dump.push (this);
21346 : :
21347 : 26932 : gcc_checking_assert (function_depth);
21348 : :
21349 : 26932 : unsigned cookie = mslot->get_lazy ();
21350 : 26932 : unsigned snum = cookie >> 2;
21351 : 27238 : dump () && dump ("Loading entity %M[%u] section:%u", this, index, snum);
21352 : :
21353 : 26932 : bool ok = load_section (snum, mslot);
21354 : :
21355 : 26932 : dump.pop (n);
21356 : :
21357 : 26932 : return ok;
21358 : : }
21359 : :
21360 : : /* Load MOD's binding for NS::ID into *MSLOT. *MSLOT contains the
21361 : : lazy cookie. OUTER is true if this is the outermost lazy, (used
21362 : : for diagnostics). */
21363 : :
21364 : : void
21365 : 2525 : lazy_load_binding (unsigned mod, tree ns, tree id, binding_slot *mslot)
21366 : : {
21367 : 2525 : int count = errorcount + warningcount;
21368 : :
21369 : 2525 : bool timer_running = timevar_cond_start (TV_MODULE_IMPORT);
21370 : :
21371 : : /* Make sure lazy loading from a template context behaves as if
21372 : : from a non-template context. */
21373 : 2525 : processing_template_decl_sentinel ptds;
21374 : :
21375 : : /* Stop GC happening, even in outermost loads (because our caller
21376 : : could well be building up a lookup set). */
21377 : 2525 : function_depth++;
21378 : :
21379 : 2525 : gcc_checking_assert (mod);
21380 : 2525 : module_state *module = (*modules)[mod];
21381 : 2525 : unsigned n = dump.push (module);
21382 : :
21383 : 2525 : unsigned snum = mslot->get_lazy ();
21384 : 2822 : dump () && dump ("Lazily binding %P@%N section:%u", ns, id,
21385 : : module->name, snum);
21386 : :
21387 : 2525 : bool ok = !recursive_lazy (snum);
21388 : 2525 : if (ok)
21389 : : {
21390 : 2525 : ok = module->load_section (snum, mslot);
21391 : 2525 : lazy_snum = 0;
21392 : 2525 : post_load_processing ();
21393 : : }
21394 : :
21395 : 2525 : dump.pop (n);
21396 : :
21397 : 2525 : function_depth--;
21398 : :
21399 : 2525 : timevar_cond_stop (TV_MODULE_IMPORT, timer_running);
21400 : :
21401 : 2525 : if (!ok)
21402 : 0 : fatal_error (input_location,
21403 : 0 : module->is_header ()
21404 : : ? G_("failed to load binding %<%E%s%E%>")
21405 : : : G_("failed to load binding %<%E%s%E@%s%>"),
21406 : 0 : ns, &"::"[ns == global_namespace ? 2 : 0], id,
21407 : : module->get_flatname ());
21408 : :
21409 : 2525 : if (count != errorcount + warningcount)
21410 : 21 : inform (input_location,
21411 : 21 : module->is_header ()
21412 : : ? G_("during load of binding %<%E%s%E%>")
21413 : : : G_("during load of binding %<%E%s%E@%s%>"),
21414 : 21 : ns, &"::"[ns == global_namespace ? 2 : 0], id,
21415 : : module->get_flatname ());
21416 : 2525 : }
21417 : :
21418 : : /* Load any pending entities keyed to the top-key of DECL. */
21419 : :
21420 : : void
21421 : 23933874 : lazy_load_pendings (tree decl)
21422 : : {
21423 : : /* Make sure lazy loading from a template context behaves as if
21424 : : from a non-template context. */
21425 : 23933874 : processing_template_decl_sentinel ptds;
21426 : :
21427 : 23933874 : tree key_decl;
21428 : 23933874 : pending_key key;
21429 : 23933874 : key.ns = find_pending_key (decl, &key_decl);
21430 : 23933874 : key.id = DECL_NAME (key_decl);
21431 : :
21432 : 23933874 : auto *pending_vec = pending_table ? pending_table->get (key) : nullptr;
21433 : 23933874 : if (!pending_vec)
21434 : 23929132 : return;
21435 : :
21436 : 4742 : int count = errorcount + warningcount;
21437 : :
21438 : 4742 : bool timer_running = timevar_cond_start (TV_MODULE_IMPORT);
21439 : 4742 : bool ok = !recursive_lazy ();
21440 : 4742 : if (ok)
21441 : : {
21442 : 4742 : function_depth++; /* Prevent GC */
21443 : 4742 : unsigned n = dump.push (NULL);
21444 : 5164 : dump () && dump ("Reading %u pending entities keyed to %P",
21445 : : pending_vec->length (), key.ns, key.id);
21446 : 4742 : for (unsigned ix = pending_vec->length (); ix--;)
21447 : : {
21448 : 50528 : unsigned index = (*pending_vec)[ix];
21449 : 50528 : binding_slot *slot = &(*entity_ary)[index];
21450 : :
21451 : 50528 : if (slot->is_lazy ())
21452 : : {
21453 : 2544 : module_state *import = import_entity_module (index);
21454 : 2544 : if (!import->lazy_load (index - import->entity_lwm, slot))
21455 : 50528 : ok = false;
21456 : : }
21457 : 103254 : else if (dump ())
21458 : : {
21459 : 313 : module_state *import = import_entity_module (index);
21460 : 313 : dump () && dump ("Entity %M[%u] already loaded",
21461 : 313 : import, index - import->entity_lwm);
21462 : : }
21463 : : }
21464 : :
21465 : 4742 : pending_table->remove (key);
21466 : 4742 : dump.pop (n);
21467 : 4742 : lazy_snum = 0;
21468 : 4742 : post_load_processing ();
21469 : 4742 : function_depth--;
21470 : : }
21471 : :
21472 : 4742 : timevar_cond_stop (TV_MODULE_IMPORT, timer_running);
21473 : :
21474 : 4742 : if (!ok)
21475 : 0 : fatal_error (input_location, "failed to load pendings for %<%E%s%E%>",
21476 : 0 : key.ns, &"::"[key.ns == global_namespace ? 2 : 0], key.id);
21477 : :
21478 : 4742 : if (count != errorcount + warningcount)
21479 : 0 : inform (input_location, "during load of pendings for %<%E%s%E%>",
21480 : 0 : key.ns, &"::"[key.ns == global_namespace ? 2 : 0], key.id);
21481 : 23933874 : }
21482 : :
21483 : : static void
21484 : 2399 : direct_import (module_state *import, cpp_reader *reader)
21485 : : {
21486 : 2399 : timevar_start (TV_MODULE_IMPORT);
21487 : 2399 : unsigned n = dump.push (import);
21488 : :
21489 : 2399 : gcc_checking_assert (import->is_direct () && import->has_location ());
21490 : 2399 : if (import->loadedness == ML_NONE)
21491 : 1511 : if (!import->do_import (reader, true))
21492 : 0 : gcc_unreachable ();
21493 : :
21494 : 2365 : if (import->loadedness < ML_LANGUAGE)
21495 : : {
21496 : 2289 : if (!keyed_table)
21497 : 2007 : keyed_table = new keyed_map_t (EXPERIMENT (1, 400));
21498 : 2289 : import->read_language (true);
21499 : : }
21500 : :
21501 : 2365 : (*modules)[0]->set_import (import, import->exported_p);
21502 : :
21503 : 2365 : dump.pop (n);
21504 : 2365 : timevar_stop (TV_MODULE_IMPORT);
21505 : 2365 : }
21506 : :
21507 : : /* Import module IMPORT. */
21508 : :
21509 : : void
21510 : 2192 : import_module (module_state *import, location_t from_loc, bool exporting_p,
21511 : : tree, cpp_reader *reader)
21512 : : {
21513 : 2192 : if (!import->check_not_purview (from_loc))
21514 : : return;
21515 : :
21516 : 2183 : if (!import->is_header () && current_lang_depth ())
21517 : : /* Only header units should appear inside language
21518 : : specifications. The std doesn't specify this, but I think
21519 : : that's an error in resolving US 033, because language linkage
21520 : : is also our escape clause to getting things into the global
21521 : : module, so we don't want to confuse things by having to think
21522 : : about whether 'extern "C++" { import foo; }' puts foo's
21523 : : contents into the global module all of a sudden. */
21524 : 6 : warning (0, "import of named module %qs inside language-linkage block",
21525 : : import->get_flatname ());
21526 : :
21527 : 2183 : if (exporting_p || module_exporting_p ())
21528 : 272 : import->exported_p = true;
21529 : :
21530 : 2183 : if (import->loadedness != ML_NONE)
21531 : : {
21532 : 885 : from_loc = ordinary_loc_of (line_table, from_loc);
21533 : 885 : linemap_module_reparent (line_table, import->loc, from_loc);
21534 : : }
21535 : 2183 : gcc_checking_assert (!import->module_p);
21536 : 2183 : gcc_checking_assert (import->is_direct () && import->has_location ());
21537 : :
21538 : 2183 : direct_import (import, reader);
21539 : : }
21540 : :
21541 : : /* Declare the name of the current module to be NAME. EXPORTING_p is
21542 : : true if this TU is the exporting module unit. */
21543 : :
21544 : : void
21545 : 2746 : declare_module (module_state *module, location_t from_loc, bool exporting_p,
21546 : : tree, cpp_reader *reader)
21547 : : {
21548 : 2746 : gcc_assert (global_namespace == current_scope ());
21549 : :
21550 : 2746 : module_state *current = (*modules)[0];
21551 : 2746 : if (module_purview_p () || module->loadedness > ML_CONFIG)
21552 : : {
21553 : 6 : auto_diagnostic_group d;
21554 : 12 : error_at (from_loc, module_purview_p ()
21555 : : ? G_("module already declared")
21556 : : : G_("module already imported"));
21557 : 6 : if (module_purview_p ())
21558 : 0 : module = current;
21559 : 12 : inform (module->loc, module_purview_p ()
21560 : : ? G_("module %qs declared here")
21561 : : : G_("module %qs imported here"),
21562 : : module->get_flatname ());
21563 : 6 : return;
21564 : 6 : }
21565 : :
21566 : 2740 : gcc_checking_assert (module->module_p);
21567 : 2740 : gcc_checking_assert (module->is_direct () && module->has_location ());
21568 : :
21569 : : /* Yer a module, 'arry. */
21570 : 2740 : module_kind = module->is_header () ? MK_HEADER : MK_NAMED | MK_ATTACH;
21571 : :
21572 : : // Even in header units, we consider the decls to be purview
21573 : 2740 : module_kind |= MK_PURVIEW;
21574 : :
21575 : 2740 : if (module->is_partition ())
21576 : 165 : module_kind |= MK_PARTITION;
21577 : 2740 : if (exporting_p)
21578 : : {
21579 : 2477 : module->interface_p = true;
21580 : 2477 : module_kind |= MK_INTERFACE;
21581 : : }
21582 : :
21583 : 2740 : if (module_has_cmi_p ())
21584 : : {
21585 : : /* Copy the importing information we may have already done. We
21586 : : do not need to separate out the imports that only happen in
21587 : : the GMF, inspite of what the literal wording of the std
21588 : : might imply. See p2191, the core list had a discussion
21589 : : where the module implementors agreed that the GMF of a named
21590 : : module is invisible to importers. */
21591 : 2524 : module->imports = current->imports;
21592 : :
21593 : 2524 : module->mod = 0;
21594 : 2524 : (*modules)[0] = module;
21595 : : }
21596 : : else
21597 : : {
21598 : 216 : module->interface_p = true;
21599 : 216 : current->parent = module; /* So mangler knows module identity. */
21600 : 216 : direct_import (module, reader);
21601 : : }
21602 : : }
21603 : :
21604 : : /* Return true IFF we must emit a module global initializer function
21605 : : (which will be called by importers' init code). */
21606 : :
21607 : : bool
21608 : 100322 : module_global_init_needed ()
21609 : : {
21610 : 100322 : return module_has_cmi_p () && !header_module_p ();
21611 : : }
21612 : :
21613 : : /* Calculate which, if any, import initializers need calling. */
21614 : :
21615 : : bool
21616 : 93543 : module_determine_import_inits ()
21617 : : {
21618 : 93543 : if (!modules || header_module_p ())
21619 : : return false;
21620 : :
21621 : : /* Prune active_init_p. We need the same bitmap allocation
21622 : : scheme as for the imports member. */
21623 : 3278 : function_depth++; /* Disable GC. */
21624 : 3278 : bitmap covered_imports (BITMAP_GGC_ALLOC ());
21625 : :
21626 : 3278 : bool any = false;
21627 : :
21628 : : /* Because indirect imports are before their direct import, and
21629 : : we're scanning the array backwards, we only need one pass! */
21630 : 5727 : for (unsigned ix = modules->length (); --ix;)
21631 : : {
21632 : 2449 : module_state *import = (*modules)[ix];
21633 : :
21634 : 2449 : if (!import->active_init_p)
21635 : : ;
21636 : 43 : else if (bitmap_bit_p (covered_imports, ix))
21637 : 6 : import->active_init_p = false;
21638 : : else
21639 : : {
21640 : : /* Everything this imports is therefore handled by its
21641 : : initializer, so doesn't need initializing by us. */
21642 : 37 : bitmap_ior_into (covered_imports, import->imports);
21643 : 37 : any = true;
21644 : : }
21645 : : }
21646 : 3278 : function_depth--;
21647 : :
21648 : 3278 : return any;
21649 : : }
21650 : :
21651 : : /* Emit calls to each direct import's global initializer. Including
21652 : : direct imports of directly imported header units. The initializers
21653 : : of (static) entities in header units will be called by their
21654 : : importing modules (for the instance contained within that), or by
21655 : : the current TU (for the instances we've brought in). Of course
21656 : : such header unit behaviour is evil, but iostream went through that
21657 : : door some time ago. */
21658 : :
21659 : : void
21660 : 37 : module_add_import_initializers ()
21661 : : {
21662 : 37 : if (!modules || header_module_p ())
21663 : 0 : return;
21664 : :
21665 : 37 : tree fntype = build_function_type (void_type_node, void_list_node);
21666 : 37 : releasing_vec args; // There are no args
21667 : :
21668 : 83 : for (unsigned ix = modules->length (); --ix;)
21669 : : {
21670 : 46 : module_state *import = (*modules)[ix];
21671 : 46 : if (import->active_init_p)
21672 : : {
21673 : 37 : tree name = mangle_module_global_init (ix);
21674 : 37 : tree fndecl = build_lang_decl (FUNCTION_DECL, name, fntype);
21675 : :
21676 : 37 : DECL_CONTEXT (fndecl) = FROB_CONTEXT (global_namespace);
21677 : 37 : SET_DECL_ASSEMBLER_NAME (fndecl, name);
21678 : 37 : TREE_PUBLIC (fndecl) = true;
21679 : 37 : determine_visibility (fndecl);
21680 : :
21681 : 37 : tree call = cp_build_function_call_vec (fndecl, &args,
21682 : : tf_warning_or_error);
21683 : 37 : finish_expr_stmt (call);
21684 : : }
21685 : : }
21686 : 37 : }
21687 : :
21688 : : /* NAME & LEN are a preprocessed header name, possibly including the
21689 : : surrounding "" or <> characters. Return the raw string name of the
21690 : : module to which it refers. This will be an absolute path, or begin
21691 : : with ./, so it is immediately distinguishable from a (non-header
21692 : : unit) module name. If READER is non-null, ask the preprocessor to
21693 : : locate the header to which it refers using the appropriate include
21694 : : path. Note that we do never do \ processing of the string, as that
21695 : : matches the preprocessor's behaviour. */
21696 : :
21697 : : static const char *
21698 : 23015 : canonicalize_header_name (cpp_reader *reader, location_t loc, bool unquoted,
21699 : : const char *str, size_t &len_r)
21700 : : {
21701 : 23015 : size_t len = len_r;
21702 : 23015 : static char *buf = 0;
21703 : 23015 : static size_t alloc = 0;
21704 : :
21705 : 23015 : if (!unquoted)
21706 : : {
21707 : 4 : gcc_checking_assert (len >= 2
21708 : : && ((reader && str[0] == '<' && str[len-1] == '>')
21709 : : || (str[0] == '"' && str[len-1] == '"')));
21710 : 4 : str += 1;
21711 : 4 : len -= 2;
21712 : : }
21713 : :
21714 : 23015 : if (reader)
21715 : : {
21716 : 4 : gcc_assert (!unquoted);
21717 : :
21718 : 4 : if (len >= alloc)
21719 : : {
21720 : 4 : alloc = len + 1;
21721 : 4 : buf = XRESIZEVEC (char, buf, alloc);
21722 : : }
21723 : 4 : memcpy (buf, str, len);
21724 : 4 : buf[len] = 0;
21725 : :
21726 : 8 : if (const char *hdr
21727 : 4 : = cpp_probe_header_unit (reader, buf, str[-1] == '<', loc))
21728 : : {
21729 : 4 : len = strlen (hdr);
21730 : 4 : str = hdr;
21731 : : }
21732 : : else
21733 : 0 : str = buf;
21734 : : }
21735 : :
21736 : 23015 : if (!(str[0] == '.' ? IS_DIR_SEPARATOR (str[1]) : IS_ABSOLUTE_PATH (str)))
21737 : : {
21738 : : /* Prepend './' */
21739 : 9 : if (len + 3 > alloc)
21740 : : {
21741 : 9 : alloc = len + 3;
21742 : 9 : buf = XRESIZEVEC (char, buf, alloc);
21743 : : }
21744 : :
21745 : 9 : buf[0] = '.';
21746 : 9 : buf[1] = DIR_SEPARATOR;
21747 : 9 : memmove (buf + 2, str, len);
21748 : 9 : len += 2;
21749 : 9 : buf[len] = 0;
21750 : 9 : str = buf;
21751 : : }
21752 : :
21753 : 23015 : len_r = len;
21754 : 23015 : return str;
21755 : : }
21756 : :
21757 : : /* Set the CMI name from a cody packet. Issue an error if
21758 : : ill-formed. */
21759 : :
21760 : 5048 : void module_state::set_filename (const Cody::Packet &packet)
21761 : : {
21762 : 5048 : if (packet.GetCode () == Cody::Client::PC_PATHNAME)
21763 : : {
21764 : : /* If we've seen this import before we better have the same CMI. */
21765 : 5045 : const std::string &path = packet.GetString ();
21766 : 5045 : if (!filename)
21767 : 5042 : filename = xstrdup (packet.GetString ().c_str ());
21768 : 3 : else if (filename != path)
21769 : 0 : error_at (loc, "mismatching compiled module interface: "
21770 : : "had %qs, got %qs", filename, path.c_str ());
21771 : : }
21772 : : else
21773 : : {
21774 : 3 : gcc_checking_assert (packet.GetCode () == Cody::Client::PC_ERROR);
21775 : 3 : fatal_error (loc, "unknown compiled module interface: %s",
21776 : 3 : packet.GetString ().c_str ());
21777 : : }
21778 : 5045 : }
21779 : :
21780 : : /* Figure out whether to treat HEADER as an include or an import. */
21781 : :
21782 : : static char *
21783 : 22136 : maybe_translate_include (cpp_reader *reader, line_maps *lmaps, location_t loc,
21784 : : const char *path)
21785 : : {
21786 : 22136 : if (!modules_p ())
21787 : : {
21788 : : /* Turn off. */
21789 : 0 : cpp_get_callbacks (reader)->translate_include = NULL;
21790 : 0 : return nullptr;
21791 : : }
21792 : :
21793 : 22136 : dump.push (NULL);
21794 : :
21795 : 24005 : dump () && dump ("Checking include translation '%s'", path);
21796 : 22136 : auto *mapper = get_mapper (cpp_main_loc (reader), cpp_get_deps (reader));
21797 : :
21798 : 22136 : size_t len = strlen (path);
21799 : 22136 : path = canonicalize_header_name (NULL, loc, true, path, len);
21800 : 22136 : auto packet = mapper->IncludeTranslate (path, Cody::Flags::None, len);
21801 : :
21802 : 22136 : enum class xlate_kind {
21803 : : unknown, text, import,
21804 : 22136 : } translate = xlate_kind::unknown;
21805 : :
21806 : 22136 : if (packet.GetCode () == Cody::Client::PC_BOOL)
21807 : 22089 : translate = packet.GetInteger () ? xlate_kind::text : xlate_kind::unknown;
21808 : 47 : else if (packet.GetCode () == Cody::Client::PC_PATHNAME)
21809 : : {
21810 : : /* Record the CMI name for when we do the import.
21811 : : We may already know about this import, but libcpp doesn't yet. */
21812 : 47 : module_state *import = get_module (build_string (len, path));
21813 : 47 : import->set_filename (packet);
21814 : 47 : translate = xlate_kind::import;
21815 : : }
21816 : : else
21817 : : {
21818 : 0 : gcc_checking_assert (packet.GetCode () == Cody::Client::PC_ERROR);
21819 : 0 : error_at (loc, "cannot determine %<#include%> translation of %s: %s",
21820 : 0 : path, packet.GetString ().c_str ());
21821 : : }
21822 : :
21823 : 22136 : bool note = false;
21824 : 22136 : if (note_include_translate_yes && translate == xlate_kind::import)
21825 : : note = true;
21826 : 22133 : else if (note_include_translate_no && translate == xlate_kind::unknown)
21827 : : note = true;
21828 : 22127 : else if (note_includes)
21829 : : /* We do not expect the note_includes vector to be large, so O(N)
21830 : : iteration. */
21831 : 410 : for (unsigned ix = note_includes->length (); !note && ix--;)
21832 : 205 : if (!strcmp ((*note_includes)[ix], path))
21833 : 1 : note = true;
21834 : :
21835 : 205 : if (note)
21836 : 16 : inform (loc, translate == xlate_kind::import
21837 : : ? G_("include %qs translated to import")
21838 : : : G_("include %qs processed textually"), path);
21839 : :
21840 : 25871 : dump () && dump (translate == xlate_kind::import
21841 : : ? "Translating include to import"
21842 : : : "Keeping include as include");
21843 : 22136 : dump.pop (0);
21844 : :
21845 : 22136 : if (translate != xlate_kind::import)
21846 : : return nullptr;
21847 : :
21848 : : /* Create the translation text. */
21849 : 47 : loc = ordinary_loc_of (lmaps, loc);
21850 : 47 : const line_map_ordinary *map
21851 : 47 : = linemap_check_ordinary (linemap_lookup (lmaps, loc));
21852 : 47 : unsigned col = SOURCE_COLUMN (map, loc);
21853 : 47 : col -= (col != 0); /* Columns are 1-based. */
21854 : :
21855 : 47 : unsigned alloc = len + col + 60;
21856 : 47 : char *res = XNEWVEC (char, alloc);
21857 : :
21858 : 47 : strcpy (res, "__import");
21859 : 47 : unsigned actual = 8;
21860 : 47 : if (col > actual)
21861 : : {
21862 : : /* Pad out so the filename appears at the same position. */
21863 : 44 : memset (res + actual, ' ', col - actual);
21864 : 44 : actual = col;
21865 : : }
21866 : : /* No need to encode characters, that's not how header names are
21867 : : handled. */
21868 : 47 : actual += snprintf (res + actual, alloc - actual,
21869 : : "\"%s\" [[__translated]];\n", path);
21870 : 47 : gcc_checking_assert (actual < alloc);
21871 : :
21872 : : /* cpplib will delete the buffer. */
21873 : : return res;
21874 : 22136 : }
21875 : :
21876 : : static void
21877 : 875 : begin_header_unit (cpp_reader *reader)
21878 : : {
21879 : : /* Set the module header name from the main_input_filename. */
21880 : 875 : const char *main = main_input_filename;
21881 : 875 : size_t len = strlen (main);
21882 : 875 : main = canonicalize_header_name (NULL, 0, true, main, len);
21883 : 875 : module_state *module = get_module (build_string (len, main));
21884 : :
21885 : 875 : preprocess_module (module, cpp_main_loc (reader), false, false, true, reader);
21886 : 875 : }
21887 : :
21888 : : /* We've just properly entered the main source file. I.e. after the
21889 : : command line, builtins and forced headers. Record the line map and
21890 : : location of this map. Note we may be called more than once. The
21891 : : first call sticks. */
21892 : :
21893 : : void
21894 : 95312 : module_begin_main_file (cpp_reader *reader, line_maps *lmaps,
21895 : : const line_map_ordinary *map)
21896 : : {
21897 : 95312 : gcc_checking_assert (lmaps == line_table);
21898 : 95312 : if (modules_p () && !spans.init_p ())
21899 : : {
21900 : 4283 : unsigned n = dump.push (NULL);
21901 : 4283 : spans.init (lmaps, map);
21902 : 4283 : dump.pop (n);
21903 : 4283 : if (flag_header_unit && !cpp_get_options (reader)->preprocessed)
21904 : : {
21905 : : /* Tell the preprocessor this is an include file. */
21906 : 866 : cpp_retrofit_as_include (reader);
21907 : 866 : begin_header_unit (reader);
21908 : : }
21909 : : }
21910 : 95312 : }
21911 : :
21912 : : /* Process the pending_import queue, making sure we know the
21913 : : filenames. */
21914 : :
21915 : : static void
21916 : 5140 : name_pending_imports (cpp_reader *reader)
21917 : : {
21918 : 5140 : auto *mapper = get_mapper (cpp_main_loc (reader), cpp_get_deps (reader));
21919 : :
21920 : 5140 : if (!vec_safe_length (pending_imports))
21921 : : /* Not doing anything. */
21922 : : return;
21923 : :
21924 : 4376 : timevar_start (TV_MODULE_MAPPER);
21925 : :
21926 : 4376 : auto n = dump.push (NULL);
21927 : 4927 : dump () && dump ("Resolving direct import names");
21928 : 4376 : bool want_deps = (bool (mapper->get_flags () & Cody::Flags::NameOnly)
21929 : 4376 : || cpp_get_deps (reader));
21930 : 4376 : bool any = false;
21931 : :
21932 : 9544 : for (unsigned ix = 0; ix != pending_imports->length (); ix++)
21933 : : {
21934 : 5168 : module_state *module = (*pending_imports)[ix];
21935 : 5168 : gcc_checking_assert (module->is_direct ());
21936 : 5168 : if (!module->filename && !module->visited_p)
21937 : : {
21938 : 5067 : bool export_p = (module->module_p
21939 : 5067 : && (module->is_partition () || module->exported_p));
21940 : :
21941 : 5067 : Cody::Flags flags = Cody::Flags::None;
21942 : 5067 : if (flag_preprocess_only
21943 : 5067 : && !(module->is_header () && !export_p))
21944 : : {
21945 : 129 : if (!want_deps)
21946 : 66 : continue;
21947 : : flags = Cody::Flags::NameOnly;
21948 : : }
21949 : :
21950 : 5001 : if (!any)
21951 : : {
21952 : 4306 : any = true;
21953 : 4306 : mapper->Cork ();
21954 : : }
21955 : 5001 : if (export_p)
21956 : 2569 : mapper->ModuleExport (module->get_flatname (), flags);
21957 : : else
21958 : 2432 : mapper->ModuleImport (module->get_flatname (), flags);
21959 : 5001 : module->visited_p = true;
21960 : : }
21961 : : }
21962 : :
21963 : 4376 : if (any)
21964 : : {
21965 : 4306 : auto response = mapper->Uncork ();
21966 : 4306 : auto r_iter = response.begin ();
21967 : 9389 : for (unsigned ix = 0; ix != pending_imports->length (); ix++)
21968 : : {
21969 : 5086 : module_state *module = (*pending_imports)[ix];
21970 : 5086 : if (module->visited_p)
21971 : : {
21972 : 5001 : module->visited_p = false;
21973 : 5001 : gcc_checking_assert (!module->filename);
21974 : :
21975 : 5001 : module->set_filename (*r_iter);
21976 : 4998 : ++r_iter;
21977 : : }
21978 : : }
21979 : 4303 : }
21980 : :
21981 : 4373 : dump.pop (n);
21982 : :
21983 : 4373 : timevar_stop (TV_MODULE_MAPPER);
21984 : : }
21985 : :
21986 : : /* We've just lexed a module-specific control line for MODULE. Mark
21987 : : the module as a direct import, and possibly load up its macro
21988 : : state. Returns the primary module, if this is a module
21989 : : declaration. */
21990 : : /* Perhaps we should offer a preprocessing mode where we read the
21991 : : directives from the header unit, rather than require the header's
21992 : : CMI. */
21993 : :
21994 : : module_state *
21995 : 5187 : preprocess_module (module_state *module, location_t from_loc,
21996 : : bool in_purview, bool is_import, bool is_export,
21997 : : cpp_reader *reader)
21998 : : {
21999 : 5187 : if (!is_import)
22000 : : {
22001 : 2884 : if (module->loc)
22002 : : /* It's already been mentioned, so ignore its module-ness. */
22003 : : is_import = true;
22004 : : else
22005 : : {
22006 : : /* Record it is the module. */
22007 : 2863 : module->module_p = true;
22008 : 2863 : if (is_export)
22009 : : {
22010 : 2567 : module->exported_p = true;
22011 : 2567 : module->interface_p = true;
22012 : : }
22013 : : }
22014 : : }
22015 : :
22016 : 5187 : if (module->directness < MD_DIRECT + in_purview)
22017 : : {
22018 : : /* Mark as a direct import. */
22019 : 5144 : module->directness = module_directness (MD_DIRECT + in_purview);
22020 : :
22021 : : /* Set the location to be most informative for users. */
22022 : 5144 : from_loc = ordinary_loc_of (line_table, from_loc);
22023 : 5144 : if (module->loadedness != ML_NONE)
22024 : 6 : linemap_module_reparent (line_table, module->loc, from_loc);
22025 : : else
22026 : : {
22027 : 5138 : module->loc = from_loc;
22028 : 5138 : if (!module->flatname)
22029 : 5111 : module->set_flatname ();
22030 : : }
22031 : : }
22032 : :
22033 : 5187 : auto desired = ML_CONFIG;
22034 : 5187 : if (is_import
22035 : 2324 : && module->is_header ()
22036 : 6057 : && (!cpp_get_options (reader)->preprocessed
22037 : 3 : || cpp_get_options (reader)->directives_only))
22038 : : /* We need preprocessor state now. */
22039 : : desired = ML_PREPROCESSOR;
22040 : :
22041 : 5187 : if (!is_import || module->loadedness < desired)
22042 : : {
22043 : 5168 : vec_safe_push (pending_imports, module);
22044 : :
22045 : 5168 : if (desired == ML_PREPROCESSOR)
22046 : : {
22047 : 851 : unsigned n = dump.push (NULL);
22048 : :
22049 : 1049 : dump () && dump ("Reading %M preprocessor state", module);
22050 : 851 : name_pending_imports (reader);
22051 : :
22052 : : /* Preserve the state of the line-map. */
22053 : 851 : auto pre_hwm = LINEMAPS_ORDINARY_USED (line_table);
22054 : :
22055 : : /* We only need to close the span, if we're going to emit a
22056 : : CMI. But that's a little tricky -- our token scanner
22057 : : needs to be smarter -- and this isn't much state.
22058 : : Remember, we've not parsed anything at this point, so
22059 : : our module state flags are inadequate. */
22060 : 851 : spans.maybe_init ();
22061 : 851 : spans.close ();
22062 : :
22063 : 851 : timevar_start (TV_MODULE_IMPORT);
22064 : :
22065 : : /* Load the config of each pending import -- we must assign
22066 : : module numbers monotonically. */
22067 : 1887 : for (unsigned ix = 0; ix != pending_imports->length (); ix++)
22068 : : {
22069 : 1036 : auto *import = (*pending_imports)[ix];
22070 : 1212 : if (!(import->module_p
22071 : 176 : && (import->is_partition () || import->exported_p))
22072 : 866 : && import->loadedness == ML_NONE
22073 : 1902 : && (import->is_header () || !flag_preprocess_only))
22074 : : {
22075 : 857 : unsigned n = dump.push (import);
22076 : 857 : import->do_import (reader, true);
22077 : 857 : dump.pop (n);
22078 : : }
22079 : : }
22080 : 851 : vec_free (pending_imports);
22081 : :
22082 : : /* Restore the line-map state. */
22083 : 851 : spans.open (linemap_module_restore (line_table, pre_hwm));
22084 : :
22085 : : /* Now read the preprocessor state of this particular
22086 : : import. */
22087 : 851 : if (module->loadedness == ML_CONFIG
22088 : 851 : && module->read_preprocessor (true))
22089 : 848 : module->import_macros ();
22090 : :
22091 : 851 : timevar_stop (TV_MODULE_IMPORT);
22092 : :
22093 : 851 : dump.pop (n);
22094 : : }
22095 : : }
22096 : :
22097 : 5187 : return is_import ? NULL : get_primary (module);
22098 : : }
22099 : :
22100 : : /* We've completed phase-4 translation. Emit any dependency
22101 : : information for the not-yet-loaded direct imports, and fill in
22102 : : their file names. We'll have already loaded up the direct header
22103 : : unit wavefront. */
22104 : :
22105 : : void
22106 : 4289 : preprocessed_module (cpp_reader *reader)
22107 : : {
22108 : 4289 : unsigned n = dump.push (NULL);
22109 : :
22110 : 4804 : dump () && dump ("Completed phase-4 (tokenization) processing");
22111 : :
22112 : 4289 : name_pending_imports (reader);
22113 : 4286 : vec_free (pending_imports);
22114 : :
22115 : 4286 : spans.maybe_init ();
22116 : 4286 : spans.close ();
22117 : :
22118 : 4286 : using iterator = hash_table<module_state_hash>::iterator;
22119 : 4286 : if (mkdeps *deps = cpp_get_deps (reader))
22120 : : {
22121 : : /* Walk the module hash, informing the dependency machinery. */
22122 : 51 : iterator end = modules_hash->end ();
22123 : 294 : for (iterator iter = modules_hash->begin (); iter != end; ++iter)
22124 : : {
22125 : 96 : module_state *module = *iter;
22126 : :
22127 : 96 : if (module->is_direct ())
22128 : : {
22129 : 81 : if (module->is_module ()
22130 : 81 : && (module->is_interface () || module->is_partition ()))
22131 : 33 : deps_add_module_target (deps, module->get_flatname (),
22132 : 33 : maybe_add_cmi_prefix (module->filename),
22133 : 33 : module->is_header (),
22134 : 33 : module->is_exported ());
22135 : : else
22136 : 48 : deps_add_module_dep (deps, module->get_flatname ());
22137 : : }
22138 : : }
22139 : : }
22140 : :
22141 : 4286 : if (flag_header_unit && !flag_preprocess_only)
22142 : : {
22143 : : /* Find the main module -- remember, it's not yet in the module
22144 : : array. */
22145 : 863 : iterator end = modules_hash->end ();
22146 : 1834 : for (iterator iter = modules_hash->begin (); iter != end; ++iter)
22147 : : {
22148 : 917 : module_state *module = *iter;
22149 : 917 : if (module->is_module ())
22150 : : {
22151 : 863 : declare_module (module, cpp_main_loc (reader), true, NULL, reader);
22152 : 863 : module_kind |= MK_EXPORTING;
22153 : 863 : break;
22154 : : }
22155 : : }
22156 : : }
22157 : :
22158 : 4286 : dump.pop (n);
22159 : 4286 : }
22160 : :
22161 : : /* VAL is a global tree, add it to the global vec if it is
22162 : : interesting. Add some of its targets, if they too are
22163 : : interesting. We do not add identifiers, as they can be re-found
22164 : : via the identifier hash table. There is a cost to the number of
22165 : : global trees. */
22166 : :
22167 : : static int
22168 : 2653515 : maybe_add_global (tree val, unsigned &crc)
22169 : : {
22170 : 2653515 : int v = 0;
22171 : :
22172 : 2653515 : if (val && !(identifier_p (val) || TREE_VISITED (val)))
22173 : : {
22174 : 817823 : TREE_VISITED (val) = true;
22175 : 817823 : crc = crc32_unsigned (crc, fixed_trees->length ());
22176 : 817823 : vec_safe_push (fixed_trees, val);
22177 : 817823 : v++;
22178 : :
22179 : 817823 : if (CODE_CONTAINS_STRUCT (TREE_CODE (val), TS_TYPED))
22180 : 817823 : v += maybe_add_global (TREE_TYPE (val), crc);
22181 : 817823 : if (CODE_CONTAINS_STRUCT (TREE_CODE (val), TS_TYPE_COMMON))
22182 : 536125 : v += maybe_add_global (TYPE_NAME (val), crc);
22183 : : }
22184 : :
22185 : 2653515 : return v;
22186 : : }
22187 : :
22188 : : /* Initialize module state. Create the hash table, determine the
22189 : : global trees. Create the module for current TU. */
22190 : :
22191 : : void
22192 : 4289 : init_modules (cpp_reader *reader)
22193 : : {
22194 : : /* PCH should not be reachable because of lang-specs, but the
22195 : : user could have overriden that. */
22196 : 4289 : if (pch_file)
22197 : 0 : fatal_error (input_location,
22198 : : "C++ modules are incompatible with precompiled headers");
22199 : :
22200 : 4289 : if (cpp_get_options (reader)->traditional)
22201 : 0 : fatal_error (input_location,
22202 : : "C++ modules are incompatible with traditional preprocessing");
22203 : :
22204 : : /* :: is always exported. */
22205 : 4289 : DECL_MODULE_EXPORT_P (global_namespace) = true;
22206 : :
22207 : 4289 : modules_hash = hash_table<module_state_hash>::create_ggc (31);
22208 : 4289 : vec_safe_reserve (modules, 20);
22209 : :
22210 : : /* Create module for current TU. */
22211 : 4289 : module_state *current
22212 : 4289 : = new (ggc_alloc<module_state> ()) module_state (NULL_TREE, NULL, false);
22213 : 4289 : current->mod = 0;
22214 : 4289 : bitmap_set_bit (current->imports, 0);
22215 : 4289 : modules->quick_push (current);
22216 : :
22217 : 4289 : gcc_checking_assert (!fixed_trees);
22218 : :
22219 : 4289 : headers = BITMAP_GGC_ALLOC ();
22220 : :
22221 : 4289 : if (note_includes)
22222 : : /* Canonicalize header names. */
22223 : 2 : for (unsigned ix = 0; ix != note_includes->length (); ix++)
22224 : : {
22225 : 1 : const char *hdr = (*note_includes)[ix];
22226 : 1 : size_t len = strlen (hdr);
22227 : :
22228 : 1 : bool system = hdr[0] == '<';
22229 : 1 : bool user = hdr[0] == '"';
22230 : 1 : bool delimed = system || user;
22231 : :
22232 : 1 : if (len <= (delimed ? 2 : 0)
22233 : 1 : || (delimed && hdr[len-1] != (system ? '>' : '"')))
22234 : 0 : error ("invalid header name %qs", hdr);
22235 : :
22236 : 1 : hdr = canonicalize_header_name (delimed ? reader : NULL,
22237 : : 0, !delimed, hdr, len);
22238 : 1 : char *path = XNEWVEC (char, len + 1);
22239 : 1 : memcpy (path, hdr, len);
22240 : 1 : path[len] = 0;
22241 : :
22242 : 1 : (*note_includes)[ix] = path;
22243 : : }
22244 : :
22245 : 4289 : if (note_cmis)
22246 : : /* Canonicalize & mark module names. */
22247 : 12 : for (unsigned ix = 0; ix != note_cmis->length (); ix++)
22248 : : {
22249 : 6 : const char *name = (*note_cmis)[ix];
22250 : 6 : size_t len = strlen (name);
22251 : :
22252 : 6 : bool is_system = name[0] == '<';
22253 : 6 : bool is_user = name[0] == '"';
22254 : 6 : bool is_pathname = false;
22255 : 6 : if (!(is_system || is_user))
22256 : 12 : for (unsigned ix = len; !is_pathname && ix--;)
22257 : 9 : is_pathname = IS_DIR_SEPARATOR (name[ix]);
22258 : 6 : if (is_system || is_user || is_pathname)
22259 : : {
22260 : 3 : if (len <= (is_pathname ? 0 : 2)
22261 : 3 : || (!is_pathname && name[len-1] != (is_system ? '>' : '"')))
22262 : : {
22263 : 0 : error ("invalid header name %qs", name);
22264 : 0 : continue;
22265 : : }
22266 : : else
22267 : 3 : name = canonicalize_header_name (is_pathname ? nullptr : reader,
22268 : : 0, is_pathname, name, len);
22269 : : }
22270 : 6 : if (auto module = get_module (name))
22271 : 6 : module->inform_cmi_p = 1;
22272 : : else
22273 : 0 : error ("invalid module name %qs", name);
22274 : : }
22275 : :
22276 : 4289 : dump.push (NULL);
22277 : :
22278 : : /* Determine lazy handle bound. */
22279 : 4289 : {
22280 : 4289 : unsigned limit = 1000;
22281 : : #if HAVE_GETRLIMIT
22282 : 4289 : struct rlimit rlimit;
22283 : 4289 : if (!getrlimit (RLIMIT_NOFILE, &rlimit))
22284 : : {
22285 : 4289 : lazy_hard_limit = (rlimit.rlim_max < 1000000
22286 : 4289 : ? unsigned (rlimit.rlim_max) : 1000000);
22287 : 4289 : lazy_hard_limit = (lazy_hard_limit > LAZY_HEADROOM
22288 : 4289 : ? lazy_hard_limit - LAZY_HEADROOM : 0);
22289 : 4289 : if (rlimit.rlim_cur < limit)
22290 : 0 : limit = unsigned (rlimit.rlim_cur);
22291 : : }
22292 : : #endif
22293 : 4289 : limit = limit > LAZY_HEADROOM ? limit - LAZY_HEADROOM : 1;
22294 : :
22295 : 4289 : if (unsigned parm = param_lazy_modules)
22296 : : {
22297 : 4289 : if (parm <= limit || !lazy_hard_limit || !try_increase_lazy (parm))
22298 : 6 : lazy_limit = parm;
22299 : : }
22300 : : else
22301 : 0 : lazy_limit = limit;
22302 : : }
22303 : :
22304 : 4289 : if (dump ())
22305 : : {
22306 : 515 : verstr_t ver;
22307 : 515 : version2string (MODULE_VERSION, ver);
22308 : 515 : dump ("Source: %s", main_input_filename);
22309 : 515 : dump ("Compiler: %s", version_string);
22310 : 515 : dump ("Modules: %s", ver);
22311 : 515 : dump ("Checking: %s",
22312 : : #if CHECKING_P
22313 : : "checking"
22314 : : #elif ENABLE_ASSERT_CHECKING
22315 : : "asserting"
22316 : : #else
22317 : : "release"
22318 : : #endif
22319 : : );
22320 : 515 : dump ("Compiled by: "
22321 : : #ifdef __GNUC__
22322 : : "GCC %d.%d, %s", __GNUC__, __GNUC_MINOR__,
22323 : : #ifdef __OPTIMIZE__
22324 : : "optimizing"
22325 : : #else
22326 : : "not optimizing"
22327 : : #endif
22328 : : #else
22329 : : "not GCC"
22330 : : #endif
22331 : : );
22332 : 515 : dump ("Reading: %s", MAPPED_READING ? "mmap" : "fileio");
22333 : 515 : dump ("Writing: %s", MAPPED_WRITING ? "mmap" : "fileio");
22334 : 515 : dump ("Lazy limit: %u", lazy_limit);
22335 : 515 : dump ("Lazy hard limit: %u", lazy_hard_limit);
22336 : 515 : dump ("");
22337 : : }
22338 : :
22339 : : /* Construct the global tree array. This is an array of unique
22340 : : global trees (& types). Do this now, rather than lazily, as
22341 : : some global trees are lazily created and we don't want that to
22342 : : mess with our syndrome of fixed trees. */
22343 : 4289 : unsigned crc = 0;
22344 : 4289 : vec_alloc (fixed_trees, 250);
22345 : :
22346 : 4804 : dump () && dump ("+Creating globals");
22347 : : /* Insert the TRANSLATION_UNIT_DECL. */
22348 : 4289 : TREE_VISITED (DECL_CONTEXT (global_namespace)) = true;
22349 : 4289 : fixed_trees->quick_push (DECL_CONTEXT (global_namespace));
22350 : 25734 : for (unsigned jx = 0; global_tree_arys[jx].first; jx++)
22351 : : {
22352 : 21445 : const tree *ptr = global_tree_arys[jx].first;
22353 : 21445 : unsigned limit = global_tree_arys[jx].second;
22354 : :
22355 : 1308145 : for (unsigned ix = 0; ix != limit; ix++, ptr++)
22356 : : {
22357 : 1286700 : !(ix & 31) && dump ("") && dump ("+\t%u:%u:", jx, ix);
22358 : 1286700 : unsigned v = maybe_add_global (*ptr, crc);
22359 : 1441200 : dump () && dump ("+%u", v);
22360 : : }
22361 : : }
22362 : : /* OS- and machine-specific types are dynamically registered at
22363 : : runtime, so cannot be part of global_tree_arys. */
22364 : 4289 : registered_builtin_types && dump ("") && dump ("+\tB:");
22365 : 17156 : for (tree t = registered_builtin_types; t; t = TREE_CHAIN (t))
22366 : : {
22367 : 12867 : unsigned v = maybe_add_global (TREE_VALUE (t), crc);
22368 : 14412 : dump () && dump ("+%u", v);
22369 : : }
22370 : 4289 : global_crc = crc32_unsigned (crc, fixed_trees->length ());
22371 : 4289 : dump ("") && dump ("Created %u unique globals, crc=%x",
22372 : : fixed_trees->length (), global_crc);
22373 : 826401 : for (unsigned ix = fixed_trees->length (); ix--;)
22374 : 822112 : TREE_VISITED ((*fixed_trees)[ix]) = false;
22375 : :
22376 : 4289 : dump.pop (0);
22377 : :
22378 : 4289 : if (!flag_module_lazy)
22379 : : /* Get the mapper now, if we're not being lazy. */
22380 : 274 : get_mapper (cpp_main_loc (reader), cpp_get_deps (reader));
22381 : :
22382 : 4289 : if (!flag_preprocess_only)
22383 : : {
22384 : 4178 : pending_table = new pending_map_t (EXPERIMENT (1, 400));
22385 : 4178 : entity_map = new entity_map_t (EXPERIMENT (1, 400));
22386 : 4178 : vec_safe_reserve (entity_ary, EXPERIMENT (1, 400));
22387 : 4178 : imported_temploid_friends
22388 : 4178 : = decl_tree_cache_map::create_ggc (EXPERIMENT (1, 400));
22389 : : }
22390 : :
22391 : : #if CHECKING_P
22392 : 4289 : note_defs = note_defs_table_t::create_ggc (1000);
22393 : : #endif
22394 : :
22395 : 4289 : if (flag_header_unit && cpp_get_options (reader)->preprocessed)
22396 : 9 : begin_header_unit (reader);
22397 : :
22398 : : /* Collect here to make sure things are tagged correctly (when
22399 : : aggressively GC'd). */
22400 : 4289 : ggc_collect ();
22401 : 4289 : }
22402 : :
22403 : : /* If NODE is a deferred macro, load it. */
22404 : :
22405 : : static int
22406 : 82449 : load_macros (cpp_reader *reader, cpp_hashnode *node, void *)
22407 : : {
22408 : 82449 : location_t main_loc
22409 : 82449 : = MAP_START_LOCATION (LINEMAPS_ORDINARY_MAP_AT (line_table, 0));
22410 : :
22411 : 82449 : if (cpp_user_macro_p (node)
22412 : 82449 : && !node->value.macro)
22413 : : {
22414 : 72 : cpp_macro *macro = cpp_get_deferred_macro (reader, node, main_loc);
22415 : 72 : dump () && dump ("Loaded macro #%s %I",
22416 : : macro ? "define" : "undef", identifier (node));
22417 : : }
22418 : :
22419 : 82449 : return 1;
22420 : : }
22421 : :
22422 : : /* At the end of tokenizing, we no longer need the macro tables of
22423 : : imports. But the user might have requested some checking. */
22424 : :
22425 : : void
22426 : 93743 : maybe_check_all_macros (cpp_reader *reader)
22427 : : {
22428 : 93743 : if (!warn_imported_macros)
22429 : : return;
22430 : :
22431 : : /* Force loading of any remaining deferred macros. This will
22432 : : produce diagnostics if they are ill-formed. */
22433 : 21 : unsigned n = dump.push (NULL);
22434 : 21 : cpp_forall_identifiers (reader, load_macros, NULL);
22435 : 21 : dump.pop (n);
22436 : : }
22437 : :
22438 : : // State propagated from finish_module_processing to fini_modules
22439 : :
22440 : : struct module_processing_cookie
22441 : : {
22442 : : elf_out out;
22443 : : module_state_config config;
22444 : : char *cmi_name;
22445 : : char *tmp_name;
22446 : : unsigned crc;
22447 : : bool began;
22448 : :
22449 : 2518 : module_processing_cookie (char *cmi, char *tmp, int fd, int e)
22450 : 2518 : : out (fd, e), cmi_name (cmi), tmp_name (tmp), crc (0), began (false)
22451 : : {
22452 : : }
22453 : 2518 : ~module_processing_cookie ()
22454 : : {
22455 : 2518 : XDELETEVEC (tmp_name);
22456 : 2518 : XDELETEVEC (cmi_name);
22457 : 2518 : }
22458 : : };
22459 : :
22460 : : /* Write the CMI, if we're a module interface. */
22461 : :
22462 : : void *
22463 : 93543 : finish_module_processing (cpp_reader *reader)
22464 : : {
22465 : 93543 : module_processing_cookie *cookie = nullptr;
22466 : :
22467 : 93543 : if (header_module_p ())
22468 : 863 : module_kind &= ~MK_EXPORTING;
22469 : :
22470 : 93543 : if (!modules || !(*modules)[0]->name)
22471 : : {
22472 : 91022 : if (flag_module_only)
22473 : 6 : warning (0, "%<-fmodule-only%> used for non-interface");
22474 : : }
22475 : 2521 : else if (!flag_syntax_only)
22476 : : {
22477 : 2518 : int fd = -1;
22478 : 2518 : int e = -1;
22479 : :
22480 : 2518 : timevar_start (TV_MODULE_EXPORT);
22481 : :
22482 : : /* Force a valid but empty line map at the end. This simplifies
22483 : : the line table preparation and writing logic. */
22484 : 2518 : linemap_add (line_table, LC_ENTER, false, "", 0);
22485 : :
22486 : : /* We write to a tmpname, and then atomically rename. */
22487 : 2518 : char *cmi_name = NULL;
22488 : 2518 : char *tmp_name = NULL;
22489 : 2518 : module_state *state = (*modules)[0];
22490 : :
22491 : 2518 : unsigned n = dump.push (state);
22492 : 2518 : state->announce ("creating");
22493 : 2518 : if (state->filename)
22494 : : {
22495 : 2518 : size_t len = 0;
22496 : 2518 : cmi_name = xstrdup (maybe_add_cmi_prefix (state->filename, &len));
22497 : 2518 : tmp_name = XNEWVEC (char, len + 3);
22498 : 2518 : memcpy (tmp_name, cmi_name, len);
22499 : 2518 : strcpy (&tmp_name[len], "~");
22500 : :
22501 : 2518 : if (!errorcount)
22502 : 34 : for (unsigned again = 2; ; again--)
22503 : : {
22504 : 2449 : fd = open (tmp_name,
22505 : : O_RDWR | O_CREAT | O_TRUNC | O_CLOEXEC | O_BINARY,
22506 : : S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP|S_IROTH|S_IWOTH);
22507 : 2449 : e = errno;
22508 : 2449 : if (fd >= 0 || !again || e != ENOENT)
22509 : : break;
22510 : 34 : create_dirs (tmp_name);
22511 : : }
22512 : 2518 : if (note_module_cmi_yes || state->inform_cmi_p)
22513 : 3 : inform (state->loc, "writing CMI %qs", cmi_name);
22514 : 2769 : dump () && dump ("CMI is %s", cmi_name);
22515 : : }
22516 : :
22517 : 2518 : cookie = new module_processing_cookie (cmi_name, tmp_name, fd, e);
22518 : :
22519 : 2518 : if (errorcount)
22520 : : /* Don't write the module if we have reported errors. */;
22521 : 2415 : else if (erroneous_templates
22522 : 2415 : && !erroneous_templates->is_empty ())
22523 : : {
22524 : : /* Don't write the module if it contains an erroneous template.
22525 : : Also emit notes about where errors occurred in case
22526 : : -Wno-template-body was passed. */
22527 : 6 : auto_diagnostic_group d;
22528 : 6 : error_at (state->loc, "not writing module %qs due to errors "
22529 : : "in template bodies", state->get_flatname ());
22530 : 6 : if (!warn_template_body)
22531 : 3 : inform (state->loc, "enable %<-Wtemplate-body%> for more details");
22532 : 12 : for (auto e : *erroneous_templates)
22533 : 6 : inform (e.second, "first error in %qD appeared here", e.first);
22534 : 6 : }
22535 : 2409 : else if (cookie->out.begin ())
22536 : : {
22537 : : /* So crashes finger-point the module decl. */
22538 : 2409 : iloc_sentinel ils = state->loc;
22539 : 2409 : if (state->write_begin (&cookie->out, reader, cookie->config,
22540 : 2409 : cookie->crc))
22541 : 2381 : cookie->began = true;
22542 : 2409 : }
22543 : :
22544 : 2518 : dump.pop (n);
22545 : 2518 : timevar_stop (TV_MODULE_EXPORT);
22546 : :
22547 : 2518 : ggc_collect ();
22548 : : }
22549 : :
22550 : 93543 : if (modules)
22551 : : {
22552 : 4141 : unsigned n = dump.push (NULL);
22553 : 4656 : dump () && dump ("Imported %u modules", modules->length () - 1);
22554 : 4656 : dump () && dump ("Containing %u clusters", available_clusters);
22555 : 4141 : dump () && dump ("Loaded %u clusters (%u%%)", loaded_clusters,
22556 : 515 : (loaded_clusters * 100 + available_clusters / 2) /
22557 : 515 : (available_clusters + !available_clusters));
22558 : 4141 : dump.pop (n);
22559 : : }
22560 : :
22561 : 93543 : return cookie;
22562 : : }
22563 : :
22564 : : // Do the final emission of a module. At this point we know whether
22565 : : // the module static initializer is a NOP or not.
22566 : :
22567 : : static void
22568 : 2518 : late_finish_module (cpp_reader *reader, module_processing_cookie *cookie,
22569 : : bool init_fn_non_empty)
22570 : : {
22571 : 2518 : timevar_start (TV_MODULE_EXPORT);
22572 : :
22573 : 2518 : module_state *state = (*modules)[0];
22574 : 2518 : unsigned n = dump.push (state);
22575 : 2518 : state->announce ("finishing");
22576 : :
22577 : 2518 : cookie->config.active_init = init_fn_non_empty;
22578 : 2518 : if (cookie->began)
22579 : 2381 : state->write_end (&cookie->out, reader, cookie->config, cookie->crc);
22580 : :
22581 : 2518 : if (cookie->out.end () && cookie->cmi_name)
22582 : : {
22583 : : /* Some OS's do not replace NEWNAME if it already exists.
22584 : : This'll have a race condition in erroneous concurrent
22585 : : builds. */
22586 : 2415 : unlink (cookie->cmi_name);
22587 : 2415 : if (rename (cookie->tmp_name, cookie->cmi_name))
22588 : : {
22589 : 0 : dump () && dump ("Rename ('%s','%s') errno=%u",
22590 : 0 : cookie->tmp_name, cookie->cmi_name, errno);
22591 : 0 : cookie->out.set_error (errno);
22592 : : }
22593 : : }
22594 : :
22595 : 2518 : if (cookie->out.get_error () && cookie->began)
22596 : : {
22597 : 0 : error_at (state->loc, "failed to write compiled module: %s",
22598 : 0 : cookie->out.get_error (state->filename));
22599 : 0 : state->note_cmi_name ();
22600 : : }
22601 : :
22602 : 2518 : if (!errorcount)
22603 : : {
22604 : 2378 : auto *mapper = get_mapper (cpp_main_loc (reader), cpp_get_deps (reader));
22605 : 2378 : mapper->ModuleCompiled (state->get_flatname ());
22606 : : }
22607 : 140 : else if (cookie->cmi_name)
22608 : : {
22609 : : /* We failed, attempt to erase all evidence we even tried. */
22610 : 140 : unlink (cookie->tmp_name);
22611 : 140 : unlink (cookie->cmi_name);
22612 : : }
22613 : :
22614 : 2518 : delete cookie;
22615 : 2518 : dump.pop (n);
22616 : 2518 : timevar_stop (TV_MODULE_EXPORT);
22617 : 2518 : }
22618 : :
22619 : : void
22620 : 93543 : fini_modules (cpp_reader *reader, void *cookie, bool has_inits)
22621 : : {
22622 : 93543 : if (cookie)
22623 : 2518 : late_finish_module (reader,
22624 : : static_cast<module_processing_cookie *> (cookie),
22625 : : has_inits);
22626 : :
22627 : : /* We're done with the macro tables now. */
22628 : 93543 : vec_free (macro_exports);
22629 : 93543 : vec_free (macro_imports);
22630 : 93543 : headers = NULL;
22631 : :
22632 : : /* We're now done with everything but the module names. */
22633 : 93543 : set_cmi_repo (NULL);
22634 : 93543 : if (mapper)
22635 : : {
22636 : 4141 : timevar_start (TV_MODULE_MAPPER);
22637 : 4141 : module_client::close_module_client (0, mapper);
22638 : 4141 : mapper = nullptr;
22639 : 4141 : timevar_stop (TV_MODULE_MAPPER);
22640 : : }
22641 : 93543 : module_state_config::release ();
22642 : :
22643 : : #if CHECKING_P
22644 : 93543 : note_defs = NULL;
22645 : : #endif
22646 : :
22647 : 93543 : if (modules)
22648 : 6695 : for (unsigned ix = modules->length (); --ix;)
22649 : 2554 : if (module_state *state = (*modules)[ix])
22650 : 2554 : state->release ();
22651 : :
22652 : : /* No need to lookup modules anymore. */
22653 : 93543 : modules_hash = NULL;
22654 : :
22655 : : /* Or entity array. We still need the entity map to find import numbers. */
22656 : 93543 : vec_free (entity_ary);
22657 : 93543 : entity_ary = NULL;
22658 : :
22659 : : /* Or remember any pending entities. */
22660 : 97684 : delete pending_table;
22661 : 93543 : pending_table = NULL;
22662 : :
22663 : : /* Or any keys -- Let it go! */
22664 : 95626 : delete keyed_table;
22665 : 93543 : keyed_table = NULL;
22666 : :
22667 : : /* Allow a GC, we've possibly made much data unreachable. */
22668 : 93543 : ggc_collect ();
22669 : 93543 : }
22670 : :
22671 : : /* If CODE is a module option, handle it & return true. Otherwise
22672 : : return false. For unknown reasons I cannot get the option
22673 : : generation machinery to set fmodule-mapper or -fmodule-header to
22674 : : make a string type option variable. */
22675 : :
22676 : : bool
22677 : 1853632 : handle_module_option (unsigned code, const char *str, int)
22678 : : {
22679 : 1853632 : auto hdr = CMS_header;
22680 : :
22681 : 1853632 : switch (opt_code (code))
22682 : : {
22683 : 42 : case OPT_fmodule_mapper_:
22684 : 42 : module_mapper_name = str;
22685 : 42 : return true;
22686 : :
22687 : 11 : case OPT_fmodule_header_:
22688 : 11 : {
22689 : 11 : if (!strcmp (str, "user"))
22690 : : hdr = CMS_user;
22691 : 11 : else if (!strcmp (str, "system"))
22692 : : hdr = CMS_system;
22693 : : else
22694 : 0 : error ("unknown header kind %qs", str);
22695 : : }
22696 : : /* Fallthrough. */
22697 : :
22698 : 878 : case OPT_fmodule_header:
22699 : 878 : flag_header_unit = hdr;
22700 : 878 : flag_modules = 1;
22701 : 878 : return true;
22702 : :
22703 : 1 : case OPT_flang_info_include_translate_:
22704 : 1 : vec_safe_push (note_includes, str);
22705 : 1 : return true;
22706 : :
22707 : 6 : case OPT_flang_info_module_cmi_:
22708 : 6 : vec_safe_push (note_cmis, str);
22709 : 6 : return true;
22710 : :
22711 : : default:
22712 : : return false;
22713 : : }
22714 : : }
22715 : :
22716 : : /* Set preprocessor callbacks and options for modules. */
22717 : :
22718 : : void
22719 : 95118 : module_preprocess_options (cpp_reader *reader)
22720 : : {
22721 : 95118 : gcc_checking_assert (!lang_hooks.preprocess_undef);
22722 : 95118 : if (modules_p ())
22723 : : {
22724 : 4289 : auto *cb = cpp_get_callbacks (reader);
22725 : :
22726 : 4289 : cb->translate_include = maybe_translate_include;
22727 : 4289 : cb->user_deferred_macro = module_state::deferred_macro;
22728 : 4289 : if (flag_header_unit)
22729 : : {
22730 : : /* If the preprocessor hook is already in use, that
22731 : : implementation will call the undef langhook. */
22732 : 875 : if (cb->undef)
22733 : 0 : lang_hooks.preprocess_undef = module_state::undef_macro;
22734 : : else
22735 : 875 : cb->undef = module_state::undef_macro;
22736 : : }
22737 : 4289 : auto *opt = cpp_get_options (reader);
22738 : 4289 : opt->module_directives = true;
22739 : 4289 : if (flag_no_output)
22740 : 12 : opt->directives_only = true;
22741 : 4289 : if (opt->main_search == CMS_none)
22742 : 4289 : opt->main_search = cpp_main_search (flag_header_unit);
22743 : : }
22744 : 95118 : }
22745 : :
22746 : : #include "gt-cp-module.h"
|