Branch data Line data Source code
1 : : /* C++ modules. Experimental!
2 : : Copyright (C) 2017-2025 Free Software Foundation, Inc.
3 : : Written by Nathan Sidwell <nathan@acm.org> while at FaceBook
4 : :
5 : : This file is part of GCC.
6 : :
7 : : GCC is free software; you can redistribute it and/or modify it
8 : : under the terms of the GNU General Public License as published by
9 : : the Free Software Foundation; either version 3, or (at your option)
10 : : any later version.
11 : :
12 : : GCC is distributed in the hope that it will be useful, but
13 : : WITHOUT ANY WARRANTY; without even the implied warranty of
14 : : MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 : : General Public License for more details.
16 : :
17 : : You should have received a copy of the GNU General Public License
18 : : along with GCC; see the file COPYING3. If not see
19 : : <http://www.gnu.org/licenses/>. */
20 : :
21 : : /* Comments in this file have a non-negligible chance of being wrong
22 : : or at least inaccurate. Due to (a) my misunderstanding, (b)
23 : : ambiguities that I have interpretted differently to original intent
24 : : (c) changes in the specification, (d) my poor wording, (e) source
25 : : changes. */
26 : :
27 : : /* (Incomplete) Design Notes
28 : :
29 : : A hash table contains all module names. Imported modules are
30 : : present in a modules array, which by construction places an
31 : : import's dependencies before the import itself. The single
32 : : exception is the current TU, which always occupies slot zero (even
33 : : when it is not a module).
34 : :
35 : : Imported decls occupy an entity_ary, an array of binding_slots, indexed
36 : : by importing module and index within that module. A flat index is
37 : : used, as each module reserves a contiguous range of indices.
38 : : Initially each slot indicates the CMI section containing the
39 : : streamed decl. When the decl is imported it will point to the decl
40 : : itself.
41 : :
42 : : Additionally each imported decl is mapped in the entity_map via its
43 : : DECL_UID to the flat index in the entity_ary. Thus we can locate
44 : : the index for any imported decl by using this map and then
45 : : de-flattening the index via a binary seach of the module vector.
46 : : Cross-module references are by (remapped) module number and
47 : : module-local index.
48 : :
49 : : Each importable DECL contains several flags. The simple set are
50 : : DECL_MODULE_EXPORT_P, DECL_MODULE_PURVIEW_P, DECL_MODULE_ATTACH_P
51 : : and DECL_MODULE_IMPORT_P. The first indicates whether it is
52 : : exported, the second whether it is in module or header-unit
53 : : purview. The third indicates it is attached to the named module in
54 : : whose purview it resides and the fourth indicates whether it was an
55 : : import into this TU or not. DECL_MODULE_ATTACH_P will be false for
56 : : all decls in a header-unit, and for those in a named module inside
57 : : a linkage declaration.
58 : :
59 : : The more detailed flags are DECL_MODULE_PARTITION_P,
60 : : DECL_MODULE_ENTITY_P. The first is set in a primary interface unit
61 : : on decls that were read from module partitions (these will have
62 : : DECL_MODULE_IMPORT_P set too). Such decls will be streamed out to
63 : : the primary's CMI. DECL_MODULE_ENTITY_P is set when an entity is
64 : : imported, even if it matched a non-imported entity. Such a decl
65 : : will not have DECL_MODULE_IMPORT_P set, even though it has an entry
66 : : in the entity map and array.
67 : :
68 : : Header units are module-like.
69 : :
70 : : For namespace-scope lookup, the decls for a particular module are
71 : : held located in a sparse array hanging off the binding of the name.
72 : : This is partitioned into two: a few fixed slots at the start
73 : : followed by the sparse slots afterwards. By construction we only
74 : : need to append new slots to the end -- there is never a need to
75 : : insert in the middle. The fixed slots are MODULE_SLOT_CURRENT for
76 : : the current TU (regardless of whether it is a module or not),
77 : : MODULE_SLOT_GLOBAL and MODULE_SLOT_PARTITION. These latter two
78 : : slots are used for merging entities across the global module and
79 : : module partitions respectively. MODULE_SLOT_PARTITION is only
80 : : present in a module. Neither of those two slots is searched during
81 : : name lookup -- they are internal use only. This vector is created
82 : : lazily once we require it, if there is only a declaration from the
83 : : current TU, a regular binding is present. It is converted on
84 : : demand.
85 : :
86 : : OPTIMIZATION: Outside of the current TU, we only need ADL to work.
87 : : We could optimize regular lookup for the current TU by glomming all
88 : : the visible decls on its slot. Perhaps wait until design is a
89 : : little more settled though.
90 : :
91 : : There is only one instance of each extern-linkage namespace. It
92 : : appears in every module slot that makes it visible. It also
93 : : appears in MODULE_SLOT_GLOBAL. (It is an ODR violation if they
94 : : collide with some other global module entity.) We also have an
95 : : optimization that shares the slot for adjacent modules that declare
96 : : the same such namespace.
97 : :
98 : : A module interface compilation produces a Compiled Module Interface
99 : : (CMI). The format used is Encapsulated Lazy Records Of Numbered
100 : : Declarations, which is essentially ELF's section encapsulation. (As
101 : : all good nerds are aware, Elrond is half Elf.) Some sections are
102 : : named, and contain information about the module as a whole (indices
103 : : etc), and other sections are referenced by number. Although I
104 : : don't defend against actively hostile CMIs, there is some
105 : : checksumming involved to verify data integrity. When dumping out
106 : : an interface, we generate a graph of all the
107 : : independently-redeclarable DECLS that are needed, and the decls
108 : : they reference. From that we determine the strongly connected
109 : : components (SCC) within this TU. Each SCC is dumped to a separate
110 : : numbered section of the CMI. We generate a binding table section,
111 : : mapping each namespace&name to a defining section. This allows
112 : : lazy loading.
113 : :
114 : : Lazy loading employs mmap to map a read-only image of the CMI.
115 : : It thus only occupies address space and is paged in on demand,
116 : : backed by the CMI file itself. If mmap is unavailable, regular
117 : : FILEIO is used. Also, there's a bespoke ELF reader/writer here,
118 : : which implements just the section table and sections (including
119 : : string sections) of a 32-bit ELF in host byte-order. You can of
120 : : course inspect it with readelf. I figured 32-bit is sufficient,
121 : : for a single module. I detect running out of section numbers, but
122 : : do not implement the ELF overflow mechanism. At least you'll get
123 : : an error if that happens.
124 : :
125 : : We do not separate declarations and definitions. My guess is that
126 : : if you refer to the declaration, you'll also need the definition
127 : : (template body, inline function, class definition etc). But this
128 : : does mean we can get larger SCCs than if we separated them. It is
129 : : unclear whether this is a win or not.
130 : :
131 : : Notice that we embed section indices into the contents of other
132 : : sections. Thus random manipulation of the CMI file by ELF tools
133 : : may well break it. The kosher way would probably be to introduce
134 : : indirection via section symbols, but that would require defining a
135 : : relocation type.
136 : :
137 : : Notice that lazy loading of one module's decls can cause lazy
138 : : loading of other decls in the same or another module. Clearly we
139 : : want to avoid loops. In a correct program there can be no loops in
140 : : the module dependency graph, and the above-mentioned SCC algorithm
141 : : places all intra-module circular dependencies in the same SCC. It
142 : : also orders the SCCs wrt each other, so dependent SCCs come first.
143 : : As we load dependent modules first, we know there can be no
144 : : reference to a higher-numbered module, and because we write out
145 : : dependent SCCs first, likewise for SCCs within the module. This
146 : : allows us to immediately detect broken references. When loading,
147 : : we must ensure the rest of the compiler doesn't cause some
148 : : unconnected load to occur (for instance, instantiate a template).
149 : :
150 : : Classes used:
151 : :
152 : : dumper - logger
153 : :
154 : : data - buffer
155 : :
156 : : bytes_in : data - scalar reader
157 : : bytes_out : data - scalar writer
158 : :
159 : : bytes_in::bits_in - bit stream reader
160 : : bytes_out::bits_out - bit stream writer
161 : :
162 : : elf - ELROND format
163 : : elf_in : elf - ELROND reader
164 : : elf_out : elf - ELROND writer
165 : :
166 : : trees_in : bytes_in - tree reader
167 : : trees_out : bytes_out - tree writer
168 : :
169 : : depset - dependency set
170 : : depset::hash - hash table of depsets
171 : : depset::tarjan - SCC determinator
172 : :
173 : : uidset<T> - set T's related to a UID
174 : : uidset<T>::hash hash table of uidset<T>
175 : :
176 : : loc_spans - location map data
177 : :
178 : : module_state - module object
179 : :
180 : : slurping - data needed during loading
181 : :
182 : : macro_import - imported macro data
183 : : macro_export - exported macro data
184 : :
185 : : The ELROND objects use mmap, for both reading and writing. If mmap
186 : : is unavailable, fileno IO is used to read and write blocks of data.
187 : :
188 : : The mapper object uses fileno IO to communicate with the server or
189 : : program. */
190 : :
191 : : /* In expermental (trunk) sources, MODULE_VERSION is a #define passed
192 : : in from the Makefile. It records the modification date of the
193 : : source directory -- that's the only way to stay sane. In release
194 : : sources, we (plan to) use the compiler's major.minor versioning.
195 : : While the format might not change between at minor versions, it
196 : : seems simplest to tie the two together. There's no concept of
197 : : inter-version compatibility. */
198 : : #define IS_EXPERIMENTAL(V) ((V) >= (1U << 20))
199 : : #define MODULE_MAJOR(V) ((V) / 10000)
200 : : #define MODULE_MINOR(V) ((V) % 10000)
201 : : #define EXPERIMENT(A,B) (IS_EXPERIMENTAL (MODULE_VERSION) ? (A) : (B))
202 : : #ifndef MODULE_VERSION
203 : : #include "bversion.h"
204 : : #define MODULE_VERSION (BUILDING_GCC_MAJOR * 10000U + BUILDING_GCC_MINOR)
205 : : #elif !IS_EXPERIMENTAL (MODULE_VERSION)
206 : : #error "This is not the version I was looking for."
207 : : #endif
208 : :
209 : : #define _DEFAULT_SOURCE 1 /* To get TZ field of struct tm, if available. */
210 : : #include "config.h"
211 : : #define INCLUDE_STRING
212 : : #define INCLUDE_VECTOR
213 : : #include "system.h"
214 : : #include "coretypes.h"
215 : : #include "cp-tree.h"
216 : : #include "timevar.h"
217 : : #include "stringpool.h"
218 : : #include "dumpfile.h"
219 : : #include "bitmap.h"
220 : : #include "cgraph.h"
221 : : #include "varasm.h"
222 : : #include "tree-iterator.h"
223 : : #include "cpplib.h"
224 : : #include "mkdeps.h"
225 : : #include "incpath.h"
226 : : #include "libiberty.h"
227 : : #include "stor-layout.h"
228 : : #include "version.h"
229 : : #include "tree-diagnostic.h"
230 : : #include "toplev.h"
231 : : #include "opts.h"
232 : : #include "attribs.h"
233 : : #include "intl.h"
234 : : #include "langhooks.h"
235 : : #include "contracts.h"
236 : : /* This TU doesn't need or want to see the networking. */
237 : : #define CODY_NETWORKING 0
238 : : #include "mapper-client.h"
239 : : #include <zlib.h> // for crc32, crc32_combine
240 : :
241 : : #if 0 // 1 for testing no mmap
242 : : #define MAPPED_READING 0
243 : : #define MAPPED_WRITING 0
244 : : #else
245 : : #if HAVE_MMAP_FILE && HAVE_MUNMAP && HAVE_MSYNC
246 : : /* mmap, munmap, msync. */
247 : : #define MAPPED_READING 1
248 : : #if HAVE_SYSCONF && defined (_SC_PAGE_SIZE)
249 : : /* sysconf (_SC_PAGE_SIZE), ftruncate */
250 : : /* posix_fallocate used if available. */
251 : : #define MAPPED_WRITING 1
252 : : #else
253 : : #define MAPPED_WRITING 0
254 : : #endif
255 : : #else
256 : : #define MAPPED_READING 0
257 : : #define MAPPED_WRITING 0
258 : : #endif
259 : : #endif
260 : :
261 : : /* Some open(2) flag differences, what a colourful world it is! */
262 : : #if defined (O_CLOEXEC)
263 : : // OK
264 : : #elif defined (_O_NOINHERIT)
265 : : /* Windows' _O_NOINHERIT matches O_CLOEXEC flag */
266 : : #define O_CLOEXEC _O_NOINHERIT
267 : : #else
268 : : #define O_CLOEXEC 0
269 : : #endif
270 : : #if defined (O_BINARY)
271 : : // Ok?
272 : : #elif defined (_O_BINARY)
273 : : /* Windows' open(2) call defaults to text! */
274 : : #define O_BINARY _O_BINARY
275 : : #else
276 : : #define O_BINARY 0
277 : : #endif
278 : :
279 : 273145 : static inline cpp_hashnode *cpp_node (tree id)
280 : : {
281 : 273145 : return CPP_HASHNODE (GCC_IDENT_TO_HT_IDENT (id));
282 : : }
283 : :
284 : 143132 : static inline tree identifier (const cpp_hashnode *node)
285 : : {
286 : : /* HT_NODE() expands to node->ident that HT_IDENT_TO_GCC_IDENT()
287 : : then subtracts a nonzero constant, deriving a pointer to
288 : : a different member than ident. That's strictly undefined
289 : : and detected by -Warray-bounds. Suppress it. See PR 101372. */
290 : 143132 : #pragma GCC diagnostic push
291 : 143132 : #pragma GCC diagnostic ignored "-Warray-bounds"
292 : 143132 : return HT_IDENT_TO_GCC_IDENT (HT_NODE (const_cast<cpp_hashnode *> (node)));
293 : 143132 : #pragma GCC diagnostic pop
294 : : }
295 : :
296 : : /* Id for dumping module information. */
297 : : int module_dump_id;
298 : :
299 : : /* We have a special module owner. */
300 : : #define MODULE_UNKNOWN (~0U) /* Not yet known. */
301 : :
302 : : /* Prefix for section names. */
303 : : #define MOD_SNAME_PFX ".gnu.c++"
304 : :
305 : : /* Format a version for user consumption. */
306 : :
307 : : typedef char verstr_t[32];
308 : : static void
309 : 3902 : version2string (unsigned version, verstr_t &out)
310 : : {
311 : 3902 : unsigned major = MODULE_MAJOR (version);
312 : 3902 : unsigned minor = MODULE_MINOR (version);
313 : :
314 : 3902 : if (IS_EXPERIMENTAL (version))
315 : 3902 : sprintf (out, "%04u/%02u/%02u-%02u:%02u%s",
316 : 3902 : 2000 + major / 10000, (major / 100) % 100, (major % 100),
317 : : minor / 100, minor % 100,
318 : : EXPERIMENT ("", " (experimental)"));
319 : : else
320 : 0 : sprintf (out, "%u.%u", major, minor);
321 : 3902 : }
322 : :
323 : : /* Include files to note translation for. */
324 : : static vec<const char *, va_heap, vl_embed> *note_includes;
325 : :
326 : : /* Modules to note CMI pathames. */
327 : : static vec<const char *, va_heap, vl_embed> *note_cmis;
328 : :
329 : : /* Traits to hash an arbitrary pointer. Entries are not deletable,
330 : : and removal is a noop (removal needed upon destruction). */
331 : : template <typename T>
332 : : struct nodel_ptr_hash : pointer_hash<T>, typed_noop_remove <T *> {
333 : : /* Nothing is deletable. Everything is insertable. */
334 : : static bool is_deleted (T *) { return false; }
335 : : static void mark_deleted (T *) { gcc_unreachable (); }
336 : : };
337 : :
338 : : /* Map from pointer to signed integer. */
339 : : typedef simple_hashmap_traits<nodel_ptr_hash<void>, int> ptr_int_traits;
340 : : typedef hash_map<void *,signed,ptr_int_traits> ptr_int_hash_map;
341 : :
342 : : /********************************************************************/
343 : : /* Basic streaming & ELF. Serialization is usually via mmap. For
344 : : writing we slide a buffer over the output file, syncing it
345 : : approproiately. For reading we simply map the whole file (as a
346 : : file-backed read-only map -- it's just address space, leaving the
347 : : OS pager to deal with getting the data to us). Some buffers need
348 : : to be more conventional malloc'd contents. */
349 : :
350 : : /* Variable length buffer. */
351 : :
352 : : namespace {
353 : :
354 : : constexpr line_map_uint_t loc_one = 1;
355 : :
356 : : class data {
357 : : public:
358 : 2718 : class allocator {
359 : : public:
360 : : /* Tools tend to moan if the dtor's not virtual. */
361 : 99642 : virtual ~allocator () {}
362 : :
363 : : public:
364 : : void grow (data &obj, unsigned needed, bool exact);
365 : : void shrink (data &obj);
366 : :
367 : : public:
368 : : virtual char *grow (char *ptr, unsigned needed);
369 : : virtual void shrink (char *ptr);
370 : : };
371 : :
372 : : public:
373 : : char *buffer; /* Buffer being transferred. */
374 : : /* Although size_t would be the usual size, we know we never get
375 : : more than 4GB of buffer -- because that's the limit of the
376 : : encapsulation format. And if you need bigger imports, you're
377 : : doing it wrong. */
378 : : unsigned size; /* Allocated size of buffer. */
379 : : unsigned pos; /* Position in buffer. */
380 : :
381 : : public:
382 : 732844 : data ()
383 : 732844 : :buffer (NULL), size (0), pos (0)
384 : : {
385 : : }
386 : 746420 : ~data ()
387 : : {
388 : : /* Make sure the derived and/or using class know what they're
389 : : doing. */
390 : 746420 : gcc_checking_assert (!buffer);
391 : 746420 : }
392 : :
393 : : protected:
394 : 508174294 : char *use (unsigned count)
395 : : {
396 : 508174294 : if (size < pos + count)
397 : : return NULL;
398 : 508174294 : char *res = &buffer[pos];
399 : 508174294 : pos += count;
400 : 255572487 : return res;
401 : : }
402 : :
403 : : unsigned calc_crc (unsigned) const;
404 : :
405 : : public:
406 : 38168985 : void unuse (unsigned count)
407 : : {
408 : 38168985 : pos -= count;
409 : 26404 : }
410 : :
411 : : public:
412 : : static allocator simple_memory;
413 : : };
414 : : } // anon namespace
415 : :
416 : : /* The simple data allocator. */
417 : : data::allocator data::simple_memory;
418 : :
419 : : /* Grow buffer to at least size NEEDED. */
420 : :
421 : : void
422 : 627191 : data::allocator::grow (data &obj, unsigned needed, bool exact)
423 : : {
424 : 627191 : gcc_checking_assert (needed ? needed > obj.size : !obj.size);
425 : 627191 : if (!needed)
426 : : /* Pick a default size. */
427 : 269184 : needed = EXPERIMENT (100, 1000);
428 : :
429 : 627191 : if (!exact)
430 : 619352 : needed *= 2;
431 : 627191 : obj.buffer = grow (obj.buffer, needed);
432 : 627191 : if (obj.buffer)
433 : 627191 : obj.size = needed;
434 : : else
435 : 0 : obj.pos = obj.size = 0;
436 : 627191 : }
437 : :
438 : : /* Free a buffer. */
439 : :
440 : : void
441 : 282786 : data::allocator::shrink (data &obj)
442 : : {
443 : 0 : shrink (obj.buffer);
444 : 282786 : obj.buffer = NULL;
445 : 282786 : obj.size = 0;
446 : 0 : }
447 : :
448 : : char *
449 : 10987 : data::allocator::grow (char *ptr, unsigned needed)
450 : : {
451 : 10987 : return XRESIZEVAR (char, ptr, needed);
452 : : }
453 : :
454 : : void
455 : 13590 : data::allocator::shrink (char *ptr)
456 : : {
457 : 13590 : XDELETEVEC (ptr);
458 : 13590 : }
459 : :
460 : : /* Calculate the crc32 of the buffer. Note the CRC is stored in the
461 : : first 4 bytes, so don't include them. */
462 : :
463 : : unsigned
464 : 471572 : data::calc_crc (unsigned l) const
465 : : {
466 : 471572 : return crc32 (0, (unsigned char *)buffer + 4, l - 4);
467 : : }
468 : :
469 : : class elf_in;
470 : :
471 : : /* Byte stream reader. */
472 : :
473 : : namespace {
474 : : class bytes_in : public data {
475 : : typedef data parent;
476 : :
477 : : protected:
478 : : bool overrun; /* Sticky read-too-much flag. */
479 : :
480 : : public:
481 : 211942 : bytes_in ()
482 : 211942 : : parent (), overrun (false)
483 : : {
484 : : }
485 : 214629 : ~bytes_in ()
486 : : {
487 : 14977 : }
488 : :
489 : : public:
490 : : /* Begin reading a named section. */
491 : : bool begin (location_t loc, elf_in *src, const char *name);
492 : : /* Begin reading a numbered section with optional name. */
493 : : bool begin (location_t loc, elf_in *src, unsigned, const char * = NULL);
494 : : /* Complete reading a buffer. Propagate errors and return true on
495 : : success. */
496 : : bool end (elf_in *src);
497 : : /* Return true if there is unread data. */
498 : 1586349 : bool more_p () const
499 : : {
500 : 1586349 : return pos != size;
501 : : }
502 : :
503 : : public:
504 : : /* Start reading at OFFSET. */
505 : 637 : void random_access (unsigned offset)
506 : : {
507 : 637 : if (offset > size)
508 : 0 : set_overrun ();
509 : 637 : pos = offset;
510 : : }
511 : :
512 : : public:
513 : 1434644 : void align (unsigned boundary)
514 : : {
515 : 1434644 : if (unsigned pad = pos & (boundary - 1))
516 : 2780037 : read (boundary - pad);
517 : : }
518 : :
519 : : public:
520 : 252601807 : const char *read (unsigned count)
521 : : {
522 : 1345393 : char *ptr = use (count);
523 : 252601807 : if (!ptr)
524 : 0 : set_overrun ();
525 : 205124608 : return ptr;
526 : : }
527 : :
528 : : public:
529 : : bool check_crc () const;
530 : : /* We store the CRC in the first 4 bytes, using host endianness. */
531 : 213055 : unsigned get_crc () const
532 : : {
533 : 213055 : return *(const unsigned *)&buffer[0];
534 : : }
535 : :
536 : : public:
537 : : /* Manipulate the overrun flag. */
538 : 147605962 : bool get_overrun () const
539 : : {
540 : 147605962 : return overrun;
541 : : }
542 : 26 : void set_overrun ()
543 : : {
544 : 26 : overrun = true;
545 : 0 : }
546 : :
547 : : public:
548 : : unsigned u32 (); /* Read uncompressed integer. */
549 : :
550 : : public:
551 : : int c () ATTRIBUTE_UNUSED; /* Read a char. */
552 : : int i (); /* Read a signed int. */
553 : : unsigned u (); /* Read an unsigned int. */
554 : : size_t z (); /* Read a size_t. */
555 : : location_t loc (); /* Read a location_t. */
556 : : HOST_WIDE_INT wi (); /* Read a HOST_WIDE_INT. */
557 : : unsigned HOST_WIDE_INT wu (); /* Read an unsigned HOST_WIDE_INT. */
558 : : const char *str (size_t * = NULL); /* Read a string. */
559 : : const void *buf (size_t); /* Read a fixed-length buffer. */
560 : : cpp_hashnode *cpp_node (); /* Read a cpp node. */
561 : :
562 : : struct bits_in;
563 : : bits_in stream_bits ();
564 : : };
565 : : } // anon namespace
566 : :
567 : : /* Verify the buffer's CRC is correct. */
568 : :
569 : : bool
570 : 210186 : bytes_in::check_crc () const
571 : : {
572 : 210186 : if (size < 4)
573 : : return false;
574 : :
575 : 210186 : unsigned c_crc = calc_crc (size);
576 : 210186 : if (c_crc != get_crc ())
577 : : return false;
578 : :
579 : : return true;
580 : : }
581 : :
582 : : class elf_out;
583 : :
584 : : /* Byte stream writer. */
585 : :
586 : : namespace {
587 : : class bytes_out : public data {
588 : : typedef data parent;
589 : :
590 : : public:
591 : : allocator *memory; /* Obtainer of memory. */
592 : :
593 : : public:
594 : 515339 : bytes_out (allocator *memory)
595 : 515339 : : parent (), memory (memory)
596 : : {
597 : : }
598 : 515339 : ~bytes_out ()
599 : : {
600 : 543536 : }
601 : :
602 : : public:
603 : 614688758 : bool streaming_p () const
604 : : {
605 : 614688758 : return memory != NULL;
606 : : }
607 : :
608 : : public:
609 : : void set_crc (unsigned *crc_ptr);
610 : :
611 : : public:
612 : : /* Begin writing, maybe reserve space for CRC. */
613 : : void begin (bool need_crc = true);
614 : : /* Finish writing. Spill to section by number. */
615 : : unsigned end (elf_out *, unsigned, unsigned *crc_ptr = NULL);
616 : :
617 : : public:
618 : 1661355 : void align (unsigned boundary)
619 : : {
620 : 1661355 : if (unsigned pad = pos & (boundary - 1))
621 : 1557401 : write (boundary - pad);
622 : 1661355 : }
623 : :
624 : : public:
625 : 255572487 : char *write (unsigned count, bool exact = false)
626 : : {
627 : 255572487 : if (size < pos + count)
628 : 344399 : memory->grow (*this, pos + count, exact);
629 : 255572487 : return use (count);
630 : : }
631 : :
632 : : public:
633 : : void u32 (unsigned); /* Write uncompressed integer. */
634 : :
635 : : public:
636 : : void c (unsigned char) ATTRIBUTE_UNUSED; /* Write unsigned char. */
637 : : void i (int); /* Write signed int. */
638 : : void u (unsigned); /* Write unsigned int. */
639 : : void z (size_t s); /* Write size_t. */
640 : : void loc (location_t); /* Write location_t. */
641 : : void wi (HOST_WIDE_INT); /* Write HOST_WIDE_INT. */
642 : : void wu (unsigned HOST_WIDE_INT); /* Write unsigned HOST_WIDE_INT. */
643 : 16457 : void str (const char *ptr)
644 : : {
645 : 16457 : str (ptr, strlen (ptr));
646 : 16457 : }
647 : 265162 : void cpp_node (const cpp_hashnode *node)
648 : : {
649 : 265162 : str ((const char *)NODE_NAME (node), NODE_LEN (node));
650 : 12488 : }
651 : : void str (const char *, size_t); /* Write string of known length. */
652 : : void buf (const void *, size_t); /* Write fixed length buffer. */
653 : : void *buf (size_t); /* Create a writable buffer */
654 : :
655 : : struct bits_out;
656 : : bits_out stream_bits ();
657 : :
658 : : public:
659 : : /* Format a NUL-terminated raw string. */
660 : : void printf (const char *, ...) ATTRIBUTE_PRINTF_2;
661 : : void print_time (const char *, const tm *, const char *);
662 : :
663 : : public:
664 : : /* Dump instrumentation. */
665 : : static void instrument ();
666 : :
667 : : protected:
668 : : /* Instrumentation. */
669 : : static unsigned spans[4];
670 : : static unsigned lengths[4];
671 : : };
672 : : } // anon namespace
673 : :
674 : : /* Finish bit packet. Rewind the bytes not used. */
675 : :
676 : : static unsigned
677 : 38116610 : bit_flush (data& bits, uint32_t& bit_val, unsigned& bit_pos)
678 : : {
679 : 38116610 : gcc_assert (bit_pos);
680 : 38116610 : unsigned bytes = (bit_pos + 7) / 8;
681 : 38116610 : bits.unuse (4 - bytes);
682 : 38116610 : bit_pos = 0;
683 : 38116610 : bit_val = 0;
684 : 38116610 : return bytes;
685 : : }
686 : :
687 : : /* Bit stream reader (RAII-enabled). Bools are packed into bytes. You
688 : : cannot mix bools and non-bools. Use bflush to flush the current stream
689 : : of bools on demand. Upon destruction bflush is called.
690 : :
691 : : When reading, we don't know how many bools we'll read in. So read
692 : : 4 bytes-worth, and then rewind when flushing if we didn't need them
693 : : all. You can't have a block of bools closer than 4 bytes to the
694 : : end of the buffer.
695 : :
696 : : Both bits_in and bits_out maintain the necessary state for bit packing,
697 : : and since these objects are locally constructed the compiler can more
698 : : easily track their state across consecutive reads/writes and optimize
699 : : away redundant buffering checks. */
700 : :
701 : : struct bytes_in::bits_in {
702 : : bytes_in& in;
703 : : uint32_t bit_val = 0;
704 : : unsigned bit_pos = 0;
705 : :
706 : 14217937 : bits_in (bytes_in& in)
707 : 14217937 : : in (in)
708 : : { }
709 : :
710 : 14217937 : ~bits_in ()
711 : : {
712 : 13323769 : bflush ();
713 : 14217937 : }
714 : :
715 : : bits_in(bits_in&&) = default;
716 : : bits_in(const bits_in&) = delete;
717 : : bits_in& operator=(const bits_in&) = delete;
718 : :
719 : : /* Completed a block of bools. */
720 : 29512177 : void bflush ()
721 : : {
722 : 29512177 : if (bit_pos)
723 : 16188408 : bit_flush (in, bit_val, bit_pos);
724 : 29512177 : }
725 : :
726 : : /* Read one bit. */
727 : 484502863 : bool b ()
728 : : {
729 : 484502863 : if (!bit_pos)
730 : 21043351 : bit_val = in.u32 ();
731 : 484502863 : bool x = (bit_val >> bit_pos) & 1;
732 : 484502863 : bit_pos = (bit_pos + 1) % 32;
733 : 484502863 : return x;
734 : : }
735 : : };
736 : :
737 : : /* Factory function for bits_in. */
738 : :
739 : : bytes_in::bits_in
740 : 14217937 : bytes_in::stream_bits ()
741 : : {
742 : 14217937 : return bits_in (*this);
743 : : }
744 : :
745 : : /* Bit stream writer (RAII-enabled), counterpart to bits_in. */
746 : :
747 : : struct bytes_out::bits_out {
748 : : bytes_out& out;
749 : : uint32_t bit_val = 0;
750 : : unsigned bit_pos = 0;
751 : : char is_set = -1;
752 : :
753 : 14650997 : bits_out (bytes_out& out)
754 : 14650997 : : out (out)
755 : : { }
756 : :
757 : 14650997 : ~bits_out ()
758 : : {
759 : 71523 : bflush ();
760 : : }
761 : :
762 : : bits_out(bits_out&&) = default;
763 : : bits_out(const bits_out&) = delete;
764 : : bits_out& operator=(const bits_out&) = delete;
765 : :
766 : : /* Completed a block of bools. */
767 : 30419154 : void bflush ()
768 : : {
769 : 30419154 : if (bit_pos)
770 : : {
771 : 16745292 : out.u32 (bit_val);
772 : 16745292 : out.lengths[2] += bit_flush (out, bit_val, bit_pos);
773 : : }
774 : 30419154 : out.spans[2]++;
775 : 30419154 : is_set = -1;
776 : 30419154 : }
777 : :
778 : : /* Write one bit.
779 : :
780 : : It may be worth optimizing for most bools being zero. Some kind of
781 : : run-length encoding? */
782 : 500248181 : void b (bool x)
783 : : {
784 : 500248181 : if (is_set != x)
785 : : {
786 : 53541116 : is_set = x;
787 : 53541116 : out.spans[x]++;
788 : : }
789 : 500248181 : out.lengths[x]++;
790 : 500248181 : bit_val |= unsigned (x) << bit_pos++;
791 : 500248181 : if (bit_pos == 32)
792 : : {
793 : 5182910 : out.u32 (bit_val);
794 : 5182910 : out.lengths[2] += bit_flush (out, bit_val, bit_pos);
795 : : }
796 : 500248181 : }
797 : : };
798 : :
799 : : /* Factory function for bits_out. */
800 : :
801 : : bytes_out::bits_out
802 : 14650997 : bytes_out::stream_bits ()
803 : : {
804 : 14650997 : return bits_out (*this);
805 : : }
806 : :
807 : : /* Instrumentation. */
808 : : unsigned bytes_out::spans[4];
809 : : unsigned bytes_out::lengths[4];
810 : :
811 : : /* If CRC_PTR non-null, set the CRC of the buffer. Mix the CRC into
812 : : that pointed to by CRC_PTR. */
813 : :
814 : : void
815 : 263966 : bytes_out::set_crc (unsigned *crc_ptr)
816 : : {
817 : 263966 : if (crc_ptr)
818 : : {
819 : 261386 : gcc_checking_assert (pos >= 4);
820 : :
821 : 261386 : unsigned crc = calc_crc (pos);
822 : 261386 : unsigned accum = *crc_ptr;
823 : : /* Only mix the existing *CRC_PTR if it is non-zero. */
824 : 261386 : accum = accum ? crc32_combine (accum, crc, pos - 4) : crc;
825 : 261386 : *crc_ptr = accum;
826 : :
827 : : /* Buffer will be sufficiently aligned. */
828 : 261386 : *(unsigned *)buffer = crc;
829 : : }
830 : 263966 : }
831 : :
832 : : /* Exactly 4 bytes. Used internally for bool packing and a few other
833 : : places. We can't simply use uint32_t because (a) alignment and
834 : : (b) we need little-endian for the bool streaming rewinding to make
835 : : sense. */
836 : :
837 : : void
838 : 21936552 : bytes_out::u32 (unsigned val)
839 : : {
840 : 21936552 : if (char *ptr = write (4))
841 : : {
842 : 21936552 : ptr[0] = val;
843 : 21936552 : ptr[1] = val >> 8;
844 : 21936552 : ptr[2] = val >> 16;
845 : 21936552 : ptr[3] = val >> 24;
846 : : }
847 : 21936552 : }
848 : :
849 : : unsigned
850 : 21052349 : bytes_in::u32 ()
851 : : {
852 : 21052349 : unsigned val = 0;
853 : 21052349 : if (const char *ptr = read (4))
854 : : {
855 : 21052349 : val |= (unsigned char)ptr[0];
856 : 21052349 : val |= (unsigned char)ptr[1] << 8;
857 : 21052349 : val |= (unsigned char)ptr[2] << 16;
858 : 21052349 : val |= (unsigned char)ptr[3] << 24;
859 : : }
860 : :
861 : 21052349 : return val;
862 : : }
863 : :
864 : : /* Chars are unsigned and written as single bytes. */
865 : :
866 : : void
867 : 0 : bytes_out::c (unsigned char v)
868 : : {
869 : 0 : if (char *ptr = write (1))
870 : 0 : *ptr = v;
871 : 0 : }
872 : :
873 : : int
874 : 0 : bytes_in::c ()
875 : : {
876 : 0 : int v = 0;
877 : 0 : if (const char *ptr = read (1))
878 : 0 : v = (unsigned char)ptr[0];
879 : 0 : return v;
880 : : }
881 : :
882 : : /* Ints 7-bit as a byte. Otherwise a 3bit count of following bytes in
883 : : big-endian form. 4 bits are in the first byte. */
884 : :
885 : : void
886 : 90051888 : bytes_out::i (int v)
887 : : {
888 : 90051888 : if (char *ptr = write (1))
889 : : {
890 : 90051888 : if (v <= 0x3f && v >= -0x40)
891 : 70991801 : *ptr = v & 0x7f;
892 : : else
893 : : {
894 : 19060087 : unsigned bytes = 0;
895 : 19060087 : int probe;
896 : 19060087 : if (v >= 0)
897 : 0 : for (probe = v >> 8; probe > 0x7; probe >>= 8)
898 : 0 : bytes++;
899 : : else
900 : 29719257 : for (probe = v >> 8; probe < -0x8; probe >>= 8)
901 : 10659170 : bytes++;
902 : 19060087 : *ptr = 0x80 | bytes << 4 | (probe & 0xf);
903 : 19060087 : if ((ptr = write (++bytes)))
904 : 48779344 : for (; bytes--; v >>= 8)
905 : 29719257 : ptr[bytes] = v & 0xff;
906 : : }
907 : : }
908 : 90051888 : }
909 : :
910 : : int
911 : 87752930 : bytes_in::i ()
912 : : {
913 : 87752930 : int v = 0;
914 : 87752930 : if (const char *ptr = read (1))
915 : : {
916 : 87752930 : v = *ptr & 0xff;
917 : 87752930 : if (v & 0x80)
918 : : {
919 : 20018266 : unsigned bytes = (v >> 4) & 0x7;
920 : 20018266 : v &= 0xf;
921 : 20018266 : if (v & 0x8)
922 : 20018266 : v |= -1 ^ 0x7;
923 : : /* unsigned necessary due to left shifts of -ve values. */
924 : 20018266 : unsigned uv = unsigned (v);
925 : 20018266 : if ((ptr = read (++bytes)))
926 : 52704294 : while (bytes--)
927 : 32686028 : uv = (uv << 8) | (*ptr++ & 0xff);
928 : 20018266 : v = int (uv);
929 : : }
930 : 67734664 : else if (v & 0x40)
931 : 8624121 : v |= -1 ^ 0x3f;
932 : : }
933 : :
934 : 87752930 : return v;
935 : : }
936 : :
937 : : void
938 : 77674270 : bytes_out::u (unsigned v)
939 : : {
940 : 77674270 : if (char *ptr = write (1))
941 : : {
942 : 77674270 : if (v <= 0x7f)
943 : 67712371 : *ptr = v;
944 : : else
945 : : {
946 : 9961899 : unsigned bytes = 0;
947 : 9961899 : unsigned probe;
948 : 11726314 : for (probe = v >> 8; probe > 0xf; probe >>= 8)
949 : 1764415 : bytes++;
950 : 9961899 : *ptr = 0x80 | bytes << 4 | probe;
951 : 9961899 : if ((ptr = write (++bytes)))
952 : 21688213 : for (; bytes--; v >>= 8)
953 : 11726314 : ptr[bytes] = v & 0xff;
954 : : }
955 : : }
956 : 77674270 : }
957 : :
958 : : unsigned
959 : 77143619 : bytes_in::u ()
960 : : {
961 : 77143619 : unsigned v = 0;
962 : :
963 : 77143619 : if (const char *ptr = read (1))
964 : : {
965 : 77143619 : v = *ptr & 0xff;
966 : 77143619 : if (v & 0x80)
967 : : {
968 : 10097690 : unsigned bytes = (v >> 4) & 0x7;
969 : 10097690 : v &= 0xf;
970 : 10097690 : if ((ptr = read (++bytes)))
971 : 22127726 : while (bytes--)
972 : 12030036 : v = (v << 8) | (*ptr++ & 0xff);
973 : : }
974 : : }
975 : :
976 : 77143619 : return v;
977 : : }
978 : :
979 : : void
980 : 18650615 : bytes_out::wi (HOST_WIDE_INT v)
981 : : {
982 : 18650615 : if (char *ptr = write (1))
983 : : {
984 : 18650615 : if (v <= 0x3f && v >= -0x40)
985 : 3684570 : *ptr = v & 0x7f;
986 : : else
987 : : {
988 : 14966045 : unsigned bytes = 0;
989 : 14966045 : HOST_WIDE_INT probe;
990 : 14966045 : if (v >= 0)
991 : 53769833 : for (probe = v >> 8; probe > 0x7; probe >>= 8)
992 : 38806345 : bytes++;
993 : : else
994 : 8361 : for (probe = v >> 8; probe < -0x8; probe >>= 8)
995 : 5804 : bytes++;
996 : 14966045 : *ptr = 0x80 | bytes << 4 | (probe & 0xf);
997 : 14966045 : if ((ptr = write (++bytes)))
998 : 68744239 : for (; bytes--; v >>= 8)
999 : 53778194 : ptr[bytes] = v & 0xff;
1000 : : }
1001 : : }
1002 : 18650615 : }
1003 : :
1004 : : HOST_WIDE_INT
1005 : 17741066 : bytes_in::wi ()
1006 : : {
1007 : 17741066 : HOST_WIDE_INT v = 0;
1008 : 17741066 : if (const char *ptr = read (1))
1009 : : {
1010 : 17741066 : v = *ptr & 0xff;
1011 : 17741066 : if (v & 0x80)
1012 : : {
1013 : 16015850 : unsigned bytes = (v >> 4) & 0x7;
1014 : 16015850 : v &= 0xf;
1015 : 16015850 : if (v & 0x8)
1016 : 1925 : v |= -1 ^ 0x7;
1017 : : /* unsigned necessary due to left shifts of -ve values. */
1018 : 16015850 : unsigned HOST_WIDE_INT uv = (unsigned HOST_WIDE_INT) v;
1019 : 16015850 : if ((ptr = read (++bytes)))
1020 : 73564543 : while (bytes--)
1021 : 57548693 : uv = (uv << 8) | (*ptr++ & 0xff);
1022 : 16015850 : v = (HOST_WIDE_INT) uv;
1023 : : }
1024 : 1725216 : else if (v & 0x40)
1025 : 7929 : v |= -1 ^ 0x3f;
1026 : : }
1027 : :
1028 : 17741066 : return v;
1029 : : }
1030 : :
1031 : : /* unsigned wide ints are just written as signed wide ints. */
1032 : :
1033 : : inline void
1034 : 18650027 : bytes_out::wu (unsigned HOST_WIDE_INT v)
1035 : : {
1036 : 18650027 : wi ((HOST_WIDE_INT) v);
1037 : : }
1038 : :
1039 : : inline unsigned HOST_WIDE_INT
1040 : 17740536 : bytes_in::wu ()
1041 : : {
1042 : 34924552 : return (unsigned HOST_WIDE_INT) wi ();
1043 : : }
1044 : :
1045 : : /* size_t written as unsigned or unsigned wide int. */
1046 : :
1047 : : inline void
1048 : 1630634 : bytes_out::z (size_t s)
1049 : : {
1050 : 1630634 : if (sizeof (s) == sizeof (unsigned))
1051 : : u (s);
1052 : : else
1053 : 3229104 : wu (s);
1054 : 12 : }
1055 : :
1056 : : inline size_t
1057 : 1418422 : bytes_in::z ()
1058 : : {
1059 : 1418422 : if (sizeof (size_t) == sizeof (unsigned))
1060 : : return u ();
1061 : : else
1062 : 2836844 : return wu ();
1063 : : }
1064 : :
1065 : : /* location_t written as 32- or 64-bit as needed. */
1066 : :
1067 : 16410450 : inline void bytes_out::loc (location_t l)
1068 : : {
1069 : 16410450 : if (sizeof (location_t) > sizeof (unsigned))
1070 : 31125072 : wu (l);
1071 : : else
1072 : : u (l);
1073 : 1693248 : }
1074 : :
1075 : 15768420 : inline location_t bytes_in::loc ()
1076 : : {
1077 : 15768420 : if (sizeof (location_t) > sizeof (unsigned))
1078 : 31533984 : return wu ();
1079 : : else
1080 : : return u ();
1081 : : }
1082 : :
1083 : : /* Buffer simply memcpied. */
1084 : : void *
1085 : 1661355 : bytes_out::buf (size_t len)
1086 : : {
1087 : 1661355 : align (sizeof (void *) * 2);
1088 : 1661355 : return write (len);
1089 : : }
1090 : :
1091 : : void
1092 : 1614401 : bytes_out::buf (const void *src, size_t len)
1093 : : {
1094 : 1614401 : if (void *ptr = buf (len))
1095 : 1614401 : memcpy (ptr, src, len);
1096 : 1614401 : }
1097 : :
1098 : : const void *
1099 : 1434644 : bytes_in::buf (size_t len)
1100 : : {
1101 : 1434644 : align (sizeof (void *) * 2);
1102 : 1434644 : const char *ptr = read (len);
1103 : :
1104 : 1434644 : return ptr;
1105 : : }
1106 : :
1107 : : /* strings as an size_t length, followed by the buffer. Make sure
1108 : : there's a NUL terminator on read. */
1109 : :
1110 : : void
1111 : 1630616 : bytes_out::str (const char *string, size_t len)
1112 : : {
1113 : 1598464 : z (len);
1114 : 1598464 : if (len)
1115 : : {
1116 : 1598464 : gcc_checking_assert (!string[len]);
1117 : 1598464 : buf (string, len + 1);
1118 : : }
1119 : 32152 : }
1120 : :
1121 : : const char *
1122 : 1418416 : bytes_in::str (size_t *len_p)
1123 : : {
1124 : 1418416 : size_t len = z ();
1125 : :
1126 : : /* We're about to trust some user data. */
1127 : 1418416 : if (overrun)
1128 : 0 : len = 0;
1129 : 1418416 : if (len_p)
1130 : 1414738 : *len_p = len;
1131 : 1418416 : const char *str = NULL;
1132 : 1418416 : if (len)
1133 : : {
1134 : 1418154 : str = reinterpret_cast<const char *> (buf (len + 1));
1135 : 1418154 : if (!str || str[len])
1136 : : {
1137 : 0 : set_overrun ();
1138 : 0 : str = NULL;
1139 : : }
1140 : : }
1141 : 0 : return str ? str : "";
1142 : : }
1143 : :
1144 : : cpp_hashnode *
1145 : 273407 : bytes_in::cpp_node ()
1146 : : {
1147 : 273407 : size_t len;
1148 : 273407 : const char *s = str (&len);
1149 : 273407 : if (!len)
1150 : : return NULL;
1151 : 273145 : return ::cpp_node (get_identifier_with_length (s, len));
1152 : : }
1153 : :
1154 : : /* Format a string directly to the buffer, including a terminating
1155 : : NUL. Intended for human consumption. */
1156 : :
1157 : : void
1158 : 26404 : bytes_out::printf (const char *format, ...)
1159 : : {
1160 : 26404 : va_list args;
1161 : : /* Exercise buffer expansion. */
1162 : 26404 : size_t len = EXPERIMENT (10, 500);
1163 : :
1164 : 52375 : while (char *ptr = write (len))
1165 : : {
1166 : 52375 : va_start (args, format);
1167 : 52375 : size_t actual = vsnprintf (ptr, len, format, args) + 1;
1168 : 52375 : va_end (args);
1169 : 52375 : if (actual <= len)
1170 : : {
1171 : 26404 : unuse (len - actual);
1172 : 26404 : break;
1173 : : }
1174 : 25971 : unuse (len);
1175 : 25971 : len = actual;
1176 : 25971 : }
1177 : 26404 : }
1178 : :
1179 : : void
1180 : 5160 : bytes_out::print_time (const char *kind, const tm *time, const char *tz)
1181 : : {
1182 : 5160 : printf ("%stime: %4u/%02u/%02u %02u:%02u:%02u %s",
1183 : 5160 : kind, time->tm_year + 1900, time->tm_mon + 1, time->tm_mday,
1184 : 5160 : time->tm_hour, time->tm_min, time->tm_sec, tz);
1185 : 5160 : }
1186 : :
1187 : : /* Encapsulated Lazy Records Of Named Declarations.
1188 : : Header: Stunningly Elf32_Ehdr-like
1189 : : Sections: Sectional data
1190 : : [1-N) : User data sections
1191 : : N .strtab : strings, stunningly ELF STRTAB-like
1192 : : Index: Section table, stunningly ELF32_Shdr-like. */
1193 : :
1194 : : class elf {
1195 : : protected:
1196 : : /* Constants used within the format. */
1197 : : enum private_constants {
1198 : : /* File kind. */
1199 : : ET_NONE = 0,
1200 : : EM_NONE = 0,
1201 : : OSABI_NONE = 0,
1202 : :
1203 : : /* File format. */
1204 : : EV_CURRENT = 1,
1205 : : CLASS32 = 1,
1206 : : DATA2LSB = 1,
1207 : : DATA2MSB = 2,
1208 : :
1209 : : /* Section numbering. */
1210 : : SHN_UNDEF = 0,
1211 : : SHN_LORESERVE = 0xff00,
1212 : : SHN_XINDEX = 0xffff,
1213 : :
1214 : : /* Section types. */
1215 : : SHT_NONE = 0, /* No contents. */
1216 : : SHT_PROGBITS = 1, /* Random bytes. */
1217 : : SHT_STRTAB = 3, /* A string table. */
1218 : :
1219 : : /* Section flags. */
1220 : : SHF_NONE = 0x00, /* Nothing. */
1221 : : SHF_STRINGS = 0x20, /* NUL-Terminated strings. */
1222 : :
1223 : : /* I really hope we do not get CMI files larger than 4GB. */
1224 : : MY_CLASS = CLASS32,
1225 : : /* It is host endianness that is relevant. */
1226 : : MY_ENDIAN = DATA2LSB
1227 : : #ifdef WORDS_BIGENDIAN
1228 : : ^ DATA2LSB ^ DATA2MSB
1229 : : #endif
1230 : : };
1231 : :
1232 : : public:
1233 : : /* Constants visible to users. */
1234 : : enum public_constants {
1235 : : /* Special error codes. Breaking layering a bit. */
1236 : : E_BAD_DATA = -1, /* Random unexpected data errors. */
1237 : : E_BAD_LAZY = -2, /* Badly ordered laziness. */
1238 : : E_BAD_IMPORT = -3 /* A nested import failed. */
1239 : : };
1240 : :
1241 : : protected:
1242 : : /* File identification. On-disk representation. */
1243 : : struct ident {
1244 : : uint8_t magic[4]; /* 0x7f, 'E', 'L', 'F' */
1245 : : uint8_t klass; /* 4:CLASS32 */
1246 : : uint8_t data; /* 5:DATA2[LM]SB */
1247 : : uint8_t version; /* 6:EV_CURRENT */
1248 : : uint8_t osabi; /* 7:OSABI_NONE */
1249 : : uint8_t abiver; /* 8: 0 */
1250 : : uint8_t pad[7]; /* 9-15 */
1251 : : };
1252 : : /* File header. On-disk representation. */
1253 : : struct header {
1254 : : struct ident ident;
1255 : : uint16_t type; /* ET_NONE */
1256 : : uint16_t machine; /* EM_NONE */
1257 : : uint32_t version; /* EV_CURRENT */
1258 : : uint32_t entry; /* 0 */
1259 : : uint32_t phoff; /* 0 */
1260 : : uint32_t shoff; /* Section Header Offset in file */
1261 : : uint32_t flags;
1262 : : uint16_t ehsize; /* ELROND Header SIZE -- sizeof (header) */
1263 : : uint16_t phentsize; /* 0 */
1264 : : uint16_t phnum; /* 0 */
1265 : : uint16_t shentsize; /* Section Header SIZE -- sizeof (section) */
1266 : : uint16_t shnum; /* Section Header NUM */
1267 : : uint16_t shstrndx; /* Section Header STRing iNDeX */
1268 : : };
1269 : : /* File section. On-disk representation. */
1270 : : struct section {
1271 : : uint32_t name; /* String table offset. */
1272 : : uint32_t type; /* SHT_* */
1273 : : uint32_t flags; /* SHF_* */
1274 : : uint32_t addr; /* 0 */
1275 : : uint32_t offset; /* OFFSET in file */
1276 : : uint32_t size; /* SIZE of section */
1277 : : uint32_t link; /* 0 */
1278 : : uint32_t info; /* 0 */
1279 : : uint32_t addralign; /* 0 */
1280 : : uint32_t entsize; /* ENTry SIZE, usually 0 */
1281 : : };
1282 : :
1283 : : protected:
1284 : : data hdr; /* The header. */
1285 : : data sectab; /* The section table. */
1286 : : data strtab; /* String table. */
1287 : : int fd; /* File descriptor we're reading or writing. */
1288 : : int err; /* Sticky error code. */
1289 : :
1290 : : public:
1291 : : /* Construct from STREAM. E is errno if STREAM NULL. */
1292 : 5563 : elf (int fd, int e)
1293 : 11126 : :hdr (), sectab (), strtab (), fd (fd), err (fd >= 0 ? 0 : e)
1294 : : {}
1295 : 5484 : ~elf ()
1296 : : {
1297 : 5484 : gcc_checking_assert (fd < 0 && !hdr.buffer
1298 : : && !sectab.buffer && !strtab.buffer);
1299 : 5484 : }
1300 : :
1301 : : public:
1302 : : /* Return the error, if we have an error. */
1303 : 410410 : int get_error () const
1304 : : {
1305 : 410410 : return err;
1306 : : }
1307 : : /* Set the error, unless it's already been set. */
1308 : 53 : void set_error (int e = E_BAD_DATA)
1309 : : {
1310 : 53 : if (!err)
1311 : 19 : err = e;
1312 : 0 : }
1313 : : /* Get an error string. */
1314 : : const char *get_error (const char *) const;
1315 : :
1316 : : public:
1317 : : /* Begin reading/writing file. Return false on error. */
1318 : 5454 : bool begin () const
1319 : : {
1320 : 5454 : return !get_error ();
1321 : : }
1322 : : /* Finish reading/writing file. Return false on error. */
1323 : : bool end ();
1324 : : };
1325 : :
1326 : : /* Return error string. */
1327 : :
1328 : : const char *
1329 : 40 : elf::get_error (const char *name) const
1330 : : {
1331 : 40 : if (!name)
1332 : : return "Unknown CMI mapping";
1333 : :
1334 : 40 : switch (err)
1335 : : {
1336 : 0 : case 0:
1337 : 0 : gcc_unreachable ();
1338 : : case E_BAD_DATA:
1339 : : return "Bad file data";
1340 : 6 : case E_BAD_IMPORT:
1341 : 6 : return "Bad import dependency";
1342 : 0 : case E_BAD_LAZY:
1343 : 0 : return "Bad lazy ordering";
1344 : 21 : default:
1345 : 21 : return xstrerror (err);
1346 : : }
1347 : : }
1348 : :
1349 : : /* Finish file, return true if there's an error. */
1350 : :
1351 : : bool
1352 : 7763 : elf::end ()
1353 : : {
1354 : : /* Close the stream and free the section table. */
1355 : 7763 : if (fd >= 0 && close (fd))
1356 : 0 : set_error (errno);
1357 : 7763 : fd = -1;
1358 : :
1359 : 7763 : return !get_error ();
1360 : : }
1361 : :
1362 : : /* ELROND reader. */
1363 : :
1364 : : class elf_in : public elf {
1365 : : typedef elf parent;
1366 : :
1367 : : private:
1368 : : /* For freezing & defrosting. */
1369 : : #if !defined (HOST_LACKS_INODE_NUMBERS)
1370 : : dev_t device;
1371 : : ino_t inode;
1372 : : #endif
1373 : :
1374 : : public:
1375 : 2845 : elf_in (int fd, int e)
1376 : 5690 : :parent (fd, e)
1377 : : {
1378 : : }
1379 : 2766 : ~elf_in ()
1380 : : {
1381 : 2766 : }
1382 : :
1383 : : public:
1384 : 192756 : bool is_frozen () const
1385 : : {
1386 : 18 : return fd < 0 && hdr.pos;
1387 : : }
1388 : 18 : bool is_freezable () const
1389 : : {
1390 : 9 : return fd >= 0 && hdr.pos;
1391 : : }
1392 : : void freeze ();
1393 : : bool defrost (const char *);
1394 : :
1395 : : /* If BYTES is in the mmapped area, allocate a new buffer for it. */
1396 : 0 : void preserve (bytes_in &bytes ATTRIBUTE_UNUSED)
1397 : : {
1398 : : #if MAPPED_READING
1399 : 0 : if (hdr.buffer && bytes.buffer >= hdr.buffer
1400 : 0 : && bytes.buffer < hdr.buffer + hdr.pos)
1401 : : {
1402 : 0 : char *buf = bytes.buffer;
1403 : 0 : bytes.buffer = data::simple_memory.grow (NULL, bytes.size);
1404 : 0 : memcpy (bytes.buffer, buf, bytes.size);
1405 : : }
1406 : : #endif
1407 : 0 : }
1408 : : /* If BYTES is not in SELF's mmapped area, free it. SELF might be
1409 : : NULL. */
1410 : 1050 : static void release (elf_in *self ATTRIBUTE_UNUSED, bytes_in &bytes)
1411 : : {
1412 : : #if MAPPED_READING
1413 : 1050 : if (!(self && self->hdr.buffer && bytes.buffer >= self->hdr.buffer
1414 : 1050 : && bytes.buffer < self->hdr.buffer + self->hdr.pos))
1415 : : #endif
1416 : 0 : data::simple_memory.shrink (bytes.buffer);
1417 : 1050 : bytes.buffer = NULL;
1418 : 1050 : bytes.size = 0;
1419 : 1050 : }
1420 : :
1421 : : public:
1422 : 215834 : static void grow (data &data, unsigned needed)
1423 : : {
1424 : 215834 : gcc_checking_assert (!data.buffer);
1425 : : #if !MAPPED_READING
1426 : : data.buffer = XNEWVEC (char, needed);
1427 : : #endif
1428 : 215834 : data.size = needed;
1429 : 215834 : }
1430 : 221986 : static void shrink (data &data)
1431 : : {
1432 : : #if !MAPPED_READING
1433 : : XDELETEVEC (data.buffer);
1434 : : #endif
1435 : 221986 : data.buffer = NULL;
1436 : 221986 : data.size = 0;
1437 : 0 : }
1438 : :
1439 : : public:
1440 : 213010 : const section *get_section (unsigned s) const
1441 : : {
1442 : 213010 : if (s * sizeof (section) < sectab.size)
1443 : 213010 : return reinterpret_cast<const section *>
1444 : 213010 : (§ab.buffer[s * sizeof (section)]);
1445 : : else
1446 : : return NULL;
1447 : : }
1448 : : unsigned get_section_limit () const
1449 : : {
1450 : : return sectab.size / sizeof (section);
1451 : : }
1452 : :
1453 : : protected:
1454 : : const char *read (data *, unsigned, unsigned);
1455 : :
1456 : : public:
1457 : : /* Read section by number. */
1458 : 213010 : bool read (data *d, const section *s)
1459 : : {
1460 : 213010 : return s && read (d, s->offset, s->size);
1461 : : }
1462 : :
1463 : : /* Find section by name. */
1464 : : unsigned find (const char *name);
1465 : : /* Find section by index. */
1466 : : const section *find (unsigned snum, unsigned type = SHT_PROGBITS);
1467 : :
1468 : : public:
1469 : : /* Release the string table, when we're done with it. */
1470 : 7844 : void release ()
1471 : : {
1472 : 7844 : shrink (strtab);
1473 : 39 : }
1474 : :
1475 : : public:
1476 : : bool begin (location_t);
1477 : 5045 : bool end ()
1478 : : {
1479 : 5045 : release ();
1480 : : #if MAPPED_READING
1481 : 5045 : if (hdr.buffer)
1482 : 2766 : munmap (hdr.buffer, hdr.pos);
1483 : 5045 : hdr.buffer = NULL;
1484 : : #endif
1485 : 5045 : shrink (sectab);
1486 : :
1487 : 5045 : return parent::end ();
1488 : : }
1489 : :
1490 : : public:
1491 : : /* Return string name at OFFSET. Checks OFFSET range. Always
1492 : : returns non-NULL. We know offset 0 is an empty string. */
1493 : 316640 : const char *name (unsigned offset)
1494 : : {
1495 : 633280 : return &strtab.buffer[offset < strtab.size ? offset : 0];
1496 : : }
1497 : : };
1498 : :
1499 : : /* ELROND writer. */
1500 : :
1501 : : class elf_out : public elf, public data::allocator {
1502 : : typedef elf parent;
1503 : : /* Desired section alignment on disk. */
1504 : : static const int SECTION_ALIGN = 16;
1505 : :
1506 : : private:
1507 : : ptr_int_hash_map identtab; /* Map of IDENTIFIERS to strtab offsets. */
1508 : : unsigned pos; /* Write position in file. */
1509 : : #if MAPPED_WRITING
1510 : : unsigned offset; /* Offset of the mapping. */
1511 : : unsigned extent; /* Length of mapping. */
1512 : : unsigned page_size; /* System page size. */
1513 : : #endif
1514 : :
1515 : : public:
1516 : 2718 : elf_out (int fd, int e)
1517 : 5333 : :parent (fd, e), identtab (500), pos (0)
1518 : : {
1519 : : #if MAPPED_WRITING
1520 : 2718 : offset = extent = 0;
1521 : 2718 : page_size = sysconf (_SC_PAGE_SIZE);
1522 : 2718 : if (page_size < SECTION_ALIGN)
1523 : : /* Something really strange. */
1524 : 0 : set_error (EINVAL);
1525 : : #endif
1526 : 2718 : }
1527 : 2718 : ~elf_out ()
1528 : 2718 : {
1529 : 2718 : data::simple_memory.shrink (hdr);
1530 : 2718 : data::simple_memory.shrink (sectab);
1531 : 2718 : data::simple_memory.shrink (strtab);
1532 : 2718 : }
1533 : :
1534 : : #if MAPPED_WRITING
1535 : : private:
1536 : : void create_mapping (unsigned ext, bool extending = true);
1537 : : void remove_mapping ();
1538 : : #endif
1539 : :
1540 : : protected:
1541 : : using allocator::grow;
1542 : : char *grow (char *, unsigned needed) final override;
1543 : : #if MAPPED_WRITING
1544 : : using allocator::shrink;
1545 : : void shrink (char *) final override;
1546 : : #endif
1547 : :
1548 : : public:
1549 : 5436 : unsigned get_section_limit () const
1550 : : {
1551 : 5436 : return sectab.pos / sizeof (section);
1552 : : }
1553 : :
1554 : : protected:
1555 : : unsigned add (unsigned type, unsigned name = 0,
1556 : : unsigned off = 0, unsigned size = 0, unsigned flags = SHF_NONE);
1557 : : unsigned write (const data &);
1558 : : #if MAPPED_WRITING
1559 : : unsigned write (const bytes_out &);
1560 : : #endif
1561 : :
1562 : : public:
1563 : : /* IDENTIFIER to strtab offset. */
1564 : : unsigned name (tree ident);
1565 : : /* String literal to strtab offset. */
1566 : : unsigned name (const char *n);
1567 : : /* Qualified name of DECL to strtab offset. */
1568 : : unsigned qualified_name (tree decl, bool is_defn);
1569 : :
1570 : : private:
1571 : : unsigned strtab_write (const char *s, unsigned l);
1572 : : void strtab_write (tree decl, int);
1573 : :
1574 : : public:
1575 : : /* Add a section with contents or strings. */
1576 : : unsigned add (const bytes_out &, bool string_p, unsigned name);
1577 : :
1578 : : public:
1579 : : /* Begin and end writing. */
1580 : : bool begin ();
1581 : : bool end ();
1582 : : };
1583 : :
1584 : : /* Begin reading section NAME (of type PROGBITS) from SOURCE.
1585 : : Data always checked for CRC. */
1586 : :
1587 : : bool
1588 : 20227 : bytes_in::begin (location_t loc, elf_in *source, const char *name)
1589 : : {
1590 : 20227 : unsigned snum = source->find (name);
1591 : :
1592 : 20227 : return begin (loc, source, snum, name);
1593 : : }
1594 : :
1595 : : /* Begin reading section numbered SNUM with NAME (may be NULL). */
1596 : :
1597 : : bool
1598 : 210186 : bytes_in::begin (location_t loc, elf_in *source, unsigned snum, const char *name)
1599 : : {
1600 : 210186 : if (!source->read (this, source->find (snum))
1601 : 210186 : || !size || !check_crc ())
1602 : : {
1603 : 0 : source->set_error (elf::E_BAD_DATA);
1604 : 0 : source->shrink (*this);
1605 : 0 : if (name)
1606 : 0 : error_at (loc, "section %qs is missing or corrupted", name);
1607 : : else
1608 : 0 : error_at (loc, "section #%u is missing or corrupted", snum);
1609 : 0 : return false;
1610 : : }
1611 : 210186 : pos = 4;
1612 : 210186 : return true;
1613 : : }
1614 : :
1615 : : /* Finish reading a section. */
1616 : :
1617 : : bool
1618 : 209097 : bytes_in::end (elf_in *src)
1619 : : {
1620 : 209097 : if (more_p ())
1621 : 13 : set_overrun ();
1622 : 209097 : if (overrun)
1623 : 13 : src->set_error ();
1624 : :
1625 : 209097 : src->shrink (*this);
1626 : :
1627 : 209097 : return !overrun;
1628 : : }
1629 : :
1630 : : /* Begin writing buffer. */
1631 : :
1632 : : void
1633 : 263966 : bytes_out::begin (bool need_crc)
1634 : : {
1635 : 0 : if (need_crc)
1636 : 0 : pos = 4;
1637 : 0 : memory->grow (*this, 0, false);
1638 : 243579 : }
1639 : :
1640 : : /* Finish writing buffer. Stream out to SINK as named section NAME.
1641 : : Return section number or 0 on failure. If CRC_PTR is true, crc
1642 : : the data. Otherwise it is a string section. */
1643 : :
1644 : : unsigned
1645 : 263966 : bytes_out::end (elf_out *sink, unsigned name, unsigned *crc_ptr)
1646 : : {
1647 : 263966 : lengths[3] += pos;
1648 : 263966 : spans[3]++;
1649 : :
1650 : 263966 : set_crc (crc_ptr);
1651 : 263966 : unsigned sec_num = sink->add (*this, !crc_ptr, name);
1652 : 263966 : memory->shrink (*this);
1653 : :
1654 : 263966 : return sec_num;
1655 : : }
1656 : :
1657 : : /* Close and open the file, without destroying it. */
1658 : :
1659 : : void
1660 : 9 : elf_in::freeze ()
1661 : : {
1662 : 9 : gcc_checking_assert (!is_frozen ());
1663 : : #if MAPPED_READING
1664 : 9 : if (munmap (hdr.buffer, hdr.pos) < 0)
1665 : 0 : set_error (errno);
1666 : : #endif
1667 : 9 : if (close (fd) < 0)
1668 : 0 : set_error (errno);
1669 : 9 : fd = -1;
1670 : 9 : }
1671 : :
1672 : : bool
1673 : 9 : elf_in::defrost (const char *name)
1674 : : {
1675 : 9 : gcc_checking_assert (is_frozen ());
1676 : 9 : struct stat stat;
1677 : :
1678 : 9 : fd = open (name, O_RDONLY | O_CLOEXEC | O_BINARY);
1679 : 9 : if (fd < 0 || fstat (fd, &stat) < 0)
1680 : 0 : set_error (errno);
1681 : : else
1682 : : {
1683 : 9 : bool ok = hdr.pos == unsigned (stat.st_size);
1684 : : #ifndef HOST_LACKS_INODE_NUMBERS
1685 : 9 : if (device != stat.st_dev
1686 : 9 : || inode != stat.st_ino)
1687 : : ok = false;
1688 : : #endif
1689 : 9 : if (!ok)
1690 : 0 : set_error (EMFILE);
1691 : : #if MAPPED_READING
1692 : 0 : if (ok)
1693 : : {
1694 : 9 : char *mapping = reinterpret_cast<char *>
1695 : 9 : (mmap (NULL, hdr.pos, PROT_READ, MAP_SHARED, fd, 0));
1696 : 9 : if (mapping == MAP_FAILED)
1697 : 0 : fail:
1698 : 0 : set_error (errno);
1699 : : else
1700 : : {
1701 : 9 : if (madvise (mapping, hdr.pos, MADV_RANDOM))
1702 : 0 : goto fail;
1703 : :
1704 : : /* These buffers are never NULL in this case. */
1705 : 9 : strtab.buffer = mapping + strtab.pos;
1706 : 9 : sectab.buffer = mapping + sectab.pos;
1707 : 9 : hdr.buffer = mapping;
1708 : : }
1709 : : }
1710 : : #endif
1711 : : }
1712 : :
1713 : 9 : return !get_error ();
1714 : : }
1715 : :
1716 : : /* Read at current position into BUFFER. Return true on success. */
1717 : :
1718 : : const char *
1719 : 215834 : elf_in::read (data *data, unsigned pos, unsigned length)
1720 : : {
1721 : : #if MAPPED_READING
1722 : 215834 : if (pos + length > hdr.pos)
1723 : : {
1724 : 0 : set_error (EINVAL);
1725 : 0 : return NULL;
1726 : : }
1727 : : #else
1728 : : if (pos != ~0u && lseek (fd, pos, SEEK_SET) < 0)
1729 : : {
1730 : : set_error (errno);
1731 : : return NULL;
1732 : : }
1733 : : #endif
1734 : 215834 : grow (*data, length);
1735 : : #if MAPPED_READING
1736 : 215834 : data->buffer = hdr.buffer + pos;
1737 : : #else
1738 : : if (::read (fd, data->buffer, data->size) != ssize_t (length))
1739 : : {
1740 : : set_error (errno);
1741 : : shrink (*data);
1742 : : return NULL;
1743 : : }
1744 : : #endif
1745 : :
1746 : 215834 : return data->buffer;
1747 : : }
1748 : :
1749 : : /* Read section SNUM of TYPE. Return section pointer or NULL on error. */
1750 : :
1751 : : const elf::section *
1752 : 213010 : elf_in::find (unsigned snum, unsigned type)
1753 : : {
1754 : 213010 : const section *sec = get_section (snum);
1755 : 213010 : if (!snum || !sec || sec->type != type)
1756 : 0 : return NULL;
1757 : : return sec;
1758 : : }
1759 : :
1760 : : /* Find a section NAME and TYPE. Return section number, or zero on
1761 : : failure. */
1762 : :
1763 : : unsigned
1764 : 20272 : elf_in::find (const char *sname)
1765 : : {
1766 : 130028 : for (unsigned pos = sectab.size; pos -= sizeof (section); )
1767 : : {
1768 : 130028 : const section *sec
1769 : 130028 : = reinterpret_cast<const section *> (§ab.buffer[pos]);
1770 : :
1771 : 260056 : if (0 == strcmp (sname, name (sec->name)))
1772 : 20272 : return pos / sizeof (section);
1773 : : }
1774 : :
1775 : : return 0;
1776 : : }
1777 : :
1778 : : /* Begin reading file. Verify header. Pull in section and string
1779 : : tables. Return true on success. */
1780 : :
1781 : : bool
1782 : 2845 : elf_in::begin (location_t loc)
1783 : : {
1784 : 2845 : if (!parent::begin ())
1785 : : return false;
1786 : :
1787 : 2824 : struct stat stat;
1788 : 2824 : unsigned size = 0;
1789 : 2824 : if (!fstat (fd, &stat))
1790 : : {
1791 : : #if !defined (HOST_LACKS_INODE_NUMBERS)
1792 : 2824 : device = stat.st_dev;
1793 : 2824 : inode = stat.st_ino;
1794 : : #endif
1795 : : /* Never generate files > 4GB, check we've not been given one. */
1796 : 2824 : if (stat.st_size == unsigned (stat.st_size))
1797 : 2824 : size = unsigned (stat.st_size);
1798 : : }
1799 : :
1800 : : #if MAPPED_READING
1801 : : /* MAP_SHARED so that the file is backing store. If someone else
1802 : : concurrently writes it, they're wrong. */
1803 : 2824 : void *mapping = mmap (NULL, size, PROT_READ, MAP_SHARED, fd, 0);
1804 : 2824 : if (mapping == MAP_FAILED)
1805 : : {
1806 : 0 : fail:
1807 : 0 : set_error (errno);
1808 : 0 : return false;
1809 : : }
1810 : : /* We'll be hopping over this randomly. Some systems declare the
1811 : : first parm as char *, and other declare it as void *. */
1812 : 2824 : if (madvise (reinterpret_cast <char *> (mapping), size, MADV_RANDOM))
1813 : 0 : goto fail;
1814 : :
1815 : 2824 : hdr.buffer = (char *)mapping;
1816 : : #else
1817 : : read (&hdr, 0, sizeof (header));
1818 : : #endif
1819 : 2824 : hdr.pos = size; /* Record size of the file. */
1820 : :
1821 : 2824 : const header *h = reinterpret_cast<const header *> (hdr.buffer);
1822 : 2824 : if (!h)
1823 : : return false;
1824 : :
1825 : 2824 : if (h->ident.magic[0] != 0x7f
1826 : 2824 : || h->ident.magic[1] != 'E'
1827 : 2824 : || h->ident.magic[2] != 'L'
1828 : 2824 : || h->ident.magic[3] != 'F')
1829 : : {
1830 : 0 : error_at (loc, "not Encapsulated Lazy Records of Named Declarations");
1831 : 0 : failed:
1832 : 0 : shrink (hdr);
1833 : 0 : return false;
1834 : : }
1835 : :
1836 : : /* We expect a particular format -- the ELF is not intended to be
1837 : : distributable. */
1838 : 2824 : if (h->ident.klass != MY_CLASS
1839 : 2824 : || h->ident.data != MY_ENDIAN
1840 : 2824 : || h->ident.version != EV_CURRENT
1841 : 2824 : || h->type != ET_NONE
1842 : 2824 : || h->machine != EM_NONE
1843 : 2824 : || h->ident.osabi != OSABI_NONE)
1844 : : {
1845 : 0 : error_at (loc, "unexpected encapsulation format or type");
1846 : 0 : goto failed;
1847 : : }
1848 : :
1849 : 2824 : int e = -1;
1850 : 2824 : if (!h->shoff || h->shentsize != sizeof (section))
1851 : : {
1852 : 0 : malformed:
1853 : 0 : set_error (e);
1854 : 0 : error_at (loc, "encapsulation is malformed");
1855 : 0 : goto failed;
1856 : : }
1857 : :
1858 : 2824 : unsigned strndx = h->shstrndx;
1859 : 2824 : unsigned shnum = h->shnum;
1860 : 2824 : if (shnum == SHN_XINDEX)
1861 : : {
1862 : 0 : if (!read (§ab, h->shoff, sizeof (section)))
1863 : : {
1864 : 0 : section_table_fail:
1865 : 0 : e = errno;
1866 : 0 : goto malformed;
1867 : : }
1868 : 0 : shnum = get_section (0)->size;
1869 : : /* Freeing does mean we'll re-read it in the case we're not
1870 : : mapping, but this is going to be rare. */
1871 : 0 : shrink (sectab);
1872 : : }
1873 : :
1874 : 2824 : if (!shnum)
1875 : 0 : goto malformed;
1876 : :
1877 : 2824 : if (!read (§ab, h->shoff, shnum * sizeof (section)))
1878 : 0 : goto section_table_fail;
1879 : :
1880 : 2824 : if (strndx == SHN_XINDEX)
1881 : 0 : strndx = get_section (0)->link;
1882 : :
1883 : 2824 : if (!read (&strtab, find (strndx, SHT_STRTAB)))
1884 : 0 : goto malformed;
1885 : :
1886 : : /* The string table should be at least one byte, with NUL chars
1887 : : at either end. */
1888 : 2824 : if (!(strtab.size && !strtab.buffer[0]
1889 : 2824 : && !strtab.buffer[strtab.size - 1]))
1890 : 0 : goto malformed;
1891 : :
1892 : : #if MAPPED_READING
1893 : : /* Record the offsets of the section and string tables. */
1894 : 2824 : sectab.pos = h->shoff;
1895 : 2824 : strtab.pos = shnum * sizeof (section);
1896 : : #else
1897 : : shrink (hdr);
1898 : : #endif
1899 : :
1900 : 2824 : return true;
1901 : : }
1902 : :
1903 : : /* Create a new mapping. */
1904 : :
1905 : : #if MAPPED_WRITING
1906 : : void
1907 : 3380 : elf_out::create_mapping (unsigned ext, bool extending)
1908 : : {
1909 : : /* A wrapper around posix_fallocate, falling back to ftruncate
1910 : : if the underlying filesystem does not support the operation. */
1911 : 6624 : auto allocate = [](int fd, off_t offset, off_t length)
1912 : : {
1913 : : #ifdef HAVE_POSIX_FALLOCATE
1914 : 3244 : int result = posix_fallocate (fd, offset, length);
1915 : 3244 : if (result != EINVAL)
1916 : 3244 : return result == 0;
1917 : : /* Not supported by the underlying filesystem, fallback to ftruncate. */
1918 : : #endif
1919 : 0 : return ftruncate (fd, offset + length) == 0;
1920 : : };
1921 : :
1922 : 3380 : void *mapping = MAP_FAILED;
1923 : 3380 : if (extending && ext < 1024 * 1024)
1924 : : {
1925 : 3124 : if (allocate (fd, offset, ext * 2))
1926 : 3124 : mapping = mmap (NULL, ext * 2, PROT_READ | PROT_WRITE,
1927 : 3124 : MAP_SHARED, fd, offset);
1928 : 3124 : if (mapping != MAP_FAILED)
1929 : : ext *= 2;
1930 : : }
1931 : : if (mapping == MAP_FAILED)
1932 : : {
1933 : 256 : if (!extending || allocate (fd, offset, ext))
1934 : 256 : mapping = mmap (NULL, ext, PROT_READ | PROT_WRITE,
1935 : 256 : MAP_SHARED, fd, offset);
1936 : 256 : if (mapping == MAP_FAILED)
1937 : : {
1938 : 0 : set_error (errno);
1939 : : mapping = NULL;
1940 : : ext = 0;
1941 : : }
1942 : : }
1943 : 3380 : hdr.buffer = (char *)mapping;
1944 : 3380 : extent = ext;
1945 : 3380 : }
1946 : : #endif
1947 : :
1948 : : /* Flush out the current mapping. */
1949 : :
1950 : : #if MAPPED_WRITING
1951 : : void
1952 : 3386 : elf_out::remove_mapping ()
1953 : : {
1954 : 3386 : if (hdr.buffer)
1955 : : {
1956 : : /* MS_ASYNC dtrt with the removed mapping, including a
1957 : : subsequent overlapping remap. */
1958 : 3380 : if (msync (hdr.buffer, extent, MS_ASYNC)
1959 : 3380 : || munmap (hdr.buffer, extent))
1960 : : /* We're somewhat screwed at this point. */
1961 : 0 : set_error (errno);
1962 : : }
1963 : :
1964 : 3386 : hdr.buffer = NULL;
1965 : 3386 : }
1966 : : #endif
1967 : :
1968 : : /* Grow a mapping of PTR to be NEEDED bytes long. This gets
1969 : : interesting if the new size grows the EXTENT. */
1970 : :
1971 : : char *
1972 : 616204 : elf_out::grow (char *data, unsigned needed)
1973 : : {
1974 : 616204 : if (!data)
1975 : : {
1976 : : /* First allocation, check we're aligned. */
1977 : 269196 : gcc_checking_assert (!(pos & (SECTION_ALIGN - 1)));
1978 : : #if MAPPED_WRITING
1979 : 269196 : data = hdr.buffer + (pos - offset);
1980 : : #endif
1981 : : }
1982 : :
1983 : : #if MAPPED_WRITING
1984 : 616204 : unsigned off = data - hdr.buffer;
1985 : 616204 : if (off + needed > extent)
1986 : : {
1987 : : /* We need to grow the mapping. */
1988 : 635 : unsigned lwm = off & ~(page_size - 1);
1989 : 635 : unsigned hwm = (off + needed + page_size - 1) & ~(page_size - 1);
1990 : :
1991 : 635 : gcc_checking_assert (hwm > extent);
1992 : :
1993 : 635 : remove_mapping ();
1994 : :
1995 : 635 : offset += lwm;
1996 : 635 : create_mapping (extent < hwm - lwm ? hwm - lwm : extent);
1997 : :
1998 : 635 : data = hdr.buffer + (off - lwm);
1999 : : }
2000 : : #else
2001 : : data = allocator::grow (data, needed);
2002 : : #endif
2003 : :
2004 : 616204 : return data;
2005 : : }
2006 : :
2007 : : #if MAPPED_WRITING
2008 : : /* Shrinking is a NOP. */
2009 : : void
2010 : 269196 : elf_out::shrink (char *)
2011 : : {
2012 : 269196 : }
2013 : : #endif
2014 : :
2015 : : /* Write S of length L to the strtab buffer. L must include the ending
2016 : : NUL, if that's what you want. */
2017 : :
2018 : : unsigned
2019 : 1154413 : elf_out::strtab_write (const char *s, unsigned l)
2020 : : {
2021 : 1154413 : if (strtab.pos + l > strtab.size)
2022 : 1220 : data::simple_memory.grow (strtab, strtab.pos + l, false);
2023 : 1154413 : memcpy (strtab.buffer + strtab.pos, s, l);
2024 : 1154413 : unsigned res = strtab.pos;
2025 : 1154413 : strtab.pos += l;
2026 : 1154413 : return res;
2027 : : }
2028 : :
2029 : : /* Write qualified name of decl. INNER >0 if this is a definition, <0
2030 : : if this is a qualifier of an outer name. */
2031 : :
2032 : : void
2033 : 452966 : elf_out::strtab_write (tree decl, int inner)
2034 : : {
2035 : 452966 : tree ctx = CP_DECL_CONTEXT (decl);
2036 : 452966 : if (TYPE_P (ctx))
2037 : 5038 : ctx = TYPE_NAME (ctx);
2038 : 452966 : if (ctx != global_namespace)
2039 : 223372 : strtab_write (ctx, -1);
2040 : :
2041 : 452966 : tree name = DECL_NAME (decl);
2042 : 452966 : if (!name)
2043 : 372 : name = DECL_ASSEMBLER_NAME_RAW (decl);
2044 : 452966 : strtab_write (IDENTIFIER_POINTER (name), IDENTIFIER_LENGTH (name));
2045 : :
2046 : 452966 : if (inner)
2047 : 319146 : strtab_write (&"::{}"[inner+1], 2);
2048 : 452966 : }
2049 : :
2050 : : /* Map IDENTIFIER IDENT to strtab offset. Inserts into strtab if not
2051 : : already there. */
2052 : :
2053 : : unsigned
2054 : 133059 : elf_out::name (tree ident)
2055 : : {
2056 : 133059 : unsigned res = 0;
2057 : 133059 : if (ident)
2058 : : {
2059 : 133013 : bool existed;
2060 : 133013 : int *slot = &identtab.get_or_insert (ident, &existed);
2061 : 133013 : if (!existed)
2062 : 238622 : *slot = strtab_write (IDENTIFIER_POINTER (ident),
2063 : 119311 : IDENTIFIER_LENGTH (ident) + 1);
2064 : 133013 : res = *slot;
2065 : : }
2066 : 133059 : return res;
2067 : : }
2068 : :
2069 : : /* Map LITERAL to strtab offset. Does not detect duplicates and
2070 : : expects LITERAL to remain live until strtab is written out. */
2071 : :
2072 : : unsigned
2073 : 33396 : elf_out::name (const char *literal)
2074 : : {
2075 : 33396 : return strtab_write (literal, strlen (literal) + 1);
2076 : : }
2077 : :
2078 : : /* Map a DECL's qualified name to strtab offset. Does not detect
2079 : : duplicates. */
2080 : :
2081 : : unsigned
2082 : 229594 : elf_out::qualified_name (tree decl, bool is_defn)
2083 : : {
2084 : 229594 : gcc_checking_assert (DECL_P (decl) && decl != global_namespace);
2085 : 229594 : unsigned result = strtab.pos;
2086 : :
2087 : 229594 : strtab_write (decl, is_defn);
2088 : 229594 : strtab_write ("", 1);
2089 : :
2090 : 229594 : return result;
2091 : : }
2092 : :
2093 : : /* Add section to file. Return section number. TYPE & NAME identify
2094 : : the section. OFF and SIZE identify the file location of its
2095 : : data. FLAGS contains additional info. */
2096 : :
2097 : : unsigned
2098 : 269190 : elf_out::add (unsigned type, unsigned name, unsigned off, unsigned size,
2099 : : unsigned flags)
2100 : : {
2101 : 269190 : gcc_checking_assert (!(off & (SECTION_ALIGN - 1)));
2102 : 269190 : if (sectab.pos + sizeof (section) > sectab.size)
2103 : 4549 : data::simple_memory.grow (sectab, sectab.pos + sizeof (section), false);
2104 : 269190 : section *sec = reinterpret_cast<section *> (sectab.buffer + sectab.pos);
2105 : 269190 : memset (sec, 0, sizeof (section));
2106 : 269190 : sec->type = type;
2107 : 269190 : sec->flags = flags;
2108 : 269190 : sec->name = name;
2109 : 269190 : sec->offset = off;
2110 : 269190 : sec->size = size;
2111 : 269190 : if (flags & SHF_STRINGS)
2112 : 5195 : sec->entsize = 1;
2113 : :
2114 : 269190 : unsigned res = sectab.pos;
2115 : 269190 : sectab.pos += sizeof (section);
2116 : 269190 : return res / sizeof (section);
2117 : : }
2118 : :
2119 : : /* Pad to the next alignment boundary, then write BUFFER to disk.
2120 : : Return the position of the start of the write, or zero on failure. */
2121 : :
2122 : : unsigned
2123 : 10454 : elf_out::write (const data &buffer)
2124 : : {
2125 : : #if MAPPED_WRITING
2126 : : /* HDR is always mapped. */
2127 : 10454 : if (&buffer != &hdr)
2128 : : {
2129 : 5230 : bytes_out out (this);
2130 : 5230 : grow (out, buffer.pos, true);
2131 : 5230 : if (out.buffer)
2132 : 5230 : memcpy (out.buffer, buffer.buffer, buffer.pos);
2133 : 5230 : shrink (out);
2134 : 5230 : }
2135 : : else
2136 : : /* We should have been aligned during the first allocation. */
2137 : 5224 : gcc_checking_assert (!(pos & (SECTION_ALIGN - 1)));
2138 : : #else
2139 : : if (::write (fd, buffer.buffer, buffer.pos) != ssize_t (buffer.pos))
2140 : : {
2141 : : set_error (errno);
2142 : : return 0;
2143 : : }
2144 : : #endif
2145 : 10454 : unsigned res = pos;
2146 : 10454 : pos += buffer.pos;
2147 : :
2148 : 10454 : if (unsigned padding = -pos & (SECTION_ALIGN - 1))
2149 : : {
2150 : : #if !MAPPED_WRITING
2151 : : /* Align the section on disk, should help the necessary copies.
2152 : : fseeking to extend is non-portable. */
2153 : : static char zero[SECTION_ALIGN];
2154 : : if (::write (fd, &zero, padding) != ssize_t (padding))
2155 : : set_error (errno);
2156 : : #endif
2157 : 8904 : pos += padding;
2158 : : }
2159 : 10454 : return res;
2160 : : }
2161 : :
2162 : : /* Write a streaming buffer. It must be using us as an allocator. */
2163 : :
2164 : : #if MAPPED_WRITING
2165 : : unsigned
2166 : 263966 : elf_out::write (const bytes_out &buf)
2167 : : {
2168 : 263966 : gcc_checking_assert (buf.memory == this);
2169 : : /* A directly mapped buffer. */
2170 : 263966 : gcc_checking_assert (buf.buffer - hdr.buffer >= 0
2171 : : && buf.buffer - hdr.buffer + buf.size <= extent);
2172 : 263966 : unsigned res = pos;
2173 : 263966 : pos += buf.pos;
2174 : :
2175 : : /* Align up. We're not going to advance into the next page. */
2176 : 263966 : pos += -pos & (SECTION_ALIGN - 1);
2177 : :
2178 : 263966 : return res;
2179 : : }
2180 : : #endif
2181 : :
2182 : : /* Write data and add section. STRING_P is true for a string
2183 : : section, false for PROGBITS. NAME identifies the section (0 is the
2184 : : empty name). DATA is the contents. Return section number or 0 on
2185 : : failure (0 is the undef section). */
2186 : :
2187 : : unsigned
2188 : 263966 : elf_out::add (const bytes_out &data, bool string_p, unsigned name)
2189 : : {
2190 : 263966 : unsigned off = write (data);
2191 : :
2192 : 527932 : return add (string_p ? SHT_STRTAB : SHT_PROGBITS, name,
2193 : 263966 : off, data.pos, string_p ? SHF_STRINGS : SHF_NONE);
2194 : : }
2195 : :
2196 : : /* Begin writing the file. Initialize the section table and write an
2197 : : empty header. Return false on failure. */
2198 : :
2199 : : bool
2200 : 2609 : elf_out::begin ()
2201 : : {
2202 : 2609 : if (!parent::begin ())
2203 : : return false;
2204 : :
2205 : : /* Let the allocators pick a default. */
2206 : 2609 : data::simple_memory.grow (strtab, 0, false);
2207 : 2609 : data::simple_memory.grow (sectab, 0, false);
2208 : :
2209 : : /* The string table starts with an empty string. */
2210 : 2609 : name ("");
2211 : :
2212 : : /* Create the UNDEF section. */
2213 : 2609 : add (SHT_NONE);
2214 : :
2215 : : #if MAPPED_WRITING
2216 : : /* Start a mapping. */
2217 : 2609 : create_mapping (EXPERIMENT (page_size,
2218 : : (32767 + page_size) & ~(page_size - 1)));
2219 : 2609 : if (!hdr.buffer)
2220 : : return false;
2221 : : #endif
2222 : :
2223 : : /* Write an empty header. */
2224 : 2609 : grow (hdr, sizeof (header), true);
2225 : 2609 : header *h = reinterpret_cast<header *> (hdr.buffer);
2226 : 2609 : memset (h, 0, sizeof (header));
2227 : 2609 : hdr.pos = hdr.size;
2228 : 2609 : write (hdr);
2229 : 2609 : return !get_error ();
2230 : : }
2231 : :
2232 : : /* Finish writing the file. Write out the string & section tables.
2233 : : Fill in the header. Return true on error. */
2234 : :
2235 : : bool
2236 : 2718 : elf_out::end ()
2237 : : {
2238 : 2718 : if (fd >= 0)
2239 : : {
2240 : : /* Write the string table. */
2241 : 2615 : unsigned strnam = name (".strtab");
2242 : 2615 : unsigned stroff = write (strtab);
2243 : 2615 : unsigned strndx = add (SHT_STRTAB, strnam, stroff, strtab.pos,
2244 : : SHF_STRINGS);
2245 : :
2246 : : /* Store escape values in section[0]. */
2247 : 2615 : if (strndx >= SHN_LORESERVE)
2248 : : {
2249 : 0 : reinterpret_cast<section *> (sectab.buffer)->link = strndx;
2250 : 0 : strndx = SHN_XINDEX;
2251 : : }
2252 : 2615 : unsigned shnum = sectab.pos / sizeof (section);
2253 : 2615 : if (shnum >= SHN_LORESERVE)
2254 : : {
2255 : 0 : reinterpret_cast<section *> (sectab.buffer)->size = shnum;
2256 : 0 : shnum = SHN_XINDEX;
2257 : : }
2258 : :
2259 : 2615 : unsigned shoff = write (sectab);
2260 : :
2261 : : #if MAPPED_WRITING
2262 : 2615 : if (offset)
2263 : : {
2264 : 136 : remove_mapping ();
2265 : 136 : offset = 0;
2266 : 136 : create_mapping ((sizeof (header) + page_size - 1) & ~(page_size - 1),
2267 : : false);
2268 : : }
2269 : 2615 : unsigned length = pos;
2270 : : #else
2271 : : if (lseek (fd, 0, SEEK_SET) < 0)
2272 : : set_error (errno);
2273 : : #endif
2274 : : /* Write header. */
2275 : 2615 : if (!get_error ())
2276 : : {
2277 : : /* Write the correct header now. */
2278 : 2615 : header *h = reinterpret_cast<header *> (hdr.buffer);
2279 : 2615 : h->ident.magic[0] = 0x7f;
2280 : 2615 : h->ident.magic[1] = 'E'; /* Elrond */
2281 : 2615 : h->ident.magic[2] = 'L'; /* is an */
2282 : 2615 : h->ident.magic[3] = 'F'; /* elf. */
2283 : 2615 : h->ident.klass = MY_CLASS;
2284 : 2615 : h->ident.data = MY_ENDIAN;
2285 : 2615 : h->ident.version = EV_CURRENT;
2286 : 2615 : h->ident.osabi = OSABI_NONE;
2287 : 2615 : h->type = ET_NONE;
2288 : 2615 : h->machine = EM_NONE;
2289 : 2615 : h->version = EV_CURRENT;
2290 : 2615 : h->shoff = shoff;
2291 : 2615 : h->ehsize = sizeof (header);
2292 : 2615 : h->shentsize = sizeof (section);
2293 : 2615 : h->shnum = shnum;
2294 : 2615 : h->shstrndx = strndx;
2295 : :
2296 : 2615 : pos = 0;
2297 : 2615 : write (hdr);
2298 : : }
2299 : :
2300 : : #if MAPPED_WRITING
2301 : 2615 : remove_mapping ();
2302 : 2615 : if (ftruncate (fd, length))
2303 : 0 : set_error (errno);
2304 : : #endif
2305 : : }
2306 : :
2307 : 2718 : data::simple_memory.shrink (sectab);
2308 : 2718 : data::simple_memory.shrink (strtab);
2309 : :
2310 : 2718 : return parent::end ();
2311 : : }
2312 : :
2313 : : /********************************************************************/
2314 : :
2315 : : /* A dependency set. This is used during stream out to determine the
2316 : : connectivity of the graph. Every namespace-scope declaration that
2317 : : needs writing has a depset. The depset is filled with the (depsets
2318 : : of) declarations within this module that it references. For a
2319 : : declaration that'll generally be named types. For definitions
2320 : : it'll also be declarations in the body.
2321 : :
2322 : : From that we can convert the graph to a DAG, via determining the
2323 : : Strongly Connected Clusters. Each cluster is streamed
2324 : : independently, and thus we achieve lazy loading.
2325 : :
2326 : : Other decls that get a depset are namespaces themselves and
2327 : : unnameable declarations. */
2328 : :
2329 : : class depset {
2330 : : private:
2331 : : tree entity; /* Entity, or containing namespace. */
2332 : : uintptr_t discriminator; /* Flags or identifier. */
2333 : :
2334 : : public:
2335 : : /* The kinds of entity the depset could describe. The ordering is
2336 : : significant, see entity_kind_name. */
2337 : : enum entity_kind
2338 : : {
2339 : : EK_DECL, /* A decl. */
2340 : : EK_SPECIALIZATION, /* A specialization. */
2341 : : EK_PARTIAL, /* A partial specialization. */
2342 : : EK_USING, /* A using declaration (at namespace scope). */
2343 : : EK_NAMESPACE, /* A namespace. */
2344 : : EK_TU_LOCAL, /* A TU-local decl for ADL. */
2345 : : EK_REDIRECT, /* Redirect to a template_decl. */
2346 : : EK_EXPLICIT_HWM,
2347 : : EK_BINDING = EK_EXPLICIT_HWM, /* Implicitly encoded. */
2348 : : EK_FOR_BINDING, /* A decl being inserted for a binding. */
2349 : : EK_INNER_DECL, /* A decl defined outside of its imported
2350 : : context. */
2351 : : EK_DIRECT_HWM = EK_PARTIAL + 1,
2352 : :
2353 : : EK_BITS = 3 /* Only need to encode below EK_EXPLICIT_HWM. */
2354 : : };
2355 : : static_assert (EK_EXPLICIT_HWM < (1u << EK_BITS),
2356 : : "not enough bits reserved for entity_kind");
2357 : :
2358 : : private:
2359 : : /* Placement of bit fields in discriminator. */
2360 : : enum disc_bits
2361 : : {
2362 : : DB_ZERO_BIT, /* Set to disambiguate identifier from flags */
2363 : : DB_SPECIAL_BIT, /* First dep slot is special. */
2364 : : DB_KIND_BIT, /* Kind of the entity. */
2365 : : DB_KIND_BITS = EK_BITS,
2366 : : DB_DEFN_BIT = DB_KIND_BIT + DB_KIND_BITS,
2367 : : DB_IS_PENDING_BIT, /* Is a maybe-pending entity. */
2368 : : DB_TU_LOCAL_BIT, /* Is a TU-local entity. */
2369 : : DB_REF_GLOBAL_BIT, /* Refers to a GMF TU-local entity. */
2370 : : DB_REF_PURVIEW_BIT, /* Refers to a purview TU-local entity. */
2371 : : DB_EXPOSE_GLOBAL_BIT, /* Exposes a GMF TU-local entity. */
2372 : : DB_EXPOSE_PURVIEW_BIT, /* Exposes a purview TU-local entity. */
2373 : : DB_IMPORTED_BIT, /* An imported entity. */
2374 : : DB_UNREACHED_BIT, /* A yet-to-be reached entity. */
2375 : : DB_MAYBE_RECURSIVE_BIT, /* An entity maybe in a recursive cluster. */
2376 : : DB_ENTRY_BIT, /* The first reached recursive dep. */
2377 : : DB_HIDDEN_BIT, /* A hidden binding. */
2378 : : /* The following bits are not independent, but enumerating them is
2379 : : awkward. */
2380 : : DB_TYPE_SPEC_BIT, /* Specialization in the type table. */
2381 : : DB_FRIEND_SPEC_BIT, /* An instantiated template friend. */
2382 : : DB_HWM,
2383 : : };
2384 : : static_assert (DB_HWM <= sizeof(discriminator) * CHAR_BIT,
2385 : : "not enough bits in discriminator");
2386 : :
2387 : : public:
2388 : : /* The first slot is special for EK_SPECIALIZATIONS it is a
2389 : : spec_entry pointer. It is not relevant for the SCC
2390 : : determination. */
2391 : : vec<depset *> deps; /* Depsets we reference. */
2392 : :
2393 : : public:
2394 : : unsigned cluster; /* Strongly connected cluster, later entity number */
2395 : : unsigned section; /* Section written to. */
2396 : : /* During SCC construction, section is lowlink, until the depset is
2397 : : removed from the stack. See Tarjan algorithm for details. */
2398 : :
2399 : : private:
2400 : : /* Construction via factories. Destruction via hash traits. */
2401 : : depset (tree entity);
2402 : : ~depset ();
2403 : :
2404 : : public:
2405 : : static depset *make_binding (tree, tree);
2406 : : static depset *make_entity (tree, entity_kind, bool = false);
2407 : : /* Late setting a binding name -- /then/ insert into hash! */
2408 : : inline void set_binding_name (tree name)
2409 : : {
2410 : : gcc_checking_assert (!get_name ());
2411 : : discriminator = reinterpret_cast<uintptr_t> (name);
2412 : : }
2413 : :
2414 : : private:
2415 : 3961732 : template<unsigned I> void set_flag_bit ()
2416 : : {
2417 : 0 : gcc_checking_assert (I < 2 || !is_binding ());
2418 : 3961732 : discriminator |= 1u << I;
2419 : 2401369 : }
2420 : 313457 : template<unsigned I> void clear_flag_bit ()
2421 : : {
2422 : 0 : gcc_checking_assert (I < 2 || !is_binding ());
2423 : 313457 : discriminator &= ~(1u << I);
2424 : 313457 : }
2425 : 402314988 : template<unsigned I> bool get_flag_bit () const
2426 : : {
2427 : 0 : gcc_checking_assert (I < 2 || !is_binding ());
2428 : 489472110 : return bool ((discriminator >> I) & 1);
2429 : : }
2430 : :
2431 : : public:
2432 : 394087584 : bool is_binding () const
2433 : : {
2434 : 89871948 : return !get_flag_bit<DB_ZERO_BIT> ();
2435 : : }
2436 : 215800355 : entity_kind get_entity_kind () const
2437 : : {
2438 : 20669 : if (is_binding ())
2439 : : return EK_BINDING;
2440 : 162528454 : return entity_kind ((discriminator >> DB_KIND_BIT) & ((1u << EK_BITS) - 1));
2441 : : }
2442 : : const char *entity_kind_name () const;
2443 : :
2444 : : public:
2445 : 7073693 : bool has_defn () const
2446 : : {
2447 : : /* Never consider TU-local entities as having definitions, since
2448 : : we will never be accessing them from importers anyway. */
2449 : 7073693 : return get_flag_bit<DB_DEFN_BIT> () && !is_tu_local ();
2450 : : }
2451 : :
2452 : : public:
2453 : : /* This entity might be found other than by namespace-scope lookup;
2454 : : see module_state::write_pendings for more details. */
2455 : 1741948 : bool is_pending_entity () const
2456 : : {
2457 : 2800494 : return (get_entity_kind () == EK_SPECIALIZATION
2458 : 1058546 : || get_entity_kind () == EK_PARTIAL
2459 : 2770092 : || (get_entity_kind () == EK_DECL
2460 : 1002978 : && get_flag_bit<DB_IS_PENDING_BIT> ()));
2461 : : }
2462 : :
2463 : : public:
2464 : : /* Only consider global module entities as being TU-local
2465 : : when STRICT is set; otherwise, as an extension we support
2466 : : emitting declarations referencing TU-local GMF entities
2467 : : (and only check purview entities), to assist in migration. */
2468 : 35302284 : bool is_tu_local (bool strict = false) const
2469 : : {
2470 : : /* Non-strict is only intended for migration purposes, so
2471 : : for simplicity's sake we only care about whether this is
2472 : : a non-purview variable or function at namespace scope;
2473 : : these are the most common cases (coming from C), and
2474 : : that way we don't have to care about diagnostics for
2475 : : nested types and so forth. */
2476 : 14835523 : tree inner = STRIP_TEMPLATE (get_entity ());
2477 : 35302284 : return (get_flag_bit<DB_TU_LOCAL_BIT> ()
2478 : 35302284 : && (strict
2479 : 3209 : || !VAR_OR_FUNCTION_DECL_P (inner)
2480 : 2052 : || !NAMESPACE_SCOPE_P (inner)
2481 : 2025 : || (DECL_LANG_SPECIFIC (inner)
2482 : 2025 : && DECL_MODULE_PURVIEW_P (inner))));
2483 : : }
2484 : 836044 : bool refs_tu_local (bool strict = false) const
2485 : : {
2486 : 836044 : return (get_flag_bit<DB_REF_PURVIEW_BIT> ()
2487 : 836044 : || (strict && get_flag_bit <DB_REF_GLOBAL_BIT> ()));
2488 : : }
2489 : 2548936 : bool is_exposure (bool strict = false) const
2490 : : {
2491 : 2548936 : return (get_flag_bit<DB_EXPOSE_PURVIEW_BIT> ()
2492 : 2548936 : || (strict && get_flag_bit <DB_EXPOSE_GLOBAL_BIT> ()));
2493 : : }
2494 : :
2495 : : public:
2496 : 21774955 : bool is_import () const
2497 : : {
2498 : 6622885 : return get_flag_bit<DB_IMPORTED_BIT> ();
2499 : : }
2500 : 13279484 : bool is_unreached () const
2501 : : {
2502 : 878729 : return get_flag_bit<DB_UNREACHED_BIT> ();
2503 : : }
2504 : 1206522 : bool is_hidden () const
2505 : : {
2506 : 1206522 : return get_flag_bit<DB_HIDDEN_BIT> ();
2507 : : }
2508 : 722149 : bool is_maybe_recursive () const
2509 : : {
2510 : 722149 : return get_flag_bit<DB_MAYBE_RECURSIVE_BIT> ();
2511 : : }
2512 : 825 : bool is_entry () const
2513 : : {
2514 : 825 : return get_flag_bit<DB_ENTRY_BIT> ();
2515 : : }
2516 : 1025025 : bool is_type_spec () const
2517 : : {
2518 : 1025025 : return get_flag_bit<DB_TYPE_SPEC_BIT> ();
2519 : : }
2520 : 1025025 : bool is_friend_spec () const
2521 : : {
2522 : 1025025 : return get_flag_bit<DB_FRIEND_SPEC_BIT> ();
2523 : : }
2524 : :
2525 : : public:
2526 : : /* We set these bit outside of depset. */
2527 : 5187 : void set_hidden_binding ()
2528 : : {
2529 : 5187 : set_flag_bit<DB_HIDDEN_BIT> ();
2530 : 5187 : }
2531 : 31 : void clear_hidden_binding ()
2532 : : {
2533 : 31 : clear_flag_bit<DB_HIDDEN_BIT> ();
2534 : 31 : }
2535 : :
2536 : : public:
2537 : 8227404 : bool is_special () const
2538 : : {
2539 : 8227404 : return get_flag_bit<DB_SPECIAL_BIT> ();
2540 : : }
2541 : 1560363 : void set_special ()
2542 : : {
2543 : 1560363 : set_flag_bit<DB_SPECIAL_BIT> ();
2544 : 0 : }
2545 : :
2546 : : public:
2547 : 132993244 : tree get_entity () const
2548 : : {
2549 : 35302284 : return entity;
2550 : : }
2551 : 16302346 : tree get_name () const
2552 : : {
2553 : 16302346 : gcc_checking_assert (is_binding ());
2554 : 16302346 : return reinterpret_cast <tree> (discriminator);
2555 : : }
2556 : :
2557 : : public:
2558 : : /* Traits for a hash table of pointers to bindings. */
2559 : : struct traits {
2560 : : /* Each entry is a pointer to a depset. */
2561 : : typedef depset *value_type;
2562 : : /* We lookup by container:maybe-identifier pair. */
2563 : : typedef std::pair<tree,tree> compare_type;
2564 : :
2565 : : static const bool empty_zero_p = true;
2566 : :
2567 : : /* hash and equality for compare_type. */
2568 : 13302970 : inline static hashval_t hash (const compare_type &p)
2569 : : {
2570 : 13302970 : hashval_t h = pointer_hash<tree_node>::hash (p.first);
2571 : 13302970 : if (p.second)
2572 : : {
2573 : 168176 : hashval_t nh = IDENTIFIER_HASH_VALUE (p.second);
2574 : 168176 : h = iterative_hash_hashval_t (h, nh);
2575 : : }
2576 : 13302970 : return h;
2577 : : }
2578 : 63136848 : inline static bool equal (const value_type b, const compare_type &p)
2579 : : {
2580 : 63136848 : if (b->entity != p.first)
2581 : : return false;
2582 : :
2583 : 9371161 : if (p.second)
2584 : 12589 : return b->discriminator == reinterpret_cast<uintptr_t> (p.second);
2585 : : else
2586 : 9358572 : return !b->is_binding ();
2587 : : }
2588 : :
2589 : : /* (re)hasher for a binding itself. */
2590 : 48386101 : inline static hashval_t hash (const value_type b)
2591 : : {
2592 : 48386101 : hashval_t h = pointer_hash<tree_node>::hash (b->entity);
2593 : 48386101 : if (b->is_binding ())
2594 : : {
2595 : 3167252 : hashval_t nh = IDENTIFIER_HASH_VALUE (b->get_name ());
2596 : 3167252 : h = iterative_hash_hashval_t (h, nh);
2597 : : }
2598 : 48386101 : return h;
2599 : : }
2600 : :
2601 : : /* Empty via NULL. */
2602 : 0 : static inline void mark_empty (value_type &p) {p = NULL;}
2603 : : static inline bool is_empty (value_type p) {return !p;}
2604 : :
2605 : : /* Nothing is deletable. Everything is insertable. */
2606 : : static bool is_deleted (value_type) { return false; }
2607 : : static void mark_deleted (value_type) { gcc_unreachable (); }
2608 : :
2609 : : /* We own the entities in the hash table. */
2610 : 2264005 : static void remove (value_type p)
2611 : : {
2612 : 2264005 : delete (p);
2613 : 2264005 : }
2614 : : };
2615 : :
2616 : : public:
2617 : : class hash : public hash_table<traits> {
2618 : : typedef traits::compare_type key_t;
2619 : : typedef hash_table<traits> parent;
2620 : :
2621 : : public:
2622 : : vec<depset *> worklist; /* Worklist of decls to walk. */
2623 : : hash *chain; /* Original table. */
2624 : : depset *current; /* Current depset being depended. */
2625 : : unsigned section; /* When writing out, the section. */
2626 : : bool reached_unreached; /* We reached an unreached entity. */
2627 : : bool writing_merge_key; /* We're writing merge key information. */
2628 : : bool ignore_tu_local; /* In a context where referencing a TU-local
2629 : : entity is not an exposure. */
2630 : :
2631 : : public:
2632 : 243563 : hash (size_t size, hash *c = NULL)
2633 : 487126 : : parent (size), chain (c), current (NULL), section (0),
2634 : 243563 : reached_unreached (false), writing_merge_key (false),
2635 : 243563 : ignore_tu_local (false)
2636 : : {
2637 : 243563 : worklist.create (size);
2638 : 243563 : }
2639 : 243563 : ~hash ()
2640 : : {
2641 : 2609 : worklist.release ();
2642 : 243563 : }
2643 : :
2644 : : public:
2645 : 74601121 : bool is_key_order () const
2646 : : {
2647 : 74601121 : return chain != NULL;
2648 : : }
2649 : :
2650 : : private:
2651 : : depset **entity_slot (tree entity, bool = true);
2652 : : depset **binding_slot (tree ctx, tree name, bool = true);
2653 : : depset *maybe_add_declaration (tree decl);
2654 : :
2655 : : public:
2656 : : depset *find_dependency (tree entity);
2657 : : depset *find_binding (tree ctx, tree name);
2658 : : depset *make_dependency (tree decl, entity_kind);
2659 : : void add_dependency (depset *);
2660 : :
2661 : : public:
2662 : : void add_mergeable (depset *);
2663 : : depset *add_dependency (tree decl, entity_kind);
2664 : : void add_namespace_context (depset *, tree ns);
2665 : :
2666 : : private:
2667 : : static bool add_binding_entity (tree, WMB_Flags, void *);
2668 : :
2669 : : public:
2670 : : bool add_namespace_entities (tree ns, bitmap partitions);
2671 : : void add_specializations (bool decl_p);
2672 : : void add_partial_entities (vec<tree, va_gc> *);
2673 : : void add_class_entities (vec<tree, va_gc> *);
2674 : :
2675 : : private:
2676 : : void add_deduction_guides (tree decl);
2677 : :
2678 : : public:
2679 : : void find_dependencies (module_state *);
2680 : : bool finalize_dependencies ();
2681 : : vec<depset *> connect ();
2682 : :
2683 : : private:
2684 : : bool diagnose_bad_internal_ref (depset *dep, bool strict = false);
2685 : : bool diagnose_template_names_tu_local (depset *dep, bool strict = false);
2686 : : };
2687 : :
2688 : : public:
2689 : : struct tarjan {
2690 : : vec<depset *> result;
2691 : : vec<depset *> stack;
2692 : : unsigned index;
2693 : :
2694 : 243534 : tarjan (unsigned size)
2695 : 243534 : : index (0)
2696 : : {
2697 : 243534 : result.create (size);
2698 : 243534 : stack.create (50);
2699 : 243534 : }
2700 : 243534 : ~tarjan ()
2701 : : {
2702 : 243534 : gcc_assert (!stack.length ());
2703 : 243534 : stack.release ();
2704 : 243534 : }
2705 : :
2706 : : public:
2707 : : void connect (depset *);
2708 : : };
2709 : : };
2710 : :
2711 : : inline
2712 : 2264005 : depset::depset (tree entity)
2713 : 2264005 : :entity (entity), discriminator (0), cluster (0), section (0)
2714 : : {
2715 : 2264005 : deps.create (0);
2716 : : }
2717 : :
2718 : : inline
2719 : 2264005 : depset::~depset ()
2720 : : {
2721 : 2264005 : deps.release ();
2722 : 2264005 : }
2723 : :
2724 : : const char *
2725 : 43487 : depset::entity_kind_name () const
2726 : : {
2727 : : /* Same order as entity_kind. */
2728 : 43487 : static const char *const names[] =
2729 : : {"decl", "specialization", "partial", "using",
2730 : : "namespace", "tu-local", "redirect", "binding"};
2731 : 43487 : static_assert (ARRAY_SIZE (names) == EK_EXPLICIT_HWM + 1,
2732 : : "names must have an entry for every explicit entity_kind");
2733 : 43487 : entity_kind kind = get_entity_kind ();
2734 : 43487 : gcc_checking_assert (kind < ARRAY_SIZE (names));
2735 : 43487 : return names[kind];
2736 : : }
2737 : :
2738 : : /* Create a depset for a namespace binding NS::NAME. */
2739 : :
2740 : 131112 : depset *depset::make_binding (tree ns, tree name)
2741 : : {
2742 : 131112 : depset *binding = new depset (ns);
2743 : :
2744 : 131112 : binding->discriminator = reinterpret_cast <uintptr_t> (name);
2745 : :
2746 : 131112 : return binding;
2747 : : }
2748 : :
2749 : 2132893 : depset *depset::make_entity (tree entity, entity_kind ek, bool is_defn)
2750 : : {
2751 : 2132893 : depset *r = new depset (entity);
2752 : :
2753 : 2132893 : r->discriminator = ((1 << DB_ZERO_BIT)
2754 : 2132893 : | (ek << DB_KIND_BIT)
2755 : 2132893 : | is_defn << DB_DEFN_BIT);
2756 : :
2757 : 2132893 : return r;
2758 : : }
2759 : :
2760 : : class pending_key
2761 : : {
2762 : : public:
2763 : : tree ns;
2764 : : tree id;
2765 : : };
2766 : :
2767 : : template<>
2768 : : struct default_hash_traits<pending_key>
2769 : : {
2770 : : using value_type = pending_key;
2771 : :
2772 : : static const bool empty_zero_p = false;
2773 : 39093950 : static hashval_t hash (const value_type &k)
2774 : : {
2775 : 39093950 : hashval_t h = IDENTIFIER_HASH_VALUE (k.id);
2776 : 39093950 : h = iterative_hash_hashval_t (DECL_UID (k.ns), h);
2777 : :
2778 : 39093950 : return h;
2779 : : }
2780 : 14873040 : static bool equal (const value_type &k, const value_type &l)
2781 : : {
2782 : 14873040 : return k.ns == l.ns && k.id == l.id;
2783 : : }
2784 : 201140 : static void mark_empty (value_type &k)
2785 : : {
2786 : 201140 : k.ns = k.id = NULL_TREE;
2787 : : }
2788 : 5686 : static void mark_deleted (value_type &k)
2789 : : {
2790 : 5686 : k.ns = NULL_TREE;
2791 : 5686 : gcc_checking_assert (k.id);
2792 : 5686 : }
2793 : 278464420 : static bool is_empty (const value_type &k)
2794 : : {
2795 : 278418061 : return k.ns == NULL_TREE && k.id == NULL_TREE;
2796 : : }
2797 : 20489666 : static bool is_deleted (const value_type &k)
2798 : : {
2799 : 20489666 : return k.ns == NULL_TREE && k.id != NULL_TREE;
2800 : : }
2801 : : static void remove (value_type &)
2802 : : {
2803 : : }
2804 : : };
2805 : :
2806 : : typedef hash_map<pending_key, auto_vec<unsigned>> pending_map_t;
2807 : :
2808 : : /* Not-loaded entities that are keyed to a namespace-scope
2809 : : identifier. See module_state::write_pendings for details. */
2810 : : pending_map_t *pending_table;
2811 : :
2812 : : /* Decls that need some post processing once a batch of lazy loads has
2813 : : completed. */
2814 : : vec<tree, va_heap, vl_embed> *post_load_decls;
2815 : :
2816 : : /* Some entities are keyed to another entitity for ODR purposes.
2817 : : For example, at namespace scope, 'inline auto var = []{};', that
2818 : : lambda is keyed to 'var', and follows its ODRness. */
2819 : : typedef hash_map<tree, auto_vec<tree>> keyed_map_t;
2820 : : static keyed_map_t *keyed_table;
2821 : :
2822 : : static tree get_keyed_decl_scope (tree);
2823 : :
2824 : : /* Instantiations of temploid friends imported from another module
2825 : : need to be attached to the same module as the temploid. This maps
2826 : : these decls to the temploid they are instantiated from, as there is
2827 : : no other easy way to get this information. */
2828 : : static GTY((cache)) decl_tree_cache_map *imported_temploid_friends;
2829 : :
2830 : : /********************************************************************/
2831 : : /* Tree streaming. The tree streaming is very specific to the tree
2832 : : structures themselves. A tag indicates the kind of tree being
2833 : : streamed. -ve tags indicate backreferences to already-streamed
2834 : : trees. Backreferences are auto-numbered. */
2835 : :
2836 : : /* Tree tags. */
2837 : : enum tree_tag {
2838 : : tt_null, /* NULL_TREE. */
2839 : : tt_tu_local, /* A TU-local entity. */
2840 : : tt_fixed, /* Fixed vector index. */
2841 : :
2842 : : tt_node, /* By-value node. */
2843 : : tt_decl, /* By-value mergeable decl. */
2844 : : tt_tpl_parm, /* Template parm. */
2845 : :
2846 : : /* The ordering of the following 5 is relied upon in
2847 : : trees_out::tree_node. */
2848 : : tt_id, /* Identifier node. */
2849 : : tt_conv_id, /* Conversion operator name. */
2850 : : tt_anon_id, /* Anonymous name. */
2851 : : tt_lambda_id, /* Lambda name. */
2852 : : tt_internal_id, /* Internal name. */
2853 : :
2854 : : tt_typedef_type, /* A (possibly implicit) typedefed type. */
2855 : : tt_derived_type, /* A type derived from another type. */
2856 : : tt_variant_type, /* A variant of another type. */
2857 : :
2858 : : tt_tinfo_var, /* Typeinfo object. */
2859 : : tt_tinfo_typedef, /* Typeinfo typedef. */
2860 : : tt_ptrmem_type, /* Pointer to member type. */
2861 : : tt_nttp_var, /* NTTP_OBJECT VAR_DECL. */
2862 : :
2863 : : tt_parm, /* Function parameter or result. */
2864 : : tt_enum_value, /* An enum value. */
2865 : : tt_enum_decl, /* An enum decl. */
2866 : : tt_data_member, /* Data member/using-decl. */
2867 : :
2868 : : tt_binfo, /* A BINFO. */
2869 : : tt_vtable, /* A vtable. */
2870 : : tt_thunk, /* A thunk. */
2871 : : tt_clone_ref,
2872 : :
2873 : : tt_entity, /* A extra-cluster entity. */
2874 : :
2875 : : tt_template, /* The TEMPLATE_RESULT of a template. */
2876 : : };
2877 : :
2878 : : enum walk_kind {
2879 : : WK_none, /* No walk to do (a back- or fixed-ref happened). */
2880 : : WK_normal, /* Normal walk (by-name if possible). */
2881 : :
2882 : : WK_value, /* By-value walk. */
2883 : : };
2884 : :
2885 : : enum merge_kind
2886 : : {
2887 : : MK_unique, /* Known unique. */
2888 : : MK_named, /* Found by CTX, NAME + maybe_arg types etc. */
2889 : : MK_field, /* Found by CTX and index on TYPE_FIELDS */
2890 : : MK_vtable, /* Found by CTX and index on TYPE_VTABLES */
2891 : : MK_as_base, /* Found by CTX. */
2892 : :
2893 : : MK_partial,
2894 : :
2895 : : MK_enum, /* Found by CTX, & 1stMemberNAME. */
2896 : : MK_keyed, /* Found by key & index. */
2897 : : MK_local_type, /* Found by CTX, index. */
2898 : :
2899 : : MK_friend_spec, /* Like named, but has a tmpl & args too. */
2900 : : MK_local_friend, /* Found by CTX, index. */
2901 : :
2902 : : MK_indirect_lwm = MK_enum,
2903 : :
2904 : : /* Template specialization kinds below. These are all found via
2905 : : primary template and specialization args. */
2906 : : MK_template_mask = 0x10, /* A template specialization. */
2907 : :
2908 : : MK_tmpl_decl_mask = 0x4, /* In decl table. */
2909 : :
2910 : : MK_tmpl_tmpl_mask = 0x1, /* We want TEMPLATE_DECL. */
2911 : :
2912 : : MK_type_spec = MK_template_mask,
2913 : : MK_decl_spec = MK_template_mask | MK_tmpl_decl_mask,
2914 : :
2915 : : MK_hwm = 0x20
2916 : : };
2917 : : /* This is more than a debugging array. NULLs are used to determine
2918 : : an invalid merge_kind number. */
2919 : : static char const *const merge_kind_name[MK_hwm] =
2920 : : {
2921 : : "unique", "named", "field", "vtable", /* 0...3 */
2922 : : "asbase", "partial", "enum", "attached", /* 4...7 */
2923 : :
2924 : : "local type", "friend spec", "local friend", NULL, /* 8...11 */
2925 : : NULL, NULL, NULL, NULL,
2926 : :
2927 : : "type spec", "type tmpl spec", /* 16,17 type (template). */
2928 : : NULL, NULL,
2929 : :
2930 : : "decl spec", "decl tmpl spec", /* 20,21 decl (template). */
2931 : : NULL, NULL,
2932 : : NULL, NULL, NULL, NULL,
2933 : : NULL, NULL, NULL, NULL,
2934 : : };
2935 : :
2936 : : /* Mergeable entity location data. */
2937 : : struct merge_key {
2938 : : cp_ref_qualifier ref_q : 2;
2939 : : unsigned index;
2940 : :
2941 : : tree ret; /* Return type, if appropriate. */
2942 : : tree args; /* Arg types, if appropriate. */
2943 : :
2944 : : tree constraints; /* Constraints. */
2945 : :
2946 : 2351103 : merge_key ()
2947 : 2351103 : :ref_q (REF_QUAL_NONE), index (0),
2948 : 2351103 : ret (NULL_TREE), args (NULL_TREE),
2949 : 2351103 : constraints (NULL_TREE)
2950 : : {
2951 : : }
2952 : : };
2953 : :
2954 : : /* Hashmap of merged duplicates. Usually decls, but can contain
2955 : : BINFOs. */
2956 : : typedef hash_map<tree,uintptr_t,
2957 : : simple_hashmap_traits<nodel_ptr_hash<tree_node>,uintptr_t> >
2958 : : duplicate_hash_map;
2959 : :
2960 : : /* Data needed for post-processing. */
2961 : : struct post_process_data {
2962 : : tree decl;
2963 : : location_t start_locus;
2964 : : location_t end_locus;
2965 : : bool returns_value;
2966 : : bool returns_null;
2967 : : bool returns_abnormally;
2968 : : bool infinite_loop;
2969 : : };
2970 : :
2971 : : /* Tree stream reader. Note that reading a stream doesn't mark the
2972 : : read trees with TREE_VISITED. Thus it's quite safe to have
2973 : : multiple concurrent readers. Which is good, because lazy
2974 : : loading.
2975 : :
2976 : : It's important that trees_in/out have internal linkage so that the
2977 : : compiler knows core_bools, lang_type_bools and lang_decl_bools have
2978 : : only a single caller (tree_node_bools) and inlines them appropriately. */
2979 : : namespace {
2980 : : class trees_in : public bytes_in {
2981 : : typedef bytes_in parent;
2982 : :
2983 : : private:
2984 : : module_state *state; /* Module being imported. */
2985 : : vec<tree> back_refs; /* Back references. */
2986 : : duplicate_hash_map *duplicates; /* Map from existings to duplicate. */
2987 : : vec<post_process_data> post_decls; /* Decls to post process. */
2988 : : unsigned unused; /* Inhibit any interior TREE_USED
2989 : : marking. */
2990 : :
2991 : : public:
2992 : : trees_in (module_state *);
2993 : : ~trees_in ();
2994 : :
2995 : : public:
2996 : : int insert (tree);
2997 : : tree back_ref (int);
2998 : :
2999 : : private:
3000 : : tree start (unsigned = 0);
3001 : :
3002 : : public:
3003 : : /* Needed for binfo writing */
3004 : : bool core_bools (tree, bits_in&);
3005 : :
3006 : : private:
3007 : : /* Stream tree_core, lang_decl_specific and lang_type_specific
3008 : : bits. */
3009 : : bool core_vals (tree);
3010 : : bool lang_type_bools (tree, bits_in&);
3011 : : bool lang_type_vals (tree);
3012 : : bool lang_decl_bools (tree, bits_in&);
3013 : : bool lang_decl_vals (tree);
3014 : : bool lang_vals (tree);
3015 : : bool tree_node_bools (tree);
3016 : : bool tree_node_vals (tree);
3017 : : tree tree_value ();
3018 : : tree decl_value ();
3019 : : tree tpl_parm_value ();
3020 : :
3021 : : private:
3022 : : tree chained_decls (); /* Follow DECL_CHAIN. */
3023 : : vec<tree, va_heap> *vec_chained_decls ();
3024 : : vec<tree, va_gc> *tree_vec (); /* vec of tree. */
3025 : : vec<tree_pair_s, va_gc> *tree_pair_vec (); /* vec of tree_pair. */
3026 : : tree tree_list (bool has_purpose);
3027 : :
3028 : : public:
3029 : : /* Read a tree node. */
3030 : : tree tree_node (bool is_use = false);
3031 : :
3032 : : private:
3033 : : bool install_entity (tree decl);
3034 : : tree tpl_parms (unsigned &tpl_levels);
3035 : : bool tpl_parms_fini (tree decl, unsigned tpl_levels);
3036 : : bool tpl_header (tree decl, unsigned *tpl_levels);
3037 : : int fn_parms_init (tree);
3038 : : void fn_parms_fini (int tag, tree fn, tree existing, bool has_defn);
3039 : : unsigned add_indirect_tpl_parms (tree);
3040 : : public:
3041 : : bool add_indirects (tree);
3042 : :
3043 : : public:
3044 : : /* Serialize various definitions. */
3045 : : bool read_definition (tree decl);
3046 : :
3047 : : private:
3048 : : void check_abi_tags (tree existing, tree decl, tree &eattr, tree &dattr);
3049 : : bool is_matching_decl (tree existing, tree decl, bool is_typedef);
3050 : : static bool install_implicit_member (tree decl);
3051 : : bool read_function_def (tree decl, tree maybe_template);
3052 : : bool read_var_def (tree decl, tree maybe_template);
3053 : : bool read_class_def (tree decl, tree maybe_template);
3054 : : bool read_enum_def (tree decl, tree maybe_template);
3055 : :
3056 : : public:
3057 : : tree decl_container ();
3058 : : tree key_mergeable (int tag, merge_kind, tree decl, tree inner, tree type,
3059 : : tree container, bool is_attached,
3060 : : bool is_imported_temploid_friend);
3061 : : unsigned binfo_mergeable (tree *);
3062 : :
3063 : : private:
3064 : : tree key_local_type (const merge_key&, tree, tree);
3065 : : uintptr_t *find_duplicate (tree existing);
3066 : : void register_duplicate (tree decl, tree existing);
3067 : : /* Mark as an already diagnosed bad duplicate. */
3068 : 54 : void unmatched_duplicate (tree existing)
3069 : : {
3070 : 108 : *find_duplicate (existing) |= 1;
3071 : 54 : }
3072 : :
3073 : : public:
3074 : 130188 : bool is_duplicate (tree decl)
3075 : : {
3076 : 260376 : return find_duplicate (decl) != NULL;
3077 : : }
3078 : 143334 : tree maybe_duplicate (tree decl)
3079 : : {
3080 : 143334 : if (uintptr_t *dup = find_duplicate (decl))
3081 : 8751 : return reinterpret_cast<tree> (*dup & ~uintptr_t (1));
3082 : : return decl;
3083 : : }
3084 : : tree odr_duplicate (tree decl, bool has_defn);
3085 : :
3086 : : public:
3087 : : /* Return the decls to postprocess. */
3088 : : const vec<post_process_data>& post_process ()
3089 : : {
3090 : : return post_decls;
3091 : : }
3092 : : private:
3093 : : /* Register DATA for postprocessing. */
3094 : 114580 : void post_process (post_process_data data)
3095 : : {
3096 : 114580 : post_decls.safe_push (data);
3097 : : }
3098 : :
3099 : : private:
3100 : : void assert_definition (tree, bool installing);
3101 : : };
3102 : : } // anon namespace
3103 : :
3104 : 193980 : trees_in::trees_in (module_state *state)
3105 : 193980 : :parent (), state (state), unused (0)
3106 : : {
3107 : 193980 : duplicates = NULL;
3108 : 193980 : back_refs.create (500);
3109 : 193980 : post_decls.create (0);
3110 : 193980 : }
3111 : :
3112 : 193980 : trees_in::~trees_in ()
3113 : : {
3114 : 291721 : delete (duplicates);
3115 : 193980 : back_refs.release ();
3116 : 193980 : post_decls.release ();
3117 : 193980 : }
3118 : :
3119 : : /* Tree stream writer. */
3120 : : namespace {
3121 : : class trees_out : public bytes_out {
3122 : : typedef bytes_out parent;
3123 : :
3124 : : private:
3125 : : module_state *state; /* The module we are writing. */
3126 : : ptr_int_hash_map tree_map; /* Trees to references */
3127 : : depset::hash *dep_hash; /* Dependency table. */
3128 : : int ref_num; /* Back reference number. */
3129 : : unsigned section;
3130 : : bool writing_local_entities; /* Whether we might walk into a TU-local
3131 : : entity we need to emit placeholders for. */
3132 : : bool walking_bit_field_unit; /* Whether we're walking the underlying
3133 : : storage for a bit field. There's no other
3134 : : great way to detect this. */
3135 : : #if CHECKING_P
3136 : : int importedness; /* Checker that imports not occurring
3137 : : inappropriately. +ve imports ok,
3138 : : -ve imports not ok. */
3139 : : #endif
3140 : :
3141 : : public:
3142 : : trees_out (allocator *, module_state *, depset::hash &deps, unsigned sec = 0);
3143 : : ~trees_out ();
3144 : :
3145 : : private:
3146 : : void mark_trees ();
3147 : : void unmark_trees ();
3148 : :
3149 : : public:
3150 : : /* Hey, let's ignore the well known STL iterator idiom. */
3151 : : void begin ();
3152 : : unsigned end (elf_out *sink, unsigned name, unsigned *crc_ptr);
3153 : : void end ();
3154 : :
3155 : : public:
3156 : : enum tags
3157 : : {
3158 : : tag_backref = -1, /* Upper bound on the backrefs. */
3159 : : tag_value = 0, /* Write by value. */
3160 : : tag_fixed /* Lower bound on the fixed trees. */
3161 : : };
3162 : :
3163 : : public:
3164 : : /* The walk is used for three similar purposes:
3165 : :
3166 : : 1. The initial scan for dependencies.
3167 : : 2. Once dependencies have been found, ordering them.
3168 : : 3. Writing dependencies to file (streaming_p).
3169 : :
3170 : : For cases where it matters, these accessers can be used to determine
3171 : : which state we're in. */
3172 : 52658857 : bool is_initial_scan () const
3173 : : {
3174 : 88968696 : return !streaming_p () && !is_key_order ();
3175 : : }
3176 : 49273040 : bool is_key_order () const
3177 : : {
3178 : 36309839 : return dep_hash->is_key_order ();
3179 : : }
3180 : :
3181 : : public:
3182 : : int insert (tree, walk_kind = WK_normal);
3183 : :
3184 : : private:
3185 : : void start (tree, bool = false);
3186 : :
3187 : : private:
3188 : : walk_kind ref_node (tree);
3189 : : public:
3190 : : int get_tag (tree);
3191 : 481908 : void set_importing (int i ATTRIBUTE_UNUSED)
3192 : : {
3193 : : #if CHECKING_P
3194 : 481908 : importedness = i;
3195 : : #endif
3196 : : }
3197 : :
3198 : : private:
3199 : : void core_bools (tree, bits_out&);
3200 : : void core_vals (tree);
3201 : : void lang_type_bools (tree, bits_out&);
3202 : : void lang_type_vals (tree);
3203 : : void lang_decl_bools (tree, bits_out&);
3204 : : void lang_decl_vals (tree);
3205 : : void lang_vals (tree);
3206 : : void tree_node_bools (tree);
3207 : : void tree_node_vals (tree);
3208 : :
3209 : : private:
3210 : : void chained_decls (tree);
3211 : : void vec_chained_decls (tree);
3212 : : void tree_vec (vec<tree, va_gc> *);
3213 : : void tree_pair_vec (vec<tree_pair_s, va_gc> *);
3214 : : void tree_list (tree, bool has_purpose);
3215 : :
3216 : : private:
3217 : : bool has_tu_local_dep (tree) const;
3218 : : tree find_tu_local_decl (tree);
3219 : :
3220 : : public:
3221 : : /* Mark a node for by-value walking. */
3222 : : void mark_by_value (tree);
3223 : :
3224 : : public:
3225 : : void tree_node (tree);
3226 : :
3227 : : private:
3228 : : void install_entity (tree decl, depset *);
3229 : : void tpl_parms (tree parms, unsigned &tpl_levels);
3230 : : void tpl_parms_fini (tree decl, unsigned tpl_levels);
3231 : : void fn_parms_fini (tree) {}
3232 : : unsigned add_indirect_tpl_parms (tree);
3233 : : public:
3234 : : void add_indirects (tree);
3235 : : void fn_parms_init (tree);
3236 : : void tpl_header (tree decl, unsigned *tpl_levels);
3237 : :
3238 : : public:
3239 : : merge_kind get_merge_kind (tree decl, depset *maybe_dep);
3240 : : tree decl_container (tree decl);
3241 : : void key_mergeable (int tag, merge_kind, tree decl, tree inner,
3242 : : tree container, depset *maybe_dep);
3243 : : void binfo_mergeable (tree binfo);
3244 : :
3245 : : private:
3246 : : void key_local_type (merge_key&, tree, tree);
3247 : : bool decl_node (tree, walk_kind ref);
3248 : : void type_node (tree);
3249 : : void tree_value (tree);
3250 : : void tpl_parm_value (tree);
3251 : :
3252 : : public:
3253 : : void decl_value (tree, depset *);
3254 : :
3255 : : public:
3256 : : /* Serialize various definitions. */
3257 : : void write_definition (tree decl, bool refs_tu_local = false);
3258 : : void mark_declaration (tree decl, bool do_defn);
3259 : :
3260 : : private:
3261 : : void mark_function_def (tree decl);
3262 : : void mark_var_def (tree decl);
3263 : : void mark_class_def (tree decl);
3264 : : void mark_enum_def (tree decl);
3265 : : void mark_class_member (tree decl, bool do_defn = true);
3266 : : void mark_binfos (tree type);
3267 : :
3268 : : private:
3269 : : void write_var_def (tree decl);
3270 : : void write_function_def (tree decl);
3271 : : void write_class_def (tree decl);
3272 : : void write_enum_def (tree decl);
3273 : :
3274 : : private:
3275 : : static void assert_definition (tree);
3276 : :
3277 : : public:
3278 : : static void instrument ();
3279 : :
3280 : : private:
3281 : : /* Tree instrumentation. */
3282 : : static unsigned tree_val_count;
3283 : : static unsigned decl_val_count;
3284 : : static unsigned back_ref_count;
3285 : : static unsigned tu_local_count;
3286 : : static unsigned null_count;
3287 : : };
3288 : : } // anon namespace
3289 : :
3290 : : /* Instrumentation counters. */
3291 : : unsigned trees_out::tree_val_count;
3292 : : unsigned trees_out::decl_val_count;
3293 : : unsigned trees_out::back_ref_count;
3294 : : unsigned trees_out::tu_local_count;
3295 : : unsigned trees_out::null_count;
3296 : :
3297 : 487142 : trees_out::trees_out (allocator *mem, module_state *state, depset::hash &deps,
3298 : 487142 : unsigned section)
3299 : 974284 : :parent (mem), state (state), tree_map (500),
3300 : 487142 : dep_hash (&deps), ref_num (0), section (section),
3301 : 487142 : writing_local_entities (false), walking_bit_field_unit (false)
3302 : : {
3303 : : #if CHECKING_P
3304 : 487142 : importedness = 0;
3305 : : #endif
3306 : 487142 : }
3307 : :
3308 : 487142 : trees_out::~trees_out ()
3309 : : {
3310 : 487142 : }
3311 : :
3312 : : /********************************************************************/
3313 : : /* Location. We're aware of the line-map concept and reproduce it
3314 : : here. Each imported module allocates a contiguous span of ordinary
3315 : : maps, and of macro maps. adhoc maps are serialized by contents,
3316 : : not pre-allocated. The scattered linemaps of a module are
3317 : : coalesced when writing. */
3318 : :
3319 : :
3320 : : /* I use half-open [first,second) ranges. */
3321 : : typedef std::pair<line_map_uint_t,line_map_uint_t> range_t;
3322 : :
3323 : : /* A range of locations. */
3324 : : typedef std::pair<location_t,location_t> loc_range_t;
3325 : :
3326 : : /* Spans of the line maps that are occupied by this TU. I.e. not
3327 : : within imports. Only extended when in an interface unit.
3328 : : Interval zero corresponds to the forced header linemap(s). This
3329 : : is a singleton object. */
3330 : :
3331 : : class loc_spans {
3332 : : public:
3333 : : /* An interval of line maps. The line maps here represent a contiguous
3334 : : non-imported range. */
3335 : 5683 : struct span {
3336 : : loc_range_t ordinary; /* Ordinary map location range. */
3337 : : loc_range_t macro; /* Macro map location range. */
3338 : : /* Add to locs to get serialized loc. */
3339 : : location_diff_t ordinary_delta;
3340 : : location_diff_t macro_delta;
3341 : : };
3342 : :
3343 : : private:
3344 : : vec<span> *spans;
3345 : : bool locs_exhausted_p;
3346 : :
3347 : : public:
3348 : : loc_spans ()
3349 : : /* Do not preallocate spans, as that causes
3350 : : --enable-detailed-mem-stats problems. */
3351 : : : spans (nullptr), locs_exhausted_p (false)
3352 : : {
3353 : : }
3354 : 96924 : ~loc_spans ()
3355 : : {
3356 : 96924 : delete spans;
3357 : 96924 : }
3358 : :
3359 : : public:
3360 : 276 : span &operator[] (unsigned ix)
3361 : : {
3362 : 552 : return (*spans)[ix];
3363 : : }
3364 : : unsigned length () const
3365 : : {
3366 : : return spans->length ();
3367 : : }
3368 : :
3369 : : public:
3370 : 14844 : bool init_p () const
3371 : : {
3372 : 14844 : return spans != nullptr;
3373 : : }
3374 : : /* Initializer. */
3375 : : void init (const line_maps *lmaps, const line_map_ordinary *map);
3376 : :
3377 : : /* Slightly skewed preprocessed files can cause us to miss an
3378 : : initialization in some places. Fallback initializer. */
3379 : 5523 : void maybe_init ()
3380 : : {
3381 : 5523 : if (!init_p ())
3382 : 6 : init (line_table, nullptr);
3383 : 5523 : }
3384 : :
3385 : : public:
3386 : : enum {
3387 : : SPAN_RESERVED = 0, /* Reserved (fixed) locations. */
3388 : : SPAN_FIRST = 1, /* LWM of locations to stream */
3389 : : SPAN_MAIN = 2 /* Main file and onwards. */
3390 : : };
3391 : :
3392 : : public:
3393 : 447613 : location_t main_start () const
3394 : : {
3395 : 447613 : return (*spans)[SPAN_MAIN].ordinary.first;
3396 : : }
3397 : :
3398 : : public:
3399 : : void open (location_t);
3400 : : void close ();
3401 : :
3402 : : public:
3403 : : /* Propagate imported linemaps to us, if needed. */
3404 : : bool maybe_propagate (module_state *import, location_t loc);
3405 : :
3406 : : public:
3407 : : /* Whether we can no longer represent new imported locations. */
3408 : 0 : bool locations_exhausted_p () const
3409 : : {
3410 : 0 : return locs_exhausted_p;
3411 : : }
3412 : 0 : void report_location_exhaustion (location_t loc)
3413 : : {
3414 : 0 : if (!locs_exhausted_p)
3415 : : {
3416 : : /* Just give the notice once. */
3417 : 0 : locs_exhausted_p = true;
3418 : 0 : inform (loc, "unable to represent further imported source locations");
3419 : : }
3420 : : }
3421 : :
3422 : : public:
3423 : : const span *ordinary (location_t);
3424 : : const span *macro (location_t);
3425 : : };
3426 : :
3427 : : static loc_spans spans;
3428 : :
3429 : : /* Information about ordinary locations we stream out. */
3430 : : struct ord_loc_info
3431 : : {
3432 : : const line_map_ordinary *src; // line map we're based on
3433 : : line_map_uint_t offset; // offset to this line
3434 : : line_map_uint_t span; // number of locs we span
3435 : : line_map_uint_t remap; // serialization
3436 : :
3437 : 216520591 : static int compare (const void *a_, const void *b_)
3438 : : {
3439 : 216520591 : auto *a = static_cast<const ord_loc_info *> (a_);
3440 : 216520591 : auto *b = static_cast<const ord_loc_info *> (b_);
3441 : :
3442 : 216520591 : if (a->src != b->src)
3443 : 48180252 : return a->src < b->src ? -1 : +1;
3444 : :
3445 : : // Ensure no overlap
3446 : 183954080 : gcc_checking_assert (a->offset + a->span <= b->offset
3447 : : || b->offset + b->span <= a->offset);
3448 : :
3449 : 183954080 : gcc_checking_assert (a->offset != b->offset);
3450 : 183954080 : return a->offset < b->offset ? -1 : +1;
3451 : : }
3452 : : };
3453 : : struct ord_loc_traits
3454 : : {
3455 : : typedef ord_loc_info value_type;
3456 : : typedef value_type compare_type;
3457 : :
3458 : : static const bool empty_zero_p = false;
3459 : :
3460 : 90212501 : static hashval_t hash (const value_type &v)
3461 : : {
3462 : 76575226 : auto h = pointer_hash<const line_map_ordinary>::hash (v.src);
3463 : 90212501 : return iterative_hash_hashval_t (v.offset, h);
3464 : : }
3465 : 99377170 : static bool equal (const value_type &v, const compare_type p)
3466 : : {
3467 : 99377170 : return v.src == p.src && v.offset == p.offset;
3468 : : }
3469 : :
3470 : 6574359 : static void mark_empty (value_type &v)
3471 : : {
3472 : 6574359 : v.src = nullptr;
3473 : : }
3474 : 290067072 : static bool is_empty (value_type &v)
3475 : : {
3476 : 290067072 : return !v.src;
3477 : : }
3478 : :
3479 : : static bool is_deleted (value_type &) { return false; }
3480 : : static void mark_deleted (value_type &) { gcc_unreachable (); }
3481 : :
3482 : 1637281 : static void remove (value_type &) {}
3483 : : };
3484 : : /* Table keyed by ord_loc_info, used for noting. */
3485 : : static hash_table<ord_loc_traits> *ord_loc_table;
3486 : : /* Sorted vector, used for writing. */
3487 : : static vec<ord_loc_info> *ord_loc_remap;
3488 : :
3489 : : /* Information about macro locations we stream out. */
3490 : : struct macro_loc_info
3491 : : {
3492 : : const line_map_macro *src; // original expansion
3493 : : line_map_uint_t remap; // serialization
3494 : :
3495 : 7862457 : static int compare (const void *a_, const void *b_)
3496 : : {
3497 : 7862457 : auto *a = static_cast<const macro_loc_info *> (a_);
3498 : 7862457 : auto *b = static_cast<const macro_loc_info *> (b_);
3499 : :
3500 : 7862457 : gcc_checking_assert (MAP_START_LOCATION (a->src)
3501 : : != MAP_START_LOCATION (b->src));
3502 : 7862457 : if (MAP_START_LOCATION (a->src) < MAP_START_LOCATION (b->src))
3503 : : return -1;
3504 : : else
3505 : 3780856 : return +1;
3506 : : }
3507 : : };
3508 : : struct macro_loc_traits
3509 : : {
3510 : : typedef macro_loc_info value_type;
3511 : : typedef const line_map_macro *compare_type;
3512 : :
3513 : : static const bool empty_zero_p = false;
3514 : :
3515 : 7228374 : static hashval_t hash (compare_type p)
3516 : : {
3517 : 7228374 : return pointer_hash<const line_map_macro>::hash (p);
3518 : : }
3519 : 6050205 : static hashval_t hash (const value_type &v)
3520 : : {
3521 : 6050205 : return hash (v.src);
3522 : : }
3523 : : static bool equal (const value_type &v, const compare_type p)
3524 : : {
3525 : : return v.src == p;
3526 : : }
3527 : :
3528 : 613250 : static void mark_empty (value_type &v)
3529 : : {
3530 : 613250 : v.src = nullptr;
3531 : : }
3532 : 24000116 : static bool is_empty (value_type &v)
3533 : : {
3534 : 24000116 : return !v.src;
3535 : : }
3536 : :
3537 : : static bool is_deleted (value_type &) { return false; }
3538 : : static void mark_deleted (value_type &) { gcc_unreachable (); }
3539 : :
3540 : 134799 : static void remove (value_type &) {}
3541 : : };
3542 : : /* Table keyed by line_map_macro, used for noting. */
3543 : : static hash_table<macro_loc_traits> *macro_loc_table;
3544 : : /* Sorted vector, used for writing. */
3545 : : static vec<macro_loc_info> *macro_loc_remap;
3546 : :
3547 : : /* Indirection to allow bsearching imports by ordinary location. */
3548 : : static vec<module_state *> *ool;
3549 : :
3550 : : /********************************************************************/
3551 : : /* Data needed by a module during the process of loading. */
3552 : : struct GTY(()) slurping {
3553 : :
3554 : : /* Remap import's module numbering to our numbering. Values are
3555 : : shifted by 1. Bit0 encodes if the import is direct. */
3556 : : vec<unsigned, va_heap, vl_embed> *
3557 : : GTY((skip)) remap; /* Module owner remapping. */
3558 : :
3559 : : elf_in *GTY((skip)) from; /* The elf loader. */
3560 : :
3561 : : /* This map is only for header imports themselves -- the global
3562 : : headers bitmap hold it for the current TU. */
3563 : : bitmap headers; /* Transitive set of direct imports, including
3564 : : self. Used for macro visibility and
3565 : : priority. */
3566 : :
3567 : : /* These objects point into the mmapped area, unless we're not doing
3568 : : that, or we got frozen or closed. In those cases they point to
3569 : : buffers we own. */
3570 : : bytes_in macro_defs; /* Macro definitions. */
3571 : : bytes_in macro_tbl; /* Macro table. */
3572 : :
3573 : : /* Location remapping. first->ordinary, second->macro. */
3574 : : range_t GTY((skip)) loc_deltas;
3575 : :
3576 : : unsigned current; /* Section currently being loaded. */
3577 : : unsigned remaining; /* Number of lazy sections yet to read. */
3578 : : unsigned lru; /* An LRU counter. */
3579 : :
3580 : : public:
3581 : : slurping (elf_in *);
3582 : : ~slurping ();
3583 : :
3584 : : public:
3585 : : /* Close the ELF file, if it's open. */
3586 : 5045 : void close ()
3587 : : {
3588 : 5045 : if (from)
3589 : : {
3590 : 2766 : from->end ();
3591 : 5532 : delete from;
3592 : 2766 : from = NULL;
3593 : : }
3594 : 5045 : }
3595 : :
3596 : : public:
3597 : : void release_macros ();
3598 : :
3599 : : public:
3600 : 2824 : void alloc_remap (unsigned size)
3601 : : {
3602 : 2824 : gcc_assert (!remap);
3603 : 2824 : vec_safe_reserve (remap, size);
3604 : 6006 : for (unsigned ix = size; ix--;)
3605 : 3182 : remap->quick_push (0);
3606 : 2824 : }
3607 : 1056517 : unsigned remap_module (unsigned owner)
3608 : : {
3609 : 1056517 : if (owner < remap->length ())
3610 : 1056517 : return (*remap)[owner] >> 1;
3611 : : return 0;
3612 : : }
3613 : :
3614 : : public:
3615 : : /* GC allocation. But we must explicitly delete it. */
3616 : 2845 : static void *operator new (size_t x)
3617 : : {
3618 : 5690 : return ggc_alloc_atomic (x);
3619 : : }
3620 : 2766 : static void operator delete (void *p)
3621 : : {
3622 : 2766 : ggc_free (p);
3623 : 2766 : }
3624 : : };
3625 : :
3626 : 2845 : slurping::slurping (elf_in *from)
3627 : 2845 : : remap (NULL), from (from),
3628 : 2845 : headers (BITMAP_GGC_ALLOC ()), macro_defs (), macro_tbl (),
3629 : 2845 : loc_deltas (0, 0),
3630 : 2845 : current (~0u), remaining (0), lru (0)
3631 : : {
3632 : 2845 : }
3633 : :
3634 : 2766 : slurping::~slurping ()
3635 : : {
3636 : 2766 : vec_free (remap);
3637 : 2766 : remap = NULL;
3638 : 2766 : release_macros ();
3639 : 2766 : close ();
3640 : 2766 : }
3641 : :
3642 : 5045 : void slurping::release_macros ()
3643 : : {
3644 : 5045 : if (macro_defs.size)
3645 : 870 : elf_in::release (from, macro_defs);
3646 : 5045 : if (macro_tbl.size)
3647 : 0 : elf_in::release (from, macro_tbl);
3648 : 5045 : }
3649 : :
3650 : : /* Flags for extensions that end up being streamed. */
3651 : :
3652 : : enum streamed_extensions {
3653 : : SE_OPENMP_SIMD = 1 << 0,
3654 : : SE_OPENMP = 1 << 1,
3655 : : SE_OPENACC = 1 << 2,
3656 : : SE_BITS = 3
3657 : : };
3658 : :
3659 : : /* Counter indices. */
3660 : : enum module_state_counts
3661 : : {
3662 : : MSC_sec_lwm,
3663 : : MSC_sec_hwm,
3664 : : MSC_pendings,
3665 : : MSC_entities,
3666 : : MSC_namespaces,
3667 : : MSC_using_directives,
3668 : : MSC_bindings,
3669 : : MSC_macros,
3670 : : MSC_inits,
3671 : : MSC_HWM
3672 : : };
3673 : :
3674 : : /********************************************************************/
3675 : : struct module_state_config;
3676 : :
3677 : : /* Increasing levels of loadedness. */
3678 : : enum module_loadedness {
3679 : : ML_NONE, /* Not loaded. */
3680 : : ML_CONFIG, /* Config loaed. */
3681 : : ML_PREPROCESSOR, /* Preprocessor loaded. */
3682 : : ML_LANGUAGE, /* Language loaded. */
3683 : : };
3684 : :
3685 : : /* Increasing levels of directness (toplevel) of import. */
3686 : : enum module_directness {
3687 : : MD_NONE, /* Not direct. */
3688 : : MD_PARTITION_DIRECT, /* Direct import of a partition. */
3689 : : MD_DIRECT, /* Direct import. */
3690 : : MD_PURVIEW_DIRECT, /* direct import in purview. */
3691 : : };
3692 : :
3693 : : /* State of a particular module. */
3694 : :
3695 : : class GTY((chain_next ("%h.parent"), for_user)) module_state {
3696 : : public:
3697 : : /* We always import & export ourselves. */
3698 : : bitmap imports; /* Transitive modules we're importing. */
3699 : : bitmap exports; /* Subset of that, that we're exporting. */
3700 : :
3701 : : /* For a named module interface A.B, parent is A and name is B.
3702 : : For a partition M:P, parent is M and name is P.
3703 : : For an implementation unit I, parent is I's interface and name is NULL.
3704 : : Otherwise parent is NULL and name will be the flatname. */
3705 : : module_state *parent;
3706 : : tree name;
3707 : :
3708 : : slurping *slurp; /* Data for loading. */
3709 : :
3710 : : const char *flatname; /* Flatname of module. */
3711 : : char *filename; /* CMI Filename */
3712 : :
3713 : : /* Indices into the entity_ary. */
3714 : : unsigned entity_lwm;
3715 : : unsigned entity_num;
3716 : :
3717 : : /* Location ranges for this module. adhoc-locs are decomposed, so
3718 : : don't have a range. */
3719 : : loc_range_t GTY((skip)) ordinary_locs;
3720 : : loc_range_t GTY((skip)) macro_locs; // [lwm,num)
3721 : :
3722 : : /* LOC is first set too the importing location. When initially
3723 : : loaded it refers to a module loc whose parent is the importing
3724 : : location. */
3725 : : location_t loc; /* Location referring to module itself. */
3726 : : unsigned crc; /* CRC we saw reading it in. */
3727 : :
3728 : : unsigned mod; /* Module owner number. */
3729 : : unsigned remap; /* Remapping during writing. */
3730 : :
3731 : : unsigned short subst; /* Mangle subst if !0. */
3732 : :
3733 : : /* How loaded this module is. */
3734 : : enum module_loadedness loadedness : 2;
3735 : :
3736 : : bool module_p : 1; /* /The/ module of this TU. */
3737 : : bool header_p : 1; /* Is a header unit. */
3738 : : bool interface_p : 1; /* An interface. */
3739 : : bool partition_p : 1; /* A partition. */
3740 : :
3741 : : /* How directly this module is imported. */
3742 : : enum module_directness directness : 2;
3743 : :
3744 : : bool exported_p : 1; /* directness != MD_NONE && exported. */
3745 : : bool cmi_noted_p : 1; /* We've told the user about the CMI, don't
3746 : : do it again */
3747 : : bool active_init_p : 1; /* This module's global initializer needs
3748 : : calling. */
3749 : : bool inform_cmi_p : 1; /* Inform of a read/write. */
3750 : : bool visited_p : 1; /* A walk-once flag. */
3751 : : /* Record extensions emitted or permitted. */
3752 : : unsigned extensions : SE_BITS;
3753 : : /* 14 bits used, 2 bits remain */
3754 : :
3755 : : public:
3756 : : module_state (tree name, module_state *, bool);
3757 : : ~module_state ();
3758 : :
3759 : : public:
3760 : 2820 : void release ()
3761 : : {
3762 : 2820 : imports = exports = NULL;
3763 : 2820 : slurped ();
3764 : 2766 : }
3765 : 5099 : void slurped ()
3766 : : {
3767 : 5099 : delete slurp;
3768 : 5099 : slurp = NULL;
3769 : 5099 : }
3770 : 1193093 : elf_in *from () const
3771 : : {
3772 : 1193093 : return slurp->from;
3773 : : }
3774 : :
3775 : : public:
3776 : : /* Kind of this module. */
3777 : 127798 : bool is_module () const
3778 : : {
3779 : 127798 : return module_p;
3780 : : }
3781 : 2058983 : bool is_header () const
3782 : : {
3783 : 2058983 : return header_p;
3784 : : }
3785 : 561 : bool is_interface () const
3786 : : {
3787 : 561 : return interface_p;
3788 : : }
3789 : 265760 : bool is_partition () const
3790 : : {
3791 : 265760 : return partition_p;
3792 : : }
3793 : :
3794 : : /* How this module is used in the current TU. */
3795 : 3075 : bool is_exported () const
3796 : : {
3797 : 3075 : return exported_p;
3798 : : }
3799 : 19615 : bool is_direct () const
3800 : : {
3801 : 19615 : return directness >= MD_DIRECT;
3802 : : }
3803 : 264 : bool is_purview_direct () const
3804 : : {
3805 : 264 : return directness == MD_PURVIEW_DIRECT;
3806 : : }
3807 : 394 : bool is_partition_direct () const
3808 : : {
3809 : 394 : return directness == MD_PARTITION_DIRECT;
3810 : : }
3811 : :
3812 : : public:
3813 : : /* Is this a real module? */
3814 : 15226 : bool has_location () const
3815 : : {
3816 : 15226 : return loc != UNKNOWN_LOCATION;
3817 : : }
3818 : :
3819 : : public:
3820 : : bool check_circular_import (location_t loc);
3821 : :
3822 : : public:
3823 : : void mangle (bool include_partition);
3824 : :
3825 : : public:
3826 : : void set_import (module_state const *, bool is_export);
3827 : : void announce (const char *) const;
3828 : :
3829 : : public:
3830 : : /* Read and write module. */
3831 : : bool write_begin (elf_out *to, cpp_reader *,
3832 : : module_state_config &, unsigned &crc);
3833 : : void write_end (elf_out *to, cpp_reader *,
3834 : : module_state_config &, unsigned &crc);
3835 : : bool read_initial (cpp_reader *);
3836 : : bool read_preprocessor (bool);
3837 : : bool read_language (bool);
3838 : :
3839 : : public:
3840 : : /* Read a section. */
3841 : : bool load_section (unsigned snum, binding_slot *mslot);
3842 : : /* Lazily read a section. */
3843 : : bool lazy_load (unsigned index, binding_slot *mslot);
3844 : :
3845 : : public:
3846 : : /* Juggle a limited number of file numbers. */
3847 : : static void freeze_an_elf ();
3848 : : bool maybe_defrost ();
3849 : :
3850 : : public:
3851 : : void maybe_completed_reading ();
3852 : : bool check_read (bool outermost, bool ok);
3853 : :
3854 : : private:
3855 : : /* The README, for human consumption. */
3856 : : void write_readme (elf_out *to, cpp_reader *, const char *dialect);
3857 : : void write_env (elf_out *to);
3858 : :
3859 : : private:
3860 : : /* Import tables. */
3861 : : void write_imports (bytes_out &cfg, bool direct);
3862 : : unsigned read_imports (bytes_in &cfg, cpp_reader *, line_maps *maps);
3863 : :
3864 : : private:
3865 : : void write_imports (elf_out *to, unsigned *crc_ptr);
3866 : : bool read_imports (cpp_reader *, line_maps *);
3867 : :
3868 : : private:
3869 : : void write_partitions (elf_out *to, unsigned, unsigned *crc_ptr);
3870 : : bool read_partitions (unsigned);
3871 : :
3872 : : private:
3873 : : void write_config (elf_out *to, struct module_state_config &, unsigned crc);
3874 : : bool read_config (struct module_state_config &, bool = true);
3875 : : static void write_counts (elf_out *to, unsigned [MSC_HWM], unsigned *crc_ptr);
3876 : : bool read_counts (unsigned *);
3877 : :
3878 : : public:
3879 : : void note_cmi_name ();
3880 : :
3881 : : private:
3882 : : static unsigned write_bindings (elf_out *to, vec<depset *> depsets,
3883 : : unsigned *crc_ptr);
3884 : : bool read_bindings (unsigned count, unsigned lwm, unsigned hwm);
3885 : :
3886 : : static void write_namespace (bytes_out &sec, depset *ns_dep);
3887 : : tree read_namespace (bytes_in &sec);
3888 : :
3889 : : void write_namespaces (elf_out *to, vec<depset *> spaces,
3890 : : unsigned, unsigned *crc_ptr);
3891 : : bool read_namespaces (unsigned);
3892 : :
3893 : : unsigned write_using_directives (elf_out *to, depset::hash &,
3894 : : vec<depset *> spaces, unsigned *crc_ptr);
3895 : : bool read_using_directives (unsigned);
3896 : :
3897 : : void intercluster_seed (trees_out &sec, unsigned index, depset *dep);
3898 : : unsigned write_cluster (elf_out *to, depset *depsets[], unsigned size,
3899 : : depset::hash &, unsigned *counts, unsigned *crc_ptr);
3900 : : bool read_cluster (unsigned snum);
3901 : : bool open_slurp (cpp_reader *);
3902 : :
3903 : : private:
3904 : : unsigned write_inits (elf_out *to, depset::hash &, unsigned *crc_ptr);
3905 : : bool read_inits (unsigned count);
3906 : :
3907 : : private:
3908 : : unsigned write_pendings (elf_out *to, vec<depset *> depsets,
3909 : : depset::hash &, unsigned *crc_ptr);
3910 : : bool read_pendings (unsigned count);
3911 : :
3912 : : private:
3913 : : void write_entities (elf_out *to, vec<depset *> depsets,
3914 : : unsigned count, unsigned *crc_ptr);
3915 : : bool read_entities (unsigned count, unsigned lwm, unsigned hwm);
3916 : :
3917 : : private:
3918 : : void write_init_maps ();
3919 : : range_t write_prepare_maps (module_state_config *, bool);
3920 : : bool read_prepare_maps (const module_state_config *);
3921 : :
3922 : : void write_ordinary_maps (elf_out *to, range_t &,
3923 : : bool, unsigned *crc_ptr);
3924 : : bool read_ordinary_maps (line_map_uint_t, unsigned);
3925 : : void write_macro_maps (elf_out *to, range_t &, unsigned *crc_ptr);
3926 : : bool read_macro_maps (line_map_uint_t);
3927 : :
3928 : : void write_diagnostic_classification (elf_out *, diagnostics::context *,
3929 : : unsigned *);
3930 : : bool read_diagnostic_classification (diagnostics::context *);
3931 : :
3932 : : private:
3933 : : void write_define (bytes_out &, const cpp_macro *);
3934 : : cpp_macro *read_define (bytes_in &, cpp_reader *) const;
3935 : : vec<cpp_hashnode *> *prepare_macros (cpp_reader *);
3936 : : unsigned write_macros (elf_out *to, vec<cpp_hashnode *> *, unsigned *crc_ptr);
3937 : : bool read_macros ();
3938 : : void install_macros ();
3939 : :
3940 : : public:
3941 : : void import_macros ();
3942 : :
3943 : : public:
3944 : : static void undef_macro (cpp_reader *, location_t, cpp_hashnode *);
3945 : : static cpp_macro *deferred_macro (cpp_reader *, location_t, cpp_hashnode *);
3946 : :
3947 : : public:
3948 : : static bool note_location (location_t);
3949 : : static void write_location (bytes_out &, location_t);
3950 : : location_t read_location (bytes_in &) const;
3951 : :
3952 : : public:
3953 : : void set_flatname ();
3954 : 44924 : const char *get_flatname () const
3955 : : {
3956 : 44924 : return flatname;
3957 : : }
3958 : : location_t imported_from () const;
3959 : :
3960 : : public:
3961 : : void set_filename (const Cody::Packet &);
3962 : : bool do_import (cpp_reader *, bool outermost);
3963 : : bool check_importable (cpp_reader *);
3964 : : };
3965 : :
3966 : : /* Hash module state by name. This cannot be a member of
3967 : : module_state, because of GTY restrictions. We never delete from
3968 : : the hash table, but ggc_ptr_hash doesn't support that
3969 : : simplification. */
3970 : :
3971 : : struct module_state_hash : ggc_ptr_hash<module_state> {
3972 : : typedef std::pair<tree,uintptr_t> compare_type; /* {name,parent} */
3973 : :
3974 : : static inline hashval_t hash (const value_type m);
3975 : : static inline hashval_t hash (const compare_type &n);
3976 : : static inline bool equal (const value_type existing,
3977 : : const compare_type &candidate);
3978 : : };
3979 : :
3980 : 10926 : module_state::module_state (tree name, module_state *parent, bool partition)
3981 : 10926 : : imports (BITMAP_GGC_ALLOC ()), exports (BITMAP_GGC_ALLOC ()),
3982 : 10926 : parent (parent), name (name), slurp (NULL),
3983 : 10926 : flatname (NULL), filename (NULL),
3984 : 10926 : entity_lwm (~0u >> 1), entity_num (0),
3985 : 10926 : ordinary_locs (0, 0), macro_locs (0, 0),
3986 : 10926 : loc (UNKNOWN_LOCATION),
3987 : 10926 : crc (0), mod (MODULE_UNKNOWN), remap (0), subst (0)
3988 : : {
3989 : 10926 : loadedness = ML_NONE;
3990 : :
3991 : 10926 : module_p = header_p = interface_p = partition_p = false;
3992 : :
3993 : 10926 : directness = MD_NONE;
3994 : 10926 : exported_p = false;
3995 : :
3996 : 10926 : cmi_noted_p = false;
3997 : 10926 : active_init_p = false;
3998 : :
3999 : 10926 : partition_p = partition;
4000 : :
4001 : 10926 : inform_cmi_p = false;
4002 : 10926 : visited_p = false;
4003 : :
4004 : 10926 : extensions = 0;
4005 : 10926 : if (name && TREE_CODE (name) == STRING_CST)
4006 : : {
4007 : 1865 : header_p = true;
4008 : :
4009 : 1865 : const char *string = TREE_STRING_POINTER (name);
4010 : 1865 : gcc_checking_assert (string[0] == '.'
4011 : : ? IS_DIR_SEPARATOR (string[1])
4012 : : : IS_ABSOLUTE_PATH (string));
4013 : : }
4014 : :
4015 : 10926 : gcc_checking_assert (!(parent && header_p));
4016 : 10926 : }
4017 : :
4018 : 54 : module_state::~module_state ()
4019 : : {
4020 : 54 : release ();
4021 : 54 : }
4022 : :
4023 : : /* Hash module state. */
4024 : : static hashval_t
4025 : 15844 : module_name_hash (const_tree name)
4026 : : {
4027 : 15844 : if (TREE_CODE (name) == STRING_CST)
4028 : 3457 : return htab_hash_string (TREE_STRING_POINTER (name));
4029 : : else
4030 : 12387 : return IDENTIFIER_HASH_VALUE (name);
4031 : : }
4032 : :
4033 : : hashval_t
4034 : 4291 : module_state_hash::hash (const value_type m)
4035 : : {
4036 : 4291 : hashval_t ph = pointer_hash<void>::hash
4037 : 4291 : (reinterpret_cast<void *> (reinterpret_cast<uintptr_t> (m->parent)
4038 : 4291 : | m->is_partition ()));
4039 : 4291 : hashval_t nh = module_name_hash (m->name);
4040 : 4291 : return iterative_hash_hashval_t (ph, nh);
4041 : : }
4042 : :
4043 : : /* Hash a name. */
4044 : : hashval_t
4045 : 11553 : module_state_hash::hash (const compare_type &c)
4046 : : {
4047 : 11553 : hashval_t ph = pointer_hash<void>::hash (reinterpret_cast<void *> (c.second));
4048 : 11553 : hashval_t nh = module_name_hash (c.first);
4049 : :
4050 : 11553 : return iterative_hash_hashval_t (ph, nh);
4051 : : }
4052 : :
4053 : : bool
4054 : 7797 : module_state_hash::equal (const value_type existing,
4055 : : const compare_type &candidate)
4056 : : {
4057 : 7797 : uintptr_t ep = (reinterpret_cast<uintptr_t> (existing->parent)
4058 : 7797 : | existing->is_partition ());
4059 : 7797 : if (ep != candidate.second)
4060 : : return false;
4061 : :
4062 : : /* Identifier comparison is by pointer. If the string_csts happen
4063 : : to be the same object, then they're equal too. */
4064 : 6466 : if (existing->name == candidate.first)
4065 : : return true;
4066 : :
4067 : : /* If neither are string csts, they can't be equal. */
4068 : 1324 : if (TREE_CODE (candidate.first) != STRING_CST
4069 : 487 : || TREE_CODE (existing->name) != STRING_CST)
4070 : : return false;
4071 : :
4072 : : /* String equality. */
4073 : 421 : if (TREE_STRING_LENGTH (existing->name)
4074 : 421 : == TREE_STRING_LENGTH (candidate.first)
4075 : 421 : && !memcmp (TREE_STRING_POINTER (existing->name),
4076 : 418 : TREE_STRING_POINTER (candidate.first),
4077 : 418 : TREE_STRING_LENGTH (existing->name)))
4078 : 135 : return true;
4079 : :
4080 : : return false;
4081 : : }
4082 : :
4083 : : /********************************************************************/
4084 : : /* Global state */
4085 : :
4086 : : /* Mapper name. */
4087 : : static const char *module_mapper_name;
4088 : :
4089 : : /* Deferred import queue (FIFO). */
4090 : : static vec<module_state *, va_heap, vl_embed> *pending_imports;
4091 : :
4092 : : /* CMI repository path and workspace. */
4093 : : static char *cmi_repo;
4094 : : static size_t cmi_repo_length;
4095 : : static char *cmi_path;
4096 : : static size_t cmi_path_alloc;
4097 : :
4098 : : /* Count of available and loaded clusters. */
4099 : : static unsigned available_clusters;
4100 : : static unsigned loaded_clusters;
4101 : :
4102 : : /* What the current TU is. */
4103 : : unsigned module_kind;
4104 : :
4105 : : /* Global trees. */
4106 : : static const std::pair<tree *, unsigned> global_tree_arys[] =
4107 : : {
4108 : : std::pair<tree *, unsigned> (sizetype_tab, stk_type_kind_last),
4109 : : std::pair<tree *, unsigned> (integer_types, itk_none),
4110 : : std::pair<tree *, unsigned> (global_trees, TI_MODULE_HWM),
4111 : : std::pair<tree *, unsigned> (c_global_trees, CTI_MODULE_HWM),
4112 : : std::pair<tree *, unsigned> (cp_global_trees, CPTI_MODULE_HWM),
4113 : : std::pair<tree *, unsigned> (NULL, 0)
4114 : : };
4115 : : static GTY(()) vec<tree, va_gc> *fixed_trees;
4116 : : static unsigned global_crc;
4117 : :
4118 : : /* Lazy loading can open many files concurrently, there are
4119 : : per-process limits on that. We pay attention to the process limit,
4120 : : and attempt to increase it when we run out. Otherwise we use an
4121 : : LRU scheme to figure out who to flush. Note that if the import
4122 : : graph /depth/ exceeds lazy_limit, we'll exceed the limit. */
4123 : : static unsigned lazy_lru; /* LRU counter. */
4124 : : static unsigned lazy_open; /* Number of open modules */
4125 : : static unsigned lazy_limit; /* Current limit of open modules. */
4126 : : static unsigned lazy_hard_limit; /* Hard limit on open modules. */
4127 : : /* Account for source, assembler and dump files & directory searches.
4128 : : We don't keep the source file's open, so we don't have to account
4129 : : for #include depth. I think dump files are opened and closed per
4130 : : pass, but ICBW. */
4131 : : #define LAZY_HEADROOM 15 /* File descriptor headroom. */
4132 : :
4133 : : /* Vector of module state. Indexed by OWNER. Index 0 is reserved for the
4134 : : current TU; imports start at 1. */
4135 : : static GTY(()) vec<module_state *, va_gc> *modules;
4136 : :
4137 : : /* Get the module state for the current TU's module. */
4138 : :
4139 : : static module_state *
4140 : 222141 : this_module() {
4141 : 222141 : return (*modules)[0];
4142 : : }
4143 : :
4144 : : /* Hash of module state, findable by {name, parent}. */
4145 : : static GTY(()) hash_table<module_state_hash> *modules_hash;
4146 : :
4147 : : /* Map of imported entities. We map DECL_UID to index of entity
4148 : : vector. */
4149 : : typedef hash_map<unsigned/*UID*/, unsigned/*index*/,
4150 : : simple_hashmap_traits<int_hash<unsigned,0>, unsigned>
4151 : : > entity_map_t;
4152 : : static entity_map_t *entity_map;
4153 : : /* Doesn't need GTYing, because any tree referenced here is also
4154 : : findable by, symbol table, specialization table, return type of
4155 : : reachable function. */
4156 : : static vec<binding_slot, va_heap, vl_embed> *entity_ary;
4157 : :
4158 : : /* Members entities of imported classes that are defined in this TU.
4159 : : These are where the entity's context is not from the current TU.
4160 : : We need to emit the definition (but not the enclosing class).
4161 : :
4162 : : We could find these by walking ALL the imported classes that we
4163 : : could provide a member definition. But that's expensive,
4164 : : especially when you consider lazy implicit member declarations,
4165 : : which could be ANY imported class. */
4166 : : static GTY(()) vec<tree, va_gc> *class_members;
4167 : :
4168 : : /* The same problem exists for class template partial
4169 : : specializations. Now that we have constraints, the invariant of
4170 : : expecting them in the instantiation table no longer holds. One of
4171 : : the constrained partial specializations will be there, but the
4172 : : others not so much. It's not even an unconstrained partial
4173 : : spacialization in the table :( so any partial template declaration
4174 : : is added to this list too. */
4175 : : static GTY(()) vec<tree, va_gc> *partial_specializations;
4176 : :
4177 : : /********************************************************************/
4178 : :
4179 : : /* Our module mapper (created lazily). */
4180 : : module_client *mapper;
4181 : :
4182 : : static module_client *make_mapper (location_t loc, class mkdeps *deps);
4183 : 31571 : inline module_client *get_mapper (location_t loc, class mkdeps *deps)
4184 : : {
4185 : 31571 : auto *res = mapper;
4186 : 290 : if (!res)
4187 : 4650 : res = make_mapper (loc, deps);
4188 : 31571 : return res;
4189 : : }
4190 : :
4191 : : /********************************************************************/
4192 : : static tree
4193 : 281893 : get_clone_target (tree decl)
4194 : : {
4195 : 281893 : tree target;
4196 : :
4197 : 281893 : if (TREE_CODE (decl) == TEMPLATE_DECL)
4198 : : {
4199 : 32980 : tree res_orig = DECL_CLONED_FUNCTION (DECL_TEMPLATE_RESULT (decl));
4200 : :
4201 : 32980 : target = DECL_TI_TEMPLATE (res_orig);
4202 : : }
4203 : : else
4204 : 248913 : target = DECL_CLONED_FUNCTION (decl);
4205 : :
4206 : 281893 : gcc_checking_assert (DECL_MAYBE_IN_CHARGE_CDTOR_P (target));
4207 : :
4208 : 281893 : return target;
4209 : : }
4210 : :
4211 : : /* Like FOR_EACH_CLONE, but will walk cloned templates. */
4212 : : #define FOR_EVERY_CLONE(CLONE, FN) \
4213 : : if (!DECL_MAYBE_IN_CHARGE_CDTOR_P (FN)); \
4214 : : else \
4215 : : for (CLONE = DECL_CHAIN (FN); \
4216 : : CLONE && DECL_CLONED_FUNCTION_P (CLONE); \
4217 : : CLONE = DECL_CHAIN (CLONE))
4218 : :
4219 : : /* It'd be nice if USE_TEMPLATE was a field of template_info
4220 : : (a) it'd solve the enum case dealt with below,
4221 : : (b) both class templates and decl templates would store this in the
4222 : : same place
4223 : : (c) this function wouldn't need the by-ref arg, which is annoying. */
4224 : :
4225 : : static tree
4226 : 185925436 : node_template_info (tree decl, int &use)
4227 : : {
4228 : 185925436 : tree ti = NULL_TREE;
4229 : 185925436 : int use_tpl = -1;
4230 : 185925436 : if (DECL_IMPLICIT_TYPEDEF_P (decl))
4231 : : {
4232 : 19882885 : tree type = TREE_TYPE (decl);
4233 : :
4234 : 19882885 : ti = TYPE_TEMPLATE_INFO (type);
4235 : 19882885 : if (ti)
4236 : : {
4237 : 3798844 : if (TYPE_LANG_SPECIFIC (type))
4238 : 3791132 : use_tpl = CLASSTYPE_USE_TEMPLATE (type);
4239 : : else
4240 : : {
4241 : : /* An enum, where we don't explicitly encode use_tpl.
4242 : : If the containing context (a type or a function), is
4243 : : an ({im,ex}plicit) instantiation, then this is too.
4244 : : If it's a partial or explicit specialization, then
4245 : : this is not!. */
4246 : 7712 : tree ctx = CP_DECL_CONTEXT (decl);
4247 : 7712 : if (TYPE_P (ctx))
4248 : 7586 : ctx = TYPE_NAME (ctx);
4249 : 7712 : node_template_info (ctx, use);
4250 : 7712 : use_tpl = use != 2 ? use : 0;
4251 : : }
4252 : : }
4253 : : }
4254 : 166042551 : else if (DECL_LANG_SPECIFIC (decl)
4255 : 166042551 : && (VAR_P (decl)
4256 : : || TREE_CODE (decl) == TYPE_DECL
4257 : : || TREE_CODE (decl) == FUNCTION_DECL
4258 : : || TREE_CODE (decl) == FIELD_DECL
4259 : : || TREE_CODE (decl) == CONCEPT_DECL
4260 : : || TREE_CODE (decl) == TEMPLATE_DECL))
4261 : : {
4262 : 88745581 : use_tpl = DECL_USE_TEMPLATE (decl);
4263 : 88745581 : ti = DECL_TEMPLATE_INFO (decl);
4264 : : }
4265 : :
4266 : 185925436 : use = use_tpl;
4267 : 185925436 : return ti;
4268 : : }
4269 : :
4270 : : /* Find the index in entity_ary for an imported DECL. It should
4271 : : always be there, but bugs can cause it to be missing, and that can
4272 : : crash the crash reporting -- let's not do that! When streaming
4273 : : out we place entities from this module there too -- with negated
4274 : : indices. */
4275 : :
4276 : : static unsigned
4277 : 1158809 : import_entity_index (tree decl, bool null_ok = false)
4278 : : {
4279 : 1158809 : if (unsigned *slot = entity_map->get (DECL_UID (decl)))
4280 : 1158764 : return *slot;
4281 : :
4282 : 45 : gcc_checking_assert (null_ok);
4283 : : return ~(~0u >> 1);
4284 : : }
4285 : :
4286 : : /* Find the module for an imported entity at INDEX in the entity ary.
4287 : : There must be one. */
4288 : :
4289 : : static module_state *
4290 : 86458 : import_entity_module (unsigned index)
4291 : : {
4292 : 86458 : if (index > ~(~0u >> 1))
4293 : : /* This is an index for an exported entity. */
4294 : 21 : return this_module ();
4295 : :
4296 : : /* Do not include the current TU (not an off-by-one error). */
4297 : 86437 : unsigned pos = 1;
4298 : 86437 : unsigned len = modules->length () - pos;
4299 : 207035 : while (len)
4300 : : {
4301 : 120598 : unsigned half = len / 2;
4302 : 120598 : module_state *probe = (*modules)[pos + half];
4303 : 120598 : if (index < probe->entity_lwm)
4304 : : len = half;
4305 : 86592 : else if (index < probe->entity_lwm + probe->entity_num)
4306 : : return probe;
4307 : : else
4308 : : {
4309 : 155 : pos += half + 1;
4310 : 155 : len = len - (half + 1);
4311 : : }
4312 : : }
4313 : 0 : gcc_unreachable ();
4314 : : }
4315 : :
4316 : :
4317 : : /********************************************************************/
4318 : : /* A dumping machinery. */
4319 : :
4320 : : class dumper {
4321 : : public:
4322 : : enum {
4323 : : LOCATION = TDF_LINENO, /* -lineno:Source location streaming. */
4324 : : DEPEND = TDF_GRAPH, /* -graph:Dependency graph construction. */
4325 : : CLUSTER = TDF_BLOCKS, /* -blocks:Clusters. */
4326 : : TREE = TDF_UID, /* -uid:Tree streaming. */
4327 : : MERGE = TDF_ALIAS, /* -alias:Mergeable Entities. */
4328 : : ELF = TDF_ASMNAME, /* -asmname:Elf data. */
4329 : : MACRO = TDF_VOPS /* -vops:Macros. */
4330 : : };
4331 : :
4332 : : private:
4333 : : struct impl {
4334 : : typedef vec<module_state *, va_heap, vl_embed> stack_t;
4335 : :
4336 : : FILE *stream; /* Dump stream. */
4337 : : unsigned indent; /* Local indentation. */
4338 : : bool bol; /* Beginning of line. */
4339 : : stack_t stack; /* Trailing array of module_state. */
4340 : :
4341 : : bool nested_name (tree); /* Dump a name following DECL_CONTEXT. */
4342 : : };
4343 : :
4344 : : public:
4345 : : /* The dumper. */
4346 : : impl *dumps;
4347 : : dump_flags_t flags;
4348 : :
4349 : : public:
4350 : : /* Push/pop module state dumping. */
4351 : : unsigned push (module_state *);
4352 : : void pop (unsigned);
4353 : :
4354 : : public:
4355 : : /* Change local indentation. */
4356 : 314197479 : void indent ()
4357 : : {
4358 : 309 : if (dumps)
4359 : 534447 : dumps->indent++;
4360 : : }
4361 : 314197479 : void outdent ()
4362 : : {
4363 : 314197479 : if (dumps)
4364 : : {
4365 : 534447 : gcc_checking_assert (dumps->indent);
4366 : 534447 : dumps->indent--;
4367 : : }
4368 : 314197479 : }
4369 : :
4370 : : public:
4371 : : /* Is dump enabled?. */
4372 : 199947856 : bool operator () (int mask = 0)
4373 : : {
4374 : 3810516 : if (!dumps || !dumps->stream)
4375 : : return false;
4376 : 406192 : if (mask && !(mask & flags))
4377 : 3944 : return false;
4378 : : return true;
4379 : : }
4380 : : /* Dump some information. */
4381 : : bool operator () (const char *, ...);
4382 : : };
4383 : :
4384 : : /* The dumper. */
4385 : : static dumper dump = {0, dump_flags_t (0)};
4386 : :
4387 : : /* Push to dumping M. Return previous indentation level. */
4388 : :
4389 : : unsigned
4390 : 115604 : dumper::push (module_state *m)
4391 : : {
4392 : 115604 : FILE *stream = NULL;
4393 : 115604 : if (!dumps || !dumps->stack.length ())
4394 : : {
4395 : 114415 : stream = dump_begin (module_dump_id, &flags);
4396 : 114415 : if (!stream)
4397 : : return 0;
4398 : : }
4399 : :
4400 : 7022 : if (!dumps || !dumps->stack.space (1))
4401 : : {
4402 : : /* Create or extend the dump implementor. */
4403 : 1161 : unsigned current = dumps ? dumps->stack.length () : 0;
4404 : 614 : unsigned count = current ? current * 2 : EXPERIMENT (1, 20);
4405 : 1161 : size_t alloc = (offsetof (impl, stack)
4406 : 1161 : + impl::stack_t::embedded_size (count));
4407 : 1161 : dumps = XRESIZEVAR (impl, dumps, alloc);
4408 : 1161 : dumps->stack.embedded_init (count, current);
4409 : : }
4410 : 7022 : if (stream)
4411 : 5833 : dumps->stream = stream;
4412 : :
4413 : 7022 : unsigned n = dumps->indent;
4414 : 7022 : dumps->indent = 0;
4415 : 7022 : dumps->bol = true;
4416 : 7022 : dumps->stack.quick_push (m);
4417 : 7022 : if (m)
4418 : : {
4419 : 1904 : module_state *from = NULL;
4420 : :
4421 : 1904 : if (dumps->stack.length () > 1)
4422 : 606 : from = dumps->stack[dumps->stack.length () - 2];
4423 : : else
4424 : 1298 : dump ("");
4425 : 3443 : dump (from ? "Starting module %M (from %M)"
4426 : : : "Starting module %M", m, from);
4427 : : }
4428 : :
4429 : : return n;
4430 : : }
4431 : :
4432 : : /* Pop from dumping. Restore indentation to N. */
4433 : :
4434 : 115561 : void dumper::pop (unsigned n)
4435 : : {
4436 : 115561 : if (!dumps)
4437 : : return;
4438 : :
4439 : 14044 : gcc_checking_assert (dump () && !dumps->indent);
4440 : 7022 : if (module_state *m = dumps->stack[dumps->stack.length () - 1])
4441 : : {
4442 : 1904 : module_state *from = (dumps->stack.length () > 1
4443 : 1904 : ? dumps->stack[dumps->stack.length () - 2] : NULL);
4444 : 2145 : dump (from ? "Finishing module %M (returning to %M)"
4445 : : : "Finishing module %M", m, from);
4446 : : }
4447 : 7022 : dumps->stack.pop ();
4448 : 7022 : dumps->indent = n;
4449 : 7022 : if (!dumps->stack.length ())
4450 : : {
4451 : 5833 : dump_end (module_dump_id, dumps->stream);
4452 : 5833 : dumps->stream = NULL;
4453 : : }
4454 : : }
4455 : :
4456 : : /* Dump a nested name for arbitrary tree T. Sometimes it won't have a
4457 : : name. */
4458 : :
4459 : : bool
4460 : 503758 : dumper::impl::nested_name (tree t)
4461 : : {
4462 : 503758 : tree ti = NULL_TREE;
4463 : 503758 : int origin = -1;
4464 : 503758 : tree name = NULL_TREE;
4465 : :
4466 : 503758 : if (t && TREE_CODE (t) == TU_LOCAL_ENTITY)
4467 : 0 : t = TU_LOCAL_ENTITY_NAME (t);
4468 : :
4469 : 503728 : if (t && TREE_CODE (t) == TREE_BINFO)
4470 : 384 : t = BINFO_TYPE (t);
4471 : :
4472 : 503758 : if (t && TYPE_P (t))
4473 : 250442 : t = TYPE_NAME (t);
4474 : :
4475 : 503716 : if (t && DECL_P (t))
4476 : : {
4477 : 420746 : if (t == global_namespace || DECL_TEMPLATE_PARM_P (t))
4478 : : ;
4479 : 389462 : else if (tree ctx = DECL_CONTEXT (t))
4480 : 299304 : if (TREE_CODE (ctx) == TRANSLATION_UNIT_DECL
4481 : 299304 : || nested_name (ctx))
4482 : 299304 : fputs ("::", stream);
4483 : :
4484 : 420746 : int use_tpl;
4485 : 420746 : ti = node_template_info (t, use_tpl);
4486 : 137316 : if (ti && TREE_CODE (TI_TEMPLATE (ti)) == TEMPLATE_DECL
4487 : 558020 : && (DECL_TEMPLATE_RESULT (TI_TEMPLATE (ti)) == t))
4488 : : t = TI_TEMPLATE (ti);
4489 : 420746 : tree not_tmpl = t;
4490 : 420746 : if (TREE_CODE (t) == TEMPLATE_DECL)
4491 : : {
4492 : 22557 : fputs ("template ", stream);
4493 : 22557 : not_tmpl = DECL_TEMPLATE_RESULT (t);
4494 : : }
4495 : :
4496 : 22557 : if (not_tmpl
4497 : 420742 : && DECL_P (not_tmpl)
4498 : 420742 : && DECL_LANG_SPECIFIC (not_tmpl)
4499 : 245023 : && DECL_MODULE_IMPORT_P (not_tmpl))
4500 : : {
4501 : : /* We need to be careful here, so as to not explode on
4502 : : inconsistent data -- we're probably debugging, because
4503 : : Something Is Wrong. */
4504 : 18248 : unsigned index = import_entity_index (t, true);
4505 : 18248 : if (!(index & ~(~0u >> 1)))
4506 : 17690 : origin = import_entity_module (index)->mod;
4507 : 558 : else if (index > ~(~0u >> 1))
4508 : : /* An imported partition member that we're emitting. */
4509 : : origin = 0;
4510 : : else
4511 : 45 : origin = -2;
4512 : : }
4513 : :
4514 : 423820 : name = DECL_NAME (t) ? DECL_NAME (t)
4515 : 4147 : : HAS_DECL_ASSEMBLER_NAME_P (t) ? DECL_ASSEMBLER_NAME_RAW (t)
4516 : : : NULL_TREE;
4517 : : }
4518 : : else
4519 : : name = t;
4520 : :
4521 : 420746 : if (name)
4522 : 468296 : switch (TREE_CODE (name))
4523 : : {
4524 : 13483 : default:
4525 : 13483 : fputs ("#unnamed#", stream);
4526 : 13483 : break;
4527 : :
4528 : 430646 : case IDENTIFIER_NODE:
4529 : 430646 : fwrite (IDENTIFIER_POINTER (name), 1, IDENTIFIER_LENGTH (name), stream);
4530 : 430646 : break;
4531 : :
4532 : 24075 : case INTEGER_CST:
4533 : 24075 : print_hex (wi::to_wide (name), stream);
4534 : 24075 : break;
4535 : :
4536 : 92 : case STRING_CST:
4537 : : /* If TREE_TYPE is NULL, this is a raw string. */
4538 : 184 : fwrite (TREE_STRING_POINTER (name), 1,
4539 : 92 : TREE_STRING_LENGTH (name) - (TREE_TYPE (name) != NULL_TREE),
4540 : : stream);
4541 : 92 : break;
4542 : : }
4543 : : else
4544 : 35462 : fputs ("#null#", stream);
4545 : :
4546 : 503758 : if (origin >= 0)
4547 : : {
4548 : 18203 : const module_state *module = (*modules)[origin];
4549 : 36406 : fprintf (stream, "@%s:%d", !module ? "" : !module->name ? "(unnamed)"
4550 : 18203 : : module->get_flatname (), origin);
4551 : : }
4552 : 485555 : else if (origin == -2)
4553 : 45 : fprintf (stream, "@???");
4554 : :
4555 : 503758 : if (ti)
4556 : : {
4557 : 137316 : tree args = INNERMOST_TEMPLATE_ARGS (TI_ARGS (ti));
4558 : 137316 : fputs ("<", stream);
4559 : 137316 : if (args)
4560 : 347240 : for (int ix = 0; ix != TREE_VEC_LENGTH (args); ix++)
4561 : : {
4562 : 209924 : if (ix)
4563 : 72608 : fputs (",", stream);
4564 : 209924 : nested_name (TREE_VEC_ELT (args, ix));
4565 : : }
4566 : 137316 : fputs (">", stream);
4567 : : }
4568 : :
4569 : 503758 : return true;
4570 : : }
4571 : :
4572 : : /* Formatted dumping. FORMAT begins with '+' do not emit a trailing
4573 : : new line. (Normally it is appended.)
4574 : : Escapes:
4575 : : %C - tree_code
4576 : : %I - identifier
4577 : : %K - location_t or line_map_uint_t
4578 : : %M - module_state
4579 : : %N - name -- DECL_NAME
4580 : : %P - context:name pair
4581 : : %R - unsigned:unsigned ratio
4582 : : %S - symbol -- DECL_ASSEMBLER_NAME
4583 : : %U - long unsigned
4584 : : %V - version
4585 : : --- the following are printf-like, but without its flexibility
4586 : : %d - decimal int
4587 : : %p - pointer
4588 : : %s - string
4589 : : %u - unsigned int
4590 : : %x - hex int
4591 : :
4592 : : We do not implement the printf modifiers. */
4593 : :
4594 : : bool
4595 : 420929 : dumper::operator () (const char *format, ...)
4596 : : {
4597 : 420929 : if (!(*this) ())
4598 : : return false;
4599 : :
4600 : 360745 : bool no_nl = format[0] == '+';
4601 : 360745 : format += no_nl;
4602 : :
4603 : 360745 : if (dumps->bol)
4604 : : {
4605 : : /* Module import indent. */
4606 : 187346 : if (unsigned depth = dumps->stack.length () - 1)
4607 : : {
4608 : 21725 : const char *prefix = ">>>>";
4609 : 43432 : fprintf (dumps->stream, (depth <= strlen (prefix)
4610 : 21707 : ? &prefix[strlen (prefix) - depth]
4611 : : : ">.%d.>"), depth);
4612 : : }
4613 : :
4614 : : /* Local indent. */
4615 : 187346 : if (unsigned indent = dumps->indent)
4616 : : {
4617 : 98647 : const char *prefix = " ";
4618 : 192344 : fprintf (dumps->stream, (indent <= strlen (prefix)
4619 : 93697 : ? &prefix[strlen (prefix) - indent]
4620 : : : " .%d. "), indent);
4621 : : }
4622 : 187346 : dumps->bol = false;
4623 : : }
4624 : :
4625 : 360745 : va_list args;
4626 : 360745 : va_start (args, format);
4627 : 1061219 : while (const char *esc = strchr (format, '%'))
4628 : : {
4629 : 700474 : fwrite (format, 1, (size_t)(esc - format), dumps->stream);
4630 : 700474 : format = ++esc;
4631 : 700474 : switch (*format++)
4632 : : {
4633 : 0 : default:
4634 : 0 : gcc_unreachable ();
4635 : :
4636 : 547 : case '%':
4637 : 547 : fputc ('%', dumps->stream);
4638 : 547 : break;
4639 : :
4640 : 108541 : case 'C': /* Code */
4641 : 108541 : {
4642 : 108541 : tree_code code = (tree_code)va_arg (args, unsigned);
4643 : 108541 : fputs (get_tree_code_name (code), dumps->stream);
4644 : : }
4645 : 108541 : break;
4646 : :
4647 : 81 : case 'I': /* Identifier. */
4648 : 81 : {
4649 : 81 : tree t = va_arg (args, tree);
4650 : 81 : dumps->nested_name (t);
4651 : : }
4652 : 81 : break;
4653 : :
4654 : 4338 : case 'K': /* location_t, either 32- or 64-bit. */
4655 : 4338 : {
4656 : 4338 : unsigned long long u = va_arg (args, location_t);
4657 : 4338 : fprintf (dumps->stream, "%llu", u);
4658 : : }
4659 : 4338 : break;
4660 : :
4661 : 7287 : case 'M': /* Module. */
4662 : 7287 : {
4663 : 7287 : const char *str = "(none)";
4664 : 7287 : if (module_state *m = va_arg (args, module_state *))
4665 : : {
4666 : 7287 : if (!m->has_location ())
4667 : : str = "(detached)";
4668 : : else
4669 : 7287 : str = m->get_flatname ();
4670 : : }
4671 : 7287 : fputs (str, dumps->stream);
4672 : : }
4673 : 7287 : break;
4674 : :
4675 : 119203 : case 'N': /* Name. */
4676 : 119203 : {
4677 : 119203 : tree t = va_arg (args, tree);
4678 : 238895 : while (t && TREE_CODE (t) == OVERLOAD)
4679 : 489 : t = OVL_FUNCTION (t);
4680 : 119203 : fputc ('\'', dumps->stream);
4681 : 119203 : dumps->nested_name (t);
4682 : 119203 : fputc ('\'', dumps->stream);
4683 : : }
4684 : 119203 : break;
4685 : :
4686 : 6841 : case 'P': /* Pair. */
4687 : 6841 : {
4688 : 6841 : tree ctx = va_arg (args, tree);
4689 : 6841 : tree name = va_arg (args, tree);
4690 : 6841 : fputc ('\'', dumps->stream);
4691 : 6841 : dumps->nested_name (ctx);
4692 : 6841 : if (ctx && ctx != global_namespace)
4693 : 855 : fputs ("::", dumps->stream);
4694 : 6841 : dumps->nested_name (name);
4695 : 6841 : fputc ('\'', dumps->stream);
4696 : : }
4697 : 6841 : break;
4698 : :
4699 : 828 : case 'R': /* Ratio */
4700 : 828 : {
4701 : 828 : unsigned a = va_arg (args, unsigned);
4702 : 828 : unsigned b = va_arg (args, unsigned);
4703 : 828 : fprintf (dumps->stream, "%.1f", (float) a / (b + !b));
4704 : : }
4705 : 828 : break;
4706 : :
4707 : 34701 : case 'S': /* Symbol name */
4708 : 34701 : {
4709 : 34701 : tree t = va_arg (args, tree);
4710 : 34701 : if (t && TYPE_P (t))
4711 : 12631 : t = TYPE_NAME (t);
4712 : 32921 : if (t && HAS_DECL_ASSEMBLER_NAME_P (t)
4713 : 31744 : && DECL_ASSEMBLER_NAME_SET_P (t))
4714 : : {
4715 : 169 : fputc ('(', dumps->stream);
4716 : 169 : fputs (IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (t)),
4717 : 169 : dumps->stream);
4718 : 169 : fputc (')', dumps->stream);
4719 : : }
4720 : : }
4721 : : break;
4722 : :
4723 : 0 : case 'U': /* long unsigned. */
4724 : 0 : {
4725 : 0 : unsigned long u = va_arg (args, unsigned long);
4726 : 0 : fprintf (dumps->stream, "%lu", u);
4727 : : }
4728 : 0 : break;
4729 : :
4730 : 775 : case 'V': /* Version. */
4731 : 775 : {
4732 : 775 : unsigned v = va_arg (args, unsigned);
4733 : 775 : verstr_t string;
4734 : :
4735 : 775 : version2string (v, string);
4736 : 775 : fputs (string, dumps->stream);
4737 : : }
4738 : 775 : break;
4739 : :
4740 : 0 : case 'c': /* Character. */
4741 : 0 : {
4742 : 0 : int c = va_arg (args, int);
4743 : 0 : fputc (c, dumps->stream);
4744 : : }
4745 : 0 : break;
4746 : :
4747 : 61503 : case 'd': /* Decimal Int. */
4748 : 61503 : {
4749 : 61503 : int d = va_arg (args, int);
4750 : 61503 : fprintf (dumps->stream, "%d", d);
4751 : : }
4752 : 61503 : break;
4753 : :
4754 : 0 : case 'p': /* Pointer. */
4755 : 0 : {
4756 : 0 : void *p = va_arg (args, void *);
4757 : 0 : fprintf (dumps->stream, "%p", p);
4758 : : }
4759 : 0 : break;
4760 : :
4761 : 122615 : case 's': /* String. */
4762 : 122615 : {
4763 : 122615 : const char *s = va_arg (args, char *);
4764 : 122615 : gcc_checking_assert (s);
4765 : 122615 : fputs (s, dumps->stream);
4766 : : }
4767 : 122615 : break;
4768 : :
4769 : 230724 : case 'u': /* Unsigned. */
4770 : 230724 : {
4771 : 230724 : unsigned u = va_arg (args, unsigned);
4772 : 230724 : fprintf (dumps->stream, "%u", u);
4773 : : }
4774 : 230724 : break;
4775 : :
4776 : 2490 : case 'x': /* Hex. */
4777 : 2490 : {
4778 : 2490 : unsigned x = va_arg (args, unsigned);
4779 : 2490 : fprintf (dumps->stream, "%x", x);
4780 : : }
4781 : 2490 : break;
4782 : : }
4783 : : }
4784 : 360745 : fputs (format, dumps->stream);
4785 : 360745 : va_end (args);
4786 : 360745 : if (!no_nl)
4787 : : {
4788 : 187346 : dumps->bol = true;
4789 : 187346 : fputc ('\n', dumps->stream);
4790 : : }
4791 : : return true;
4792 : : }
4793 : :
4794 : : struct note_def_cache_hasher : ggc_cache_ptr_hash<tree_node>
4795 : : {
4796 : 249186 : static int keep_cache_entry (tree t)
4797 : : {
4798 : 249186 : if (!CHECKING_P)
4799 : : /* GTY is unfortunately not clever enough to conditionalize
4800 : : this. */
4801 : : gcc_unreachable ();
4802 : :
4803 : 249186 : if (ggc_marked_p (t))
4804 : : return -1;
4805 : :
4806 : 0 : unsigned n = dump.push (NULL);
4807 : : /* This might or might not be an error. We should note its
4808 : : dropping whichever. */
4809 : 0 : dump () && dump ("Dropping %N from note_defs table", t);
4810 : 0 : dump.pop (n);
4811 : :
4812 : 0 : return 0;
4813 : : }
4814 : : };
4815 : :
4816 : : /* We should stream each definition at most once.
4817 : : This needs to be a cache because there are cases where a definition
4818 : : ends up being not retained, and we need to drop those so we don't
4819 : : get confused if memory is reallocated. */
4820 : : typedef hash_table<note_def_cache_hasher> note_defs_table_t;
4821 : : static GTY((cache)) note_defs_table_t *note_defs;
4822 : :
4823 : : void
4824 : 304284 : trees_in::assert_definition (tree decl ATTRIBUTE_UNUSED,
4825 : : bool installing ATTRIBUTE_UNUSED)
4826 : : {
4827 : : #if CHECKING_P
4828 : 304284 : tree *slot = note_defs->find_slot (decl, installing ? INSERT : NO_INSERT);
4829 : 304284 : tree not_tmpl = STRIP_TEMPLATE (decl);
4830 : 304284 : if (installing)
4831 : : {
4832 : : /* We must be inserting for the first time. */
4833 : 174096 : gcc_assert (!*slot);
4834 : 174096 : *slot = decl;
4835 : : }
4836 : : else
4837 : : /* If this is not the mergeable entity, it should not be in the
4838 : : table. If it is a non-global-module mergeable entity, it
4839 : : should be in the table. Global module entities could have been
4840 : : defined textually in the current TU and so might or might not
4841 : : be present. */
4842 : 130188 : gcc_assert (!is_duplicate (decl)
4843 : : ? !slot
4844 : : : (slot
4845 : : || !DECL_LANG_SPECIFIC (not_tmpl)
4846 : : || !DECL_MODULE_PURVIEW_P (not_tmpl)
4847 : : || (!DECL_MODULE_IMPORT_P (not_tmpl)
4848 : : && header_module_p ())));
4849 : :
4850 : 304284 : if (not_tmpl != decl)
4851 : 178966 : gcc_assert (!note_defs->find_slot (not_tmpl, NO_INSERT));
4852 : : #endif
4853 : 304284 : }
4854 : :
4855 : : void
4856 : 336047 : trees_out::assert_definition (tree decl ATTRIBUTE_UNUSED)
4857 : : {
4858 : : #if CHECKING_P
4859 : 336047 : tree *slot = note_defs->find_slot (decl, INSERT);
4860 : 336047 : gcc_assert (!*slot);
4861 : 336047 : *slot = decl;
4862 : 336047 : if (TREE_CODE (decl) == TEMPLATE_DECL)
4863 : 183957 : gcc_assert (!note_defs->find_slot (DECL_TEMPLATE_RESULT (decl), NO_INSERT));
4864 : : #endif
4865 : 336047 : }
4866 : :
4867 : : /********************************************************************/
4868 : : static bool
4869 : 11929 : noisy_p ()
4870 : : {
4871 : 0 : if (quiet_flag)
4872 : : return false;
4873 : :
4874 : 0 : pp_needs_newline (global_dc->get_reference_printer ()) = true;
4875 : 0 : diagnostic_set_last_function (global_dc,
4876 : : (diagnostics::diagnostic_info *) nullptr);
4877 : :
4878 : 0 : return true;
4879 : : }
4880 : :
4881 : : /* Set the cmi repo. Strip trailing '/', '.' becomes NULL. */
4882 : :
4883 : : static void
4884 : 99823 : set_cmi_repo (const char *r)
4885 : : {
4886 : 99823 : XDELETEVEC (cmi_repo);
4887 : 99823 : XDELETEVEC (cmi_path);
4888 : 99823 : cmi_path_alloc = 0;
4889 : :
4890 : 99823 : cmi_repo = NULL;
4891 : 99823 : cmi_repo_length = 0;
4892 : :
4893 : 99823 : if (!r || !r[0])
4894 : : return;
4895 : :
4896 : 4647 : size_t len = strlen (r);
4897 : 4647 : cmi_repo = XNEWVEC (char, len + 1);
4898 : 4647 : memcpy (cmi_repo, r, len + 1);
4899 : :
4900 : 4647 : if (len > 1 && IS_DIR_SEPARATOR (cmi_repo[len-1]))
4901 : 4647 : len--;
4902 : 4647 : if (len == 1 && cmi_repo[0] == '.')
4903 : 21 : len--;
4904 : 4647 : cmi_repo[len] = 0;
4905 : 4647 : cmi_repo_length = len;
4906 : : }
4907 : :
4908 : : /* TO is a repo-relative name. Provide one that we may use from where
4909 : : we are. */
4910 : :
4911 : : static const char *
4912 : 5645 : maybe_add_cmi_prefix (const char *to, size_t *len_p = NULL)
4913 : : {
4914 : 5645 : size_t len = len_p || cmi_repo_length ? strlen (to) : 0;
4915 : :
4916 : 5645 : if (cmi_repo_length && !IS_ABSOLUTE_PATH (to))
4917 : : {
4918 : 5624 : if (cmi_path_alloc < cmi_repo_length + len + 2)
4919 : : {
4920 : 4534 : XDELETEVEC (cmi_path);
4921 : 4534 : cmi_path_alloc = cmi_repo_length + len * 2 + 2;
4922 : 4534 : cmi_path = XNEWVEC (char, cmi_path_alloc);
4923 : :
4924 : 4534 : memcpy (cmi_path, cmi_repo, cmi_repo_length);
4925 : 4534 : cmi_path[cmi_repo_length] = DIR_SEPARATOR;
4926 : : }
4927 : :
4928 : 5624 : memcpy (&cmi_path[cmi_repo_length + 1], to, len + 1);
4929 : 5624 : len += cmi_repo_length + 1;
4930 : 5624 : to = cmi_path;
4931 : : }
4932 : :
4933 : 5645 : if (len_p)
4934 : 2718 : *len_p = len;
4935 : :
4936 : 5645 : return to;
4937 : : }
4938 : :
4939 : : /* Try and create the directories of PATH. */
4940 : :
4941 : : static void
4942 : 37 : create_dirs (char *path)
4943 : : {
4944 : 37 : char *base = path;
4945 : : /* Skip past initial slashes of absolute path. */
4946 : 37 : while (IS_DIR_SEPARATOR (*base))
4947 : 0 : base++;
4948 : :
4949 : : /* Try and create the missing directories. */
4950 : 2333 : for (; *base; base++)
4951 : 2296 : if (IS_DIR_SEPARATOR (*base))
4952 : : {
4953 : 227 : char sep = *base;
4954 : 227 : *base = 0;
4955 : 227 : int failed = mkdir (path, S_IRWXU | S_IRWXG | S_IRWXO);
4956 : 231 : dump () && dump ("Mkdir ('%s') errno:=%u", path, failed ? errno : 0);
4957 : 227 : *base = sep;
4958 : 227 : if (failed
4959 : : /* Maybe racing with another creator (of a *different*
4960 : : module). */
4961 : 43 : && errno != EEXIST)
4962 : : break;
4963 : : }
4964 : 37 : }
4965 : :
4966 : : /* Given a CLASSTYPE_DECL_LIST VALUE get the template friend decl,
4967 : : if that's what this is. */
4968 : :
4969 : : static tree
4970 : 69714 : friend_from_decl_list (tree frnd)
4971 : : {
4972 : 69714 : tree res = frnd;
4973 : :
4974 : 69714 : if (TREE_CODE (frnd) != TEMPLATE_DECL)
4975 : : {
4976 : 40885 : tree tmpl = NULL_TREE;
4977 : 40885 : if (TYPE_P (frnd))
4978 : : {
4979 : 6558 : res = TYPE_NAME (frnd);
4980 : 6459 : if (CLASS_TYPE_P (frnd)
4981 : 13017 : && CLASSTYPE_TEMPLATE_INFO (frnd))
4982 : 6450 : tmpl = CLASSTYPE_TI_TEMPLATE (frnd);
4983 : : }
4984 : 34327 : else if (DECL_TEMPLATE_INFO (frnd))
4985 : : {
4986 : 34327 : tmpl = DECL_TI_TEMPLATE (frnd);
4987 : 34327 : if (TREE_CODE (tmpl) != TEMPLATE_DECL)
4988 : : tmpl = NULL_TREE;
4989 : : }
4990 : :
4991 : 46655 : if (tmpl && DECL_TEMPLATE_RESULT (tmpl) == res)
4992 : : res = tmpl;
4993 : : }
4994 : :
4995 : 69714 : return res;
4996 : : }
4997 : :
4998 : : static tree
4999 : 26660 : find_enum_member (tree ctx, tree name)
5000 : : {
5001 : 26660 : for (tree values = TYPE_VALUES (ctx);
5002 : 470129 : values; values = TREE_CHAIN (values))
5003 : 462644 : if (DECL_NAME (TREE_VALUE (values)) == name)
5004 : : return TREE_VALUE (values);
5005 : :
5006 : : return NULL_TREE;
5007 : : }
5008 : :
5009 : : /********************************************************************/
5010 : : /* Instrumentation gathered writing bytes. */
5011 : :
5012 : : void
5013 : 276 : bytes_out::instrument ()
5014 : : {
5015 : 276 : dump ("Wrote %u bytes in %u blocks", lengths[3], spans[3]);
5016 : 276 : dump ("Wrote %u bits in %u bytes", lengths[0] + lengths[1], lengths[2]);
5017 : 828 : for (unsigned ix = 0; ix < 2; ix++)
5018 : 828 : dump (" %u %s spans of %R bits", spans[ix],
5019 : : ix ? "one" : "zero", lengths[ix], spans[ix]);
5020 : 276 : dump (" %u blocks with %R bits padding", spans[2],
5021 : 276 : lengths[2] * 8 - (lengths[0] + lengths[1]), spans[2]);
5022 : 276 : }
5023 : :
5024 : : /* Instrumentation gathered writing trees. */
5025 : : void
5026 : 2580 : trees_out::instrument ()
5027 : : {
5028 : 2580 : if (dump (""))
5029 : : {
5030 : 276 : bytes_out::instrument ();
5031 : 276 : dump ("Wrote:");
5032 : 276 : dump (" %u decl trees", decl_val_count);
5033 : 276 : dump (" %u other trees", tree_val_count);
5034 : 276 : dump (" %u back references", back_ref_count);
5035 : 276 : dump (" %u TU-local entities", tu_local_count);
5036 : 276 : dump (" %u null trees", null_count);
5037 : : }
5038 : 2580 : }
5039 : :
5040 : : /* Setup and teardown for a tree walk. */
5041 : :
5042 : : void
5043 : 1985670 : trees_out::begin ()
5044 : : {
5045 : 1985670 : gcc_assert (!streaming_p () || !tree_map.elements ());
5046 : :
5047 : 1985670 : mark_trees ();
5048 : 1985670 : if (streaming_p ())
5049 : 243579 : parent::begin ();
5050 : 1985670 : }
5051 : :
5052 : : unsigned
5053 : 243579 : trees_out::end (elf_out *sink, unsigned name, unsigned *crc_ptr)
5054 : : {
5055 : 243579 : gcc_checking_assert (streaming_p ());
5056 : :
5057 : 243579 : unmark_trees ();
5058 : 243579 : return parent::end (sink, name, crc_ptr);
5059 : : }
5060 : :
5061 : : void
5062 : 1742091 : trees_out::end ()
5063 : : {
5064 : 1742091 : gcc_assert (!streaming_p ());
5065 : :
5066 : 1742091 : unmark_trees ();
5067 : : /* Do not parent::end -- we weren't streaming. */
5068 : 1742091 : }
5069 : :
5070 : : void
5071 : 1985670 : trees_out::mark_trees ()
5072 : : {
5073 : 1985670 : if (size_t size = tree_map.elements ())
5074 : : {
5075 : : /* This isn't our first rodeo, destroy and recreate the
5076 : : tree_map. I'm a bad bad man. Use the previous size as a
5077 : : guess for the next one (so not all bad). */
5078 : 1510127 : tree_map.~ptr_int_hash_map ();
5079 : 1510127 : new (&tree_map) ptr_int_hash_map (size);
5080 : : }
5081 : :
5082 : : /* Install the fixed trees, with +ve references. */
5083 : 1985670 : unsigned limit = fixed_trees->length ();
5084 : 383030497 : for (unsigned ix = 0; ix != limit; ix++)
5085 : : {
5086 : 381044827 : tree val = (*fixed_trees)[ix];
5087 : 381044827 : bool existed = tree_map.put (val, ix + tag_fixed);
5088 : 381044827 : gcc_checking_assert (!TREE_VISITED (val) && !existed);
5089 : 381044827 : TREE_VISITED (val) = true;
5090 : : }
5091 : :
5092 : 1985670 : ref_num = 0;
5093 : 1985670 : }
5094 : :
5095 : : /* Unmark the trees we encountered */
5096 : :
5097 : : void
5098 : 1985670 : trees_out::unmark_trees ()
5099 : : {
5100 : 1985670 : ptr_int_hash_map::iterator end (tree_map.end ());
5101 : 450245207 : for (ptr_int_hash_map::iterator iter (tree_map.begin ()); iter != end; ++iter)
5102 : : {
5103 : 448259537 : tree node = reinterpret_cast<tree> ((*iter).first);
5104 : 448259537 : int ref = (*iter).second;
5105 : : /* We should have visited the node, and converted its mergeable
5106 : : reference to a regular reference. */
5107 : 448259537 : gcc_checking_assert (TREE_VISITED (node)
5108 : : && (ref <= tag_backref || ref >= tag_fixed));
5109 : 448259537 : TREE_VISITED (node) = false;
5110 : : }
5111 : 1985670 : }
5112 : :
5113 : : /* Mark DECL for by-value walking. We do this by inserting it into
5114 : : the tree map with a reference of zero. May be called multiple
5115 : : times on the same node. */
5116 : :
5117 : : void
5118 : 3077637 : trees_out::mark_by_value (tree decl)
5119 : : {
5120 : 3077637 : gcc_checking_assert (DECL_P (decl)
5121 : : /* Enum consts are INTEGER_CSTS. */
5122 : : || TREE_CODE (decl) == INTEGER_CST
5123 : : || TREE_CODE (decl) == TREE_BINFO);
5124 : :
5125 : 3077637 : if (TREE_VISITED (decl))
5126 : : /* Must already be forced or fixed. */
5127 : 3262 : gcc_checking_assert (*tree_map.get (decl) >= tag_value);
5128 : : else
5129 : : {
5130 : 3074375 : bool existed = tree_map.put (decl, tag_value);
5131 : 3074375 : gcc_checking_assert (!existed);
5132 : 3074375 : TREE_VISITED (decl) = true;
5133 : : }
5134 : 3077637 : }
5135 : :
5136 : : int
5137 : 81666108 : trees_out::get_tag (tree t)
5138 : : {
5139 : 81666108 : gcc_checking_assert (TREE_VISITED (t));
5140 : 81666108 : return *tree_map.get (t);
5141 : : }
5142 : :
5143 : : /* Insert T into the map, return its tag number. */
5144 : :
5145 : : int
5146 : 67214710 : trees_out::insert (tree t, walk_kind walk)
5147 : : {
5148 : 67214710 : gcc_checking_assert (walk != WK_normal || !TREE_VISITED (t));
5149 : 67214710 : int tag = --ref_num;
5150 : 67214710 : bool existed;
5151 : 67214710 : int &slot = tree_map.get_or_insert (t, &existed);
5152 : 67214710 : gcc_checking_assert (TREE_VISITED (t) == existed
5153 : : && (!existed
5154 : : || (walk == WK_value && slot == tag_value)));
5155 : 67214710 : TREE_VISITED (t) = true;
5156 : 67214710 : slot = tag;
5157 : :
5158 : 67214710 : return tag;
5159 : : }
5160 : :
5161 : : /* Insert T into the backreference array. Return its back reference
5162 : : number. */
5163 : :
5164 : : int
5165 : 18491340 : trees_in::insert (tree t)
5166 : : {
5167 : 18491340 : gcc_checking_assert (t || get_overrun ());
5168 : 18491340 : back_refs.safe_push (t);
5169 : 18491340 : return -(int)back_refs.length ();
5170 : : }
5171 : :
5172 : : /* A chained set of decls. */
5173 : :
5174 : : void
5175 : 113497 : trees_out::chained_decls (tree decls)
5176 : : {
5177 : 253509 : for (; decls; decls = DECL_CHAIN (decls))
5178 : 140012 : tree_node (decls);
5179 : 113497 : tree_node (NULL_TREE);
5180 : 113497 : }
5181 : :
5182 : : tree
5183 : 49866 : trees_in::chained_decls ()
5184 : : {
5185 : 49866 : tree decls = NULL_TREE;
5186 : 49866 : for (tree *chain = &decls;;)
5187 : 118544 : if (tree decl = tree_node ())
5188 : : {
5189 : 68678 : if (!DECL_P (decl) || DECL_CHAIN (decl))
5190 : : {
5191 : 0 : set_overrun ();
5192 : 0 : break;
5193 : : }
5194 : 68678 : *chain = decl;
5195 : 68678 : chain = &DECL_CHAIN (decl);
5196 : : }
5197 : : else
5198 : 68678 : break;
5199 : :
5200 : 49866 : return decls;
5201 : : }
5202 : :
5203 : : /* A vector of decls following DECL_CHAIN. */
5204 : :
5205 : : void
5206 : 301680 : trees_out::vec_chained_decls (tree decls)
5207 : : {
5208 : 301680 : if (streaming_p ())
5209 : : {
5210 : : unsigned len = 0;
5211 : :
5212 : 882469 : for (tree decl = decls; decl; decl = DECL_CHAIN (decl))
5213 : 731669 : len++;
5214 : 150800 : u (len);
5215 : : }
5216 : :
5217 : 1765344 : for (tree decl = decls; decl; decl = DECL_CHAIN (decl))
5218 : : {
5219 : 314295 : if (DECL_IMPLICIT_TYPEDEF_P (decl)
5220 : 1475297 : && TYPE_NAME (TREE_TYPE (decl)) != decl)
5221 : : /* An anonynmous struct with a typedef name. An odd thing to
5222 : : write. */
5223 : 8 : tree_node (NULL_TREE);
5224 : : else
5225 : 1463656 : tree_node (decl);
5226 : : }
5227 : 301680 : }
5228 : :
5229 : : vec<tree, va_heap> *
5230 : 123316 : trees_in::vec_chained_decls ()
5231 : : {
5232 : 123316 : vec<tree, va_heap> *v = NULL;
5233 : :
5234 : 123316 : if (unsigned len = u ())
5235 : : {
5236 : 64693 : vec_alloc (v, len);
5237 : :
5238 : 757843 : for (unsigned ix = 0; ix < len; ix++)
5239 : : {
5240 : 693150 : tree decl = tree_node ();
5241 : 693150 : if (decl && !DECL_P (decl))
5242 : : {
5243 : 0 : set_overrun ();
5244 : 0 : break;
5245 : : }
5246 : 693150 : v->quick_push (decl);
5247 : : }
5248 : :
5249 : 64693 : if (get_overrun ())
5250 : : {
5251 : 0 : vec_free (v);
5252 : 0 : v = NULL;
5253 : : }
5254 : : }
5255 : :
5256 : 123316 : return v;
5257 : : }
5258 : :
5259 : : /* A vector of trees. */
5260 : :
5261 : : void
5262 : 213364 : trees_out::tree_vec (vec<tree, va_gc> *v)
5263 : : {
5264 : 213364 : unsigned len = vec_safe_length (v);
5265 : 213364 : if (streaming_p ())
5266 : 106662 : u (len);
5267 : 274392 : for (unsigned ix = 0; ix != len; ix++)
5268 : 61028 : tree_node ((*v)[ix]);
5269 : 213364 : }
5270 : :
5271 : : vec<tree, va_gc> *
5272 : 86952 : trees_in::tree_vec ()
5273 : : {
5274 : 86952 : vec<tree, va_gc> *v = NULL;
5275 : 86952 : if (unsigned len = u ())
5276 : : {
5277 : 22488 : vec_alloc (v, len);
5278 : 47291 : for (unsigned ix = 0; ix != len; ix++)
5279 : 24803 : v->quick_push (tree_node ());
5280 : : }
5281 : 86952 : return v;
5282 : : }
5283 : :
5284 : : /* A vector of tree pairs. */
5285 : :
5286 : : void
5287 : 5152 : trees_out::tree_pair_vec (vec<tree_pair_s, va_gc> *v)
5288 : : {
5289 : 5152 : unsigned len = vec_safe_length (v);
5290 : 5152 : if (streaming_p ())
5291 : 2576 : u (len);
5292 : 5152 : if (len)
5293 : 27700 : for (unsigned ix = 0; ix != len; ix++)
5294 : : {
5295 : 22658 : tree_pair_s const &s = (*v)[ix];
5296 : 22658 : tree_node (s.purpose);
5297 : 22658 : tree_node (s.value);
5298 : : }
5299 : 5152 : }
5300 : :
5301 : : vec<tree_pair_s, va_gc> *
5302 : 2431 : trees_in::tree_pair_vec ()
5303 : : {
5304 : 2431 : vec<tree_pair_s, va_gc> *v = NULL;
5305 : 2431 : if (unsigned len = u ())
5306 : : {
5307 : 2381 : vec_alloc (v, len);
5308 : 13344 : for (unsigned ix = 0; ix != len; ix++)
5309 : : {
5310 : 10963 : tree_pair_s s;
5311 : 10963 : s.purpose = tree_node ();
5312 : 10963 : s.value = tree_node ();
5313 : 10963 : v->quick_push (s);
5314 : : }
5315 : : }
5316 : 2431 : return v;
5317 : : }
5318 : :
5319 : : void
5320 : 319694 : trees_out::tree_list (tree list, bool has_purpose)
5321 : : {
5322 : 1343217 : for (; list; list = TREE_CHAIN (list))
5323 : : {
5324 : 1023523 : gcc_checking_assert (TREE_VALUE (list));
5325 : 1023523 : tree_node (TREE_VALUE (list));
5326 : 1023523 : if (has_purpose)
5327 : 989491 : tree_node (TREE_PURPOSE (list));
5328 : : }
5329 : 319694 : tree_node (NULL_TREE);
5330 : 319694 : }
5331 : :
5332 : : tree
5333 : 133115 : trees_in::tree_list (bool has_purpose)
5334 : : {
5335 : 133115 : tree res = NULL_TREE;
5336 : :
5337 : 619350 : for (tree *chain = &res; tree value = tree_node ();
5338 : 972470 : chain = &TREE_CHAIN (*chain))
5339 : : {
5340 : 486235 : tree purpose = has_purpose ? tree_node () : NULL_TREE;
5341 : 486235 : *chain = build_tree_list (purpose, value);
5342 : 486235 : }
5343 : :
5344 : 133115 : return res;
5345 : : }
5346 : :
5347 : : #define CASE_OMP_SIMD_CODE \
5348 : : case OMP_SIMD: \
5349 : : case OMP_STRUCTURED_BLOCK: \
5350 : : case OMP_LOOP: \
5351 : : case OMP_ORDERED: \
5352 : : case OMP_TILE: \
5353 : : case OMP_UNROLL
5354 : : #define CASE_OMP_CODE \
5355 : : case OMP_PARALLEL: \
5356 : : case OMP_TASK: \
5357 : : case OMP_FOR: \
5358 : : case OMP_DISTRIBUTE: \
5359 : : case OMP_TASKLOOP: \
5360 : : case OMP_TEAMS: \
5361 : : case OMP_TARGET_DATA: \
5362 : : case OMP_TARGET: \
5363 : : case OMP_SECTIONS: \
5364 : : case OMP_CRITICAL: \
5365 : : case OMP_SINGLE: \
5366 : : case OMP_SCOPE: \
5367 : : case OMP_TASKGROUP: \
5368 : : case OMP_MASKED: \
5369 : : case OMP_DISPATCH: \
5370 : : case OMP_INTEROP: \
5371 : : case OMP_MASTER: \
5372 : : case OMP_TARGET_UPDATE: \
5373 : : case OMP_TARGET_ENTER_DATA: \
5374 : : case OMP_TARGET_EXIT_DATA: \
5375 : : case OMP_METADIRECTIVE: \
5376 : : case OMP_ATOMIC: \
5377 : : case OMP_ATOMIC_READ: \
5378 : : case OMP_ATOMIC_CAPTURE_OLD: \
5379 : : case OMP_ATOMIC_CAPTURE_NEW
5380 : : #define CASE_OACC_CODE \
5381 : : case OACC_PARALLEL: \
5382 : : case OACC_KERNELS: \
5383 : : case OACC_SERIAL: \
5384 : : case OACC_DATA: \
5385 : : case OACC_HOST_DATA: \
5386 : : case OACC_LOOP: \
5387 : : case OACC_CACHE: \
5388 : : case OACC_DECLARE: \
5389 : : case OACC_ENTER_DATA: \
5390 : : case OACC_EXIT_DATA: \
5391 : : case OACC_UPDATE
5392 : :
5393 : : /* Start tree write. Write information to allocate the receiving
5394 : : node. */
5395 : :
5396 : : void
5397 : 13580458 : trees_out::start (tree t, bool code_streamed)
5398 : : {
5399 : 13580458 : if (TYPE_P (t))
5400 : : {
5401 : 543096 : enum tree_code code = TREE_CODE (t);
5402 : 543096 : gcc_checking_assert (TYPE_MAIN_VARIANT (t) == t);
5403 : : /* All these types are TYPE_NON_COMMON. */
5404 : 543096 : gcc_checking_assert (code == RECORD_TYPE
5405 : : || code == UNION_TYPE
5406 : : || code == ENUMERAL_TYPE
5407 : : || code == TEMPLATE_TYPE_PARM
5408 : : || code == TEMPLATE_TEMPLATE_PARM
5409 : : || code == BOUND_TEMPLATE_TEMPLATE_PARM);
5410 : : }
5411 : :
5412 : 13580458 : if (!code_streamed)
5413 : 13093037 : u (TREE_CODE (t));
5414 : :
5415 : 13580458 : switch (TREE_CODE (t))
5416 : : {
5417 : 12055139 : default:
5418 : 12055139 : if (VL_EXP_CLASS_P (t))
5419 : 506097 : u (VL_EXP_OPERAND_LENGTH (t));
5420 : : break;
5421 : :
5422 : 607130 : case INTEGER_CST:
5423 : 607130 : u (TREE_INT_CST_NUNITS (t));
5424 : 607130 : u (TREE_INT_CST_EXT_NUNITS (t));
5425 : 607130 : break;
5426 : :
5427 : 18 : case OMP_CLAUSE:
5428 : 18 : u (OMP_CLAUSE_CODE (t));
5429 : 18 : break;
5430 : :
5431 : 6 : CASE_OMP_SIMD_CODE:
5432 : 6 : state->extensions |= SE_OPENMP_SIMD;
5433 : 6 : break;
5434 : :
5435 : 9 : CASE_OMP_CODE:
5436 : 9 : state->extensions |= SE_OPENMP;
5437 : 9 : break;
5438 : :
5439 : 6 : CASE_OACC_CODE:
5440 : 6 : state->extensions |= SE_OPENACC;
5441 : 6 : break;
5442 : :
5443 : 44687 : case STRING_CST:
5444 : 44687 : str (TREE_STRING_POINTER (t), TREE_STRING_LENGTH (t));
5445 : 44687 : break;
5446 : :
5447 : 18 : case RAW_DATA_CST:
5448 : 18 : if (RAW_DATA_OWNER (t) == NULL_TREE)
5449 : : {
5450 : : /* Stream RAW_DATA_CST with no owner (i.e. data pointing
5451 : : into libcpp buffers) as something we can stream in as
5452 : : STRING_CST which owns the data. */
5453 : 6 : u (0);
5454 : : /* Can't use str (RAW_DATA_POINTER (t), RAW_DATA_LENGTH (t));
5455 : : here as there isn't a null termination after it. */
5456 : 6 : z (RAW_DATA_LENGTH (t));
5457 : 6 : if (RAW_DATA_LENGTH (t))
5458 : 6 : if (void *ptr = buf (RAW_DATA_LENGTH (t) + 1))
5459 : : {
5460 : 6 : memcpy (ptr, RAW_DATA_POINTER (t), RAW_DATA_LENGTH (t));
5461 : 6 : ((char *) ptr)[RAW_DATA_LENGTH (t)] = '\0';
5462 : : }
5463 : : }
5464 : : else
5465 : : {
5466 : 12 : gcc_assert (RAW_DATA_LENGTH (t));
5467 : 12 : u (RAW_DATA_LENGTH (t));
5468 : : }
5469 : : break;
5470 : :
5471 : 18 : case VECTOR_CST:
5472 : 18 : u (VECTOR_CST_LOG2_NPATTERNS (t));
5473 : 18 : u (VECTOR_CST_NELTS_PER_PATTERN (t));
5474 : 18 : break;
5475 : :
5476 : 104086 : case TREE_BINFO:
5477 : 104086 : u (BINFO_N_BASE_BINFOS (t));
5478 : 104086 : break;
5479 : :
5480 : 769341 : case TREE_VEC:
5481 : 769341 : u (TREE_VEC_LENGTH (t));
5482 : 769341 : break;
5483 : :
5484 : 0 : case FIXED_CST:
5485 : 0 : gcc_unreachable (); /* Not supported in C++. */
5486 : 0 : break;
5487 : :
5488 : 0 : case IDENTIFIER_NODE:
5489 : 0 : case SSA_NAME:
5490 : 0 : case TARGET_MEM_REF:
5491 : 0 : case TRANSLATION_UNIT_DECL:
5492 : : /* We shouldn't meet these. */
5493 : 0 : gcc_unreachable ();
5494 : 13580458 : break;
5495 : : }
5496 : 13580458 : }
5497 : :
5498 : : /* Start tree read. Allocate the receiving node. */
5499 : :
5500 : : tree
5501 : 13304253 : trees_in::start (unsigned code)
5502 : : {
5503 : 13304253 : tree t = NULL_TREE;
5504 : :
5505 : 13304253 : if (!code)
5506 : 12039924 : code = u ();
5507 : :
5508 : 13304253 : switch (code)
5509 : : {
5510 : 11879363 : default:
5511 : 11879363 : if (code >= MAX_TREE_CODES)
5512 : : {
5513 : 0 : fail:
5514 : 0 : set_overrun ();
5515 : 0 : return NULL_TREE;
5516 : : }
5517 : 11879363 : else if (TREE_CODE_CLASS (code) == tcc_vl_exp)
5518 : : {
5519 : 507420 : unsigned ops = u ();
5520 : 507420 : t = build_vl_exp (tree_code (code), ops);
5521 : : }
5522 : : else
5523 : 11371943 : t = make_node (tree_code (code));
5524 : : break;
5525 : :
5526 : 552093 : case INTEGER_CST:
5527 : 552093 : {
5528 : 552093 : unsigned n = u ();
5529 : 552093 : unsigned e = u ();
5530 : 552093 : t = make_int_cst (n, e);
5531 : : }
5532 : 552093 : break;
5533 : :
5534 : 18 : case OMP_CLAUSE:
5535 : 18 : t = build_omp_clause (UNKNOWN_LOCATION, omp_clause_code (u ()));
5536 : 18 : break;
5537 : :
5538 : 9 : CASE_OMP_SIMD_CODE:
5539 : 9 : if (!(state->extensions & SE_OPENMP_SIMD))
5540 : 0 : goto fail;
5541 : 9 : t = make_node (tree_code (code));
5542 : 9 : break;
5543 : :
5544 : 9 : CASE_OMP_CODE:
5545 : 9 : if (!(state->extensions & SE_OPENMP))
5546 : 0 : goto fail;
5547 : 9 : t = make_node (tree_code (code));
5548 : 9 : break;
5549 : :
5550 : 6 : CASE_OACC_CODE:
5551 : 6 : if (!(state->extensions & SE_OPENACC))
5552 : 0 : goto fail;
5553 : 6 : t = make_node (tree_code (code));
5554 : 6 : break;
5555 : :
5556 : 46709 : case STRING_CST:
5557 : 46709 : {
5558 : 46709 : size_t l;
5559 : 46709 : const char *chars = str (&l);
5560 : 46709 : t = build_string (l, chars);
5561 : : }
5562 : 46709 : break;
5563 : :
5564 : 9 : case RAW_DATA_CST:
5565 : 9 : {
5566 : 9 : size_t l = u ();
5567 : 9 : if (l == 0)
5568 : : {
5569 : : /* Stream in RAW_DATA_CST with no owner as STRING_CST
5570 : : which owns the data. */
5571 : 3 : const char *chars = str (&l);
5572 : 3 : t = build_string (l, chars);
5573 : : }
5574 : : else
5575 : : {
5576 : 6 : t = make_node (RAW_DATA_CST);
5577 : 6 : RAW_DATA_LENGTH (t) = l;
5578 : : }
5579 : : }
5580 : 9 : break;
5581 : :
5582 : 24 : case VECTOR_CST:
5583 : 24 : {
5584 : 24 : unsigned log2_npats = u ();
5585 : 24 : unsigned elts_per = u ();
5586 : 24 : t = make_vector (log2_npats, elts_per);
5587 : : }
5588 : 24 : break;
5589 : :
5590 : 84521 : case TREE_BINFO:
5591 : 84521 : t = make_tree_binfo (u ());
5592 : 84521 : break;
5593 : :
5594 : 741492 : case TREE_VEC:
5595 : 741492 : t = make_tree_vec (u ());
5596 : 741492 : break;
5597 : :
5598 : 0 : case FIXED_CST:
5599 : 0 : case IDENTIFIER_NODE:
5600 : 0 : case SSA_NAME:
5601 : 0 : case TARGET_MEM_REF:
5602 : 0 : case TRANSLATION_UNIT_DECL:
5603 : 0 : goto fail;
5604 : : }
5605 : :
5606 : : return t;
5607 : : }
5608 : :
5609 : : /* The kinds of interface an importer could have for a decl. */
5610 : :
5611 : : enum class importer_interface {
5612 : : unknown, /* The definition may or may not need to be emitted. */
5613 : : external, /* The definition can always be found in another TU. */
5614 : : internal, /* The definition should be emitted in the importer's TU. */
5615 : : always_emit, /* The definition must be emitted in the importer's TU,
5616 : : regardless of if it's used or not. */
5617 : : };
5618 : :
5619 : : /* Returns what kind of interface an importer will have of DECL. */
5620 : :
5621 : : static importer_interface
5622 : 515369 : get_importer_interface (tree decl)
5623 : : {
5624 : : /* Internal linkage entities must be emitted in each importer if
5625 : : there is a definition available. */
5626 : 515369 : if (!TREE_PUBLIC (decl))
5627 : : return importer_interface::internal;
5628 : :
5629 : : /* Other entities that aren't vague linkage are either not definitions
5630 : : or will be publicly emitted in this TU, so importers can just refer
5631 : : to an external definition. */
5632 : 265026 : if (!vague_linkage_p (decl))
5633 : : return importer_interface::external;
5634 : :
5635 : : /* For explicit instantiations, importers can always rely on there
5636 : : being a definition in another TU, unless this is a definition
5637 : : in a header module: in which case the importer will always need
5638 : : to emit it. */
5639 : 259694 : if (DECL_LANG_SPECIFIC (decl)
5640 : 259694 : && DECL_EXPLICIT_INSTANTIATION (decl))
5641 : 19895 : return (header_module_p () && !DECL_EXTERNAL (decl)
5642 : 19895 : ? importer_interface::always_emit
5643 : : : importer_interface::external);
5644 : :
5645 : : /* A gnu_inline function is never emitted in any TU. */
5646 : 239799 : if (TREE_CODE (decl) == FUNCTION_DECL
5647 : 166208 : && DECL_DECLARED_INLINE_P (decl)
5648 : 400569 : && lookup_attribute ("gnu_inline", DECL_ATTRIBUTES (decl)))
5649 : : return importer_interface::external;
5650 : :
5651 : : /* Everything else has vague linkage. */
5652 : : return importer_interface::unknown;
5653 : : }
5654 : :
5655 : : /* The structure streamers access the raw fields, because the
5656 : : alternative, of using the accessor macros can require using
5657 : : different accessors for the same underlying field, depending on the
5658 : : tree code. That's both confusing and annoying. */
5659 : :
5660 : : /* Read & write the core boolean flags. */
5661 : :
5662 : : void
5663 : 13602339 : trees_out::core_bools (tree t, bits_out& bits)
5664 : : {
5665 : : #define WB(X) (bits.b (X))
5666 : : /* Stream X if COND holds, and if !COND stream a dummy value so that the
5667 : : overall number of bits streamed is independent of the runtime value
5668 : : of COND, which allows the compiler to better optimize this function. */
5669 : : #define WB_IF(COND, X) WB ((COND) ? (X) : false)
5670 : 13602339 : tree_code code = TREE_CODE (t);
5671 : :
5672 : 13602339 : WB (t->base.side_effects_flag);
5673 : 13602339 : WB (t->base.constant_flag);
5674 : 13602339 : WB (t->base.addressable_flag);
5675 : 13602339 : WB (t->base.volatile_flag);
5676 : 13602339 : WB (t->base.readonly_flag);
5677 : : /* base.asm_written_flag is a property of the current TU's use of
5678 : : this decl. */
5679 : 13602339 : WB (t->base.nowarning_flag);
5680 : : /* base.visited read as zero (it's set for writer, because that's
5681 : : how we mark nodes). */
5682 : : /* base.used_flag is not streamed. Readers may set TREE_USED of
5683 : : decls they use. */
5684 : 13602339 : WB (t->base.nothrow_flag);
5685 : 13602339 : WB (t->base.static_flag);
5686 : : /* This is TYPE_CACHED_VALUES_P for types. */
5687 : 13602339 : WB_IF (TREE_CODE_CLASS (code) != tcc_type, t->base.public_flag);
5688 : 13602339 : WB (t->base.private_flag);
5689 : 13602339 : WB (t->base.protected_flag);
5690 : 13602339 : WB (t->base.deprecated_flag);
5691 : 13602339 : WB (t->base.default_def_flag);
5692 : :
5693 : 13602339 : switch (code)
5694 : : {
5695 : : case CALL_EXPR:
5696 : : case INTEGER_CST:
5697 : : case SSA_NAME:
5698 : : case TARGET_MEM_REF:
5699 : : case TREE_VEC:
5700 : : /* These use different base.u fields. */
5701 : : return;
5702 : :
5703 : 11731807 : default:
5704 : 11731807 : WB (t->base.u.bits.lang_flag_0);
5705 : 11731807 : bool flag_1 = t->base.u.bits.lang_flag_1;
5706 : 11731807 : if (!flag_1)
5707 : : ;
5708 : 374462 : else if (code == TEMPLATE_INFO)
5709 : : /* This is TI_PENDING_TEMPLATE_FLAG, not relevant to reader. */
5710 : : flag_1 = false;
5711 : 370325 : else if (code == VAR_DECL)
5712 : : {
5713 : : /* This is DECL_INITIALIZED_P. */
5714 : 74043 : if (TREE_CODE (DECL_CONTEXT (t)) != FUNCTION_DECL)
5715 : : /* We'll set this when reading the definition. */
5716 : 11731807 : flag_1 = false;
5717 : : }
5718 : 11731807 : WB (flag_1);
5719 : 11731807 : WB (t->base.u.bits.lang_flag_2);
5720 : 11731807 : WB (t->base.u.bits.lang_flag_3);
5721 : 11731807 : WB (t->base.u.bits.lang_flag_4);
5722 : 11731807 : WB (t->base.u.bits.lang_flag_5);
5723 : 11731807 : WB (t->base.u.bits.lang_flag_6);
5724 : 11731807 : WB (t->base.u.bits.saturating_flag);
5725 : 11731807 : WB (t->base.u.bits.unsigned_flag);
5726 : 11731807 : WB (t->base.u.bits.packed_flag);
5727 : 11731807 : WB (t->base.u.bits.user_align);
5728 : 11731807 : WB (t->base.u.bits.nameless_flag);
5729 : 11731807 : WB (t->base.u.bits.atomic_flag);
5730 : 11731807 : WB (t->base.u.bits.unavailable_flag);
5731 : 11731807 : break;
5732 : : }
5733 : :
5734 : 11731807 : if (TREE_CODE_CLASS (code) == tcc_type)
5735 : : {
5736 : 564977 : WB (t->type_common.no_force_blk_flag);
5737 : 564977 : WB (t->type_common.needs_constructing_flag);
5738 : 564977 : WB (t->type_common.transparent_aggr_flag);
5739 : 564977 : WB (t->type_common.restrict_flag);
5740 : 564977 : WB (t->type_common.string_flag);
5741 : 564977 : WB (t->type_common.lang_flag_0);
5742 : 564977 : WB (t->type_common.lang_flag_1);
5743 : 564977 : WB (t->type_common.lang_flag_2);
5744 : 564977 : WB (t->type_common.lang_flag_3);
5745 : 564977 : WB (t->type_common.lang_flag_4);
5746 : 564977 : WB (t->type_common.lang_flag_5);
5747 : 564977 : WB (t->type_common.lang_flag_6);
5748 : 564977 : WB (t->type_common.typeless_storage);
5749 : : }
5750 : :
5751 : 11731807 : if (TREE_CODE_CLASS (code) != tcc_declaration)
5752 : : return;
5753 : :
5754 : 3027317 : if (CODE_CONTAINS_STRUCT (code, TS_DECL_COMMON))
5755 : : {
5756 : 3027317 : WB (t->decl_common.nonlocal_flag);
5757 : 3027317 : WB (t->decl_common.virtual_flag);
5758 : 3027317 : WB (t->decl_common.ignored_flag);
5759 : 3027317 : WB (t->decl_common.abstract_flag);
5760 : 3027317 : WB (t->decl_common.artificial_flag);
5761 : 3027317 : WB (t->decl_common.preserve_flag);
5762 : 3027317 : WB (t->decl_common.debug_expr_is_from);
5763 : 3027317 : WB (t->decl_common.lang_flag_0);
5764 : 3027317 : WB (t->decl_common.lang_flag_1);
5765 : 3027317 : WB (t->decl_common.lang_flag_2);
5766 : 3027317 : WB (t->decl_common.lang_flag_3);
5767 : 3027317 : WB (t->decl_common.lang_flag_4);
5768 : :
5769 : 3027317 : {
5770 : : /* This is DECL_INTERFACE_KNOWN: We should redetermine whether
5771 : : we need to import or export any vague-linkage entities on
5772 : : stream-in. */
5773 : 3027317 : bool interface_known = t->decl_common.lang_flag_5;
5774 : 3027317 : if (interface_known
5775 : 3027317 : && get_importer_interface (t) == importer_interface::unknown)
5776 : : interface_known = false;
5777 : 3027317 : WB (interface_known);
5778 : : }
5779 : :
5780 : 3027317 : WB (t->decl_common.lang_flag_6);
5781 : 3027317 : WB (t->decl_common.lang_flag_7);
5782 : 3027317 : WB (t->decl_common.lang_flag_8);
5783 : 3027317 : WB (t->decl_common.decl_flag_0);
5784 : :
5785 : 3027317 : {
5786 : : /* DECL_EXTERNAL -> decl_flag_1
5787 : : == it is defined elsewhere
5788 : : DECL_NOT_REALLY_EXTERN -> base.not_really_extern
5789 : : == that was a lie, it is here */
5790 : :
5791 : 3027317 : bool is_external = t->decl_common.decl_flag_1;
5792 : : /* maybe_emit_vtables relies on vtables being marked as
5793 : : DECL_EXTERNAL and DECL_NOT_REALLY_EXTERN before processing. */
5794 : 3027317 : if (!is_external && VAR_P (t) && DECL_VTABLE_OR_VTT_P (t))
5795 : : is_external = true;
5796 : : /* Things we emit here might well be external from the POV of an
5797 : : importer. */
5798 : 3027110 : if (!is_external
5799 : 2541474 : && VAR_OR_FUNCTION_DECL_P (t)
5800 : 3254009 : && get_importer_interface (t) == importer_interface::external)
5801 : : is_external = true;
5802 : 3027317 : WB (is_external);
5803 : : }
5804 : :
5805 : 3027317 : WB (t->decl_common.decl_flag_2);
5806 : 3027317 : WB (t->decl_common.decl_flag_3);
5807 : 3027317 : WB (t->decl_common.not_gimple_reg_flag);
5808 : 3027317 : WB (t->decl_common.decl_by_reference_flag);
5809 : 3027317 : WB (t->decl_common.decl_read_flag);
5810 : 3027317 : WB (t->decl_common.decl_nonshareable_flag);
5811 : 3027317 : WB (t->decl_common.decl_not_flexarray);
5812 : : }
5813 : : else
5814 : : return;
5815 : :
5816 : 3027317 : if (CODE_CONTAINS_STRUCT (code, TS_DECL_WITH_VIS))
5817 : : {
5818 : 1435481 : WB (t->decl_with_vis.defer_output);
5819 : 1435481 : WB (t->decl_with_vis.hard_register);
5820 : 1435481 : WB (t->decl_with_vis.common_flag);
5821 : 1435481 : WB (t->decl_with_vis.in_text_section);
5822 : 1435481 : WB (t->decl_with_vis.in_constant_pool);
5823 : 1435481 : WB (t->decl_with_vis.dllimport_flag);
5824 : 1435481 : WB (t->decl_with_vis.weak_flag);
5825 : 1435481 : WB (t->decl_with_vis.seen_in_bind_expr);
5826 : 1435481 : WB (t->decl_with_vis.comdat_flag);
5827 : 1435481 : WB (t->decl_with_vis.visibility_specified);
5828 : 1435481 : WB (t->decl_with_vis.init_priority_p);
5829 : 1435481 : WB (t->decl_with_vis.shadowed_for_var_p);
5830 : 1435481 : WB (t->decl_with_vis.cxx_constructor);
5831 : 1435481 : WB (t->decl_with_vis.cxx_destructor);
5832 : 1435481 : WB (t->decl_with_vis.final);
5833 : 1435481 : WB (t->decl_with_vis.regdecl_flag);
5834 : : }
5835 : : else
5836 : : return;
5837 : :
5838 : 1435481 : if (CODE_CONTAINS_STRUCT (code, TS_FUNCTION_DECL))
5839 : : {
5840 : 416623 : WB (t->function_decl.static_ctor_flag);
5841 : 416623 : WB (t->function_decl.static_dtor_flag);
5842 : 416623 : WB (t->function_decl.uninlinable);
5843 : 416623 : WB (t->function_decl.possibly_inlined);
5844 : 416623 : WB (t->function_decl.novops_flag);
5845 : 416623 : WB (t->function_decl.returns_twice_flag);
5846 : 416623 : WB (t->function_decl.malloc_flag);
5847 : 416623 : WB (t->function_decl.declared_inline_flag);
5848 : 416623 : WB (t->function_decl.no_inline_warning_flag);
5849 : 416623 : WB (t->function_decl.no_instrument_function_entry_exit);
5850 : 416623 : WB (t->function_decl.no_limit_stack);
5851 : 416623 : WB (t->function_decl.disregard_inline_limits);
5852 : 416623 : WB (t->function_decl.pure_flag);
5853 : 416623 : WB (t->function_decl.looping_const_or_pure_flag);
5854 : :
5855 : 416623 : WB (t->function_decl.has_debug_args_flag);
5856 : 416623 : WB (t->function_decl.versioned_function);
5857 : 416623 : WB (t->function_decl.replaceable_operator);
5858 : :
5859 : : /* decl_type is a (misnamed) 2 bit discriminator. */
5860 : 416623 : unsigned kind = (unsigned)t->function_decl.decl_type;
5861 : 416623 : WB ((kind >> 0) & 1);
5862 : 416623 : WB ((kind >> 1) & 1);
5863 : : }
5864 : : #undef WB_IF
5865 : : #undef WB
5866 : : }
5867 : :
5868 : : bool
5869 : 13323138 : trees_in::core_bools (tree t, bits_in& bits)
5870 : : {
5871 : : #define RB(X) ((X) = bits.b ())
5872 : : /* See the comment for WB_IF in trees_out::core_bools. */
5873 : : #define RB_IF(COND, X) ((COND) ? RB (X) : bits.b ())
5874 : :
5875 : 13323138 : tree_code code = TREE_CODE (t);
5876 : :
5877 : 13323138 : RB (t->base.side_effects_flag);
5878 : 13323138 : RB (t->base.constant_flag);
5879 : 13323138 : RB (t->base.addressable_flag);
5880 : 13323138 : RB (t->base.volatile_flag);
5881 : 13323138 : RB (t->base.readonly_flag);
5882 : : /* base.asm_written_flag is not streamed. */
5883 : 13323138 : RB (t->base.nowarning_flag);
5884 : : /* base.visited is not streamed. */
5885 : : /* base.used_flag is not streamed. */
5886 : 13323138 : RB (t->base.nothrow_flag);
5887 : 13323138 : RB (t->base.static_flag);
5888 : 13323138 : RB_IF (TREE_CODE_CLASS (code) != tcc_type, t->base.public_flag);
5889 : 13323138 : RB (t->base.private_flag);
5890 : 13323138 : RB (t->base.protected_flag);
5891 : 13323138 : RB (t->base.deprecated_flag);
5892 : 13323138 : RB (t->base.default_def_flag);
5893 : :
5894 : 13323138 : switch (code)
5895 : : {
5896 : 1789264 : case CALL_EXPR:
5897 : 1789264 : case INTEGER_CST:
5898 : 1789264 : case SSA_NAME:
5899 : 1789264 : case TARGET_MEM_REF:
5900 : 1789264 : case TREE_VEC:
5901 : : /* These use different base.u fields. */
5902 : 1789264 : goto done;
5903 : :
5904 : 11533874 : default:
5905 : 11533874 : RB (t->base.u.bits.lang_flag_0);
5906 : 11533874 : RB (t->base.u.bits.lang_flag_1);
5907 : 11533874 : RB (t->base.u.bits.lang_flag_2);
5908 : 11533874 : RB (t->base.u.bits.lang_flag_3);
5909 : 11533874 : RB (t->base.u.bits.lang_flag_4);
5910 : 11533874 : RB (t->base.u.bits.lang_flag_5);
5911 : 11533874 : RB (t->base.u.bits.lang_flag_6);
5912 : 11533874 : RB (t->base.u.bits.saturating_flag);
5913 : 11533874 : RB (t->base.u.bits.unsigned_flag);
5914 : 11533874 : RB (t->base.u.bits.packed_flag);
5915 : 11533874 : RB (t->base.u.bits.user_align);
5916 : 11533874 : RB (t->base.u.bits.nameless_flag);
5917 : 11533874 : RB (t->base.u.bits.atomic_flag);
5918 : 11533874 : RB (t->base.u.bits.unavailable_flag);
5919 : 11533874 : break;
5920 : : }
5921 : :
5922 : 11533874 : if (TREE_CODE_CLASS (code) == tcc_type)
5923 : : {
5924 : 507195 : RB (t->type_common.no_force_blk_flag);
5925 : 507195 : RB (t->type_common.needs_constructing_flag);
5926 : 507195 : RB (t->type_common.transparent_aggr_flag);
5927 : 507195 : RB (t->type_common.restrict_flag);
5928 : 507195 : RB (t->type_common.string_flag);
5929 : 507195 : RB (t->type_common.lang_flag_0);
5930 : 507195 : RB (t->type_common.lang_flag_1);
5931 : 507195 : RB (t->type_common.lang_flag_2);
5932 : 507195 : RB (t->type_common.lang_flag_3);
5933 : 507195 : RB (t->type_common.lang_flag_4);
5934 : 507195 : RB (t->type_common.lang_flag_5);
5935 : 507195 : RB (t->type_common.lang_flag_6);
5936 : 507195 : RB (t->type_common.typeless_storage);
5937 : : }
5938 : :
5939 : 11533874 : if (TREE_CODE_CLASS (code) != tcc_declaration)
5940 : 8656653 : goto done;
5941 : :
5942 : 2877221 : if (CODE_CONTAINS_STRUCT (code, TS_DECL_COMMON))
5943 : : {
5944 : 2877221 : RB (t->decl_common.nonlocal_flag);
5945 : 2877221 : RB (t->decl_common.virtual_flag);
5946 : 2877221 : RB (t->decl_common.ignored_flag);
5947 : 2877221 : RB (t->decl_common.abstract_flag);
5948 : 2877221 : RB (t->decl_common.artificial_flag);
5949 : 2877221 : RB (t->decl_common.preserve_flag);
5950 : 2877221 : RB (t->decl_common.debug_expr_is_from);
5951 : 2877221 : RB (t->decl_common.lang_flag_0);
5952 : 2877221 : RB (t->decl_common.lang_flag_1);
5953 : 2877221 : RB (t->decl_common.lang_flag_2);
5954 : 2877221 : RB (t->decl_common.lang_flag_3);
5955 : 2877221 : RB (t->decl_common.lang_flag_4);
5956 : 2877221 : RB (t->decl_common.lang_flag_5);
5957 : 2877221 : RB (t->decl_common.lang_flag_6);
5958 : 2877221 : RB (t->decl_common.lang_flag_7);
5959 : 2877221 : RB (t->decl_common.lang_flag_8);
5960 : 2877221 : RB (t->decl_common.decl_flag_0);
5961 : 2877221 : RB (t->decl_common.decl_flag_1);
5962 : 2877221 : RB (t->decl_common.decl_flag_2);
5963 : 2877221 : RB (t->decl_common.decl_flag_3);
5964 : 2877221 : RB (t->decl_common.not_gimple_reg_flag);
5965 : 2877221 : RB (t->decl_common.decl_by_reference_flag);
5966 : 2877221 : RB (t->decl_common.decl_read_flag);
5967 : 2877221 : RB (t->decl_common.decl_nonshareable_flag);
5968 : 2877221 : RB (t->decl_common.decl_not_flexarray);
5969 : : }
5970 : : else
5971 : 0 : goto done;
5972 : :
5973 : 2877221 : if (CODE_CONTAINS_STRUCT (code, TS_DECL_WITH_VIS))
5974 : : {
5975 : 1332190 : RB (t->decl_with_vis.defer_output);
5976 : 1332190 : RB (t->decl_with_vis.hard_register);
5977 : 1332190 : RB (t->decl_with_vis.common_flag);
5978 : 1332190 : RB (t->decl_with_vis.in_text_section);
5979 : 1332190 : RB (t->decl_with_vis.in_constant_pool);
5980 : 1332190 : RB (t->decl_with_vis.dllimport_flag);
5981 : 1332190 : RB (t->decl_with_vis.weak_flag);
5982 : 1332190 : RB (t->decl_with_vis.seen_in_bind_expr);
5983 : 1332190 : RB (t->decl_with_vis.comdat_flag);
5984 : 1332190 : RB (t->decl_with_vis.visibility_specified);
5985 : 1332190 : RB (t->decl_with_vis.init_priority_p);
5986 : 1332190 : RB (t->decl_with_vis.shadowed_for_var_p);
5987 : 1332190 : RB (t->decl_with_vis.cxx_constructor);
5988 : 1332190 : RB (t->decl_with_vis.cxx_destructor);
5989 : 1332190 : RB (t->decl_with_vis.final);
5990 : 1332190 : RB (t->decl_with_vis.regdecl_flag);
5991 : : }
5992 : : else
5993 : 1545031 : goto done;
5994 : :
5995 : 1332190 : if (CODE_CONTAINS_STRUCT (code, TS_FUNCTION_DECL))
5996 : : {
5997 : 406715 : RB (t->function_decl.static_ctor_flag);
5998 : 406715 : RB (t->function_decl.static_dtor_flag);
5999 : 406715 : RB (t->function_decl.uninlinable);
6000 : 406715 : RB (t->function_decl.possibly_inlined);
6001 : 406715 : RB (t->function_decl.novops_flag);
6002 : 406715 : RB (t->function_decl.returns_twice_flag);
6003 : 406715 : RB (t->function_decl.malloc_flag);
6004 : 406715 : RB (t->function_decl.declared_inline_flag);
6005 : 406715 : RB (t->function_decl.no_inline_warning_flag);
6006 : 406715 : RB (t->function_decl.no_instrument_function_entry_exit);
6007 : 406715 : RB (t->function_decl.no_limit_stack);
6008 : 406715 : RB (t->function_decl.disregard_inline_limits);
6009 : 406715 : RB (t->function_decl.pure_flag);
6010 : 406715 : RB (t->function_decl.looping_const_or_pure_flag);
6011 : :
6012 : 406715 : RB (t->function_decl.has_debug_args_flag);
6013 : 406715 : RB (t->function_decl.versioned_function);
6014 : 406715 : RB (t->function_decl.replaceable_operator);
6015 : :
6016 : : /* decl_type is a (misnamed) 2 bit discriminator. */
6017 : 406715 : unsigned kind = 0;
6018 : 406715 : kind |= unsigned (bits.b ()) << 0;
6019 : 406715 : kind |= unsigned (bits.b ()) << 1;
6020 : 406715 : t->function_decl.decl_type = function_decl_type (kind);
6021 : : }
6022 : : #undef RB_IF
6023 : : #undef RB
6024 : 925475 : done:
6025 : 13323138 : return !get_overrun ();
6026 : : }
6027 : :
6028 : : void
6029 : 1939160 : trees_out::lang_decl_bools (tree t, bits_out& bits)
6030 : : {
6031 : : #define WB(X) (bits.b (X))
6032 : 1939160 : const struct lang_decl *lang = DECL_LANG_SPECIFIC (t);
6033 : :
6034 : 1939160 : bits.bflush ();
6035 : 1939160 : WB (lang->u.base.language == lang_cplusplus);
6036 : 1939160 : WB ((lang->u.base.use_template >> 0) & 1);
6037 : 1939160 : WB ((lang->u.base.use_template >> 1) & 1);
6038 : : /* Do not write lang->u.base.not_really_extern, importer will set
6039 : : when reading the definition (if any). */
6040 : 1939160 : WB (lang->u.base.initialized_in_class);
6041 : 1939160 : WB (lang->u.base.threadprivate_or_deleted_p);
6042 : : /* Do not write lang->u.base.anticipated_p, it is a property of the
6043 : : current TU. */
6044 : 1939160 : WB (lang->u.base.friend_or_tls);
6045 : 1939160 : WB (lang->u.base.unknown_bound_p);
6046 : : /* Do not write lang->u.base.odr_used, importer will recalculate if
6047 : : they do ODR use this decl. */
6048 : 1939160 : WB (lang->u.base.concept_p);
6049 : 1939160 : WB (lang->u.base.var_declared_inline_p);
6050 : 1939160 : WB (lang->u.base.dependent_init_p);
6051 : : /* When building a header unit, everthing is marked as purview, (so
6052 : : we know which decls to write). But when we import them we do not
6053 : : want to mark them as in module purview. */
6054 : 3800411 : WB (lang->u.base.module_purview_p && !header_module_p ());
6055 : 1939160 : WB (lang->u.base.module_attach_p);
6056 : 1939160 : WB (lang->u.base.module_keyed_decls_p);
6057 : 1939160 : switch (lang->u.base.selector)
6058 : : {
6059 : 0 : default:
6060 : 0 : gcc_unreachable ();
6061 : :
6062 : 416623 : case lds_fn: /* lang_decl_fn. */
6063 : 416623 : WB (lang->u.fn.global_ctor_p);
6064 : 416623 : WB (lang->u.fn.global_dtor_p);
6065 : 416623 : WB (lang->u.fn.static_function);
6066 : 416623 : WB (lang->u.fn.pure_virtual);
6067 : 416623 : WB (lang->u.fn.defaulted_p);
6068 : 416623 : WB (lang->u.fn.has_in_charge_parm_p);
6069 : 416623 : WB (lang->u.fn.has_vtt_parm_p);
6070 : : /* There shouldn't be a pending inline at this point. */
6071 : 416623 : gcc_assert (!lang->u.fn.pending_inline_p);
6072 : 416623 : WB (lang->u.fn.nonconverting);
6073 : 416623 : WB (lang->u.fn.thunk_p);
6074 : 416623 : WB (lang->u.fn.this_thunk_p);
6075 : : /* Do not stream lang->u.hidden_friend_p, it is a property of
6076 : : the TU. */
6077 : 416623 : WB (lang->u.fn.omp_declare_reduction_p);
6078 : 416623 : WB (lang->u.fn.has_dependent_explicit_spec_p);
6079 : 416623 : WB (lang->u.fn.immediate_fn_p);
6080 : 416623 : WB (lang->u.fn.maybe_deleted);
6081 : 416623 : WB (lang->u.fn.implicit_constexpr);
6082 : 416623 : WB (lang->u.fn.escalated_p);
6083 : 416623 : WB (lang->u.fn.xobj_func);
6084 : 416623 : goto lds_min;
6085 : :
6086 : 1528 : case lds_decomp: /* lang_decl_decomp. */
6087 : : /* No bools. */
6088 : 1528 : goto lds_min;
6089 : :
6090 : : case lds_min: /* lang_decl_min. */
6091 : 1939160 : lds_min:
6092 : : /* No bools. */
6093 : : break;
6094 : :
6095 : : case lds_ns: /* lang_decl_ns. */
6096 : : /* No bools. */
6097 : : break;
6098 : :
6099 : : case lds_parm: /* lang_decl_parm. */
6100 : : /* No bools. */
6101 : : break;
6102 : : }
6103 : : #undef WB
6104 : 1939160 : }
6105 : :
6106 : : bool
6107 : 1832134 : trees_in::lang_decl_bools (tree t, bits_in& bits)
6108 : : {
6109 : : #define RB(X) ((X) = bits.b ())
6110 : 1832134 : struct lang_decl *lang = DECL_LANG_SPECIFIC (t);
6111 : :
6112 : 1832134 : bits.bflush ();
6113 : 1832134 : lang->u.base.language = bits.b () ? lang_cplusplus : lang_c;
6114 : 1832134 : unsigned v;
6115 : 1832134 : v = bits.b () << 0;
6116 : 1832134 : v |= bits.b () << 1;
6117 : 1832134 : lang->u.base.use_template = v;
6118 : : /* lang->u.base.not_really_extern is not streamed. */
6119 : 1832134 : RB (lang->u.base.initialized_in_class);
6120 : 1832134 : RB (lang->u.base.threadprivate_or_deleted_p);
6121 : : /* lang->u.base.anticipated_p is not streamed. */
6122 : 1832134 : RB (lang->u.base.friend_or_tls);
6123 : 1832134 : RB (lang->u.base.unknown_bound_p);
6124 : : /* lang->u.base.odr_used is not streamed. */
6125 : 1832134 : RB (lang->u.base.concept_p);
6126 : 1832134 : RB (lang->u.base.var_declared_inline_p);
6127 : 1832134 : RB (lang->u.base.dependent_init_p);
6128 : 1832134 : RB (lang->u.base.module_purview_p);
6129 : 1832134 : RB (lang->u.base.module_attach_p);
6130 : 1832134 : RB (lang->u.base.module_keyed_decls_p);
6131 : 1832134 : switch (lang->u.base.selector)
6132 : : {
6133 : 0 : default:
6134 : 0 : gcc_unreachable ();
6135 : :
6136 : 406715 : case lds_fn: /* lang_decl_fn. */
6137 : 406715 : RB (lang->u.fn.global_ctor_p);
6138 : 406715 : RB (lang->u.fn.global_dtor_p);
6139 : 406715 : RB (lang->u.fn.static_function);
6140 : 406715 : RB (lang->u.fn.pure_virtual);
6141 : 406715 : RB (lang->u.fn.defaulted_p);
6142 : 406715 : RB (lang->u.fn.has_in_charge_parm_p);
6143 : 406715 : RB (lang->u.fn.has_vtt_parm_p);
6144 : 406715 : RB (lang->u.fn.nonconverting);
6145 : 406715 : RB (lang->u.fn.thunk_p);
6146 : 406715 : RB (lang->u.fn.this_thunk_p);
6147 : : /* lang->u.fn.hidden_friend_p is not streamed. */
6148 : 406715 : RB (lang->u.fn.omp_declare_reduction_p);
6149 : 406715 : RB (lang->u.fn.has_dependent_explicit_spec_p);
6150 : 406715 : RB (lang->u.fn.immediate_fn_p);
6151 : 406715 : RB (lang->u.fn.maybe_deleted);
6152 : 406715 : RB (lang->u.fn.implicit_constexpr);
6153 : 406715 : RB (lang->u.fn.escalated_p);
6154 : 406715 : RB (lang->u.fn.xobj_func);
6155 : 406715 : goto lds_min;
6156 : :
6157 : 1729 : case lds_decomp: /* lang_decl_decomp. */
6158 : : /* No bools. */
6159 : 1729 : goto lds_min;
6160 : :
6161 : : case lds_min: /* lang_decl_min. */
6162 : 1832134 : lds_min:
6163 : : /* No bools. */
6164 : : break;
6165 : :
6166 : : case lds_ns: /* lang_decl_ns. */
6167 : : /* No bools. */
6168 : : break;
6169 : :
6170 : : case lds_parm: /* lang_decl_parm. */
6171 : : /* No bools. */
6172 : : break;
6173 : : }
6174 : : #undef RB
6175 : 1832134 : return !get_overrun ();
6176 : : }
6177 : :
6178 : : void
6179 : 155135 : trees_out::lang_type_bools (tree t, bits_out& bits)
6180 : : {
6181 : : #define WB(X) (bits.b (X))
6182 : 155135 : const struct lang_type *lang = TYPE_LANG_SPECIFIC (t);
6183 : :
6184 : 155135 : bits.bflush ();
6185 : 155135 : WB (lang->has_type_conversion);
6186 : 155135 : WB (lang->has_copy_ctor);
6187 : 155135 : WB (lang->has_default_ctor);
6188 : 155135 : WB (lang->const_needs_init);
6189 : 155135 : WB (lang->ref_needs_init);
6190 : 155135 : WB (lang->has_const_copy_assign);
6191 : 155135 : WB ((lang->use_template >> 0) & 1);
6192 : 155135 : WB ((lang->use_template >> 1) & 1);
6193 : :
6194 : 155135 : WB (lang->has_mutable);
6195 : 155135 : WB (lang->com_interface);
6196 : 155135 : WB (lang->non_pod_class);
6197 : 155135 : WB (lang->nearly_empty_p);
6198 : 155135 : WB (lang->user_align);
6199 : 155135 : WB (lang->has_copy_assign);
6200 : 155135 : WB (lang->has_new);
6201 : 155135 : WB (lang->has_array_new);
6202 : :
6203 : 155135 : WB ((lang->gets_delete >> 0) & 1);
6204 : 155135 : WB ((lang->gets_delete >> 1) & 1);
6205 : 155135 : WB (lang->interface_only);
6206 : 155135 : WB (lang->interface_unknown);
6207 : 155135 : WB (lang->contains_empty_class_p);
6208 : 155135 : WB (lang->anon_aggr);
6209 : 155135 : WB (lang->non_zero_init);
6210 : 155135 : WB (lang->empty_p);
6211 : :
6212 : 155135 : WB (lang->vec_new_uses_cookie);
6213 : 155135 : WB (lang->declared_class);
6214 : 155135 : WB (lang->diamond_shaped);
6215 : 155135 : WB (lang->repeated_base);
6216 : 155135 : gcc_checking_assert (!lang->being_defined);
6217 : : // lang->debug_requested
6218 : 155135 : WB (lang->fields_readonly);
6219 : 155135 : WB (lang->ptrmemfunc_flag);
6220 : :
6221 : 155135 : WB (lang->lazy_default_ctor);
6222 : 155135 : WB (lang->lazy_copy_ctor);
6223 : 155135 : WB (lang->lazy_copy_assign);
6224 : 155135 : WB (lang->lazy_destructor);
6225 : 155135 : WB (lang->has_const_copy_ctor);
6226 : 155135 : WB (lang->has_complex_copy_ctor);
6227 : 155135 : WB (lang->has_complex_copy_assign);
6228 : 155135 : WB (lang->non_aggregate);
6229 : :
6230 : 155135 : WB (lang->has_complex_dflt);
6231 : 155135 : WB (lang->has_list_ctor);
6232 : 155135 : WB (lang->non_std_layout);
6233 : 155135 : WB (lang->is_literal);
6234 : 155135 : WB (lang->lazy_move_ctor);
6235 : 155135 : WB (lang->lazy_move_assign);
6236 : 155135 : WB (lang->has_complex_move_ctor);
6237 : 155135 : WB (lang->has_complex_move_assign);
6238 : :
6239 : 155135 : WB (lang->has_constexpr_ctor);
6240 : 155135 : WB (lang->unique_obj_representations);
6241 : 155135 : WB (lang->unique_obj_representations_set);
6242 : 155135 : gcc_checking_assert (!lang->erroneous);
6243 : 155135 : WB (lang->non_pod_aggregate);
6244 : 155135 : WB (lang->non_aggregate_pod);
6245 : : #undef WB
6246 : 155135 : }
6247 : :
6248 : : bool
6249 : 138337 : trees_in::lang_type_bools (tree t, bits_in& bits)
6250 : : {
6251 : : #define RB(X) ((X) = bits.b ())
6252 : 138337 : struct lang_type *lang = TYPE_LANG_SPECIFIC (t);
6253 : :
6254 : 138337 : bits.bflush ();
6255 : 138337 : RB (lang->has_type_conversion);
6256 : 138337 : RB (lang->has_copy_ctor);
6257 : 138337 : RB (lang->has_default_ctor);
6258 : 138337 : RB (lang->const_needs_init);
6259 : 138337 : RB (lang->ref_needs_init);
6260 : 138337 : RB (lang->has_const_copy_assign);
6261 : 138337 : unsigned v;
6262 : 138337 : v = bits.b () << 0;
6263 : 138337 : v |= bits.b () << 1;
6264 : 138337 : lang->use_template = v;
6265 : :
6266 : 138337 : RB (lang->has_mutable);
6267 : 138337 : RB (lang->com_interface);
6268 : 138337 : RB (lang->non_pod_class);
6269 : 138337 : RB (lang->nearly_empty_p);
6270 : 138337 : RB (lang->user_align);
6271 : 138337 : RB (lang->has_copy_assign);
6272 : 138337 : RB (lang->has_new);
6273 : 138337 : RB (lang->has_array_new);
6274 : :
6275 : 138337 : v = bits.b () << 0;
6276 : 138337 : v |= bits.b () << 1;
6277 : 138337 : lang->gets_delete = v;
6278 : 138337 : RB (lang->interface_only);
6279 : 138337 : RB (lang->interface_unknown);
6280 : 138337 : RB (lang->contains_empty_class_p);
6281 : 138337 : RB (lang->anon_aggr);
6282 : 138337 : RB (lang->non_zero_init);
6283 : 138337 : RB (lang->empty_p);
6284 : :
6285 : 138337 : RB (lang->vec_new_uses_cookie);
6286 : 138337 : RB (lang->declared_class);
6287 : 138337 : RB (lang->diamond_shaped);
6288 : 138337 : RB (lang->repeated_base);
6289 : 138337 : gcc_checking_assert (!lang->being_defined);
6290 : 138337 : gcc_checking_assert (!lang->debug_requested);
6291 : 138337 : RB (lang->fields_readonly);
6292 : 138337 : RB (lang->ptrmemfunc_flag);
6293 : :
6294 : 138337 : RB (lang->lazy_default_ctor);
6295 : 138337 : RB (lang->lazy_copy_ctor);
6296 : 138337 : RB (lang->lazy_copy_assign);
6297 : 138337 : RB (lang->lazy_destructor);
6298 : 138337 : RB (lang->has_const_copy_ctor);
6299 : 138337 : RB (lang->has_complex_copy_ctor);
6300 : 138337 : RB (lang->has_complex_copy_assign);
6301 : 138337 : RB (lang->non_aggregate);
6302 : :
6303 : 138337 : RB (lang->has_complex_dflt);
6304 : 138337 : RB (lang->has_list_ctor);
6305 : 138337 : RB (lang->non_std_layout);
6306 : 138337 : RB (lang->is_literal);
6307 : 138337 : RB (lang->lazy_move_ctor);
6308 : 138337 : RB (lang->lazy_move_assign);
6309 : 138337 : RB (lang->has_complex_move_ctor);
6310 : 138337 : RB (lang->has_complex_move_assign);
6311 : :
6312 : 138337 : RB (lang->has_constexpr_ctor);
6313 : 138337 : RB (lang->unique_obj_representations);
6314 : 138337 : RB (lang->unique_obj_representations_set);
6315 : 138337 : gcc_checking_assert (!lang->erroneous);
6316 : 138337 : RB (lang->non_pod_aggregate);
6317 : 138337 : RB (lang->non_aggregate_pod);
6318 : : #undef RB
6319 : 138337 : return !get_overrun ();
6320 : : }
6321 : :
6322 : : /* Read & write the core values and pointers. */
6323 : :
6324 : : void
6325 : 35197229 : trees_out::core_vals (tree t)
6326 : : {
6327 : : #define WU(X) (u (X))
6328 : : #define WT(X) (tree_node (X))
6329 : 35197229 : tree_code code = TREE_CODE (t);
6330 : :
6331 : : /* First by shape of the tree. */
6332 : :
6333 : 35197229 : if (CODE_CONTAINS_STRUCT (code, TS_DECL_MINIMAL))
6334 : : {
6335 : : /* Write this early, for better log information. */
6336 : 7905481 : WT (t->decl_minimal.name);
6337 : 7905481 : if (!DECL_TEMPLATE_PARM_P (t))
6338 : 6120646 : WT (t->decl_minimal.context);
6339 : :
6340 : 7905481 : if (state)
6341 : 6515767 : state->write_location (*this, t->decl_minimal.locus);
6342 : :
6343 : 7905481 : if (streaming_p ())
6344 : 3027317 : if (has_warning_spec (t))
6345 : 582 : u (get_warning_spec (t));
6346 : : }
6347 : :
6348 : 35197229 : if (CODE_CONTAINS_STRUCT (code, TS_TYPE_COMMON))
6349 : : {
6350 : : /* The only types we write also have TYPE_NON_COMMON. */
6351 : 1969885 : gcc_checking_assert (CODE_CONTAINS_STRUCT (code, TS_TYPE_NON_COMMON));
6352 : :
6353 : : /* We only stream the main variant. */
6354 : 1969885 : gcc_checking_assert (TYPE_MAIN_VARIANT (t) == t);
6355 : :
6356 : : /* Stream the name & context first, for better log information */
6357 : 1969885 : WT (t->type_common.name);
6358 : 1969885 : WT (t->type_common.context);
6359 : :
6360 : : /* By construction we want to make sure we have the canonical
6361 : : and main variants already in the type table, so emit them
6362 : : now. */
6363 : 1969885 : WT (t->type_common.main_variant);
6364 : :
6365 : 1969885 : tree canonical = t->type_common.canonical;
6366 : 1969885 : if (canonical && DECL_TEMPLATE_PARM_P (TYPE_NAME (t)))
6367 : : /* We do not want to wander into different templates.
6368 : : Reconstructed on stream in. */
6369 : : canonical = t;
6370 : 1969885 : WT (canonical);
6371 : :
6372 : : /* type_common.next_variant is internally manipulated. */
6373 : : /* type_common.pointer_to, type_common.reference_to. */
6374 : :
6375 : 1969885 : if (streaming_p ())
6376 : : {
6377 : 543096 : WU (t->type_common.precision);
6378 : 543096 : WU (t->type_common.contains_placeholder_bits);
6379 : 543096 : WU (t->type_common.mode);
6380 : 543096 : WU (t->type_common.align);
6381 : : }
6382 : :
6383 : 1969885 : if (!RECORD_OR_UNION_CODE_P (code))
6384 : : {
6385 : 1613535 : WT (t->type_common.size);
6386 : 1613535 : WT (t->type_common.size_unit);
6387 : : }
6388 : 1969885 : WT (t->type_common.attributes);
6389 : :
6390 : 1969885 : WT (t->type_common.common.chain); /* TYPE_STUB_DECL. */
6391 : : }
6392 : :
6393 : 35197229 : if (CODE_CONTAINS_STRUCT (code, TS_DECL_COMMON))
6394 : : {
6395 : 7905481 : if (streaming_p ())
6396 : : {
6397 : 3027317 : WU (t->decl_common.mode);
6398 : 3027317 : WU (t->decl_common.off_align);
6399 : 3027317 : WU (t->decl_common.align);
6400 : : }
6401 : :
6402 : : /* For templates these hold instantiation (partial and/or
6403 : : specialization) information. */
6404 : 7905481 : if (code != TEMPLATE_DECL)
6405 : : {
6406 : 7290900 : WT (t->decl_common.size);
6407 : 7290900 : WT (t->decl_common.size_unit);
6408 : : }
6409 : :
6410 : 7905481 : WT (t->decl_common.attributes);
6411 : : // FIXME: Does this introduce cross-decl links? For instance
6412 : : // from instantiation to the template. If so, we'll need more
6413 : : // deduplication logic. I think we'll need to walk the blocks
6414 : : // of the owning function_decl's abstract origin in tandem, to
6415 : : // generate the locating data needed?
6416 : 7905481 : WT (t->decl_common.abstract_origin);
6417 : : }
6418 : :
6419 : 35197229 : if (CODE_CONTAINS_STRUCT (code, TS_DECL_WITH_VIS))
6420 : : {
6421 : 3765644 : WT (t->decl_with_vis.assembler_name);
6422 : 3765644 : if (streaming_p ())
6423 : 1435481 : WU (t->decl_with_vis.visibility);
6424 : : }
6425 : :
6426 : 35197229 : if (CODE_CONTAINS_STRUCT (code, TS_TYPE_NON_COMMON))
6427 : : {
6428 : 1969885 : if (code == ENUMERAL_TYPE)
6429 : : {
6430 : : /* These fields get set even for opaque enums that lack a
6431 : : definition, so we stream them directly for each ENUMERAL_TYPE.
6432 : : We stream TYPE_VALUES as part of the definition. */
6433 : 7938 : WT (t->type_non_common.maxval);
6434 : 7938 : WT (t->type_non_common.minval);
6435 : : }
6436 : : /* Records and unions hold FIELDS, VFIELD & BINFO on these
6437 : : things. */
6438 : 1961947 : else if (!RECORD_OR_UNION_CODE_P (code))
6439 : : {
6440 : : // FIXME: These are from tpl_parm_value's 'type' writing.
6441 : : // Perhaps it should just be doing them directly?
6442 : 1605597 : gcc_checking_assert (code == TEMPLATE_TYPE_PARM
6443 : : || code == TEMPLATE_TEMPLATE_PARM
6444 : : || code == BOUND_TEMPLATE_TEMPLATE_PARM);
6445 : 1605597 : gcc_checking_assert (!TYPE_CACHED_VALUES_P (t));
6446 : 1605597 : WT (t->type_non_common.values);
6447 : 1605597 : WT (t->type_non_common.maxval);
6448 : 1605597 : WT (t->type_non_common.minval);
6449 : : }
6450 : :
6451 : 1969885 : WT (t->type_non_common.lang_1);
6452 : : }
6453 : :
6454 : 35197229 : if (CODE_CONTAINS_STRUCT (code, TS_EXP))
6455 : : {
6456 : 10418467 : if (state)
6457 : 10201035 : state->write_location (*this, t->exp.locus);
6458 : :
6459 : 10418467 : if (streaming_p ())
6460 : 5048231 : if (has_warning_spec (t))
6461 : 400028 : u (get_warning_spec (t));
6462 : :
6463 : 10418467 : bool vl = TREE_CODE_CLASS (code) == tcc_vl_exp;
6464 : 10418467 : unsigned limit = (vl ? VL_EXP_OPERAND_LENGTH (t)
6465 : 10418467 : : TREE_OPERAND_LENGTH (t));
6466 : 10418467 : unsigned ix = unsigned (vl);
6467 : 10418467 : if (code == REQUIRES_EXPR)
6468 : : {
6469 : : /* The first operand of a REQUIRES_EXPR is a tree chain
6470 : : of PARM_DECLs. We need to stream this separately as
6471 : : otherwise we would only stream the first one. */
6472 : 18043 : chained_decls (REQUIRES_EXPR_PARMS (t));
6473 : 18043 : ++ix;
6474 : : }
6475 : 28776069 : for (; ix != limit; ix++)
6476 : 18357602 : WT (TREE_OPERAND (t, ix));
6477 : : }
6478 : : else
6479 : : /* The CODE_CONTAINS tables were inaccurate when I started. */
6480 : 24778762 : gcc_checking_assert (TREE_CODE_CLASS (code) != tcc_expression
6481 : : && TREE_CODE_CLASS (code) != tcc_binary
6482 : : && TREE_CODE_CLASS (code) != tcc_unary
6483 : : && TREE_CODE_CLASS (code) != tcc_reference
6484 : : && TREE_CODE_CLASS (code) != tcc_comparison
6485 : : && TREE_CODE_CLASS (code) != tcc_statement
6486 : : && TREE_CODE_CLASS (code) != tcc_vl_exp);
6487 : :
6488 : : /* Then by CODE. Special cases and/or 1:1 tree shape
6489 : : correspondance. */
6490 : 35197229 : switch (code)
6491 : : {
6492 : : default:
6493 : : break;
6494 : :
6495 : 0 : case ARGUMENT_PACK_SELECT: /* Transient during instantiation. */
6496 : 0 : case DEFERRED_PARSE: /* Expanded upon completion of
6497 : : outermost class. */
6498 : 0 : case IDENTIFIER_NODE: /* Streamed specially. */
6499 : 0 : case BINDING_VECTOR: /* Only in namespace-scope symbol
6500 : : table. */
6501 : 0 : case SSA_NAME:
6502 : 0 : case TRANSLATION_UNIT_DECL: /* There is only one, it is a
6503 : : global_tree. */
6504 : 0 : case USERDEF_LITERAL: /* Expanded during parsing. */
6505 : 0 : gcc_unreachable (); /* Should never meet. */
6506 : :
6507 : : /* Constants. */
6508 : 18 : case COMPLEX_CST:
6509 : 18 : WT (TREE_REALPART (t));
6510 : 18 : WT (TREE_IMAGPART (t));
6511 : 18 : break;
6512 : :
6513 : 0 : case FIXED_CST:
6514 : 0 : gcc_unreachable (); /* Not supported in C++. */
6515 : :
6516 : 3337262 : case INTEGER_CST:
6517 : 3337262 : if (streaming_p ())
6518 : : {
6519 : 607130 : unsigned num = TREE_INT_CST_EXT_NUNITS (t);
6520 : 1216052 : for (unsigned ix = 0; ix != num; ix++)
6521 : 608922 : wu (TREE_INT_CST_ELT (t, ix));
6522 : : }
6523 : : break;
6524 : :
6525 : 0 : case POLY_INT_CST:
6526 : 0 : if (streaming_p ())
6527 : 0 : for (unsigned ix = 0; ix != NUM_POLY_INT_COEFFS; ix++)
6528 : 0 : WT (POLY_INT_CST_COEFF (t, ix));
6529 : : break;
6530 : :
6531 : 31931 : case REAL_CST:
6532 : 31931 : if (streaming_p ())
6533 : 15937 : buf (TREE_REAL_CST_PTR (t), sizeof (real_value));
6534 : : break;
6535 : :
6536 : : case STRING_CST:
6537 : : /* Streamed during start. */
6538 : : break;
6539 : :
6540 : 36 : case RAW_DATA_CST:
6541 : 36 : if (RAW_DATA_OWNER (t) == NULL_TREE)
6542 : : break; /* Streamed as STRING_CST during start. */
6543 : 24 : WT (RAW_DATA_OWNER (t));
6544 : 24 : if (streaming_p ())
6545 : : {
6546 : 12 : if (TREE_CODE (RAW_DATA_OWNER (t)) == RAW_DATA_CST)
6547 : 6 : z (RAW_DATA_POINTER (t) - RAW_DATA_POINTER (RAW_DATA_OWNER (t)));
6548 : 6 : else if (TREE_CODE (RAW_DATA_OWNER (t)) == STRING_CST)
6549 : 6 : z (RAW_DATA_POINTER (t)
6550 : 6 : - TREE_STRING_POINTER (RAW_DATA_OWNER (t)));
6551 : : else
6552 : 0 : gcc_unreachable ();
6553 : : }
6554 : : break;
6555 : :
6556 : 36 : case VECTOR_CST:
6557 : 102 : for (unsigned ix = vector_cst_encoded_nelts (t); ix--;)
6558 : 66 : WT (VECTOR_CST_ENCODED_ELT (t, ix));
6559 : : break;
6560 : :
6561 : : /* Decls. */
6562 : 420103 : case VAR_DECL:
6563 : 420103 : if (DECL_CONTEXT (t)
6564 : 420103 : && TREE_CODE (DECL_CONTEXT (t)) != FUNCTION_DECL)
6565 : : {
6566 : 109276 : if (DECL_HAS_VALUE_EXPR_P (t))
6567 : 18 : WT (DECL_VALUE_EXPR (t));
6568 : : break;
6569 : : }
6570 : : /* FALLTHROUGH */
6571 : :
6572 : 3529382 : case RESULT_DECL:
6573 : 3529382 : case PARM_DECL:
6574 : 3529382 : if (DECL_HAS_VALUE_EXPR_P (t))
6575 : 28798 : WT (DECL_VALUE_EXPR (t));
6576 : : /* FALLTHROUGH */
6577 : :
6578 : 3683628 : case CONST_DECL:
6579 : 3683628 : case IMPORTED_DECL:
6580 : 3683628 : WT (t->decl_common.initial);
6581 : 3683628 : break;
6582 : :
6583 : 108181 : case FIELD_DECL:
6584 : 108181 : WT (t->field_decl.offset);
6585 : 108181 : WT (t->field_decl.bit_field_type);
6586 : 108181 : {
6587 : 108181 : auto ovr = make_temp_override (walking_bit_field_unit, true);
6588 : 108181 : WT (t->field_decl.qualifier); /* bitfield unit. */
6589 : 108181 : }
6590 : 108181 : WT (t->field_decl.bit_offset);
6591 : 108181 : WT (t->field_decl.fcontext);
6592 : 108181 : WT (t->decl_common.initial);
6593 : 108181 : break;
6594 : :
6595 : 32040 : case LABEL_DECL:
6596 : 32040 : if (streaming_p ())
6597 : : {
6598 : 16020 : WU (t->label_decl.label_decl_uid);
6599 : 16020 : WU (t->label_decl.eh_landing_pad_nr);
6600 : : }
6601 : : break;
6602 : :
6603 : 833427 : case FUNCTION_DECL:
6604 : 833427 : if (streaming_p ())
6605 : : {
6606 : : /* Builtins can be streamed by value when a header declares
6607 : : them. */
6608 : 416623 : WU (DECL_BUILT_IN_CLASS (t));
6609 : 416623 : if (DECL_BUILT_IN_CLASS (t) != NOT_BUILT_IN)
6610 : 8346 : WU (DECL_UNCHECKED_FUNCTION_CODE (t));
6611 : : }
6612 : :
6613 : 833427 : WT (t->function_decl.personality);
6614 : : /* Rather than streaming target/optimize nodes, we should reconstruct
6615 : : them on stream-in from any attributes applied to the function. */
6616 : 833427 : if (streaming_p () && t->function_decl.function_specific_target)
6617 : 0 : warning_at (DECL_SOURCE_LOCATION (t), 0,
6618 : : "%<target%> attribute currently unsupported in modules");
6619 : 833427 : if (streaming_p () && t->function_decl.function_specific_optimization)
6620 : 3 : warning_at (DECL_SOURCE_LOCATION (t), 0,
6621 : : "%<optimize%> attribute currently unsupported in modules");
6622 : 833427 : WT (t->function_decl.vindex);
6623 : :
6624 : 833427 : if (DECL_HAS_DEPENDENT_EXPLICIT_SPEC_P (t))
6625 : 5324 : WT (lookup_explicit_specifier (t));
6626 : : break;
6627 : :
6628 : 102459 : case USING_DECL:
6629 : : /* USING_DECL_DECLS */
6630 : 102459 : WT (t->decl_common.initial);
6631 : : /* FALLTHROUGH */
6632 : :
6633 : 2511847 : case TYPE_DECL:
6634 : : /* USING_DECL: USING_DECL_SCOPE */
6635 : : /* TYPE_DECL: DECL_ORIGINAL_TYPE */
6636 : 2511847 : WT (t->decl_non_common.result);
6637 : 2511847 : break;
6638 : :
6639 : : /* Miscellaneous common nodes. */
6640 : 534524 : case BLOCK:
6641 : 534524 : if (state)
6642 : : {
6643 : 534524 : state->write_location (*this, t->block.locus);
6644 : 534524 : state->write_location (*this, t->block.end_locus);
6645 : : }
6646 : :
6647 : : /* DECL_LOCAL_DECL_P decls are first encountered here and
6648 : : streamed by value. */
6649 : 798114 : for (tree decls = t->block.vars; decls; decls = DECL_CHAIN (decls))
6650 : : {
6651 : 263590 : if (VAR_OR_FUNCTION_DECL_P (decls)
6652 : 263590 : && DECL_LOCAL_DECL_P (decls))
6653 : : {
6654 : : /* Make sure this is the first encounter, and mark for
6655 : : walk-by-value. */
6656 : 252 : gcc_checking_assert (!TREE_VISITED (decls)
6657 : : && !DECL_TEMPLATE_INFO (decls));
6658 : 252 : mark_by_value (decls);
6659 : : }
6660 : 263590 : tree_node (decls);
6661 : : }
6662 : 534524 : tree_node (NULL_TREE);
6663 : :
6664 : : /* nonlocalized_vars is a middle-end thing. */
6665 : 534524 : WT (t->block.subblocks);
6666 : 534524 : WT (t->block.supercontext);
6667 : : // FIXME: As for decl's abstract_origin, does this introduce crosslinks?
6668 : 534524 : WT (t->block.abstract_origin);
6669 : : /* fragment_origin, fragment_chain are middle-end things. */
6670 : 534524 : WT (t->block.chain);
6671 : : /* nonlocalized_vars, block_num & die are middle endy/debug
6672 : : things. */
6673 : 534524 : break;
6674 : :
6675 : 1036846 : case CALL_EXPR:
6676 : 1036846 : if (streaming_p ())
6677 : 494061 : WU (t->base.u.ifn);
6678 : : break;
6679 : :
6680 : : case CONSTRUCTOR:
6681 : : // This must be streamed /after/ we've streamed the type,
6682 : : // because it can directly refer to elements of the type. Eg,
6683 : : // FIELD_DECLs of a RECORD_TYPE.
6684 : : break;
6685 : :
6686 : 36 : case OMP_CLAUSE:
6687 : 36 : {
6688 : : /* The ompcode is serialized in start. */
6689 : 36 : if (streaming_p ())
6690 : 18 : WU (t->omp_clause.subcode.map_kind);
6691 : 36 : if (state)
6692 : 36 : state->write_location (*this, t->omp_clause.locus);
6693 : :
6694 : 36 : unsigned len = omp_clause_num_ops[OMP_CLAUSE_CODE (t)];
6695 : 120 : for (unsigned ix = 0; ix != len; ix++)
6696 : 84 : WT (t->omp_clause.ops[ix]);
6697 : : }
6698 : : break;
6699 : :
6700 : 408447 : case STATEMENT_LIST:
6701 : 1649174 : for (tree stmt : tsi_range (t))
6702 : 1240727 : if (stmt)
6703 : 1240727 : WT (stmt);
6704 : 408447 : WT (NULL_TREE);
6705 : 408447 : break;
6706 : :
6707 : 0 : case OPTIMIZATION_NODE:
6708 : 0 : case TARGET_OPTION_NODE:
6709 : : // FIXME: Our representation for these two nodes is a cache of
6710 : : // the resulting set of options. Not a record of the options
6711 : : // that got changed by a particular attribute or pragma. Instead
6712 : : // of recording that, we probably should just rebuild the options
6713 : : // on stream-in from the function attributes. This could introduce
6714 : : // strangeness if the importer has some incompatible set of flags
6715 : : // but we currently assume users "know what they're doing" in such
6716 : : // a case anyway.
6717 : 0 : gcc_unreachable ();
6718 : 208212 : break;
6719 : :
6720 : 208212 : case TREE_BINFO:
6721 : 208212 : {
6722 : 208212 : WT (t->binfo.common.chain);
6723 : 208212 : WT (t->binfo.offset);
6724 : 208212 : WT (t->binfo.inheritance);
6725 : 208212 : WT (t->binfo.vptr_field);
6726 : :
6727 : 208212 : WT (t->binfo.vtable);
6728 : 208212 : WT (t->binfo.virtuals);
6729 : 208212 : WT (t->binfo.vtt_subvtt);
6730 : 208212 : WT (t->binfo.vtt_vptr);
6731 : :
6732 : 208212 : tree_vec (BINFO_BASE_ACCESSES (t));
6733 : 208212 : unsigned num = vec_safe_length (BINFO_BASE_ACCESSES (t));
6734 : 266952 : for (unsigned ix = 0; ix != num; ix++)
6735 : 58740 : WT (BINFO_BASE_BINFO (t, ix));
6736 : : }
6737 : : break;
6738 : :
6739 : 2729161 : case TREE_LIST:
6740 : 2729161 : WT (t->list.purpose);
6741 : 2729161 : WT (t->list.value);
6742 : 2729161 : WT (t->list.common.chain);
6743 : 2729161 : break;
6744 : :
6745 : 2326423 : case TREE_VEC:
6746 : 6386823 : for (unsigned ix = TREE_VEC_LENGTH (t); ix--;)
6747 : 4060400 : WT (TREE_VEC_ELT (t, ix));
6748 : : /* We stash NON_DEFAULT_TEMPLATE_ARGS_COUNT on TREE_CHAIN! */
6749 : 2326423 : gcc_checking_assert (!t->type_common.common.chain
6750 : : || (TREE_CODE (t->type_common.common.chain)
6751 : : == INTEGER_CST));
6752 : 2326423 : WT (t->type_common.common.chain);
6753 : 2326423 : break;
6754 : :
6755 : : /* C++-specific nodes ... */
6756 : 183637 : case BASELINK:
6757 : 183637 : WT (((lang_tree_node *)t)->baselink.binfo);
6758 : 183637 : WT (((lang_tree_node *)t)->baselink.functions);
6759 : 183637 : WT (((lang_tree_node *)t)->baselink.access_binfo);
6760 : 183637 : WT (((lang_tree_node *)t)->baselink.common.chain);
6761 : 183637 : break;
6762 : :
6763 : 80762 : case CONSTRAINT_INFO:
6764 : 80762 : WT (((lang_tree_node *)t)->constraint_info.template_reqs);
6765 : 80762 : WT (((lang_tree_node *)t)->constraint_info.declarator_reqs);
6766 : 80762 : WT (((lang_tree_node *)t)->constraint_info.associated_constr);
6767 : 80762 : break;
6768 : :
6769 : 12710 : case DEFERRED_NOEXCEPT:
6770 : 12710 : WT (((lang_tree_node *)t)->deferred_noexcept.pattern);
6771 : 12710 : WT (((lang_tree_node *)t)->deferred_noexcept.args);
6772 : 12710 : break;
6773 : :
6774 : 11723 : case LAMBDA_EXPR:
6775 : 11723 : WT (((lang_tree_node *)t)->lambda_expression.capture_list);
6776 : 11723 : WT (((lang_tree_node *)t)->lambda_expression.this_capture);
6777 : 11723 : WT (((lang_tree_node *)t)->lambda_expression.extra_scope);
6778 : 11723 : WT (((lang_tree_node *)t)->lambda_expression.regen_info);
6779 : 11723 : WT (((lang_tree_node *)t)->lambda_expression.extra_args);
6780 : : /* pending_proxies is a parse-time thing. */
6781 : 11723 : gcc_assert (!((lang_tree_node *)t)->lambda_expression.pending_proxies);
6782 : 11723 : if (state)
6783 : 11453 : state->write_location
6784 : 11453 : (*this, ((lang_tree_node *)t)->lambda_expression.locus);
6785 : 11723 : if (streaming_p ())
6786 : : {
6787 : 3911 : WU (((lang_tree_node *)t)->lambda_expression.default_capture_mode);
6788 : 3911 : WU (((lang_tree_node *)t)->lambda_expression.discriminator_scope);
6789 : 3911 : WU (((lang_tree_node *)t)->lambda_expression.discriminator_sig);
6790 : : }
6791 : : break;
6792 : :
6793 : 1713020 : case OVERLOAD:
6794 : 1713020 : WT (((lang_tree_node *)t)->overload.function);
6795 : 1713020 : WT (t->common.chain);
6796 : 1713020 : break;
6797 : :
6798 : 0 : case PTRMEM_CST:
6799 : 0 : WT (((lang_tree_node *)t)->ptrmem.member);
6800 : 0 : break;
6801 : :
6802 : 14920 : case STATIC_ASSERT:
6803 : 14920 : WT (((lang_tree_node *)t)->static_assertion.condition);
6804 : 14920 : WT (((lang_tree_node *)t)->static_assertion.message);
6805 : 14920 : if (state)
6806 : 14920 : state->write_location
6807 : 14920 : (*this, ((lang_tree_node *)t)->static_assertion.location);
6808 : : break;
6809 : :
6810 : 614581 : case TEMPLATE_DECL:
6811 : : /* Streamed with the template_decl node itself. */
6812 : 614581 : gcc_checking_assert
6813 : : (TREE_VISITED (((lang_tree_node *)t)->template_decl.arguments));
6814 : 614581 : gcc_checking_assert
6815 : : (TREE_VISITED (((lang_tree_node *)t)->template_decl.result));
6816 : 614581 : if (DECL_UNINSTANTIATED_TEMPLATE_FRIEND_P (t))
6817 : 11230 : WT (DECL_CHAIN (t));
6818 : : break;
6819 : :
6820 : 1374265 : case TEMPLATE_INFO:
6821 : 1374265 : {
6822 : 1374265 : WT (((lang_tree_node *)t)->template_info.tmpl);
6823 : 1374265 : WT (((lang_tree_node *)t)->template_info.args);
6824 : 1374265 : WT (((lang_tree_node *)t)->template_info.partial);
6825 : :
6826 : 1374265 : const auto *ac = (((lang_tree_node *)t)
6827 : : ->template_info.deferred_access_checks);
6828 : 1374265 : unsigned len = vec_safe_length (ac);
6829 : 1374265 : if (streaming_p ())
6830 : 685467 : u (len);
6831 : 1374265 : if (len)
6832 : : {
6833 : 0 : for (unsigned ix = 0; ix != len; ix++)
6834 : : {
6835 : 0 : const auto &m = (*ac)[ix];
6836 : 0 : WT (m.binfo);
6837 : 0 : WT (m.decl);
6838 : 0 : WT (m.diag_decl);
6839 : 0 : if (state)
6840 : 0 : state->write_location (*this, m.loc);
6841 : : }
6842 : : }
6843 : : }
6844 : : break;
6845 : :
6846 : 1720967 : case TEMPLATE_PARM_INDEX:
6847 : 1720967 : if (streaming_p ())
6848 : : {
6849 : 383381 : WU (((lang_tree_node *)t)->tpi.index);
6850 : 383381 : WU (((lang_tree_node *)t)->tpi.level);
6851 : 383381 : WU (((lang_tree_node *)t)->tpi.orig_level);
6852 : : }
6853 : 1720967 : WT (((lang_tree_node *)t)->tpi.decl);
6854 : : /* TEMPLATE_PARM_DESCENDANTS (AKA TREE_CHAIN) is an internal
6855 : : cache, do not stream. */
6856 : 1720967 : break;
6857 : :
6858 : 25525 : case TRAIT_EXPR:
6859 : 25525 : WT (((lang_tree_node *)t)->trait_expression.type1);
6860 : 25525 : WT (((lang_tree_node *)t)->trait_expression.type2);
6861 : 25525 : if (streaming_p ())
6862 : 9822 : WU (((lang_tree_node *)t)->trait_expression.kind);
6863 : : break;
6864 : :
6865 : 4 : case TU_LOCAL_ENTITY:
6866 : 4 : WT (((lang_tree_node *)t)->tu_local_entity.name);
6867 : 4 : if (state)
6868 : 4 : state->write_location
6869 : 4 : (*this, ((lang_tree_node *)t)->tu_local_entity.loc);
6870 : : break;
6871 : : }
6872 : :
6873 : 35197229 : if (CODE_CONTAINS_STRUCT (code, TS_TYPED))
6874 : : {
6875 : : /* We want to stream the type of a expression-like nodes /after/
6876 : : we've streamed the operands. The type often contains (bits
6877 : : of the) types of the operands, and with things like decltype
6878 : : and noexcept in play, we really want to stream the decls
6879 : : defining the type before we try and stream the type on its
6880 : : own. Otherwise we can find ourselves trying to read in a
6881 : : decl, when we're already partially reading in a component of
6882 : : its type. And that's bad. */
6883 : 33180044 : tree type = t->typed.type;
6884 : 33180044 : unsigned prec = 0;
6885 : :
6886 : 33180044 : switch (code)
6887 : : {
6888 : : default:
6889 : : break;
6890 : :
6891 : : case TEMPLATE_DECL:
6892 : : /* We fill in the template's type separately. */
6893 : 33180044 : type = NULL_TREE;
6894 : : break;
6895 : :
6896 : 2409388 : case TYPE_DECL:
6897 : 2409388 : if (DECL_ORIGINAL_TYPE (t) && t == TYPE_NAME (type))
6898 : : /* This is a typedef. We set its type separately. */
6899 : : type = NULL_TREE;
6900 : : break;
6901 : :
6902 : 7938 : case ENUMERAL_TYPE:
6903 : 7938 : if (type && !ENUM_FIXED_UNDERLYING_TYPE_P (t))
6904 : : {
6905 : : /* Type is a restricted range integer type derived from the
6906 : : integer_types. Find the right one. */
6907 : 5058 : prec = TYPE_PRECISION (type);
6908 : 5058 : tree name = DECL_NAME (TYPE_NAME (type));
6909 : :
6910 : 66092 : for (unsigned itk = itk_none; itk--;)
6911 : 66092 : if (integer_types[itk]
6912 : 66092 : && DECL_NAME (TYPE_NAME (integer_types[itk])) == name)
6913 : : {
6914 : : type = integer_types[itk];
6915 : : break;
6916 : : }
6917 : 5058 : gcc_assert (type != t->typed.type);
6918 : : }
6919 : : break;
6920 : : }
6921 : :
6922 : 33180044 : WT (type);
6923 : 33180044 : if (prec && streaming_p ())
6924 : 2527 : WU (prec);
6925 : : }
6926 : :
6927 : 35197229 : if (TREE_CODE (t) == CONSTRUCTOR)
6928 : : {
6929 : 94289 : unsigned len = vec_safe_length (t->constructor.elts);
6930 : 94289 : if (streaming_p ())
6931 : 46419 : WU (len);
6932 : 94289 : if (len)
6933 : 355600 : for (unsigned ix = 0; ix != len; ix++)
6934 : : {
6935 : 303841 : const constructor_elt &elt = (*t->constructor.elts)[ix];
6936 : :
6937 : 303841 : WT (elt.index);
6938 : 303841 : WT (elt.value);
6939 : : }
6940 : : }
6941 : :
6942 : : #undef WT
6943 : : #undef WU
6944 : 35197229 : }
6945 : :
6946 : : // Streaming in a reference to a decl can cause that decl to be
6947 : : // TREE_USED, which is the mark_used behaviour we need most of the
6948 : : // time. The trees_in::unused can be incremented to inhibit this,
6949 : : // which is at least needed for vtables.
6950 : :
6951 : : bool
6952 : 13304253 : trees_in::core_vals (tree t)
6953 : : {
6954 : : #define RU(X) ((X) = u ())
6955 : : #define RUC(T,X) ((X) = T (u ()))
6956 : : #define RT(X) ((X) = tree_node ())
6957 : : #define RTU(X) ((X) = tree_node (true))
6958 : 13304253 : tree_code code = TREE_CODE (t);
6959 : :
6960 : : /* First by tree shape. */
6961 : 13304253 : if (CODE_CONTAINS_STRUCT (code, TS_DECL_MINIMAL))
6962 : : {
6963 : 2877221 : RT (t->decl_minimal.name);
6964 : 2877221 : if (!DECL_TEMPLATE_PARM_P (t))
6965 : 2511025 : RT (t->decl_minimal.context);
6966 : :
6967 : : /* Don't zap the locus just yet, we don't record it correctly
6968 : : and thus lose all location information. */
6969 : 2877221 : t->decl_minimal.locus = state->read_location (*this);
6970 : 2877221 : if (has_warning_spec (t))
6971 : 548 : put_warning_spec (t, u ());
6972 : : }
6973 : :
6974 : 13304253 : if (CODE_CONTAINS_STRUCT (code, TS_TYPE_COMMON))
6975 : : {
6976 : 488310 : RT (t->type_common.name);
6977 : 488310 : RT (t->type_common.context);
6978 : :
6979 : 488310 : RT (t->type_common.main_variant);
6980 : 488310 : RT (t->type_common.canonical);
6981 : :
6982 : : /* type_common.next_variant is internally manipulated. */
6983 : : /* type_common.pointer_to, type_common.reference_to. */
6984 : :
6985 : 488310 : RU (t->type_common.precision);
6986 : 488310 : RU (t->type_common.contains_placeholder_bits);
6987 : 488310 : RUC (machine_mode, t->type_common.mode);
6988 : 488310 : RU (t->type_common.align);
6989 : :
6990 : 488310 : if (!RECORD_OR_UNION_CODE_P (code))
6991 : : {
6992 : 331754 : RT (t->type_common.size);
6993 : 331754 : RT (t->type_common.size_unit);
6994 : : }
6995 : 488310 : RT (t->type_common.attributes);
6996 : :
6997 : 488310 : RT (t->type_common.common.chain); /* TYPE_STUB_DECL. */
6998 : : }
6999 : :
7000 : 13304253 : if (CODE_CONTAINS_STRUCT (code, TS_DECL_COMMON))
7001 : : {
7002 : 2877221 : RUC (machine_mode, t->decl_common.mode);
7003 : 2877221 : RU (t->decl_common.off_align);
7004 : 2877221 : RU (t->decl_common.align);
7005 : :
7006 : 2877221 : if (code != TEMPLATE_DECL)
7007 : : {
7008 : 2585059 : RT (t->decl_common.size);
7009 : 2585059 : RT (t->decl_common.size_unit);
7010 : : }
7011 : :
7012 : 2877221 : RT (t->decl_common.attributes);
7013 : 2877221 : RT (t->decl_common.abstract_origin);
7014 : : }
7015 : :
7016 : 13304253 : if (CODE_CONTAINS_STRUCT (code, TS_DECL_WITH_VIS))
7017 : : {
7018 : 1332190 : RT (t->decl_with_vis.assembler_name);
7019 : 1332190 : RUC (symbol_visibility, t->decl_with_vis.visibility);
7020 : : }
7021 : :
7022 : 13304253 : if (CODE_CONTAINS_STRUCT (code, TS_TYPE_NON_COMMON))
7023 : : {
7024 : 488310 : if (code == ENUMERAL_TYPE)
7025 : : {
7026 : : /* These fields get set even for opaque enums that lack a
7027 : : definition, so we stream them directly for each ENUMERAL_TYPE.
7028 : : We stream TYPE_VALUES as part of the definition. */
7029 : 2870 : RT (t->type_non_common.maxval);
7030 : 2870 : RT (t->type_non_common.minval);
7031 : : }
7032 : : /* Records and unions hold FIELDS, VFIELD & BINFO on these
7033 : : things. */
7034 : 485440 : else if (!RECORD_OR_UNION_CODE_P (code))
7035 : : {
7036 : : /* This is not clobbering TYPE_CACHED_VALUES, because this
7037 : : is a type that doesn't have any. */
7038 : 328884 : gcc_checking_assert (!TYPE_CACHED_VALUES_P (t));
7039 : 328884 : RT (t->type_non_common.values);
7040 : 328884 : RT (t->type_non_common.maxval);
7041 : 328884 : RT (t->type_non_common.minval);
7042 : : }
7043 : :
7044 : 488310 : RT (t->type_non_common.lang_1);
7045 : : }
7046 : :
7047 : 13304253 : if (CODE_CONTAINS_STRUCT (code, TS_EXP))
7048 : : {
7049 : 5174346 : t->exp.locus = state->read_location (*this);
7050 : 5174346 : if (has_warning_spec (t))
7051 : 405200 : put_warning_spec (t, u ());
7052 : :
7053 : 5174346 : bool vl = TREE_CODE_CLASS (code) == tcc_vl_exp;
7054 : 5174346 : unsigned limit = (vl ? VL_EXP_OPERAND_LENGTH (t)
7055 : 5174346 : : TREE_OPERAND_LENGTH (t));
7056 : 5174346 : unsigned ix = unsigned (vl);
7057 : 5174346 : if (code == REQUIRES_EXPR)
7058 : : {
7059 : 6310 : REQUIRES_EXPR_PARMS (t) = chained_decls ();
7060 : 6310 : ++ix;
7061 : : }
7062 : 14182177 : for (; ix != limit; ix++)
7063 : 9007831 : RTU (TREE_OPERAND (t, ix));
7064 : : }
7065 : :
7066 : : /* Then by CODE. Special cases and/or 1:1 tree shape
7067 : : correspondance. */
7068 : 13304253 : switch (code)
7069 : : {
7070 : : default:
7071 : : break;
7072 : :
7073 : : case ARGUMENT_PACK_SELECT:
7074 : : case DEFERRED_PARSE:
7075 : : case IDENTIFIER_NODE:
7076 : : case BINDING_VECTOR:
7077 : : case SSA_NAME:
7078 : : case TRANSLATION_UNIT_DECL:
7079 : : case USERDEF_LITERAL:
7080 : : return false; /* Should never meet. */
7081 : :
7082 : : /* Constants. */
7083 : 9 : case COMPLEX_CST:
7084 : 9 : RT (TREE_REALPART (t));
7085 : 9 : RT (TREE_IMAGPART (t));
7086 : 9 : break;
7087 : :
7088 : : case FIXED_CST:
7089 : : /* Not suported in C++. */
7090 : : return false;
7091 : :
7092 : 552093 : case INTEGER_CST:
7093 : 552093 : {
7094 : 552093 : unsigned num = TREE_INT_CST_EXT_NUNITS (t);
7095 : 1105757 : for (unsigned ix = 0; ix != num; ix++)
7096 : 553664 : TREE_INT_CST_ELT (t, ix) = wu ();
7097 : : }
7098 : : break;
7099 : :
7100 : : case POLY_INT_CST:
7101 : 0 : for (unsigned ix = 0; ix != NUM_POLY_INT_COEFFS; ix++)
7102 : 0 : RT (POLY_INT_CST_COEFF (t, ix));
7103 : : break;
7104 : :
7105 : 16031 : case REAL_CST:
7106 : 16031 : if (const void *bytes = buf (sizeof (real_value)))
7107 : 16031 : memcpy (TREE_REAL_CST_PTR (t), bytes, sizeof (real_value));
7108 : : break;
7109 : :
7110 : : case STRING_CST:
7111 : : /* Streamed during start. */
7112 : : break;
7113 : :
7114 : 6 : case RAW_DATA_CST:
7115 : 6 : RT (RAW_DATA_OWNER (t));
7116 : 6 : gcc_assert (TREE_CODE (RAW_DATA_OWNER (t)) == STRING_CST
7117 : : && TREE_STRING_LENGTH (RAW_DATA_OWNER (t)));
7118 : 6 : RAW_DATA_POINTER (t) = TREE_STRING_POINTER (RAW_DATA_OWNER (t)) + z ();
7119 : 6 : break;
7120 : :
7121 : 24 : case VECTOR_CST:
7122 : 63 : for (unsigned ix = vector_cst_encoded_nelts (t); ix--;)
7123 : 39 : RT (VECTOR_CST_ENCODED_ELT (t, ix));
7124 : : break;
7125 : :
7126 : : /* Decls. */
7127 : 194676 : case VAR_DECL:
7128 : 194676 : if (DECL_CONTEXT (t)
7129 : 194676 : && TREE_CODE (DECL_CONTEXT (t)) != FUNCTION_DECL)
7130 : : {
7131 : 39407 : if (DECL_HAS_VALUE_EXPR_P (t))
7132 : : {
7133 : 9 : tree val = tree_node ();
7134 : 9 : SET_DECL_VALUE_EXPR (t, val);
7135 : : }
7136 : : break;
7137 : : }
7138 : : /* FALLTHROUGH */
7139 : :
7140 : 1295878 : case RESULT_DECL:
7141 : 1295878 : case PARM_DECL:
7142 : 1295878 : if (DECL_HAS_VALUE_EXPR_P (t))
7143 : : {
7144 : : /* The DECL_VALUE hash table is a cache, thus if we're
7145 : : reading a duplicate (which we end up discarding), the
7146 : : value expr will also be cleaned up at the next gc. */
7147 : 11806 : tree val = tree_node ();
7148 : 11806 : SET_DECL_VALUE_EXPR (t, val);
7149 : : }
7150 : : /* FALLTHROUGH */
7151 : :
7152 : 1336424 : case CONST_DECL:
7153 : 1336424 : case IMPORTED_DECL:
7154 : 1336424 : RT (t->decl_common.initial);
7155 : 1336424 : break;
7156 : :
7157 : 50702 : case FIELD_DECL:
7158 : 50702 : RT (t->field_decl.offset);
7159 : 50702 : RT (t->field_decl.bit_field_type);
7160 : 50702 : RT (t->field_decl.qualifier);
7161 : 50702 : RT (t->field_decl.bit_offset);
7162 : 50702 : RT (t->field_decl.fcontext);
7163 : 50702 : RT (t->decl_common.initial);
7164 : 50702 : break;
7165 : :
7166 : 15608 : case LABEL_DECL:
7167 : 15608 : RU (t->label_decl.label_decl_uid);
7168 : 15608 : RU (t->label_decl.eh_landing_pad_nr);
7169 : 15608 : break;
7170 : :
7171 : 406715 : case FUNCTION_DECL:
7172 : 406715 : {
7173 : 406715 : unsigned bltin = u ();
7174 : 406715 : t->function_decl.built_in_class = built_in_class (bltin);
7175 : 406715 : if (bltin != NOT_BUILT_IN)
7176 : : {
7177 : 8221 : bltin = u ();
7178 : 8221 : DECL_UNCHECKED_FUNCTION_CODE (t) = built_in_function (bltin);
7179 : : }
7180 : :
7181 : 406715 : RT (t->function_decl.personality);
7182 : : /* These properties are not streamed, and should be reconstructed
7183 : : from any function attributes. */
7184 : : // t->function_decl.function_specific_target);
7185 : : // t->function_decl.function_specific_optimization);
7186 : 406715 : RT (t->function_decl.vindex);
7187 : :
7188 : 406715 : if (DECL_HAS_DEPENDENT_EXPLICIT_SPEC_P (t))
7189 : : {
7190 : 3126 : tree spec;
7191 : 3126 : RT (spec);
7192 : 3126 : store_explicit_specifier (t, spec);
7193 : : }
7194 : : }
7195 : : break;
7196 : :
7197 : 48835 : case USING_DECL:
7198 : : /* USING_DECL_DECLS */
7199 : 48835 : RT (t->decl_common.initial);
7200 : : /* FALLTHROUGH */
7201 : :
7202 : 730628 : case TYPE_DECL:
7203 : : /* USING_DECL: USING_DECL_SCOPE */
7204 : : /* TYPE_DECL: DECL_ORIGINAL_TYPE */
7205 : 730628 : RT (t->decl_non_common.result);
7206 : 730628 : break;
7207 : :
7208 : : /* Miscellaneous common nodes. */
7209 : 267577 : case BLOCK:
7210 : 267577 : t->block.locus = state->read_location (*this);
7211 : 267577 : t->block.end_locus = state->read_location (*this);
7212 : :
7213 : 267577 : for (tree *chain = &t->block.vars;;)
7214 : 404164 : if (tree decl = tree_node ())
7215 : : {
7216 : : /* For a deduplicated local type or enumerator, chain the
7217 : : duplicate decl instead of the canonical in-TU decl. Seeing
7218 : : a duplicate here means the containing function whose body
7219 : : we're streaming in is a duplicate too, so we'll end up
7220 : : discarding this BLOCK (and the rest of the duplicate function
7221 : : body) anyway. */
7222 : 136587 : decl = maybe_duplicate (decl);
7223 : :
7224 : 136587 : if (!DECL_P (decl))
7225 : : {
7226 : 0 : set_overrun ();
7227 : 0 : break;
7228 : : }
7229 : :
7230 : : /* If DECL_CHAIN is already set then this was a backreference to a
7231 : : local type or enumerator from a previous read (PR c++/114630).
7232 : : Let's copy the node so we can keep building the chain for ODR
7233 : : checking later. */
7234 : 136587 : if (DECL_CHAIN (decl))
7235 : : {
7236 : 12 : gcc_checking_assert (TREE_CODE (decl) == TYPE_DECL
7237 : : && find_duplicate (DECL_CONTEXT (decl)));
7238 : 6 : decl = copy_decl (decl);
7239 : : }
7240 : :
7241 : 136587 : *chain = decl;
7242 : 136587 : chain = &DECL_CHAIN (decl);
7243 : : }
7244 : : else
7245 : 136587 : break;
7246 : :
7247 : : /* nonlocalized_vars is middle-end. */
7248 : 267577 : RT (t->block.subblocks);
7249 : 267577 : RT (t->block.supercontext);
7250 : 267577 : RT (t->block.abstract_origin);
7251 : : /* fragment_origin, fragment_chain are middle-end. */
7252 : 267577 : RT (t->block.chain);
7253 : : /* nonlocalized_vars, block_num, die are middle endy/debug
7254 : : things. */
7255 : 267577 : break;
7256 : :
7257 : 495679 : case CALL_EXPR:
7258 : 495679 : RUC (internal_fn, t->base.u.ifn);
7259 : 495679 : break;
7260 : :
7261 : : case CONSTRUCTOR:
7262 : : // Streamed after the node's type.
7263 : : break;
7264 : :
7265 : 18 : case OMP_CLAUSE:
7266 : 18 : {
7267 : 18 : RU (t->omp_clause.subcode.map_kind);
7268 : 18 : t->omp_clause.locus = state->read_location (*this);
7269 : :
7270 : 18 : unsigned len = omp_clause_num_ops[OMP_CLAUSE_CODE (t)];
7271 : 66 : for (unsigned ix = 0; ix != len; ix++)
7272 : 48 : RT (t->omp_clause.ops[ix]);
7273 : : }
7274 : : break;
7275 : :
7276 : 227812 : case STATEMENT_LIST:
7277 : 227812 : {
7278 : 227812 : tree_stmt_iterator iter = tsi_start (t);
7279 : 946515 : for (tree stmt; RT (stmt);)
7280 : : {
7281 : 718703 : if (TREE_CODE (stmt) == DEBUG_BEGIN_STMT
7282 : 114512 : && !MAY_HAVE_DEBUG_MARKER_STMTS)
7283 : 0 : continue;
7284 : 718703 : tsi_link_after (&iter, stmt, TSI_CONTINUE_LINKING);
7285 : : }
7286 : : }
7287 : 227812 : break;
7288 : :
7289 : 0 : case OPTIMIZATION_NODE:
7290 : 0 : case TARGET_OPTION_NODE:
7291 : : /* Not implemented, see trees_out::core_vals. */
7292 : 0 : gcc_unreachable ();
7293 : 84521 : break;
7294 : :
7295 : 84521 : case TREE_BINFO:
7296 : 84521 : RT (t->binfo.common.chain);
7297 : 84521 : RT (t->binfo.offset);
7298 : 84521 : RT (t->binfo.inheritance);
7299 : 84521 : RT (t->binfo.vptr_field);
7300 : :
7301 : : /* Do not mark the vtables as USED in the address expressions
7302 : : here. */
7303 : 84521 : unused++;
7304 : 84521 : RT (t->binfo.vtable);
7305 : 84521 : RT (t->binfo.virtuals);
7306 : 84521 : RT (t->binfo.vtt_subvtt);
7307 : 84521 : RT (t->binfo.vtt_vptr);
7308 : 84521 : unused--;
7309 : :
7310 : 84521 : BINFO_BASE_ACCESSES (t) = tree_vec ();
7311 : 84521 : if (!get_overrun ())
7312 : : {
7313 : 84521 : unsigned num = vec_safe_length (BINFO_BASE_ACCESSES (t));
7314 : 108087 : for (unsigned ix = 0; ix != num; ix++)
7315 : 23566 : BINFO_BASE_APPEND (t, tree_node ());
7316 : : }
7317 : : break;
7318 : :
7319 : 1124111 : case TREE_LIST:
7320 : 1124111 : RT (t->list.purpose);
7321 : 1124111 : RT (t->list.value);
7322 : 1124111 : RT (t->list.common.chain);
7323 : 1124111 : break;
7324 : :
7325 : 741492 : case TREE_VEC:
7326 : 1994345 : for (unsigned ix = TREE_VEC_LENGTH (t); ix--;)
7327 : 1252853 : RT (TREE_VEC_ELT (t, ix));
7328 : 741492 : RT (t->type_common.common.chain);
7329 : 741492 : break;
7330 : :
7331 : : /* C++-specific nodes ... */
7332 : 86827 : case BASELINK:
7333 : 86827 : RT (((lang_tree_node *)t)->baselink.binfo);
7334 : 86827 : RTU (((lang_tree_node *)t)->baselink.functions);
7335 : 86827 : RT (((lang_tree_node *)t)->baselink.access_binfo);
7336 : 86827 : RT (((lang_tree_node *)t)->baselink.common.chain);
7337 : 86827 : break;
7338 : :
7339 : 36421 : case CONSTRAINT_INFO:
7340 : 36421 : RT (((lang_tree_node *)t)->constraint_info.template_reqs);
7341 : 36421 : RT (((lang_tree_node *)t)->constraint_info.declarator_reqs);
7342 : 36421 : RT (((lang_tree_node *)t)->constraint_info.associated_constr);
7343 : 36421 : break;
7344 : :
7345 : 6752 : case DEFERRED_NOEXCEPT:
7346 : 6752 : RT (((lang_tree_node *)t)->deferred_noexcept.pattern);
7347 : 6752 : RT (((lang_tree_node *)t)->deferred_noexcept.args);
7348 : 6752 : break;
7349 : :
7350 : 3845 : case LAMBDA_EXPR:
7351 : 3845 : RT (((lang_tree_node *)t)->lambda_expression.capture_list);
7352 : 3845 : RT (((lang_tree_node *)t)->lambda_expression.this_capture);
7353 : 3845 : RT (((lang_tree_node *)t)->lambda_expression.extra_scope);
7354 : 3845 : RT (((lang_tree_node *)t)->lambda_expression.regen_info);
7355 : 3845 : RT (((lang_tree_node *)t)->lambda_expression.extra_args);
7356 : : /* lambda_expression.pending_proxies is NULL */
7357 : 3845 : ((lang_tree_node *)t)->lambda_expression.locus
7358 : 3845 : = state->read_location (*this);
7359 : 3845 : RUC (cp_lambda_default_capture_mode_type,
7360 : : ((lang_tree_node *)t)->lambda_expression.default_capture_mode);
7361 : 3845 : RU (((lang_tree_node *)t)->lambda_expression.discriminator_scope);
7362 : 3845 : RU (((lang_tree_node *)t)->lambda_expression.discriminator_sig);
7363 : 3845 : break;
7364 : :
7365 : 506276 : case OVERLOAD:
7366 : 506276 : RT (((lang_tree_node *)t)->overload.function);
7367 : 506276 : RT (t->common.chain);
7368 : 506276 : break;
7369 : :
7370 : 0 : case PTRMEM_CST:
7371 : 0 : RT (((lang_tree_node *)t)->ptrmem.member);
7372 : 0 : break;
7373 : :
7374 : 7081 : case STATIC_ASSERT:
7375 : 7081 : RT (((lang_tree_node *)t)->static_assertion.condition);
7376 : 7081 : RT (((lang_tree_node *)t)->static_assertion.message);
7377 : 7081 : ((lang_tree_node *)t)->static_assertion.location
7378 : 7081 : = state->read_location (*this);
7379 : 7081 : break;
7380 : :
7381 : 292162 : case TEMPLATE_DECL:
7382 : : /* Streamed when reading the raw template decl itself. */
7383 : 292162 : gcc_assert (((lang_tree_node *)t)->template_decl.arguments);
7384 : 292162 : gcc_assert (((lang_tree_node *)t)->template_decl.result);
7385 : 292162 : if (DECL_UNINSTANTIATED_TEMPLATE_FRIEND_P (t))
7386 : 6325 : RT (DECL_CHAIN (t));
7387 : : break;
7388 : :
7389 : 650545 : case TEMPLATE_INFO:
7390 : 650545 : RT (((lang_tree_node *)t)->template_info.tmpl);
7391 : 650545 : RT (((lang_tree_node *)t)->template_info.args);
7392 : 650545 : RT (((lang_tree_node *)t)->template_info.partial);
7393 : 650545 : if (unsigned len = u ())
7394 : : {
7395 : 0 : auto &ac = (((lang_tree_node *)t)
7396 : : ->template_info.deferred_access_checks);
7397 : 0 : vec_alloc (ac, len);
7398 : 0 : for (unsigned ix = 0; ix != len; ix++)
7399 : : {
7400 : 0 : deferred_access_check m;
7401 : :
7402 : 0 : RT (m.binfo);
7403 : 0 : RT (m.decl);
7404 : 0 : RT (m.diag_decl);
7405 : 0 : m.loc = state->read_location (*this);
7406 : 0 : ac->quick_push (m);
7407 : : }
7408 : : }
7409 : : break;
7410 : :
7411 : 350425 : case TEMPLATE_PARM_INDEX:
7412 : 350425 : RU (((lang_tree_node *)t)->tpi.index);
7413 : 350425 : RU (((lang_tree_node *)t)->tpi.level);
7414 : 350425 : RU (((lang_tree_node *)t)->tpi.orig_level);
7415 : 350425 : RT (((lang_tree_node *)t)->tpi.decl);
7416 : 350425 : break;
7417 : :
7418 : 7599 : case TRAIT_EXPR:
7419 : 7599 : RT (((lang_tree_node *)t)->trait_expression.type1);
7420 : 7599 : RT (((lang_tree_node *)t)->trait_expression.type2);
7421 : 7599 : RUC (cp_trait_kind, ((lang_tree_node *)t)->trait_expression.kind);
7422 : 7599 : break;
7423 : :
7424 : 2 : case TU_LOCAL_ENTITY:
7425 : 2 : RT (((lang_tree_node *)t)->tu_local_entity.name);
7426 : 2 : ((lang_tree_node *)t)->tu_local_entity.loc
7427 : 2 : = state->read_location (*this);
7428 : : }
7429 : :
7430 : 13304253 : if (CODE_CONTAINS_STRUCT (code, TS_TYPED))
7431 : : {
7432 : 12335875 : tree type = tree_node ();
7433 : :
7434 : 12335875 : if (type && code == ENUMERAL_TYPE && !ENUM_FIXED_UNDERLYING_TYPE_P (t))
7435 : : {
7436 : 1646 : unsigned precision = u ();
7437 : :
7438 : 1646 : type = build_distinct_type_copy (type);
7439 : 1646 : TYPE_PRECISION (type) = precision;
7440 : 3292 : set_min_and_max_values_for_integral_type (type, precision,
7441 : 1646 : TYPE_SIGN (type));
7442 : : }
7443 : :
7444 : 12335875 : if (code != TEMPLATE_DECL)
7445 : 12043713 : t->typed.type = type;
7446 : : }
7447 : :
7448 : 13304253 : if (TREE_CODE (t) == CONSTRUCTOR)
7449 : 48197 : if (unsigned len = u ())
7450 : : {
7451 : 29655 : vec_alloc (t->constructor.elts, len);
7452 : 202919 : for (unsigned ix = 0; ix != len; ix++)
7453 : : {
7454 : 173264 : constructor_elt elt;
7455 : :
7456 : 173264 : RT (elt.index);
7457 : 173264 : RTU (elt.value);
7458 : 173264 : t->constructor.elts->quick_push (elt);
7459 : : }
7460 : : }
7461 : :
7462 : : #undef RT
7463 : : #undef RM
7464 : : #undef RU
7465 : 13304253 : return !get_overrun ();
7466 : : }
7467 : :
7468 : : void
7469 : 4473977 : trees_out::lang_decl_vals (tree t)
7470 : : {
7471 : 4473977 : const struct lang_decl *lang = DECL_LANG_SPECIFIC (t);
7472 : : #define WU(X) (u (X))
7473 : : #define WT(X) (tree_node (X))
7474 : : /* Module index already written. */
7475 : 4473977 : switch (lang->u.base.selector)
7476 : : {
7477 : 0 : default:
7478 : 0 : gcc_unreachable ();
7479 : :
7480 : 833427 : case lds_fn: /* lang_decl_fn. */
7481 : 833427 : if (streaming_p ())
7482 : : {
7483 : 416623 : if (DECL_NAME (t) && IDENTIFIER_OVL_OP_P (DECL_NAME (t)))
7484 : 69699 : WU (lang->u.fn.ovl_op_code);
7485 : : }
7486 : :
7487 : 1056048 : if (DECL_CLASS_SCOPE_P (t) || DECL_UNIQUE_FRIEND_P (t))
7488 : 633038 : WT (lang->u.fn.context);
7489 : :
7490 : 833427 : if (lang->u.fn.thunk_p)
7491 : : {
7492 : : /* The thunked-to function. */
7493 : 1176 : WT (lang->u.fn.befriending_classes);
7494 : 1176 : if (streaming_p ())
7495 : 588 : wi (lang->u.fn.u5.fixed_offset);
7496 : : }
7497 : 832251 : else if (decl_tls_wrapper_p (t))
7498 : : /* The wrapped variable. */
7499 : 18 : WT (lang->u.fn.befriending_classes);
7500 : : else
7501 : 832233 : WT (lang->u.fn.u5.cloned_function);
7502 : :
7503 : 833427 : if (FNDECL_USED_AUTO (t))
7504 : 2724 : WT (lang->u.fn.u.saved_auto_return_type);
7505 : :
7506 : 833427 : goto lds_min;
7507 : :
7508 : 3068 : case lds_decomp: /* lang_decl_decomp. */
7509 : 3068 : WT (lang->u.decomp.base);
7510 : 3068 : goto lds_min;
7511 : :
7512 : 2654189 : case lds_min: /* lang_decl_min. */
7513 : 2654189 : lds_min:
7514 : 2654189 : WT (lang->u.min.template_info);
7515 : 2654189 : {
7516 : 2654189 : tree access = lang->u.min.access;
7517 : :
7518 : : /* DECL_ACCESS needs to be maintained by the definition of the
7519 : : (derived) class that changes the access. The other users
7520 : : of DECL_ACCESS need to write it here. */
7521 : 833427 : if (!DECL_THUNK_P (t)
7522 : 3486440 : && (DECL_CONTEXT (t) && TYPE_P (DECL_CONTEXT (t))))
7523 : : access = NULL_TREE;
7524 : :
7525 : 2654189 : WT (access);
7526 : : }
7527 : : /* A friend template specialisation stashes its owning class on its
7528 : : DECL_CHAIN; we need to reconstruct this, but it needs to happen
7529 : : after we stream the template_info so readers can know this is such
7530 : : an entity. */
7531 : 2654189 : if (decl_specialization_friend_p (t))
7532 : 90 : WT (t->common.chain);
7533 : : break;
7534 : :
7535 : : case lds_ns: /* lang_decl_ns. */
7536 : : break;
7537 : :
7538 : 1819595 : case lds_parm: /* lang_decl_parm. */
7539 : 1819595 : if (streaming_p ())
7540 : : {
7541 : 619802 : WU (lang->u.parm.level);
7542 : 619802 : WU (lang->u.parm.index);
7543 : : }
7544 : : break;
7545 : : }
7546 : : #undef WU
7547 : : #undef WT
7548 : 4473977 : }
7549 : :
7550 : : bool
7551 : 1832134 : trees_in::lang_decl_vals (tree t)
7552 : : {
7553 : 1832134 : struct lang_decl *lang = DECL_LANG_SPECIFIC (t);
7554 : : #define RU(X) ((X) = u ())
7555 : : #define RT(X) ((X) = tree_node ())
7556 : :
7557 : : /* Module index already read. */
7558 : 1832134 : switch (lang->u.base.selector)
7559 : : {
7560 : 0 : default:
7561 : 0 : gcc_unreachable ();
7562 : :
7563 : 406715 : case lds_fn: /* lang_decl_fn. */
7564 : 406715 : if (DECL_NAME (t) && IDENTIFIER_OVL_OP_P (DECL_NAME (t)))
7565 : : {
7566 : 72062 : unsigned code = u ();
7567 : :
7568 : : /* Check consistency. */
7569 : 72062 : if (code >= OVL_OP_MAX
7570 : 72062 : || (ovl_op_info[IDENTIFIER_ASSIGN_OP_P (DECL_NAME (t))][code]
7571 : 72062 : .ovl_op_code) == OVL_OP_ERROR_MARK)
7572 : 0 : set_overrun ();
7573 : : else
7574 : 72062 : lang->u.fn.ovl_op_code = code;
7575 : : }
7576 : :
7577 : 506988 : if (DECL_CLASS_SCOPE_P (t) || DECL_UNIQUE_FRIEND_P (t))
7578 : 318664 : RT (lang->u.fn.context);
7579 : :
7580 : 406715 : if (lang->u.fn.thunk_p)
7581 : : {
7582 : 530 : RT (lang->u.fn.befriending_classes);
7583 : 530 : lang->u.fn.u5.fixed_offset = wi ();
7584 : : }
7585 : 406185 : else if (decl_tls_wrapper_p (t))
7586 : 15 : RT (lang->u.fn.befriending_classes);
7587 : : else
7588 : 406170 : RT (lang->u.fn.u5.cloned_function);
7589 : :
7590 : 406715 : if (FNDECL_USED_AUTO (t))
7591 : 1317 : RT (lang->u.fn.u.saved_auto_return_type);
7592 : 406715 : goto lds_min;
7593 : :
7594 : 1729 : case lds_decomp: /* lang_decl_decomp. */
7595 : 1729 : RT (lang->u.decomp.base);
7596 : 1729 : goto lds_min;
7597 : :
7598 : 1241697 : case lds_min: /* lang_decl_min. */
7599 : 1241697 : lds_min:
7600 : 1241697 : RT (lang->u.min.template_info);
7601 : 1241697 : RT (lang->u.min.access);
7602 : 1241697 : if (decl_specialization_friend_p (t))
7603 : 49 : RT (t->common.chain);
7604 : : break;
7605 : :
7606 : : case lds_ns: /* lang_decl_ns. */
7607 : : break;
7608 : :
7609 : 590302 : case lds_parm: /* lang_decl_parm. */
7610 : 590302 : RU (lang->u.parm.level);
7611 : 590302 : RU (lang->u.parm.index);
7612 : 590302 : break;
7613 : : }
7614 : : #undef RU
7615 : : #undef RT
7616 : 1832134 : return !get_overrun ();
7617 : : }
7618 : :
7619 : : /* Most of the value contents of lang_type is streamed in
7620 : : define_class. */
7621 : :
7622 : : void
7623 : 310310 : trees_out::lang_type_vals (tree t)
7624 : : {
7625 : 310310 : const struct lang_type *lang = TYPE_LANG_SPECIFIC (t);
7626 : : #define WU(X) (u (X))
7627 : : #define WT(X) (tree_node (X))
7628 : 310310 : if (streaming_p ())
7629 : 155135 : WU (lang->align);
7630 : : #undef WU
7631 : : #undef WT
7632 : 310310 : }
7633 : :
7634 : : bool
7635 : 138337 : trees_in::lang_type_vals (tree t)
7636 : : {
7637 : 138337 : struct lang_type *lang = TYPE_LANG_SPECIFIC (t);
7638 : : #define RU(X) ((X) = u ())
7639 : : #define RT(X) ((X) = tree_node ())
7640 : 138337 : RU (lang->align);
7641 : : #undef RU
7642 : : #undef RT
7643 : 138337 : return !get_overrun ();
7644 : : }
7645 : :
7646 : : /* Write out the bools of T, including information about any
7647 : : LANG_SPECIFIC information. Including allocation of any lang
7648 : : specific object. */
7649 : :
7650 : : void
7651 : 13602339 : trees_out::tree_node_bools (tree t)
7652 : : {
7653 : 13602339 : gcc_checking_assert (streaming_p ());
7654 : :
7655 : : /* We should never stream a namespace. */
7656 : 13602339 : gcc_checking_assert (TREE_CODE (t) != NAMESPACE_DECL
7657 : : || DECL_NAMESPACE_ALIAS (t));
7658 : :
7659 : 13602339 : bits_out bits = stream_bits ();
7660 : 13602339 : core_bools (t, bits);
7661 : :
7662 : 13602339 : switch (TREE_CODE_CLASS (TREE_CODE (t)))
7663 : : {
7664 : 3027317 : case tcc_declaration:
7665 : 3027317 : {
7666 : 3027317 : bool specific = DECL_LANG_SPECIFIC (t) != NULL;
7667 : 3027317 : bits.b (specific);
7668 : 3027317 : if (specific && VAR_P (t))
7669 : 307964 : bits.b (DECL_DECOMPOSITION_P (t));
7670 : 1939160 : if (specific)
7671 : 1939160 : lang_decl_bools (t, bits);
7672 : : }
7673 : : break;
7674 : :
7675 : 564977 : case tcc_type:
7676 : 564977 : {
7677 : 564977 : bool specific = (TYPE_MAIN_VARIANT (t) == t
7678 : 564977 : && TYPE_LANG_SPECIFIC (t) != NULL);
7679 : 564977 : gcc_assert (TYPE_LANG_SPECIFIC (t)
7680 : : == TYPE_LANG_SPECIFIC (TYPE_MAIN_VARIANT (t)));
7681 : :
7682 : 564977 : bits.b (specific);
7683 : 564977 : if (specific)
7684 : 155135 : lang_type_bools (t, bits);
7685 : : }
7686 : : break;
7687 : :
7688 : : default:
7689 : : break;
7690 : : }
7691 : :
7692 : 13602339 : bits.bflush ();
7693 : 13602339 : }
7694 : :
7695 : : bool
7696 : 13323138 : trees_in::tree_node_bools (tree t)
7697 : : {
7698 : 13323138 : bits_in bits = stream_bits ();
7699 : 13323138 : bool ok = core_bools (t, bits);
7700 : :
7701 : 13323138 : if (ok)
7702 : 13323138 : switch (TREE_CODE_CLASS (TREE_CODE (t)))
7703 : : {
7704 : 2877221 : case tcc_declaration:
7705 : 2877221 : if (bits.b ())
7706 : : {
7707 : 1832134 : bool decomp = VAR_P (t) && bits.b ();
7708 : :
7709 : 1832134 : ok = maybe_add_lang_decl_raw (t, decomp);
7710 : 1832134 : if (ok)
7711 : 1832134 : ok = lang_decl_bools (t, bits);
7712 : : }
7713 : : break;
7714 : :
7715 : 507195 : case tcc_type:
7716 : 507195 : if (bits.b ())
7717 : : {
7718 : 138337 : ok = maybe_add_lang_type_raw (t);
7719 : 138337 : if (ok)
7720 : 138337 : ok = lang_type_bools (t, bits);
7721 : : }
7722 : : break;
7723 : :
7724 : : default:
7725 : : break;
7726 : : }
7727 : :
7728 : 13323138 : bits.bflush ();
7729 : 13323138 : if (!ok || get_overrun ())
7730 : 0 : return false;
7731 : :
7732 : : return true;
7733 : 13323138 : }
7734 : :
7735 : :
7736 : : /* Write out the lang-specific vals of node T. */
7737 : :
7738 : : void
7739 : 35197229 : trees_out::lang_vals (tree t)
7740 : : {
7741 : 35197229 : switch (TREE_CODE_CLASS (TREE_CODE (t)))
7742 : : {
7743 : 7905481 : case tcc_declaration:
7744 : 7905481 : if (DECL_LANG_SPECIFIC (t))
7745 : 4473977 : lang_decl_vals (t);
7746 : : break;
7747 : :
7748 : 1969885 : case tcc_type:
7749 : 1969885 : if (TYPE_MAIN_VARIANT (t) == t && TYPE_LANG_SPECIFIC (t))
7750 : 310310 : lang_type_vals (t);
7751 : : break;
7752 : :
7753 : : default:
7754 : : break;
7755 : : }
7756 : 35197229 : }
7757 : :
7758 : : bool
7759 : 13304253 : trees_in::lang_vals (tree t)
7760 : : {
7761 : 13304253 : bool ok = true;
7762 : :
7763 : 13304253 : switch (TREE_CODE_CLASS (TREE_CODE (t)))
7764 : : {
7765 : 2877221 : case tcc_declaration:
7766 : 2877221 : if (DECL_LANG_SPECIFIC (t))
7767 : 1832134 : ok = lang_decl_vals (t);
7768 : : break;
7769 : :
7770 : 488310 : case tcc_type:
7771 : 488310 : if (TYPE_LANG_SPECIFIC (t))
7772 : 138337 : ok = lang_type_vals (t);
7773 : : else
7774 : 349973 : TYPE_LANG_SPECIFIC (t) = TYPE_LANG_SPECIFIC (TYPE_MAIN_VARIANT (t));
7775 : : break;
7776 : :
7777 : : default:
7778 : : break;
7779 : : }
7780 : :
7781 : 13304253 : return ok;
7782 : : }
7783 : :
7784 : : /* Write out the value fields of node T. */
7785 : :
7786 : : void
7787 : 35197229 : trees_out::tree_node_vals (tree t)
7788 : : {
7789 : 35197229 : core_vals (t);
7790 : 35197229 : lang_vals (t);
7791 : 35197229 : }
7792 : :
7793 : : bool
7794 : 13304253 : trees_in::tree_node_vals (tree t)
7795 : : {
7796 : 13304253 : bool ok = core_vals (t);
7797 : 13304253 : if (ok)
7798 : 13304253 : ok = lang_vals (t);
7799 : :
7800 : 13304253 : return ok;
7801 : : }
7802 : :
7803 : :
7804 : : /* If T is a back reference, fixed reference or NULL, write out its
7805 : : code and return WK_none. Otherwise return WK_value if we must write
7806 : : by value, or WK_normal otherwise. */
7807 : :
7808 : : walk_kind
7809 : 234009055 : trees_out::ref_node (tree t)
7810 : : {
7811 : 234009055 : if (!t)
7812 : : {
7813 : 95114728 : if (streaming_p ())
7814 : : {
7815 : : /* NULL_TREE -> tt_null. */
7816 : 36293267 : null_count++;
7817 : 36293267 : i (tt_null);
7818 : : }
7819 : 95114728 : return WK_none;
7820 : : }
7821 : :
7822 : 138894327 : if (!TREE_VISITED (t))
7823 : : return WK_normal;
7824 : :
7825 : : /* An already-visited tree. It must be in the map. */
7826 : 81666108 : int val = get_tag (t);
7827 : :
7828 : 81666108 : if (val == tag_value)
7829 : : /* An entry we should walk into. */
7830 : : return WK_value;
7831 : :
7832 : 80330633 : const char *kind;
7833 : :
7834 : 80330633 : if (val <= tag_backref)
7835 : : {
7836 : : /* Back reference -> -ve number */
7837 : 61244985 : if (streaming_p ())
7838 : 29022298 : i (val);
7839 : : kind = "backref";
7840 : : }
7841 : 19085648 : else if (val >= tag_fixed)
7842 : : {
7843 : : /* Fixed reference -> tt_fixed */
7844 : 19085648 : val -= tag_fixed;
7845 : 19085648 : if (streaming_p ())
7846 : 6761610 : i (tt_fixed), u (val);
7847 : : kind = "fixed";
7848 : : }
7849 : :
7850 : 80330633 : if (streaming_p ())
7851 : : {
7852 : 35783908 : back_ref_count++;
7853 : 35783908 : dump (dumper::TREE)
7854 : 14184 : && dump ("Wrote %s:%d %C:%N%S", kind, val, TREE_CODE (t), t, t);
7855 : : }
7856 : : return WK_none;
7857 : : }
7858 : :
7859 : : tree
7860 : 28171416 : trees_in::back_ref (int tag)
7861 : : {
7862 : 28171416 : tree res = NULL_TREE;
7863 : :
7864 : 28171416 : if (tag < 0 && unsigned (~tag) < back_refs.length ())
7865 : 28171416 : res = back_refs[~tag];
7866 : :
7867 : 28171416 : if (!res
7868 : : /* Checking TREE_CODE is a dereference, so we know this is not a
7869 : : wild pointer. Checking the code provides evidence we've not
7870 : : corrupted something. */
7871 : 28171416 : || TREE_CODE (res) >= MAX_TREE_CODES)
7872 : 0 : set_overrun ();
7873 : : else
7874 : 28185953 : dump (dumper::TREE) && dump ("Read backref:%d found %C:%N%S", tag,
7875 : : TREE_CODE (res), res, res);
7876 : 28171416 : return res;
7877 : : }
7878 : :
7879 : : unsigned
7880 : 3100487 : trees_out::add_indirect_tpl_parms (tree parms)
7881 : : {
7882 : 3100487 : unsigned len = 0;
7883 : 5811710 : for (; parms; parms = TREE_CHAIN (parms), len++)
7884 : : {
7885 : 3273577 : if (TREE_VISITED (parms))
7886 : : break;
7887 : :
7888 : 2711223 : int tag = insert (parms);
7889 : 2711223 : if (streaming_p ())
7890 : 2711514 : dump (dumper::TREE)
7891 : 153 : && dump ("Indirect:%d template's parameter %u %C:%N",
7892 : 153 : tag, len, TREE_CODE (parms), parms);
7893 : : }
7894 : :
7895 : 3100487 : if (streaming_p ())
7896 : 509794 : u (len);
7897 : :
7898 : 3100487 : return len;
7899 : : }
7900 : :
7901 : : unsigned
7902 : 460986 : trees_in::add_indirect_tpl_parms (tree parms)
7903 : : {
7904 : 460986 : unsigned len = u ();
7905 : 836956 : for (unsigned ix = 0; ix != len; parms = TREE_CHAIN (parms), ix++)
7906 : : {
7907 : 375970 : int tag = insert (parms);
7908 : 376414 : dump (dumper::TREE)
7909 : 159 : && dump ("Indirect:%d template's parameter %u %C:%N",
7910 : 159 : tag, ix, TREE_CODE (parms), parms);
7911 : : }
7912 : :
7913 : 460986 : return len;
7914 : : }
7915 : :
7916 : : /* We've just found DECL by name. Insert nodes that come with it, but
7917 : : cannot be found by name, so we'll not accidentally walk into them. */
7918 : :
7919 : : void
7920 : 7113461 : trees_out::add_indirects (tree decl)
7921 : : {
7922 : 7113461 : unsigned count = 0;
7923 : :
7924 : : // FIXME:OPTIMIZATION We'll eventually want default fn parms of
7925 : : // templates and perhaps default template parms too. The former can
7926 : : // be referenced from instantiations (as they are lazily
7927 : : // instantiated). Also (deferred?) exception specifications of
7928 : : // templates. See the note about PARM_DECLs in trees_out::decl_node.
7929 : 7113461 : tree inner = decl;
7930 : 7113461 : if (TREE_CODE (decl) == TEMPLATE_DECL)
7931 : : {
7932 : 3100487 : count += add_indirect_tpl_parms (DECL_TEMPLATE_PARMS (decl));
7933 : :
7934 : 3100487 : inner = DECL_TEMPLATE_RESULT (decl);
7935 : 3100487 : int tag = insert (inner);
7936 : 3100487 : if (streaming_p ())
7937 : 509794 : dump (dumper::TREE)
7938 : 219 : && dump ("Indirect:%d template's result %C:%N",
7939 : 219 : tag, TREE_CODE (inner), inner);
7940 : 3100487 : count++;
7941 : : }
7942 : :
7943 : 7113461 : if (TREE_CODE (inner) == TYPE_DECL)
7944 : : {
7945 : : /* Make sure the type is in the map too. Otherwise we get
7946 : : different RECORD_TYPEs for the same type, and things go
7947 : : south. */
7948 : 3871760 : tree type = TREE_TYPE (inner);
7949 : 3871760 : gcc_checking_assert (DECL_ORIGINAL_TYPE (inner)
7950 : : || TYPE_NAME (type) == inner);
7951 : 3871760 : int tag = insert (type);
7952 : 3871760 : if (streaming_p ())
7953 : 438591 : dump (dumper::TREE) && dump ("Indirect:%d decl's type %C:%N", tag,
7954 : 362 : TREE_CODE (type), type);
7955 : 3871760 : count++;
7956 : : }
7957 : :
7958 : 7113461 : if (streaming_p ())
7959 : : {
7960 : 1086083 : u (count);
7961 : 1086612 : dump (dumper::TREE) && dump ("Inserted %u indirects", count);
7962 : : }
7963 : 7113461 : }
7964 : :
7965 : : bool
7966 : 939063 : trees_in::add_indirects (tree decl)
7967 : : {
7968 : 939063 : unsigned count = 0;
7969 : :
7970 : 939063 : tree inner = decl;
7971 : 939063 : if (TREE_CODE (inner) == TEMPLATE_DECL)
7972 : : {
7973 : 460986 : count += add_indirect_tpl_parms (DECL_TEMPLATE_PARMS (decl));
7974 : :
7975 : 460986 : inner = DECL_TEMPLATE_RESULT (decl);
7976 : 460986 : int tag = insert (inner);
7977 : 460986 : dump (dumper::TREE)
7978 : 228 : && dump ("Indirect:%d templates's result %C:%N", tag,
7979 : 228 : TREE_CODE (inner), inner);
7980 : 460986 : count++;
7981 : : }
7982 : :
7983 : 939063 : if (TREE_CODE (inner) == TYPE_DECL)
7984 : : {
7985 : 357785 : tree type = TREE_TYPE (inner);
7986 : 357785 : gcc_checking_assert (DECL_ORIGINAL_TYPE (inner)
7987 : : || TYPE_NAME (type) == inner);
7988 : 357785 : int tag = insert (type);
7989 : 357785 : dump (dumper::TREE)
7990 : 362 : && dump ("Indirect:%d decl's type %C:%N", tag, TREE_CODE (type), type);
7991 : 357785 : count++;
7992 : : }
7993 : :
7994 : 939668 : dump (dumper::TREE) && dump ("Inserted %u indirects", count);
7995 : 939063 : return count == u ();
7996 : : }
7997 : :
7998 : : /* Stream a template parameter. There are 4.5 kinds of parameter:
7999 : : a) Template - TEMPLATE_DECL->TYPE_DECL->TEMPLATE_TEMPLATE_PARM
8000 : : TEMPLATE_TYPE_PARM_INDEX TPI
8001 : : b) Type - TYPE_DECL->TEMPLATE_TYPE_PARM TEMPLATE_TYPE_PARM_INDEX TPI
8002 : : c.1) NonTYPE - PARM_DECL DECL_INITIAL TPI We meet this first
8003 : : c.2) NonTYPE - CONST_DECL DECL_INITIAL Same TPI
8004 : : d) BoundTemplate - TYPE_DECL->BOUND_TEMPLATE_TEMPLATE_PARM
8005 : : TEMPLATE_TYPE_PARM_INDEX->TPI
8006 : : TEMPLATE_TEMPLATE_PARM_INFO->TEMPLATE_INFO
8007 : :
8008 : : All of these point to a TEMPLATE_PARM_INDEX, and #B also has a TEMPLATE_INFO
8009 : : */
8010 : :
8011 : : void
8012 : 1784835 : trees_out::tpl_parm_value (tree parm)
8013 : : {
8014 : 1784835 : gcc_checking_assert (DECL_P (parm) && DECL_TEMPLATE_PARM_P (parm));
8015 : :
8016 : 1784835 : int parm_tag = insert (parm);
8017 : 1784835 : if (streaming_p ())
8018 : : {
8019 : 400094 : i (tt_tpl_parm);
8020 : 400094 : dump (dumper::TREE) && dump ("Writing template parm:%d %C:%N",
8021 : 114 : parm_tag, TREE_CODE (parm), parm);
8022 : 400094 : start (parm);
8023 : 400094 : tree_node_bools (parm);
8024 : : }
8025 : :
8026 : 1784835 : tree inner = parm;
8027 : 1784835 : if (TREE_CODE (inner) == TEMPLATE_DECL)
8028 : : {
8029 : 5388 : inner = DECL_TEMPLATE_RESULT (inner);
8030 : 5388 : int inner_tag = insert (inner);
8031 : 5388 : if (streaming_p ())
8032 : : {
8033 : 1398 : dump (dumper::TREE) && dump ("Writing inner template parm:%d %C:%N",
8034 : 0 : inner_tag, TREE_CODE (inner), inner);
8035 : 1398 : start (inner);
8036 : 1398 : tree_node_bools (inner);
8037 : : }
8038 : : }
8039 : :
8040 : 1784835 : tree type = NULL_TREE;
8041 : 1784835 : if (TREE_CODE (inner) == TYPE_DECL)
8042 : : {
8043 : 1605597 : type = TREE_TYPE (inner);
8044 : 1605597 : int type_tag = insert (type);
8045 : 1605597 : if (streaming_p ())
8046 : : {
8047 : 360983 : dump (dumper::TREE) && dump ("Writing template parm type:%d %C:%N",
8048 : 108 : type_tag, TREE_CODE (type), type);
8049 : 360983 : start (type);
8050 : 360983 : tree_node_bools (type);
8051 : : }
8052 : : }
8053 : :
8054 : 1784835 : if (inner != parm)
8055 : : {
8056 : : /* This is a template-template parameter. */
8057 : 5388 : unsigned tpl_levels = 0;
8058 : 5388 : tpl_header (parm, &tpl_levels);
8059 : 5388 : tpl_parms_fini (parm, tpl_levels);
8060 : : }
8061 : :
8062 : 1784835 : tree_node_vals (parm);
8063 : 1784835 : if (inner != parm)
8064 : 5388 : tree_node_vals (inner);
8065 : 1784835 : if (type)
8066 : : {
8067 : 1605597 : tree_node_vals (type);
8068 : 1605597 : if (DECL_NAME (inner) == auto_identifier
8069 : 1605597 : || DECL_NAME (inner) == decltype_auto_identifier)
8070 : : {
8071 : : /* Placeholder auto. */
8072 : 72147 : tree_node (DECL_INITIAL (inner));
8073 : 72147 : tree_node (DECL_SIZE_UNIT (inner));
8074 : : }
8075 : : }
8076 : :
8077 : 1784835 : if (streaming_p ())
8078 : 400094 : dump (dumper::TREE) && dump ("Wrote template parm:%d %C:%N",
8079 : 114 : parm_tag, TREE_CODE (parm), parm);
8080 : 1784835 : }
8081 : :
8082 : : tree
8083 : 366196 : trees_in::tpl_parm_value ()
8084 : : {
8085 : 366196 : tree parm = start ();
8086 : 366196 : if (!parm || !tree_node_bools (parm))
8087 : 0 : return NULL_TREE;
8088 : :
8089 : 366196 : int parm_tag = insert (parm);
8090 : 366196 : dump (dumper::TREE) && dump ("Reading template parm:%d %C:%N",
8091 : 198 : parm_tag, TREE_CODE (parm), parm);
8092 : :
8093 : 366196 : tree inner = parm;
8094 : 366196 : if (TREE_CODE (inner) == TEMPLATE_DECL)
8095 : : {
8096 : 1088 : inner = start ();
8097 : 1088 : if (!inner || !tree_node_bools (inner))
8098 : 0 : return NULL_TREE;
8099 : 1088 : int inner_tag = insert (inner);
8100 : 1088 : dump (dumper::TREE) && dump ("Reading inner template parm:%d %C:%N",
8101 : 0 : inner_tag, TREE_CODE (inner), inner);
8102 : 1088 : DECL_TEMPLATE_RESULT (parm) = inner;
8103 : : }
8104 : :
8105 : 366196 : tree type = NULL_TREE;
8106 : 366196 : if (TREE_CODE (inner) == TYPE_DECL)
8107 : : {
8108 : 328884 : type = start ();
8109 : 328884 : if (!type || !tree_node_bools (type))
8110 : 0 : return NULL_TREE;
8111 : 328884 : int type_tag = insert (type);
8112 : 328884 : dump (dumper::TREE) && dump ("Reading template parm type:%d %C:%N",
8113 : 120 : type_tag, TREE_CODE (type), type);
8114 : :
8115 : 328884 : TREE_TYPE (inner) = TREE_TYPE (parm) = type;
8116 : 328884 : TYPE_NAME (type) = parm;
8117 : : }
8118 : :
8119 : 366196 : if (inner != parm)
8120 : : {
8121 : : /* A template template parameter. */
8122 : 1088 : unsigned tpl_levels = 0;
8123 : 1088 : tpl_header (parm, &tpl_levels);
8124 : 1088 : tpl_parms_fini (parm, tpl_levels);
8125 : : }
8126 : :
8127 : 366196 : tree_node_vals (parm);
8128 : 366196 : if (inner != parm)
8129 : 1088 : tree_node_vals (inner);
8130 : 366196 : if (type)
8131 : : {
8132 : 328884 : tree_node_vals (type);
8133 : 328884 : if (DECL_NAME (inner) == auto_identifier
8134 : 328884 : || DECL_NAME (inner) == decltype_auto_identifier)
8135 : : {
8136 : : /* Placeholder auto. */
8137 : 30088 : DECL_INITIAL (inner) = tree_node ();
8138 : 30088 : DECL_SIZE_UNIT (inner) = tree_node ();
8139 : : }
8140 : 328884 : if (TYPE_CANONICAL (type))
8141 : : {
8142 : 328884 : gcc_checking_assert (TYPE_CANONICAL (type) == type);
8143 : 328884 : TYPE_CANONICAL (type) = canonical_type_parameter (type);
8144 : : }
8145 : : }
8146 : :
8147 : 366196 : dump (dumper::TREE) && dump ("Read template parm:%d %C:%N",
8148 : 198 : parm_tag, TREE_CODE (parm), parm);
8149 : :
8150 : : return parm;
8151 : : }
8152 : :
8153 : : void
8154 : 1153025 : trees_out::install_entity (tree decl, depset *dep)
8155 : : {
8156 : 1153025 : gcc_checking_assert (streaming_p ());
8157 : :
8158 : : /* Write the entity index, so we can insert it as soon as we
8159 : : know this is new. */
8160 : 1153025 : u (dep ? dep->cluster + 1 : 0);
8161 : 1153025 : if (CHECKING_P && dep)
8162 : : {
8163 : : /* Add it to the entity map, such that we can tell it is
8164 : : part of us. */
8165 : 858105 : bool existed;
8166 : 858105 : unsigned *slot = &entity_map->get_or_insert
8167 : 858105 : (DECL_UID (decl), &existed);
8168 : 858105 : if (existed)
8169 : : /* If it existed, it should match. */
8170 : 1302 : gcc_checking_assert (decl == (*entity_ary)[*slot]);
8171 : 858105 : *slot = ~dep->cluster;
8172 : : }
8173 : 1153025 : }
8174 : :
8175 : : bool
8176 : 1077604 : trees_in::install_entity (tree decl)
8177 : : {
8178 : 1077604 : unsigned entity_index = u ();
8179 : 1077604 : if (!entity_index)
8180 : : return false;
8181 : :
8182 : 783876 : if (entity_index > state->entity_num)
8183 : : {
8184 : 0 : set_overrun ();
8185 : 0 : return false;
8186 : : }
8187 : :
8188 : : /* Insert the real decl into the entity ary. */
8189 : 783876 : unsigned ident = state->entity_lwm + entity_index - 1;
8190 : 783876 : (*entity_ary)[ident] = decl;
8191 : :
8192 : : /* And into the entity map, if it's not already there. */
8193 : 783876 : tree not_tmpl = STRIP_TEMPLATE (decl);
8194 : 783876 : if (!DECL_LANG_SPECIFIC (not_tmpl)
8195 : 1481860 : || !DECL_MODULE_ENTITY_P (not_tmpl))
8196 : : {
8197 : : /* We don't want to use retrofit_lang_decl directly so that we aren't
8198 : : affected by the language state when we load in. */
8199 : 783035 : if (!DECL_LANG_SPECIFIC (not_tmpl))
8200 : : {
8201 : 85892 : maybe_add_lang_decl_raw (not_tmpl, false);
8202 : 85892 : SET_DECL_LANGUAGE (not_tmpl, lang_cplusplus);
8203 : : }
8204 : 783035 : DECL_MODULE_ENTITY_P (not_tmpl) = true;
8205 : :
8206 : : /* Insert into the entity hash (it cannot already be there). */
8207 : 783035 : bool existed;
8208 : 783035 : unsigned &slot = entity_map->get_or_insert (DECL_UID (decl), &existed);
8209 : 783035 : gcc_checking_assert (!existed);
8210 : 783035 : slot = ident;
8211 : : }
8212 : : else
8213 : : {
8214 : 841 : unsigned *slot = entity_map->get (DECL_UID (decl));
8215 : :
8216 : : /* The entity must be in the entity map already. However, DECL may
8217 : : be the DECL_TEMPLATE_RESULT of an existing partial specialisation
8218 : : if we matched it while streaming another instantiation; in this
8219 : : case we already registered that TEMPLATE_DECL. */
8220 : 841 : if (!slot)
8221 : : {
8222 : 9 : tree type = TREE_TYPE (decl);
8223 : 9 : gcc_checking_assert (TREE_CODE (decl) == TYPE_DECL
8224 : : && CLASS_TYPE_P (type)
8225 : : && CLASSTYPE_TEMPLATE_SPECIALIZATION (type));
8226 : 9 : slot = entity_map->get (DECL_UID (CLASSTYPE_TI_TEMPLATE (type)));
8227 : : }
8228 : 9 : gcc_checking_assert (slot);
8229 : :
8230 : 841 : if (state->is_partition ())
8231 : : {
8232 : : /* The decl is already in the entity map, but we see it again now
8233 : : from a partition: we want to overwrite if the original decl
8234 : : wasn't also from a (possibly different) partition. Otherwise,
8235 : : for things like template instantiations, make_dependency might
8236 : : not realise that this is also provided from a partition and
8237 : : should be considered part of this module (and thus always
8238 : : emitted into the primary interface's CMI). */
8239 : 150 : module_state *imp = import_entity_module (*slot);
8240 : 150 : if (!imp->is_partition ())
8241 : 24 : *slot = ident;
8242 : : }
8243 : : }
8244 : :
8245 : : return true;
8246 : : }
8247 : :
8248 : : static bool has_definition (tree decl);
8249 : :
8250 : : /* DECL is a decl node that must be written by value. DEP is the
8251 : : decl's depset. */
8252 : :
8253 : : void
8254 : 3176985 : trees_out::decl_value (tree decl, depset *dep)
8255 : : {
8256 : : /* We should not be writing clones or template parms. */
8257 : 3176985 : gcc_checking_assert (DECL_P (decl)
8258 : : && !DECL_CLONED_FUNCTION_P (decl)
8259 : : && !DECL_TEMPLATE_PARM_P (decl));
8260 : :
8261 : : /* We should never be writing non-typedef ptrmemfuncs by value. */
8262 : 3176985 : gcc_checking_assert (TREE_CODE (decl) != TYPE_DECL
8263 : : || DECL_ORIGINAL_TYPE (decl)
8264 : : || !TYPE_PTRMEMFUNC_P (TREE_TYPE (decl)));
8265 : :
8266 : : /* There's no need to walk any of the contents of a known TU-local entity,
8267 : : since importers should never see any of it regardless. But make sure we
8268 : : at least note its location so importers can use it for diagnostics. */
8269 : 3176985 : if (dep && dep->is_tu_local ())
8270 : : {
8271 : 434 : gcc_checking_assert (is_initial_scan ());
8272 : 434 : insert (decl, WK_value);
8273 : 434 : state->note_location (DECL_SOURCE_LOCATION (decl));
8274 : 434 : return;
8275 : : }
8276 : :
8277 : 3176551 : merge_kind mk = get_merge_kind (decl, dep);
8278 : :
8279 : 3176551 : if (CHECKING_P)
8280 : : {
8281 : : /* Never start in the middle of a template. */
8282 : 3176551 : int use_tpl = -1;
8283 : 3176551 : if (tree ti = node_template_info (decl, use_tpl))
8284 : 1077391 : gcc_checking_assert (TREE_CODE (TI_TEMPLATE (ti)) == OVERLOAD
8285 : : || TREE_CODE (TI_TEMPLATE (ti)) == FIELD_DECL
8286 : : || (DECL_TEMPLATE_RESULT (TI_TEMPLATE (ti))
8287 : : != decl));
8288 : : }
8289 : :
8290 : 3176551 : if (streaming_p ())
8291 : : {
8292 : : /* A new node -> tt_decl. */
8293 : 1153025 : decl_val_count++;
8294 : 1153025 : i (tt_decl);
8295 : 1153025 : u (mk);
8296 : 1153025 : start (decl);
8297 : :
8298 : 1153025 : if (mk != MK_unique)
8299 : : {
8300 : 977135 : bits_out bits = stream_bits ();
8301 : 977135 : if (!(mk & MK_template_mask) && !state->is_header ())
8302 : : {
8303 : : /* Tell the importer whether this is a global module entity,
8304 : : or a module entity. */
8305 : 119017 : tree o = get_originating_module_decl (decl);
8306 : 119017 : bool is_attached = false;
8307 : :
8308 : 119017 : tree not_tmpl = STRIP_TEMPLATE (o);
8309 : 119017 : if (DECL_LANG_SPECIFIC (not_tmpl)
8310 : 182729 : && DECL_MODULE_ATTACH_P (not_tmpl))
8311 : : is_attached = true;
8312 : :
8313 : 119017 : bits.b (is_attached);
8314 : : }
8315 : 977135 : bits.b (dep && dep->has_defn ());
8316 : 977135 : }
8317 : 1153025 : tree_node_bools (decl);
8318 : : }
8319 : :
8320 : 3176551 : int tag = insert (decl, WK_value);
8321 : 3176551 : if (streaming_p ())
8322 : 1153025 : dump (dumper::TREE)
8323 : 683 : && dump ("Writing %s:%d %C:%N%S", merge_kind_name[mk], tag,
8324 : 683 : TREE_CODE (decl), decl, decl);
8325 : :
8326 : 3176551 : tree inner = decl;
8327 : 3176551 : int inner_tag = 0;
8328 : 3176551 : if (TREE_CODE (decl) == TEMPLATE_DECL)
8329 : : {
8330 : 913765 : inner = DECL_TEMPLATE_RESULT (decl);
8331 : 913765 : inner_tag = insert (inner, WK_value);
8332 : :
8333 : : /* On stream-in we assume that a template and its result will
8334 : : have the same type. */
8335 : 913765 : gcc_checking_assert (TREE_TYPE (decl) == TREE_TYPE (inner));
8336 : :
8337 : 913765 : if (streaming_p ())
8338 : : {
8339 : 304572 : int code = TREE_CODE (inner);
8340 : 304572 : u (code);
8341 : 304572 : start (inner, true);
8342 : 304572 : tree_node_bools (inner);
8343 : 304572 : dump (dumper::TREE)
8344 : 132 : && dump ("Writing %s:%d %C:%N%S", merge_kind_name[mk], inner_tag,
8345 : 132 : TREE_CODE (inner), inner, inner);
8346 : : }
8347 : : }
8348 : :
8349 : 3176551 : tree type = NULL_TREE;
8350 : 3176551 : int type_tag = 0;
8351 : 3176551 : tree stub_decl = NULL_TREE;
8352 : 3176551 : int stub_tag = 0;
8353 : 3176551 : if (TREE_CODE (inner) == TYPE_DECL)
8354 : : {
8355 : 1187253 : type = TREE_TYPE (inner);
8356 : 1187253 : bool has_type = (type == TYPE_MAIN_VARIANT (type)
8357 : 1187253 : && TYPE_NAME (type) == inner);
8358 : :
8359 : 1187253 : if (streaming_p ())
8360 : 400261 : u (has_type ? TREE_CODE (type) : 0);
8361 : :
8362 : 1187253 : if (has_type)
8363 : : {
8364 : 546359 : type_tag = insert (type, WK_value);
8365 : 546359 : if (streaming_p ())
8366 : : {
8367 : 182099 : start (type, true);
8368 : 182099 : tree_node_bools (type);
8369 : 182099 : dump (dumper::TREE)
8370 : 155 : && dump ("Writing type:%d %C:%N", type_tag,
8371 : 155 : TREE_CODE (type), type);
8372 : : }
8373 : :
8374 : 546359 : stub_decl = TYPE_STUB_DECL (type);
8375 : 546359 : bool has_stub = inner != stub_decl;
8376 : 546359 : if (streaming_p ())
8377 : 182099 : u (has_stub ? TREE_CODE (stub_decl) : 0);
8378 : 546359 : if (has_stub)
8379 : : {
8380 : 2253 : stub_tag = insert (stub_decl);
8381 : 2253 : if (streaming_p ())
8382 : : {
8383 : 750 : start (stub_decl, true);
8384 : 750 : tree_node_bools (stub_decl);
8385 : 750 : dump (dumper::TREE)
8386 : 0 : && dump ("Writing stub_decl:%d %C:%N", stub_tag,
8387 : 0 : TREE_CODE (stub_decl), stub_decl);
8388 : : }
8389 : : }
8390 : : else
8391 : : stub_decl = NULL_TREE;
8392 : : }
8393 : : else
8394 : : /* Regular typedef. */
8395 : : type = NULL_TREE;
8396 : : }
8397 : :
8398 : : /* Stream the container, we want it correctly canonicalized before
8399 : : we start emitting keys for this decl. */
8400 : 3176551 : tree container = decl_container (decl);
8401 : 3176551 : unsigned tpl_levels = 0;
8402 : :
8403 : : /* Also tell the importer whether this is a temploid friend attached
8404 : : to a different module (which has implications for merging), so that
8405 : : importers can reconstruct this information on stream-in. */
8406 : 3176551 : if (TREE_CODE (inner) == FUNCTION_DECL || TREE_CODE (inner) == TYPE_DECL)
8407 : : {
8408 : 2436598 : tree* temploid_friend_slot = imported_temploid_friends->get (decl);
8409 : 2436598 : gcc_checking_assert (!temploid_friend_slot || *temploid_friend_slot);
8410 : 2436598 : tree_node (temploid_friend_slot ? *temploid_friend_slot : NULL_TREE);
8411 : : }
8412 : :
8413 : 3176551 : {
8414 : 3176551 : auto wmk = make_temp_override (dep_hash->writing_merge_key, true);
8415 : 3176551 : if (decl != inner)
8416 : 913765 : tpl_header (decl, &tpl_levels);
8417 : 3176551 : if (TREE_CODE (inner) == FUNCTION_DECL)
8418 : 1249345 : fn_parms_init (inner);
8419 : :
8420 : : /* Now write out the merging information, and then really
8421 : : install the tag values. */
8422 : 3176551 : key_mergeable (tag, mk, decl, inner, container, dep);
8423 : :
8424 : 3176551 : if (streaming_p ())
8425 : 3179313 : dump (dumper::MERGE)
8426 : 798 : && dump ("Wrote:%d's %s merge key %C:%N", tag,
8427 : 798 : merge_kind_name[mk], TREE_CODE (decl), decl);
8428 : 3176551 : }
8429 : :
8430 : 3176551 : if (TREE_CODE (inner) == FUNCTION_DECL)
8431 : 3176551 : fn_parms_fini (inner);
8432 : :
8433 : 3176551 : if (!is_key_order ())
8434 : 2317119 : tree_node_vals (decl);
8435 : :
8436 : 3176551 : if (inner_tag)
8437 : : {
8438 : 913765 : if (!is_key_order ())
8439 : 609193 : tree_node_vals (inner);
8440 : 913765 : tpl_parms_fini (decl, tpl_levels);
8441 : : }
8442 : :
8443 : 3176551 : if (type && !is_key_order ())
8444 : : {
8445 : 364260 : tree_node_vals (type);
8446 : 364260 : if (stub_decl)
8447 : 1503 : tree_node_vals (stub_decl);
8448 : : }
8449 : :
8450 : 3176551 : if (!is_key_order ())
8451 : : {
8452 : 2317119 : if (mk & MK_template_mask
8453 : 1633769 : || mk == MK_partial
8454 : 1633769 : || mk == MK_friend_spec)
8455 : : {
8456 : 30402 : if (mk != MK_partial)
8457 : : {
8458 : : // FIXME: We should make use of the merge-key by
8459 : : // exposing it outside of key_mergeable. But this gets
8460 : : // the job done.
8461 : 683350 : auto *entry = reinterpret_cast <spec_entry *> (dep->deps[0]);
8462 : :
8463 : 683350 : if (streaming_p ())
8464 : 341675 : u (get_mergeable_specialization_flags (mk & MK_tmpl_decl_mask,
8465 : : entry->tmpl, decl));
8466 : 683350 : tree_node (entry->tmpl);
8467 : 683350 : tree_node (entry->args);
8468 : : }
8469 : : else
8470 : : {
8471 : 30402 : tree ti = get_template_info (inner);
8472 : 30402 : tree_node (TI_TEMPLATE (ti));
8473 : 30402 : tree_node (TI_ARGS (ti));
8474 : : }
8475 : : }
8476 : 2317119 : tree_node (get_constraints (decl));
8477 : : }
8478 : :
8479 : 3176551 : if (streaming_p ())
8480 : : {
8481 : : /* Do not stray outside this section. */
8482 : 1153025 : gcc_checking_assert (!dep || dep->section == dep_hash->section);
8483 : :
8484 : : /* Write the entity index, so we can insert it as soon as we
8485 : : know this is new. */
8486 : 1153025 : install_entity (decl, dep);
8487 : : }
8488 : :
8489 : 3176551 : if (DECL_LANG_SPECIFIC (inner)
8490 : 2873713 : && DECL_MODULE_KEYED_DECLS_P (inner)
8491 : 3177082 : && !is_key_order ())
8492 : : {
8493 : : /* Stream the keyed entities. */
8494 : 358 : auto *attach_vec = keyed_table->get (inner);
8495 : 358 : unsigned num = attach_vec->length ();
8496 : 358 : if (streaming_p ())
8497 : 173 : u (num);
8498 : 739 : for (unsigned ix = 0; ix != num; ix++)
8499 : : {
8500 : 381 : tree attached = (*attach_vec)[ix];
8501 : 381 : tree_node (attached);
8502 : 381 : if (streaming_p ())
8503 : 396 : dump (dumper::MERGE)
8504 : 15 : && dump ("Written %d[%u] attached decl %N", tag, ix, attached);
8505 : : }
8506 : : }
8507 : :
8508 : 3176551 : bool is_typedef = false;
8509 : 3176551 : if (!type && TREE_CODE (inner) == TYPE_DECL)
8510 : : {
8511 : 640894 : tree t = TREE_TYPE (inner);
8512 : 640894 : unsigned tdef_flags = 0;
8513 : 640894 : if (DECL_ORIGINAL_TYPE (inner)
8514 : 640894 : && TYPE_NAME (TREE_TYPE (inner)) == inner)
8515 : : {
8516 : 640894 : tdef_flags |= 1;
8517 : 640894 : if (TYPE_STRUCTURAL_EQUALITY_P (t)
8518 : 138101 : && TYPE_DEPENDENT_P_VALID (t)
8519 : 772797 : && TYPE_DEPENDENT_P (t))
8520 : : tdef_flags |= 2;
8521 : : }
8522 : 640894 : if (streaming_p ())
8523 : 218162 : u (tdef_flags);
8524 : :
8525 : 640894 : if (tdef_flags & 1)
8526 : : {
8527 : : /* A typedef type. */
8528 : 640894 : int type_tag = insert (t);
8529 : 640894 : if (streaming_p ())
8530 : 218162 : dump (dumper::TREE)
8531 : 206 : && dump ("Cloned:%d %s %C:%N", type_tag,
8532 : : tdef_flags & 2 ? "depalias" : "typedef",
8533 : 206 : TREE_CODE (t), t);
8534 : :
8535 : : is_typedef = true;
8536 : : }
8537 : : }
8538 : :
8539 : 3176551 : if (streaming_p () && DECL_MAYBE_IN_CHARGE_CDTOR_P (decl))
8540 : : {
8541 : 88073 : bool cloned_p
8542 : 88073 : = (DECL_CHAIN (decl) && DECL_CLONED_FUNCTION_P (DECL_CHAIN (decl)));
8543 : 61566 : bool needs_vtt_parm_p
8544 : 61566 : = (cloned_p && CLASSTYPE_VBASECLASSES (DECL_CONTEXT (decl)));
8545 : 61566 : bool omit_inherited_parms_p
8546 : 61566 : = (cloned_p && DECL_MAYBE_IN_CHARGE_CONSTRUCTOR_P (decl)
8547 : 48791 : && base_ctor_omit_inherited_parms (decl));
8548 : 88073 : unsigned flags = (int (cloned_p) << 0
8549 : 88073 : | int (needs_vtt_parm_p) << 1
8550 : 88073 : | int (omit_inherited_parms_p) << 2);
8551 : 88073 : u (flags);
8552 : 88152 : dump (dumper::TREE) && dump ("CDTOR %N is %scloned",
8553 : : decl, cloned_p ? "" : "not ");
8554 : : }
8555 : :
8556 : 3176551 : if (streaming_p () && VAR_P (decl) && CP_DECL_THREAD_LOCAL_P (decl))
8557 : 174 : u (decl_tls_model (decl));
8558 : :
8559 : 3176551 : if (streaming_p ())
8560 : 1153025 : dump (dumper::TREE) && dump ("Written decl:%d %C:%N", tag,
8561 : 683 : TREE_CODE (decl), decl);
8562 : :
8563 : 3176551 : if (NAMESPACE_SCOPE_P (inner))
8564 : 1894488 : gcc_checking_assert (!dep == (VAR_OR_FUNCTION_DECL_P (inner)
8565 : : && DECL_LOCAL_DECL_P (inner)));
8566 : 2229184 : else if ((TREE_CODE (inner) == TYPE_DECL
8567 : 634245 : && !is_typedef
8568 : 123719 : && TYPE_NAME (TREE_TYPE (inner)) == inner)
8569 : 2739710 : || TREE_CODE (inner) == FUNCTION_DECL)
8570 : : {
8571 : 1039293 : bool write_defn = !dep && has_definition (decl);
8572 : 1039293 : if (streaming_p ())
8573 : 346583 : u (write_defn);
8574 : 1039293 : if (write_defn)
8575 : 6 : write_definition (decl);
8576 : : }
8577 : : }
8578 : :
8579 : : tree
8580 : 1077604 : trees_in::decl_value ()
8581 : : {
8582 : 1077604 : int tag = 0;
8583 : 1077604 : bool is_attached = false;
8584 : 1077604 : bool has_defn = false;
8585 : 1077604 : unsigned mk_u = u ();
8586 : 1077604 : if (mk_u >= MK_hwm || !merge_kind_name[mk_u])
8587 : : {
8588 : 0 : set_overrun ();
8589 : 0 : return NULL_TREE;
8590 : : }
8591 : :
8592 : 1077604 : unsigned saved_unused = unused;
8593 : 1077604 : unused = 0;
8594 : :
8595 : 1077604 : merge_kind mk = merge_kind (mk_u);
8596 : :
8597 : 1077604 : tree decl = start ();
8598 : 1077604 : if (decl)
8599 : : {
8600 : 1077604 : if (mk != MK_unique)
8601 : : {
8602 : 894168 : bits_in bits = stream_bits ();
8603 : 894168 : if (!(mk & MK_template_mask) && !state->is_header ())
8604 : 48339 : is_attached = bits.b ();
8605 : :
8606 : 894168 : has_defn = bits.b ();
8607 : 894168 : }
8608 : :
8609 : 1077604 : if (!tree_node_bools (decl))
8610 : 0 : decl = NULL_TREE;
8611 : : }
8612 : :
8613 : : /* Insert into map. */
8614 : 1077604 : tag = insert (decl);
8615 : 1077604 : if (decl)
8616 : 1077604 : dump (dumper::TREE)
8617 : 964 : && dump ("Reading:%d %C", tag, TREE_CODE (decl));
8618 : :
8619 : 1077604 : tree inner = decl;
8620 : 1077604 : int inner_tag = 0;
8621 : 1077604 : if (decl && TREE_CODE (decl) == TEMPLATE_DECL)
8622 : : {
8623 : 291074 : int code = u ();
8624 : 291074 : inner = start (code);
8625 : 291074 : if (inner && tree_node_bools (inner))
8626 : 291074 : DECL_TEMPLATE_RESULT (decl) = inner;
8627 : : else
8628 : 0 : decl = NULL_TREE;
8629 : :
8630 : 291074 : inner_tag = insert (inner);
8631 : 291074 : if (decl)
8632 : 291074 : dump (dumper::TREE)
8633 : 204 : && dump ("Reading:%d %C", inner_tag, TREE_CODE (inner));
8634 : : }
8635 : :
8636 : 1077604 : tree type = NULL_TREE;
8637 : 1077604 : int type_tag = 0;
8638 : 1077604 : tree stub_decl = NULL_TREE;
8639 : 1077604 : int stub_tag = 0;
8640 : 1077604 : if (decl && TREE_CODE (inner) == TYPE_DECL)
8641 : : {
8642 : 352457 : if (unsigned type_code = u ())
8643 : : {
8644 : 159412 : type = start (type_code);
8645 : 159412 : if (type && tree_node_bools (type))
8646 : : {
8647 : 159412 : TREE_TYPE (inner) = type;
8648 : 159412 : TYPE_NAME (type) = inner;
8649 : : }
8650 : : else
8651 : 0 : decl = NULL_TREE;
8652 : :
8653 : 159412 : type_tag = insert (type);
8654 : 159412 : if (decl)
8655 : 159412 : dump (dumper::TREE)
8656 : 212 : && dump ("Reading type:%d %C", type_tag, TREE_CODE (type));
8657 : :
8658 : 159412 : if (unsigned stub_code = u ())
8659 : : {
8660 : 438 : stub_decl = start (stub_code);
8661 : 438 : if (stub_decl && tree_node_bools (stub_decl))
8662 : : {
8663 : 438 : TREE_TYPE (stub_decl) = type;
8664 : 438 : TYPE_STUB_DECL (type) = stub_decl;
8665 : : }
8666 : : else
8667 : 0 : decl = NULL_TREE;
8668 : :
8669 : 438 : stub_tag = insert (stub_decl);
8670 : 438 : if (decl)
8671 : 438 : dump (dumper::TREE)
8672 : 0 : && dump ("Reading stub_decl:%d %C", stub_tag,
8673 : 0 : TREE_CODE (stub_decl));
8674 : : }
8675 : : }
8676 : : }
8677 : :
8678 : 1077604 : if (!decl)
8679 : : {
8680 : 0 : bail:
8681 : 0 : if (inner_tag != 0)
8682 : 0 : back_refs[~inner_tag] = NULL_TREE;
8683 : 0 : if (type_tag != 0)
8684 : 0 : back_refs[~type_tag] = NULL_TREE;
8685 : 0 : if (stub_tag != 0)
8686 : 0 : back_refs[~stub_tag] = NULL_TREE;
8687 : 0 : if (tag != 0)
8688 : 0 : back_refs[~tag] = NULL_TREE;
8689 : 0 : set_overrun ();
8690 : : /* Bail. */
8691 : 0 : unused = saved_unused;
8692 : 0 : return NULL_TREE;
8693 : : }
8694 : :
8695 : : /* Read the container, to ensure it's already been streamed in. */
8696 : 1077604 : tree container = decl_container ();
8697 : 1077604 : unsigned tpl_levels = 0;
8698 : :
8699 : : /* If this is an imported temploid friend, get the owning decl its
8700 : : attachment is determined by (or NULL_TREE otherwise). */
8701 : 1077604 : tree temploid_friend = NULL_TREE;
8702 : 1077604 : if (TREE_CODE (inner) == FUNCTION_DECL || TREE_CODE (inner) == TYPE_DECL)
8703 : 759172 : temploid_friend = tree_node ();
8704 : :
8705 : : /* Figure out if this decl is already known about. */
8706 : 1077604 : int parm_tag = 0;
8707 : :
8708 : 1077604 : if (decl != inner)
8709 : 291074 : if (!tpl_header (decl, &tpl_levels))
8710 : 0 : goto bail;
8711 : 1077604 : if (TREE_CODE (inner) == FUNCTION_DECL)
8712 : 406715 : parm_tag = fn_parms_init (inner);
8713 : :
8714 : 1077604 : tree existing = key_mergeable (tag, mk, decl, inner, type, container,
8715 : : is_attached, temploid_friend);
8716 : 1077604 : tree existing_inner = existing;
8717 : 1077604 : if (existing)
8718 : : {
8719 : 421937 : if (existing == error_mark_node)
8720 : 0 : goto bail;
8721 : :
8722 : 421937 : if (TREE_CODE (STRIP_TEMPLATE (existing)) == TYPE_DECL)
8723 : : {
8724 : 158523 : tree etype = TREE_TYPE (existing);
8725 : 158523 : if (TYPE_LANG_SPECIFIC (etype)
8726 : 114697 : && COMPLETE_TYPE_P (etype)
8727 : 225356 : && !CLASSTYPE_MEMBER_VEC (etype))
8728 : : /* Give it a member vec, we're likely gonna be looking
8729 : : inside it. */
8730 : 13992 : set_class_bindings (etype, -1);
8731 : : }
8732 : :
8733 : : /* Install the existing decl into the back ref array. */
8734 : 421937 : register_duplicate (decl, existing);
8735 : 421937 : back_refs[~tag] = existing;
8736 : 421937 : if (inner_tag != 0)
8737 : : {
8738 : 142179 : existing_inner = DECL_TEMPLATE_RESULT (existing);
8739 : 142179 : back_refs[~inner_tag] = existing_inner;
8740 : : }
8741 : :
8742 : 421937 : if (type_tag != 0)
8743 : : {
8744 : 75970 : tree existing_type = TREE_TYPE (existing);
8745 : 75970 : back_refs[~type_tag] = existing_type;
8746 : 75970 : if (stub_tag != 0)
8747 : 254 : back_refs[~stub_tag] = TYPE_STUB_DECL (existing_type);
8748 : : }
8749 : : }
8750 : :
8751 : 1077604 : if (parm_tag)
8752 : 406715 : fn_parms_fini (parm_tag, inner, existing_inner, has_defn);
8753 : :
8754 : 1077604 : if (!tree_node_vals (decl))
8755 : 0 : goto bail;
8756 : :
8757 : 1077604 : if (inner_tag)
8758 : : {
8759 : 291074 : gcc_checking_assert (DECL_TEMPLATE_RESULT (decl) == inner);
8760 : :
8761 : 291074 : if (!tree_node_vals (inner))
8762 : 0 : goto bail;
8763 : :
8764 : 291074 : if (!tpl_parms_fini (decl, tpl_levels))
8765 : 0 : goto bail;
8766 : : }
8767 : :
8768 : 1077604 : if (type && (!tree_node_vals (type)
8769 : 159412 : || (stub_decl && !tree_node_vals (stub_decl))))
8770 : 0 : goto bail;
8771 : :
8772 : 1077604 : spec_entry spec;
8773 : 1077604 : unsigned spec_flags = 0;
8774 : 1077604 : if (mk & MK_template_mask
8775 : 746921 : || mk == MK_partial
8776 : 746921 : || mk == MK_friend_spec)
8777 : : {
8778 : 10428 : if (mk == MK_partial)
8779 : : spec_flags = 2;
8780 : : else
8781 : 330683 : spec_flags = u ();
8782 : :
8783 : 341111 : spec.tmpl = tree_node ();
8784 : 341111 : spec.args = tree_node ();
8785 : : }
8786 : : /* Hold constraints on the spec field, for a short while. */
8787 : 1077604 : spec.spec = tree_node ();
8788 : :
8789 : 1078568 : dump (dumper::TREE) && dump ("Read:%d %C:%N", tag, TREE_CODE (decl), decl);
8790 : :
8791 : 1077604 : existing = back_refs[~tag];
8792 : 1077604 : bool installed = install_entity (existing);
8793 : 1077604 : bool is_new = existing == decl;
8794 : :
8795 : 1077604 : if (DECL_LANG_SPECIFIC (inner)
8796 : 2032238 : && DECL_MODULE_KEYED_DECLS_P (inner))
8797 : : {
8798 : : /* Read and maybe install the attached entities. */
8799 : 245 : bool existed;
8800 : 245 : auto &set = keyed_table->get_or_insert (STRIP_TEMPLATE (existing),
8801 : : &existed);
8802 : 245 : unsigned num = u ();
8803 : 245 : if (is_new == existed)
8804 : 0 : set_overrun ();
8805 : 245 : if (is_new)
8806 : 130 : set.reserve (num);
8807 : 509 : for (unsigned ix = 0; !get_overrun () && ix != num; ix++)
8808 : : {
8809 : 264 : tree attached = tree_node ();
8810 : 264 : dump (dumper::MERGE)
8811 : 60 : && dump ("Read %d[%u] %s attached decl %N", tag, ix,
8812 : : is_new ? "new" : "matched", attached);
8813 : 264 : if (is_new)
8814 : 139 : set.quick_push (attached);
8815 : 125 : else if (set[ix] != attached)
8816 : 0 : set_overrun ();
8817 : : }
8818 : : }
8819 : :
8820 : : /* Regular typedefs will have a NULL TREE_TYPE at this point. */
8821 : 1077604 : unsigned tdef_flags = 0;
8822 : 1077604 : bool is_typedef = false;
8823 : 1077604 : if (!type && TREE_CODE (inner) == TYPE_DECL)
8824 : : {
8825 : 193045 : tdef_flags = u ();
8826 : 193045 : if (tdef_flags & 1)
8827 : 193045 : is_typedef = true;
8828 : : }
8829 : :
8830 : 1077604 : if (is_new)
8831 : : {
8832 : : /* A newly discovered node. */
8833 : 655667 : if (TREE_CODE (decl) == FUNCTION_DECL && DECL_VIRTUAL_P (decl))
8834 : : /* Mark this identifier as naming a virtual function --
8835 : : lookup_overrides relies on this optimization. */
8836 : 5658 : IDENTIFIER_VIRTUAL_P (DECL_NAME (decl)) = true;
8837 : :
8838 : 655667 : if (installed)
8839 : : {
8840 : : /* Mark the entity as imported. */
8841 : 413757 : retrofit_lang_decl (inner);
8842 : 413757 : DECL_MODULE_IMPORT_P (inner) = true;
8843 : : }
8844 : :
8845 : 655667 : if (temploid_friend)
8846 : 38 : imported_temploid_friends->put (decl, temploid_friend);
8847 : :
8848 : 655667 : if (spec.spec)
8849 : 27115 : set_constraints (decl, spec.spec);
8850 : :
8851 : 655667 : if (TREE_CODE (decl) == INTEGER_CST && !TREE_OVERFLOW (decl))
8852 : : {
8853 : 0 : decl = cache_integer_cst (decl, true);
8854 : 0 : back_refs[~tag] = decl;
8855 : : }
8856 : :
8857 : 655667 : if (is_typedef)
8858 : : {
8859 : : /* Frob it to be ready for cloning. */
8860 : 110492 : TREE_TYPE (inner) = DECL_ORIGINAL_TYPE (inner);
8861 : 110492 : DECL_ORIGINAL_TYPE (inner) = NULL_TREE;
8862 : 110492 : if (TREE_CODE (TREE_TYPE (inner)) != TU_LOCAL_ENTITY)
8863 : : {
8864 : 110489 : set_underlying_type (inner);
8865 : 110489 : if (tdef_flags & 2)
8866 : : {
8867 : : /* Match instantiate_alias_template's handling. */
8868 : 27558 : tree type = TREE_TYPE (inner);
8869 : 27558 : TYPE_DEPENDENT_P (type) = true;
8870 : 27558 : TYPE_DEPENDENT_P_VALID (type) = true;
8871 : 27558 : SET_TYPE_STRUCTURAL_EQUALITY (type);
8872 : : }
8873 : : }
8874 : : }
8875 : :
8876 : 655667 : if (inner_tag)
8877 : : /* Set the TEMPLATE_DECL's type. */
8878 : 148895 : TREE_TYPE (decl) = TREE_TYPE (inner);
8879 : :
8880 : : /* Redetermine whether we need to import or export this declaration
8881 : : for this TU. But for extern templates we know we must import:
8882 : : they'll be defined in a different TU.
8883 : : FIXME: How do dllexport and dllimport interact across a module?
8884 : : See also https://github.com/itanium-cxx-abi/cxx-abi/issues/170.
8885 : : May have to revisit? */
8886 : 655667 : if (type
8887 : 83442 : && CLASS_TYPE_P (type)
8888 : 71616 : && TYPE_LANG_SPECIFIC (type)
8889 : 727283 : && !(CLASSTYPE_EXPLICIT_INSTANTIATION (type)
8890 : 670 : && CLASSTYPE_INTERFACE_KNOWN (type)
8891 : 670 : && CLASSTYPE_INTERFACE_ONLY (type)))
8892 : : {
8893 : 70995 : CLASSTYPE_INTERFACE_ONLY (type) = false;
8894 : 70995 : CLASSTYPE_INTERFACE_UNKNOWN (type) = true;
8895 : : }
8896 : :
8897 : : /* Add to specialization tables now that constraints etc are
8898 : : added. */
8899 : 655667 : if (mk == MK_partial)
8900 : : {
8901 : 4330 : bool is_type = TREE_CODE (inner) == TYPE_DECL;
8902 : 4330 : spec.spec = is_type ? type : inner;
8903 : 4330 : add_mergeable_specialization (!is_type, &spec, decl, spec_flags);
8904 : : }
8905 : 651337 : else if (mk & MK_template_mask)
8906 : : {
8907 : 186495 : bool is_type = !(mk & MK_tmpl_decl_mask);
8908 : 186495 : spec.spec = is_type ? type : mk & MK_tmpl_tmpl_mask ? inner : decl;
8909 : 186495 : add_mergeable_specialization (!is_type, &spec, decl, spec_flags);
8910 : : }
8911 : :
8912 : 655667 : if (NAMESPACE_SCOPE_P (decl)
8913 : 137902 : && (mk == MK_named || mk == MK_unique
8914 : 137902 : || mk == MK_enum || mk == MK_friend_spec)
8915 : 717605 : && !(VAR_OR_FUNCTION_DECL_P (decl) && DECL_LOCAL_DECL_P (decl)))
8916 : 61848 : add_module_namespace_decl (CP_DECL_CONTEXT (decl), decl);
8917 : :
8918 : 655667 : if (DECL_ARTIFICIAL (decl)
8919 : 179329 : && TREE_CODE (decl) == FUNCTION_DECL
8920 : 18659 : && !DECL_TEMPLATE_INFO (decl)
8921 : 18349 : && DECL_CONTEXT (decl) && TYPE_P (DECL_CONTEXT (decl))
8922 : 18189 : && TYPE_SIZE (DECL_CONTEXT (decl))
8923 : 657083 : && !DECL_THUNK_P (decl))
8924 : : /* A new implicit member function, when the class is
8925 : : complete. This means the importee declared it, and
8926 : : we must now add it to the class. Note that implicit
8927 : : member fns of template instantiations do not themselves
8928 : : look like templates. */
8929 : 886 : if (!install_implicit_member (inner))
8930 : 0 : set_overrun ();
8931 : :
8932 : : /* When importing a TLS wrapper from a header unit, we haven't
8933 : : actually emitted its definition yet. Remember it so we can
8934 : : do this later. */
8935 : 655667 : if (state->is_header ()
8936 : 655667 : && decl_tls_wrapper_p (decl))
8937 : 6 : note_vague_linkage_fn (decl);
8938 : :
8939 : : /* Setup aliases for the declaration. */
8940 : 655667 : if (tree alias = lookup_attribute ("alias", DECL_ATTRIBUTES (decl)))
8941 : : {
8942 : 3 : alias = TREE_VALUE (TREE_VALUE (alias));
8943 : 3 : alias = get_identifier (TREE_STRING_POINTER (alias));
8944 : 3 : assemble_alias (decl, alias);
8945 : : }
8946 : : }
8947 : : else
8948 : : {
8949 : : /* DECL is the to-be-discarded decl. Its internal pointers will
8950 : : be to the EXISTING's structure. Frob it to point to its
8951 : : own other structures, so loading its definition will alter
8952 : : it, and not the existing decl. */
8953 : 423372 : dump (dumper::MERGE) && dump ("Deduping %N", existing);
8954 : :
8955 : 421937 : if (inner_tag)
8956 : 142179 : DECL_TEMPLATE_RESULT (decl) = inner;
8957 : :
8958 : 421937 : if (type)
8959 : : {
8960 : : /* Point at the to-be-discarded type & decl. */
8961 : 75970 : TYPE_NAME (type) = inner;
8962 : 75970 : TREE_TYPE (inner) = type;
8963 : :
8964 : 151686 : TYPE_STUB_DECL (type) = stub_decl ? stub_decl : inner;
8965 : 75970 : if (stub_decl)
8966 : 254 : TREE_TYPE (stub_decl) = type;
8967 : :
8968 : 75970 : tree etype = TREE_TYPE (existing);
8969 : :
8970 : : /* Handle separate declarations with different attributes. */
8971 : 75970 : tree &dattr = TYPE_ATTRIBUTES (type);
8972 : 75970 : tree &eattr = TYPE_ATTRIBUTES (etype);
8973 : 75970 : check_abi_tags (existing, decl, eattr, dattr);
8974 : : // TODO: handle other conflicting type attributes
8975 : 75970 : eattr = merge_attributes (eattr, dattr);
8976 : :
8977 : : /* When merging a partial specialisation, the existing decl may have
8978 : : had its TYPE_CANONICAL adjusted. If so we should use structural
8979 : : equality to ensure is_matching_decl doesn't get confused. */
8980 : 75970 : if ((spec_flags & 2)
8981 : 75970 : && TYPE_CANONICAL (type) != TYPE_CANONICAL (etype))
8982 : 3 : SET_TYPE_STRUCTURAL_EQUALITY (type);
8983 : : }
8984 : :
8985 : 421937 : if (inner_tag)
8986 : : /* Set the TEMPLATE_DECL's type. */
8987 : 142179 : TREE_TYPE (decl) = TREE_TYPE (inner);
8988 : :
8989 : 421937 : if (!is_matching_decl (existing, decl, is_typedef))
8990 : 42 : unmatched_duplicate (existing);
8991 : :
8992 : 421937 : if (TREE_CODE (inner) == FUNCTION_DECL)
8993 : : {
8994 : 190585 : tree e_inner = STRIP_TEMPLATE (existing);
8995 : 190585 : for (auto parm = DECL_ARGUMENTS (inner);
8996 : 569015 : parm; parm = DECL_CHAIN (parm))
8997 : 378430 : DECL_CONTEXT (parm) = e_inner;
8998 : : }
8999 : :
9000 : : /* And our result is the existing node. */
9001 : 421937 : decl = existing;
9002 : : }
9003 : :
9004 : 1077604 : if (mk == MK_friend_spec)
9005 : : {
9006 : 0 : tree e = match_mergeable_specialization (true, &spec);
9007 : 0 : if (!e)
9008 : : {
9009 : 0 : spec.spec = inner;
9010 : 0 : add_mergeable_specialization (true, &spec, decl, spec_flags);
9011 : : }
9012 : 0 : else if (e != existing)
9013 : 0 : set_overrun ();
9014 : : }
9015 : :
9016 : 1077604 : if (is_typedef)
9017 : : {
9018 : : /* Insert the type into the array now. */
9019 : 193045 : tag = insert (TREE_TYPE (decl));
9020 : 193045 : dump (dumper::TREE)
9021 : 247 : && dump ("Cloned:%d typedef %C:%N",
9022 : 247 : tag, TREE_CODE (TREE_TYPE (decl)), TREE_TYPE (decl));
9023 : : }
9024 : :
9025 : 1077604 : unused = saved_unused;
9026 : :
9027 : 1077604 : if (DECL_MAYBE_IN_CHARGE_CDTOR_P (decl))
9028 : : {
9029 : 90299 : unsigned flags = u ();
9030 : :
9031 : 90299 : if (is_new)
9032 : : {
9033 : 51496 : bool cloned_p = flags & 1;
9034 : 51596 : dump (dumper::TREE) && dump ("CDTOR %N is %scloned",
9035 : : decl, cloned_p ? "" : "not ");
9036 : 51496 : if (cloned_p)
9037 : : {
9038 : : /* Update the member vec, if there is one (we're in a different
9039 : : cluster to the class defn) and this isn't a primary template
9040 : : specialization (as in tsubst_function_decl). */
9041 : 37032 : bool up = (CLASSTYPE_MEMBER_VEC (DECL_CONTEXT (decl))
9042 : 37032 : && !primary_template_specialization_p (decl));
9043 : 37032 : build_cdtor_clones (decl, flags & 2, flags & 4, up);
9044 : : }
9045 : : }
9046 : : }
9047 : :
9048 : 1077604 : if (VAR_P (decl) && CP_DECL_THREAD_LOCAL_P (decl))
9049 : : {
9050 : 168 : enum tls_model model = tls_model (u ());
9051 : 168 : if (is_new)
9052 : 148 : set_decl_tls_model (decl, model);
9053 : : }
9054 : :
9055 : 1077604 : if (!NAMESPACE_SCOPE_P (inner)
9056 : 798475 : && ((TREE_CODE (inner) == TYPE_DECL
9057 : 189756 : && !is_typedef
9058 : 34409 : && TYPE_NAME (TREE_TYPE (inner)) == inner)
9059 : 764066 : || TREE_CODE (inner) == FUNCTION_DECL)
9060 : 1418458 : && u ())
9061 : 3 : read_definition (decl);
9062 : :
9063 : : return decl;
9064 : : }
9065 : :
9066 : : /* DECL is an unnameable member of CTX. Return a suitable identifying
9067 : : index. */
9068 : :
9069 : : static unsigned
9070 : 904 : get_field_ident (tree ctx, tree decl)
9071 : : {
9072 : 904 : gcc_checking_assert (TREE_CODE (decl) == USING_DECL
9073 : : || !DECL_NAME (decl)
9074 : : || IDENTIFIER_ANON_P (DECL_NAME (decl)));
9075 : :
9076 : 904 : unsigned ix = 0;
9077 : 904 : for (tree fields = TYPE_FIELDS (ctx);
9078 : 7583 : fields; fields = DECL_CHAIN (fields))
9079 : : {
9080 : 7583 : if (fields == decl)
9081 : 904 : return ix;
9082 : :
9083 : 6679 : if (DECL_CONTEXT (fields) == ctx
9084 : 6679 : && (TREE_CODE (fields) == USING_DECL
9085 : 6668 : || (TREE_CODE (fields) == FIELD_DECL
9086 : 35 : && (!DECL_NAME (fields)
9087 : 13 : || IDENTIFIER_ANON_P (DECL_NAME (fields))))))
9088 : : /* Count this field. */
9089 : 25 : ix++;
9090 : : }
9091 : 0 : gcc_unreachable ();
9092 : : }
9093 : :
9094 : : static tree
9095 : 889 : lookup_field_ident (tree ctx, unsigned ix)
9096 : : {
9097 : 889 : for (tree fields = TYPE_FIELDS (ctx);
9098 : 7458 : fields; fields = DECL_CHAIN (fields))
9099 : 7458 : if (DECL_CONTEXT (fields) == ctx
9100 : 7458 : && (TREE_CODE (fields) == USING_DECL
9101 : 7446 : || (TREE_CODE (fields) == FIELD_DECL
9102 : 936 : && (!DECL_NAME (fields)
9103 : 15 : || IDENTIFIER_ANON_P (DECL_NAME (fields))))))
9104 : 927 : if (!ix--)
9105 : : return fields;
9106 : :
9107 : : return NULL_TREE;
9108 : : }
9109 : :
9110 : : /* Reference DECL. REF indicates the walk kind we are performing.
9111 : : Return true if we should write this decl by value. */
9112 : :
9113 : : bool
9114 : 10507737 : trees_out::decl_node (tree decl, walk_kind ref)
9115 : : {
9116 : 10507737 : gcc_checking_assert (DECL_P (decl) && !DECL_TEMPLATE_PARM_P (decl)
9117 : : && DECL_CONTEXT (decl));
9118 : :
9119 : 10507737 : if (ref == WK_value)
9120 : : {
9121 : 1098383 : depset *dep = dep_hash->find_dependency (decl);
9122 : 1098383 : decl_value (decl, dep);
9123 : 1098383 : return false;
9124 : : }
9125 : :
9126 : 9409354 : switch (TREE_CODE (decl))
9127 : : {
9128 : : default:
9129 : : break;
9130 : :
9131 : 981776 : case FUNCTION_DECL:
9132 : 981776 : gcc_checking_assert (!DECL_LOCAL_DECL_P (decl));
9133 : : break;
9134 : :
9135 : : case RESULT_DECL:
9136 : : /* Unlike PARM_DECLs, RESULT_DECLs are only generated and
9137 : : referenced when we're inside the function itself. */
9138 : : return true;
9139 : :
9140 : 149486 : case PARM_DECL:
9141 : 149486 : {
9142 : 149486 : if (streaming_p ())
9143 : 66097 : i (tt_parm);
9144 : 149486 : tree_node (DECL_CONTEXT (decl));
9145 : :
9146 : : /* That must have put this in the map. */
9147 : 149486 : walk_kind ref = ref_node (decl);
9148 : 149486 : if (ref != WK_none)
9149 : : // FIXME:OPTIMIZATION We can wander into bits of the
9150 : : // template this was instantiated from, for instance
9151 : : // deferred noexcept and default parms, or references
9152 : : // to parms from earlier forward-decls (PR c++/119608).
9153 : : //
9154 : : // Currently we'll end up cloning those bits of tree.
9155 : : // It would be nice to reference those specific nodes.
9156 : : // I think putting those things in the map when we
9157 : : // reference their template by name.
9158 : : //
9159 : : // See the note in add_indirects.
9160 : : return true;
9161 : :
9162 : 0 : if (streaming_p ())
9163 : 0 : dump (dumper::TREE)
9164 : 0 : && dump ("Wrote %s reference %N",
9165 : 0 : TREE_CODE (decl) == PARM_DECL ? "parameter" : "result",
9166 : : decl);
9167 : : }
9168 : : return false;
9169 : :
9170 : : case IMPORTED_DECL:
9171 : : /* This describes a USING_DECL to the ME's debug machinery. It
9172 : : originates from the fortran FE, and has nothing to do with
9173 : : C++ modules. */
9174 : : return true;
9175 : :
9176 : : case LABEL_DECL:
9177 : : return true;
9178 : :
9179 : 52905 : case CONST_DECL:
9180 : 52905 : {
9181 : : /* If I end up cloning enum decls, implementing C++20 using
9182 : : E::v, this will need tweaking. */
9183 : 52905 : if (streaming_p ())
9184 : 12040 : i (tt_enum_decl);
9185 : 52905 : tree ctx = DECL_CONTEXT (decl);
9186 : 52905 : gcc_checking_assert (TREE_CODE (ctx) == ENUMERAL_TYPE);
9187 : 52905 : tree_node (ctx);
9188 : 52905 : tree_node (DECL_NAME (decl));
9189 : :
9190 : 52905 : int tag = insert (decl);
9191 : 52905 : if (streaming_p ())
9192 : 12040 : dump (dumper::TREE)
9193 : 21 : && dump ("Wrote enum decl:%d %C:%N", tag, TREE_CODE (decl), decl);
9194 : : return false;
9195 : : }
9196 : 17514 : break;
9197 : :
9198 : 17514 : case USING_DECL:
9199 : 17514 : if (TREE_CODE (DECL_CONTEXT (decl)) == FUNCTION_DECL)
9200 : : break;
9201 : : /* FALLTHROUGH */
9202 : :
9203 : 126441 : case FIELD_DECL:
9204 : 126441 : {
9205 : 126441 : if (streaming_p ())
9206 : 7688 : i (tt_data_member);
9207 : :
9208 : 126441 : tree ctx = DECL_CONTEXT (decl);
9209 : 126441 : tree_node (ctx);
9210 : :
9211 : 126441 : tree name = NULL_TREE;
9212 : :
9213 : 126441 : if (TREE_CODE (decl) == USING_DECL)
9214 : : ;
9215 : : else
9216 : : {
9217 : 125533 : name = DECL_NAME (decl);
9218 : 244384 : if (name && IDENTIFIER_ANON_P (name))
9219 : : name = NULL_TREE;
9220 : : }
9221 : :
9222 : 126441 : tree_node (name);
9223 : 126441 : if (!name && streaming_p ())
9224 : : {
9225 : 904 : unsigned ix = get_field_ident (ctx, decl);
9226 : 904 : u (ix);
9227 : : }
9228 : :
9229 : 126441 : int tag = insert (decl);
9230 : 126441 : if (streaming_p ())
9231 : 7688 : dump (dumper::TREE)
9232 : 26 : && dump ("Wrote member:%d %C:%N", tag, TREE_CODE (decl), decl);
9233 : : return false;
9234 : : }
9235 : 413280 : break;
9236 : :
9237 : 413280 : case VAR_DECL:
9238 : 413280 : gcc_checking_assert (!DECL_LOCAL_DECL_P (decl));
9239 : 413280 : if (DECL_VTABLE_OR_VTT_P (decl))
9240 : : {
9241 : : /* VTT or VTABLE, they are all on the vtables list. */
9242 : 2025 : tree ctx = CP_DECL_CONTEXT (decl);
9243 : 2025 : tree vtable = CLASSTYPE_VTABLES (ctx);
9244 : 2058 : for (unsigned ix = 0; ; vtable = DECL_CHAIN (vtable), ix++)
9245 : 2058 : if (vtable == decl)
9246 : : {
9247 : 2025 : gcc_checking_assert (DECL_VIRTUAL_P (decl));
9248 : 2025 : if (streaming_p ())
9249 : : {
9250 : 36 : u (tt_vtable);
9251 : 36 : u (ix);
9252 : 36 : dump (dumper::TREE)
9253 : 0 : && dump ("Writing vtable %N[%u]", ctx, ix);
9254 : : }
9255 : 2025 : tree_node (ctx);
9256 : 2025 : return false;
9257 : : }
9258 : : gcc_unreachable ();
9259 : : }
9260 : :
9261 : 411255 : if (DECL_TINFO_P (decl))
9262 : : {
9263 : 5699 : tinfo:
9264 : : /* A typeinfo, tt_tinfo_typedef or tt_tinfo_var. */
9265 : 10472 : bool is_var = VAR_P (decl);
9266 : 10472 : tree type = TREE_TYPE (decl);
9267 : 10472 : unsigned ix = get_pseudo_tinfo_index (type);
9268 : 10472 : if (streaming_p ())
9269 : : {
9270 : 6659 : i (is_var ? tt_tinfo_var : tt_tinfo_typedef);
9271 : 4724 : u (ix);
9272 : : }
9273 : :
9274 : 10472 : if (is_var)
9275 : : {
9276 : : /* We also need the type it is for and mangled name, so
9277 : : the reader doesn't need to complete the type (which
9278 : : would break section ordering). The type it is for is
9279 : : stashed on the name's TREE_TYPE. */
9280 : 5699 : tree name = DECL_NAME (decl);
9281 : 5699 : tree_node (name);
9282 : 5699 : type = TREE_TYPE (name);
9283 : 5699 : tree_node (type);
9284 : : }
9285 : :
9286 : 10472 : int tag = insert (decl);
9287 : 10472 : if (streaming_p ())
9288 : 4724 : dump (dumper::TREE)
9289 : 27 : && dump ("Wrote tinfo_%s:%d %u %N", is_var ? "var" : "type",
9290 : : tag, ix, type);
9291 : :
9292 : 10472 : if (!is_var)
9293 : : {
9294 : 4773 : tag = insert (type);
9295 : 4773 : if (streaming_p ())
9296 : 1935 : dump (dumper::TREE)
9297 : 9 : && dump ("Wrote tinfo_type:%d %u %N", tag, ix, type);
9298 : : }
9299 : 10472 : return false;
9300 : : }
9301 : :
9302 : 405556 : if (DECL_NTTP_OBJECT_P (decl))
9303 : : {
9304 : : /* A NTTP parm object. */
9305 : 33 : if (streaming_p ())
9306 : 7 : i (tt_nttp_var);
9307 : 33 : tree_node (tparm_object_argument (decl));
9308 : 33 : tree_node (DECL_NAME (decl));
9309 : 33 : int tag = insert (decl);
9310 : 33 : if (streaming_p ())
9311 : 7 : dump (dumper::TREE)
9312 : 0 : && dump ("Wrote nttp object:%d %N", tag, DECL_NAME (decl));
9313 : 33 : return false;
9314 : : }
9315 : :
9316 : : break;
9317 : :
9318 : 3311254 : case TYPE_DECL:
9319 : 3311254 : if (DECL_TINFO_P (decl))
9320 : 4773 : goto tinfo;
9321 : : break;
9322 : : }
9323 : :
9324 : 8551097 : if (DECL_THUNK_P (decl))
9325 : : {
9326 : : /* Thunks are similar to binfos -- write the thunked-to decl and
9327 : : then thunk-specific key info. */
9328 : 0 : if (streaming_p ())
9329 : : {
9330 : 0 : i (tt_thunk);
9331 : 0 : i (THUNK_FIXED_OFFSET (decl));
9332 : : }
9333 : :
9334 : : tree target = decl;
9335 : 0 : while (DECL_THUNK_P (target))
9336 : 0 : target = THUNK_TARGET (target);
9337 : 0 : tree_node (target);
9338 : 0 : tree_node (THUNK_VIRTUAL_OFFSET (decl));
9339 : 0 : int tag = insert (decl);
9340 : 0 : if (streaming_p ())
9341 : 0 : dump (dumper::TREE)
9342 : 0 : && dump ("Wrote:%d thunk %N to %N", tag, DECL_NAME (decl), target);
9343 : 0 : return false;
9344 : : }
9345 : :
9346 : 8551097 : if (DECL_CLONED_FUNCTION_P (decl))
9347 : : {
9348 : 281893 : tree target = get_clone_target (decl);
9349 : 281893 : if (streaming_p ())
9350 : 133457 : i (tt_clone_ref);
9351 : :
9352 : 281893 : tree_node (target);
9353 : 281893 : tree_node (DECL_NAME (decl));
9354 : 281893 : if (DECL_VIRTUAL_P (decl))
9355 : 20715 : tree_node (DECL_VINDEX (decl));
9356 : 281893 : int tag = insert (decl);
9357 : 281893 : if (streaming_p ())
9358 : 133457 : dump (dumper::TREE)
9359 : 164 : && dump ("Wrote:%d clone %N of %N", tag, DECL_NAME (decl), target);
9360 : 281893 : return false;
9361 : : }
9362 : :
9363 : : /* Everything left should be a thing that is in the entity table.
9364 : : Mostly things that can be defined outside of their (original
9365 : : declaration) context. */
9366 : 8269204 : gcc_checking_assert (TREE_CODE (decl) == TEMPLATE_DECL
9367 : : || VAR_P (decl)
9368 : : || TREE_CODE (decl) == FUNCTION_DECL
9369 : : || TREE_CODE (decl) == TYPE_DECL
9370 : : || TREE_CODE (decl) == USING_DECL
9371 : : || TREE_CODE (decl) == CONCEPT_DECL
9372 : : || TREE_CODE (decl) == NAMESPACE_DECL);
9373 : :
9374 : 8269204 : int use_tpl = -1;
9375 : 8269204 : tree ti = node_template_info (decl, use_tpl);
9376 : 8269204 : tree tpl = NULL_TREE;
9377 : :
9378 : : /* If this is the TEMPLATE_DECL_RESULT of a TEMPLATE_DECL, get the
9379 : : TEMPLATE_DECL. Note TI_TEMPLATE is not a TEMPLATE_DECL for
9380 : : (some) friends, so we need to check that. */
9381 : : // FIXME: Should local friend template specializations be by value?
9382 : : // They don't get idents so we'll never know they're imported, but I
9383 : : // think we can only reach them from the TU that defines the
9384 : : // befriending class?
9385 : 3088513 : if (ti && TREE_CODE (TI_TEMPLATE (ti)) == TEMPLATE_DECL
9386 : 11357666 : && DECL_TEMPLATE_RESULT (TI_TEMPLATE (ti)) == decl)
9387 : : {
9388 : : tpl = TI_TEMPLATE (ti);
9389 : 793923 : partial_template:
9390 : 793923 : if (streaming_p ())
9391 : : {
9392 : 2569 : i (tt_template);
9393 : 2569 : dump (dumper::TREE)
9394 : 9 : && dump ("Writing implicit template %C:%N%S",
9395 : 9 : TREE_CODE (tpl), tpl, tpl);
9396 : : }
9397 : 793923 : tree_node (tpl);
9398 : :
9399 : : /* Streaming TPL caused us to visit DECL and maybe its type,
9400 : : if it wasn't TU-local. */
9401 : 793923 : if (CHECKING_P && !has_tu_local_dep (tpl))
9402 : : {
9403 : 793896 : gcc_checking_assert (TREE_VISITED (decl));
9404 : 793896 : if (DECL_IMPLICIT_TYPEDEF_P (decl))
9405 : 413173 : gcc_checking_assert (TREE_VISITED (TREE_TYPE (decl)));
9406 : : }
9407 : : return false;
9408 : : }
9409 : :
9410 : 7562900 : tree ctx = CP_DECL_CONTEXT (decl);
9411 : 7562900 : depset *dep = NULL;
9412 : 7562900 : if (streaming_p ())
9413 : 1260877 : dep = dep_hash->find_dependency (decl);
9414 : 6302023 : else if (TREE_CODE (ctx) != FUNCTION_DECL
9415 : 234302 : || TREE_CODE (decl) == TEMPLATE_DECL
9416 : 211071 : || DECL_IMPLICIT_TYPEDEF_P (decl)
9417 : 6488780 : || (DECL_LANG_SPECIFIC (decl)
9418 : 135039 : && DECL_MODULE_IMPORT_P (decl)))
9419 : : {
9420 : 6115266 : auto kind = (TREE_CODE (decl) == NAMESPACE_DECL
9421 : 516707 : && !DECL_NAMESPACE_ALIAS (decl)
9422 : 6115266 : ? depset::EK_NAMESPACE : depset::EK_DECL);
9423 : 6115266 : dep = dep_hash->add_dependency (decl, kind);
9424 : : }
9425 : :
9426 : 7376143 : if (!dep || dep->is_tu_local ())
9427 : : {
9428 : : /* Some internal entity of context. Do by value. */
9429 : 361820 : decl_value (decl, dep);
9430 : 361820 : return false;
9431 : : }
9432 : :
9433 : 7201080 : if (dep->get_entity_kind () == depset::EK_REDIRECT)
9434 : : {
9435 : : /* The DECL_TEMPLATE_RESULT of a partial specialization.
9436 : : Write the partial specialization's template. */
9437 : 87619 : depset *redirect = dep->deps[0];
9438 : 87619 : gcc_checking_assert (redirect->get_entity_kind () == depset::EK_PARTIAL);
9439 : 87619 : tpl = redirect->get_entity ();
9440 : 87619 : goto partial_template;
9441 : : }
9442 : :
9443 : 7113461 : if (streaming_p ())
9444 : : {
9445 : : /* Locate the entity. */
9446 : 1086083 : unsigned index = dep->cluster;
9447 : 1086083 : unsigned import = 0;
9448 : :
9449 : 1086083 : if (dep->is_import ())
9450 : 7935 : import = dep->section;
9451 : 1078148 : else if (CHECKING_P)
9452 : : /* It should be what we put there. */
9453 : 1078148 : gcc_checking_assert (index == ~import_entity_index (decl));
9454 : :
9455 : : #if CHECKING_P
9456 : 7935 : gcc_assert (!import || importedness >= 0);
9457 : : #endif
9458 : 1086083 : i (tt_entity);
9459 : 1086083 : u (import);
9460 : 1086083 : u (index);
9461 : : }
9462 : :
9463 : 7113461 : int tag = insert (decl);
9464 : 7113461 : if (streaming_p () && dump (dumper::TREE))
9465 : : {
9466 : 529 : char const *kind = "import";
9467 : 529 : module_state *from = this_module ();
9468 : 529 : if (dep->is_import ())
9469 : : /* Rediscover the unremapped index. */
9470 : 78 : from = import_entity_module (import_entity_index (decl));
9471 : : else
9472 : : {
9473 : 451 : tree o = get_originating_module_decl (decl);
9474 : 451 : o = STRIP_TEMPLATE (o);
9475 : 902 : kind = (DECL_LANG_SPECIFIC (o) && DECL_MODULE_PURVIEW_P (o)
9476 : 451 : ? "purview" : "GMF");
9477 : : }
9478 : 529 : dump ("Wrote %s:%d %C:%N@%M", kind,
9479 : 529 : tag, TREE_CODE (decl), decl, from);
9480 : : }
9481 : :
9482 : 7113461 : add_indirects (decl);
9483 : :
9484 : 7113461 : return false;
9485 : : }
9486 : :
9487 : : void
9488 : 8589734 : trees_out::type_node (tree type)
9489 : : {
9490 : 8589734 : gcc_assert (TYPE_P (type));
9491 : :
9492 : 8589734 : tree root = (TYPE_NAME (type)
9493 : 8589734 : ? TREE_TYPE (TYPE_NAME (type)) : TYPE_MAIN_VARIANT (type));
9494 : 8589734 : gcc_checking_assert (root);
9495 : :
9496 : 8589734 : if (type != root)
9497 : : {
9498 : 1943017 : if (streaming_p ())
9499 : 404953 : i (tt_variant_type);
9500 : 1943017 : tree_node (root);
9501 : :
9502 : 1943017 : int flags = -1;
9503 : :
9504 : 1943017 : if (TREE_CODE (type) == FUNCTION_TYPE
9505 : 1943017 : || TREE_CODE (type) == METHOD_TYPE)
9506 : : {
9507 : 464187 : int quals = type_memfn_quals (type);
9508 : 464187 : int rquals = type_memfn_rqual (type);
9509 : 464187 : tree raises = TYPE_RAISES_EXCEPTIONS (type);
9510 : 464187 : bool late = TYPE_HAS_LATE_RETURN_TYPE (type);
9511 : :
9512 : 464187 : if (raises != TYPE_RAISES_EXCEPTIONS (root)
9513 : 12661 : || rquals != type_memfn_rqual (root)
9514 : 9249 : || quals != type_memfn_quals (root)
9515 : 473418 : || late != TYPE_HAS_LATE_RETURN_TYPE (root))
9516 : 464187 : flags = rquals | (int (late) << 2) | (quals << 3);
9517 : : }
9518 : : else
9519 : : {
9520 : 1478830 : if (TYPE_USER_ALIGN (type))
9521 : 13810 : flags = TYPE_ALIGN_RAW (type);
9522 : : }
9523 : :
9524 : 1943017 : if (streaming_p ())
9525 : 404953 : i (flags);
9526 : :
9527 : 1943017 : if (flags < 0)
9528 : : ;
9529 : 477997 : else if (TREE_CODE (type) == FUNCTION_TYPE
9530 : 477997 : || TREE_CODE (type) == METHOD_TYPE)
9531 : : {
9532 : 464187 : tree raises = TYPE_RAISES_EXCEPTIONS (type);
9533 : 464187 : if (raises == TYPE_RAISES_EXCEPTIONS (root))
9534 : 12661 : raises = error_mark_node;
9535 : 464187 : tree_node (raises);
9536 : : }
9537 : :
9538 : : /* build_type_attribute_variant creates a new TYPE_MAIN_VARIANT, so
9539 : : variants should all have the same set of attributes. */
9540 : 1943017 : gcc_checking_assert (TYPE_ATTRIBUTES (type)
9541 : : == TYPE_ATTRIBUTES (TYPE_MAIN_VARIANT (type)));
9542 : :
9543 : 1943017 : if (streaming_p ())
9544 : : {
9545 : : /* Qualifiers. */
9546 : 404953 : int rquals = cp_type_quals (root);
9547 : 404953 : int quals = cp_type_quals (type);
9548 : 404953 : if (quals == rquals)
9549 : 188625 : quals = -1;
9550 : 404953 : i (quals);
9551 : : }
9552 : :
9553 : 1943017 : if (ref_node (type) != WK_none)
9554 : : {
9555 : 1943017 : int tag = insert (type);
9556 : 1943017 : if (streaming_p ())
9557 : : {
9558 : 404953 : i (0);
9559 : 404953 : dump (dumper::TREE)
9560 : 203 : && dump ("Wrote:%d variant type %C", tag, TREE_CODE (type));
9561 : : }
9562 : : }
9563 : 1943017 : return;
9564 : : }
9565 : :
9566 : 6646717 : if (tree name = TYPE_NAME (type))
9567 : 2621336 : if ((TREE_CODE (name) == TYPE_DECL && DECL_ORIGINAL_TYPE (name))
9568 : 2052063 : || DECL_TEMPLATE_PARM_P (name)
9569 : 1380861 : || TREE_CODE (type) == RECORD_TYPE
9570 : 249128 : || TREE_CODE (type) == UNION_TYPE
9571 : 2864742 : || TREE_CODE (type) == ENUMERAL_TYPE)
9572 : : {
9573 : 2443109 : gcc_checking_assert (DECL_P (name));
9574 : :
9575 : : /* We can meet template parms that we didn't meet in the
9576 : : tpl_parms walk, because we're referring to a derived type
9577 : : that was previously constructed from equivalent template
9578 : : parms. */
9579 : 2443109 : if (streaming_p ())
9580 : : {
9581 : 170166 : i (tt_typedef_type);
9582 : 170166 : dump (dumper::TREE)
9583 : 59 : && dump ("Writing %stypedef %C:%N",
9584 : 59 : DECL_IMPLICIT_TYPEDEF_P (name) ? "implicit " : "",
9585 : 59 : TREE_CODE (name), name);
9586 : : }
9587 : 2443109 : tree_node (name);
9588 : 2443109 : if (streaming_p ())
9589 : 170166 : dump (dumper::TREE) && dump ("Wrote typedef %C:%N%S",
9590 : 59 : TREE_CODE (name), name, name);
9591 : :
9592 : : /* We'll have either visited this type or have newly discovered
9593 : : that it's TU-local; either way we won't need to visit it again. */
9594 : 2443109 : gcc_checking_assert (TREE_VISITED (type) || has_tu_local_dep (name));
9595 : 2443109 : return;
9596 : : }
9597 : :
9598 : 4203608 : if (TYPE_PTRMEMFUNC_P (type))
9599 : : {
9600 : : /* This is a distinct type node, masquerading as a structure. */
9601 : 4138 : tree fn_type = TYPE_PTRMEMFUNC_FN_TYPE (type);
9602 : 4138 : if (streaming_p ())
9603 : 1162 : i (tt_ptrmem_type);
9604 : 4138 : tree_node (fn_type);
9605 : 4138 : int tag = insert (type);
9606 : 4138 : if (streaming_p ())
9607 : 1165 : dump (dumper::TREE) && dump ("Written:%d ptrmem type", tag);
9608 : 4138 : return;
9609 : : }
9610 : :
9611 : 4199470 : if (streaming_p ())
9612 : : {
9613 : 1336506 : u (tt_derived_type);
9614 : 1336506 : u (TREE_CODE (type));
9615 : : }
9616 : :
9617 : 4199470 : tree_node (TREE_TYPE (type));
9618 : 4199470 : switch (TREE_CODE (type))
9619 : : {
9620 : 0 : default:
9621 : : /* We should never meet a type here that is indescribable in
9622 : : terms of other types. */
9623 : 0 : gcc_unreachable ();
9624 : :
9625 : 67768 : case ARRAY_TYPE:
9626 : 67768 : tree_node (TYPE_DOMAIN (type));
9627 : 67768 : if (streaming_p ())
9628 : : /* Dependent arrays are constructed with TYPE_DEPENENT_P
9629 : : already set. */
9630 : 21822 : u (TYPE_DEPENDENT_P (type));
9631 : : break;
9632 : :
9633 : : case COMPLEX_TYPE:
9634 : : /* No additional data. */
9635 : : break;
9636 : :
9637 : 12 : case BOOLEAN_TYPE:
9638 : : /* A non-standard boolean type. */
9639 : 12 : if (streaming_p ())
9640 : 6 : u (TYPE_PRECISION (type));
9641 : : break;
9642 : :
9643 : 62290 : case INTEGER_TYPE:
9644 : 62290 : if (TREE_TYPE (type))
9645 : : {
9646 : : /* A range type (representing an array domain). */
9647 : 59234 : tree_node (TYPE_MIN_VALUE (type));
9648 : 59234 : tree_node (TYPE_MAX_VALUE (type));
9649 : : }
9650 : : else
9651 : : {
9652 : : /* A new integral type (representing a bitfield). */
9653 : 3056 : if (streaming_p ())
9654 : : {
9655 : 881 : unsigned prec = TYPE_PRECISION (type);
9656 : 881 : bool unsigned_p = TYPE_UNSIGNED (type);
9657 : :
9658 : 881 : u ((prec << 1) | unsigned_p);
9659 : : }
9660 : : }
9661 : : break;
9662 : :
9663 : 923695 : case METHOD_TYPE:
9664 : 923695 : case FUNCTION_TYPE:
9665 : 923695 : {
9666 : 923695 : gcc_checking_assert (type_memfn_rqual (type) == REF_QUAL_NONE);
9667 : :
9668 : 923695 : tree arg_types = TYPE_ARG_TYPES (type);
9669 : 923695 : if (TREE_CODE (type) == METHOD_TYPE)
9670 : : {
9671 : 567267 : tree_node (TREE_TYPE (TREE_VALUE (arg_types)));
9672 : 567267 : arg_types = TREE_CHAIN (arg_types);
9673 : : }
9674 : 923695 : tree_node (arg_types);
9675 : : }
9676 : 923695 : break;
9677 : :
9678 : 1277 : case OFFSET_TYPE:
9679 : 1277 : tree_node (TYPE_OFFSET_BASETYPE (type));
9680 : 1277 : break;
9681 : :
9682 : : case POINTER_TYPE:
9683 : : /* No additional data. */
9684 : : break;
9685 : :
9686 : 707784 : case REFERENCE_TYPE:
9687 : 707784 : if (streaming_p ())
9688 : 154580 : u (TYPE_REF_IS_RVALUE (type));
9689 : : break;
9690 : :
9691 : 836727 : case DECLTYPE_TYPE:
9692 : 836727 : case TYPEOF_TYPE:
9693 : 836727 : case DEPENDENT_OPERATOR_TYPE:
9694 : 836727 : tree_node (TYPE_VALUES_RAW (type));
9695 : 836727 : if (TREE_CODE (type) == DECLTYPE_TYPE)
9696 : : /* We stash a whole bunch of things into decltype's
9697 : : flags. */
9698 : 64065 : if (streaming_p ())
9699 : 21881 : tree_node_bools (type);
9700 : : break;
9701 : :
9702 : 7997 : case TRAIT_TYPE:
9703 : 7997 : tree_node (TRAIT_TYPE_KIND_RAW (type));
9704 : 7997 : tree_node (TRAIT_TYPE_TYPE1 (type));
9705 : 7997 : tree_node (TRAIT_TYPE_TYPE2 (type));
9706 : 7997 : break;
9707 : :
9708 : : case TYPE_ARGUMENT_PACK:
9709 : : /* No additional data. */
9710 : : break;
9711 : :
9712 : 143212 : case TYPE_PACK_EXPANSION:
9713 : 143212 : if (streaming_p ())
9714 : 57745 : u (PACK_EXPANSION_LOCAL_P (type));
9715 : 286424 : tree_node (PACK_EXPANSION_PARAMETER_PACKS (type));
9716 : 143212 : tree_node (PACK_EXPANSION_EXTRA_ARGS (type));
9717 : 143212 : break;
9718 : :
9719 : 29 : case PACK_INDEX_TYPE:
9720 : 29 : tree_node (PACK_INDEX_PACK (type));
9721 : 29 : tree_node (PACK_INDEX_INDEX (type));
9722 : 29 : break;
9723 : :
9724 : 180579 : case TYPENAME_TYPE:
9725 : 180579 : {
9726 : 180579 : tree_node (TYPE_CONTEXT (type));
9727 : 180579 : tree_node (DECL_NAME (TYPE_NAME (type)));
9728 : 180579 : tree_node (TYPENAME_TYPE_FULLNAME (type));
9729 : 180579 : if (streaming_p ())
9730 : : {
9731 : 63289 : enum tag_types tag_type = none_type;
9732 : 63289 : if (TYPENAME_IS_ENUM_P (type))
9733 : : tag_type = enum_type;
9734 : 63289 : else if (TYPENAME_IS_CLASS_P (type))
9735 : : tag_type = class_type;
9736 : 61418 : else if (TYPENAME_IS_UNION_P (type))
9737 : 0 : tag_type = union_type;
9738 : 63289 : u (int (tag_type));
9739 : : }
9740 : : }
9741 : : break;
9742 : :
9743 : 252 : case UNBOUND_CLASS_TEMPLATE:
9744 : 252 : {
9745 : 252 : tree decl = TYPE_NAME (type);
9746 : 252 : tree_node (DECL_CONTEXT (decl));
9747 : 252 : tree_node (DECL_NAME (decl));
9748 : 252 : tree_node (DECL_TEMPLATE_PARMS (decl));
9749 : : }
9750 : 252 : break;
9751 : :
9752 : 42 : case VECTOR_TYPE:
9753 : 42 : if (streaming_p ())
9754 : : {
9755 : 21 : poly_uint64 nunits = TYPE_VECTOR_SUBPARTS (type);
9756 : 42 : for (unsigned ix = 0; ix != NUM_POLY_INT_COEFFS; ix++)
9757 : 21 : wu (nunits.coeffs[ix]);
9758 : : }
9759 : : break;
9760 : : }
9761 : :
9762 : 4199470 : tree_node (TYPE_ATTRIBUTES (type));
9763 : :
9764 : : /* We may have met the type during emitting the above. */
9765 : 4199470 : if (ref_node (type) != WK_none)
9766 : : {
9767 : 3813139 : int tag = insert (type);
9768 : 3813139 : if (streaming_p ())
9769 : : {
9770 : 1156145 : i (0);
9771 : 1156145 : dump (dumper::TREE)
9772 : 558 : && dump ("Wrote:%d derived type %C", tag, TREE_CODE (type));
9773 : : }
9774 : : }
9775 : :
9776 : : return;
9777 : : }
9778 : :
9779 : : /* T is (mostly*) a non-mergeable node that must be written by value.
9780 : : The mergeable case is a BINFO, which are as-if DECLSs. */
9781 : :
9782 : : void
9783 : 26010362 : trees_out::tree_value (tree t)
9784 : : {
9785 : : /* We should never be writing a type by value. tree_type should
9786 : : have streamed it, or we're going via its TYPE_DECL. */
9787 : 26010362 : gcc_checking_assert (!TYPE_P (t));
9788 : :
9789 : 26010362 : if (DECL_P (t))
9790 : : /* No template, type, var or function, except anonymous
9791 : : non-context vars and types. */
9792 : 688499 : gcc_checking_assert ((TREE_CODE (t) != TEMPLATE_DECL
9793 : : && (TREE_CODE (t) != TYPE_DECL
9794 : : || (DECL_ARTIFICIAL (t) && !DECL_CONTEXT (t)))
9795 : : && (TREE_CODE (t) != VAR_DECL
9796 : : || ((!DECL_NAME (t)
9797 : : || IDENTIFIER_INTERNAL_P (DECL_NAME (t)))
9798 : : && !DECL_CONTEXT (t)))
9799 : : && TREE_CODE (t) != FUNCTION_DECL));
9800 : :
9801 : 26010362 : if (streaming_p ())
9802 : : {
9803 : : /* A new node -> tt_node. */
9804 : 10344573 : tree_val_count++;
9805 : 10344573 : i (tt_node);
9806 : 10344573 : start (t);
9807 : 10344573 : tree_node_bools (t);
9808 : : }
9809 : :
9810 : 26010362 : if (TREE_CODE (t) == TREE_BINFO)
9811 : : /* Binfos are decl-like and need merging information. */
9812 : 208212 : binfo_mergeable (t);
9813 : :
9814 : 26010362 : int tag = insert (t, WK_value);
9815 : 26010362 : if (streaming_p ())
9816 : 10344573 : dump (dumper::TREE)
9817 : 2823 : && dump ("Writing tree:%d %C:%N", tag, TREE_CODE (t), t);
9818 : :
9819 : 26010362 : int type_tag = 0;
9820 : 26010362 : tree type = NULL_TREE;
9821 : 26010362 : if (TREE_CODE (t) == TYPE_DECL)
9822 : : {
9823 : 28 : type = TREE_TYPE (t);
9824 : :
9825 : : /* We only support a limited set of features for uncontexted types;
9826 : : these are typically types created in the language-independent
9827 : : parts of the frontend (such as ubsan). */
9828 : 28 : gcc_checking_assert (RECORD_OR_UNION_TYPE_P (type)
9829 : : && TYPE_MAIN_VARIANT (type) == type
9830 : : && TYPE_NAME (type) == t
9831 : : && TYPE_STUB_DECL (type) == t
9832 : : && !TYPE_VFIELD (type)
9833 : : && !TYPE_BINFO (type)
9834 : : && !CLASS_TYPE_P (type));
9835 : :
9836 : 28 : if (streaming_p ())
9837 : : {
9838 : 14 : start (type);
9839 : 14 : tree_node_bools (type);
9840 : : }
9841 : :
9842 : 28 : type_tag = insert (type, WK_value);
9843 : 28 : if (streaming_p ())
9844 : 14 : dump (dumper::TREE)
9845 : 0 : && dump ("Writing type: %d %C:%N", type_tag,
9846 : 0 : TREE_CODE (type), type);
9847 : : }
9848 : :
9849 : 26010362 : tree_node_vals (t);
9850 : :
9851 : 26010362 : if (type)
9852 : : {
9853 : 28 : tree_node_vals (type);
9854 : 28 : tree_node (TYPE_SIZE (type));
9855 : 28 : tree_node (TYPE_SIZE_UNIT (type));
9856 : 28 : chained_decls (TYPE_FIELDS (type));
9857 : 28 : if (streaming_p ())
9858 : 14 : dump (dumper::TREE)
9859 : 0 : && dump ("Written type:%d %C:%N", type_tag, TREE_CODE (type), type);
9860 : : }
9861 : :
9862 : : /* For uncontexted VAR_DECLs we need to stream the definition so that
9863 : : importers can recreate their value. */
9864 : 26010362 : if (TREE_CODE (t) == VAR_DECL)
9865 : : {
9866 : 614 : gcc_checking_assert (!DECL_NONTRIVIALLY_INITIALIZED_P (t));
9867 : 614 : tree_node (DECL_INITIAL (t));
9868 : : }
9869 : :
9870 : 26010362 : if (streaming_p ())
9871 : 10347396 : dump (dumper::TREE) && dump ("Written tree:%d %C:%N", tag, TREE_CODE (t), t);
9872 : 26010362 : }
9873 : :
9874 : : tree
9875 : 10266138 : trees_in::tree_value ()
9876 : : {
9877 : 10266138 : tree t = start ();
9878 : 10266138 : if (!t || !tree_node_bools (t))
9879 : 0 : return NULL_TREE;
9880 : :
9881 : 10266138 : tree existing = t;
9882 : 10266138 : if (TREE_CODE (t) == TREE_BINFO)
9883 : : {
9884 : 84521 : tree type;
9885 : 84521 : unsigned ix = binfo_mergeable (&type);
9886 : 84521 : if (TYPE_BINFO (type))
9887 : : {
9888 : : /* We already have a definition, this must be a duplicate. */
9889 : 40834 : dump (dumper::MERGE)
9890 : 232 : && dump ("Deduping binfo %N[%u]", type, ix);
9891 : 40834 : existing = TYPE_BINFO (type);
9892 : 55962 : while (existing && ix--)
9893 : 15128 : existing = TREE_CHAIN (existing);
9894 : 40834 : if (existing)
9895 : 40834 : register_duplicate (t, existing);
9896 : : else
9897 : : /* Error, mismatch -- diagnose in read_class_def's
9898 : : checking. */
9899 : : existing = t;
9900 : : }
9901 : : }
9902 : :
9903 : : /* Insert into map. */
9904 : 10266138 : int tag = insert (existing);
9905 : 10266138 : dump (dumper::TREE)
9906 : 3677 : && dump ("Reading tree:%d %C", tag, TREE_CODE (t));
9907 : :
9908 : 10266138 : int type_tag = 0;
9909 : 10266138 : tree type = NULL_TREE;
9910 : 10266138 : if (TREE_CODE (t) == TYPE_DECL)
9911 : : {
9912 : 14 : type = start ();
9913 : 14 : if (!type || !tree_node_bools (type))
9914 : : t = NULL_TREE;
9915 : :
9916 : 14 : type_tag = insert (type);
9917 : 14 : if (t)
9918 : 14 : dump (dumper::TREE)
9919 : 0 : && dump ("Reading type:%d %C", type_tag, TREE_CODE (type));
9920 : : }
9921 : :
9922 : : if (!t)
9923 : : {
9924 : 0 : bail:
9925 : 0 : back_refs[~tag] = NULL_TREE;
9926 : 0 : if (type_tag)
9927 : 0 : back_refs[~type_tag] = NULL_TREE;
9928 : 0 : set_overrun ();
9929 : 0 : return NULL_TREE;
9930 : : }
9931 : :
9932 : 10266138 : if (!tree_node_vals (t))
9933 : 0 : goto bail;
9934 : :
9935 : 10266138 : if (type)
9936 : : {
9937 : 14 : if (!tree_node_vals (type))
9938 : 0 : goto bail;
9939 : :
9940 : 14 : TYPE_SIZE (type) = tree_node ();
9941 : 14 : TYPE_SIZE_UNIT (type) = tree_node ();
9942 : 14 : TYPE_FIELDS (type) = chained_decls ();
9943 : 14 : if (get_overrun ())
9944 : 0 : goto bail;
9945 : :
9946 : 14 : dump (dumper::TREE)
9947 : 0 : && dump ("Read type:%d %C:%N", type_tag, TREE_CODE (type), type);
9948 : : }
9949 : :
9950 : 10266138 : if (TREE_CODE (t) == VAR_DECL)
9951 : : {
9952 : 325 : DECL_INITIAL (t) = tree_node ();
9953 : 325 : if (TREE_STATIC (t))
9954 : 8 : varpool_node::finalize_decl (t);
9955 : : }
9956 : :
9957 : 10266138 : if (TREE_CODE (t) == LAMBDA_EXPR
9958 : 10266138 : && CLASSTYPE_LAMBDA_EXPR (TREE_TYPE (t)))
9959 : : {
9960 : 1986 : existing = CLASSTYPE_LAMBDA_EXPR (TREE_TYPE (t));
9961 : 1986 : back_refs[~tag] = existing;
9962 : : }
9963 : :
9964 : 10269815 : dump (dumper::TREE) && dump ("Read tree:%d %C:%N", tag, TREE_CODE (t), t);
9965 : :
9966 : 10266138 : if (TREE_CODE (existing) == INTEGER_CST && !TREE_OVERFLOW (existing))
9967 : : {
9968 : 552093 : existing = cache_integer_cst (t, true);
9969 : 552093 : back_refs[~tag] = existing;
9970 : : }
9971 : :
9972 : : return existing;
9973 : : }
9974 : :
9975 : : /* Whether DECL has a TU-local dependency in the hash. */
9976 : :
9977 : : bool
9978 : 794272 : trees_out::has_tu_local_dep (tree decl) const
9979 : : {
9980 : : /* Only the contexts of fields or enums remember that they're
9981 : : TU-local. */
9982 : 794272 : if (DECL_CONTEXT (decl)
9983 : 794272 : && (TREE_CODE (decl) == FIELD_DECL
9984 : 794269 : || TREE_CODE (decl) == CONST_DECL))
9985 : 3 : decl = TYPE_NAME (DECL_CONTEXT (decl));
9986 : :
9987 : 794272 : depset *dep = dep_hash->find_dependency (decl);
9988 : 794272 : if (!dep)
9989 : : {
9990 : : /* This might be the DECL_TEMPLATE_RESULT of a TEMPLATE_DECL
9991 : : which we found was TU-local and gave up early. */
9992 : 9240 : int use_tpl = -1;
9993 : 9240 : if (tree ti = node_template_info (decl, use_tpl))
9994 : 1831 : dep = dep_hash->find_dependency (TI_TEMPLATE (ti));
9995 : : }
9996 : :
9997 : 794272 : return dep && dep->is_tu_local ();
9998 : : }
9999 : :
10000 : : /* If T depends on a TU-local entity, return that decl. */
10001 : :
10002 : : tree
10003 : 371 : trees_out::find_tu_local_decl (tree t)
10004 : : {
10005 : : /* We need to have walked all deps first before we can check. */
10006 : 371 : gcc_checking_assert (!is_initial_scan ());
10007 : :
10008 : 903 : auto walker = [](tree *tp, int *walk_subtrees, void *data) -> tree
10009 : : {
10010 : 532 : auto self = (trees_out *)data;
10011 : :
10012 : 532 : tree decl = NULL_TREE;
10013 : 532 : if (TYPE_P (*tp))
10014 : : {
10015 : : /* A PMF type is a record type, which we otherwise wouldn't walk;
10016 : : return whether the function type is TU-local. */
10017 : 358 : if (TYPE_PTRMEMFUNC_P (*tp))
10018 : : {
10019 : 3 : *walk_subtrees = 0;
10020 : 3 : return self->find_tu_local_decl (TYPE_PTRMEMFUNC_FN_TYPE (*tp));
10021 : : }
10022 : : else
10023 : 355 : decl = TYPE_MAIN_DECL (*tp);
10024 : : }
10025 : 174 : else if (DECL_P (*tp))
10026 : : decl = *tp;
10027 : :
10028 : 361 : if (decl)
10029 : : {
10030 : : /* We found a DECL, this will tell us whether we're TU-local. */
10031 : 59 : *walk_subtrees = 0;
10032 : 59 : return self->has_tu_local_dep (decl) ? decl : NULL_TREE;
10033 : : }
10034 : : return NULL_TREE;
10035 : : };
10036 : :
10037 : : /* We need to walk without duplicates so that we step into the pointed-to
10038 : : types of array types. */
10039 : 371 : return cp_walk_tree_without_duplicates (&t, walker, this);
10040 : : }
10041 : :
10042 : : /* Get the name for TU-local decl T to be used in diagnostics. */
10043 : :
10044 : : static tree
10045 : 197 : name_for_tu_local_decl (tree t)
10046 : : {
10047 : 197 : int flags = (TFF_SCOPE | TFF_DECL_SPECIFIERS);
10048 : 197 : const char *str = decl_as_string (t, flags);
10049 : 197 : return get_identifier (str);
10050 : : }
10051 : :
10052 : : /* Stream out tree node T. We automatically create local back
10053 : : references, which is essentially a single pass lisp
10054 : : self-referential structure pretty-printer. */
10055 : :
10056 : : void
10057 : 227292131 : trees_out::tree_node (tree t)
10058 : : {
10059 : 227292131 : dump.indent ();
10060 : 227292131 : walk_kind ref = ref_node (t);
10061 : 227292131 : if (ref == WK_none)
10062 : 174634079 : goto done;
10063 : :
10064 : : /* Find TU-local entities and intercept streaming to instead write a
10065 : : placeholder value; this way we don't need to emit such decls.
10066 : : We only need to do this when writing a definition of an entity
10067 : : that we know names a TU-local entity. */
10068 : 61720051 : if (!is_initial_scan () && writing_local_entities)
10069 : : {
10070 : 904 : tree local_decl = NULL_TREE;
10071 : 904 : if (DECL_P (t) && has_tu_local_dep (t))
10072 : : local_decl = t;
10073 : : /* Consider a type to be TU-local if it refers to any TU-local decl,
10074 : : no matter how deep.
10075 : :
10076 : : This worsens diagnostics slightly, as we often no longer point
10077 : : directly to the at-fault entity when instantiating. However, this
10078 : : reduces the module size slightly and means that much less of pt.cc
10079 : : needs to know about us. */
10080 : 806 : else if (TYPE_P (t))
10081 : 142 : local_decl = find_tu_local_decl (t);
10082 : 664 : else if (EXPR_P (t))
10083 : 226 : local_decl = find_tu_local_decl (TREE_TYPE (t));
10084 : :
10085 : 466 : if (local_decl)
10086 : : {
10087 : 152 : int tag = insert (t, WK_value);
10088 : 152 : if (streaming_p ())
10089 : : {
10090 : 152 : tu_local_count++;
10091 : 152 : i (tt_tu_local);
10092 : 152 : dump (dumper::TREE)
10093 : 0 : && dump ("Writing TU-local entity:%d %C:%N",
10094 : 0 : tag, TREE_CODE (t), t);
10095 : : }
10096 : 152 : tree_node (name_for_tu_local_decl (local_decl));
10097 : 152 : if (state)
10098 : 152 : state->write_location (*this, DECL_SOURCE_LOCATION (local_decl));
10099 : 152 : goto done;
10100 : : }
10101 : : }
10102 : :
10103 : 52657900 : if (ref != WK_normal)
10104 : 1335475 : goto skip_normal;
10105 : :
10106 : 51322425 : if (TREE_CODE (t) == IDENTIFIER_NODE)
10107 : : {
10108 : : /* An identifier node -> tt_id, tt_conv_id, tt_anon_id, tt_lambda_id,
10109 : : tt_internal_id. */
10110 : 6353017 : int code = tt_id;
10111 : 6353017 : if (IDENTIFIER_ANON_P (t))
10112 : 24451 : code = IDENTIFIER_LAMBDA_P (t) ? tt_lambda_id : tt_anon_id;
10113 : 6328566 : else if (IDENTIFIER_INTERNAL_P (t))
10114 : : code = tt_internal_id;
10115 : 6328552 : else if (IDENTIFIER_CONV_OP_P (t))
10116 : 9291 : code = tt_conv_id;
10117 : :
10118 : 6353017 : if (streaming_p ())
10119 : 1282694 : i (code);
10120 : :
10121 : 6353017 : if (code == tt_conv_id)
10122 : : {
10123 : 9291 : tree type = TREE_TYPE (t);
10124 : 9291 : gcc_checking_assert (type || t == conv_op_identifier);
10125 : 9291 : tree_node (type);
10126 : : }
10127 : 6343726 : else if (code == tt_id && streaming_p ())
10128 : 1272158 : str (IDENTIFIER_POINTER (t), IDENTIFIER_LENGTH (t));
10129 : 5071568 : else if (code == tt_internal_id && streaming_p ())
10130 : 7 : str (prefix_for_internal_label (t));
10131 : :
10132 : 6353017 : int tag = insert (t);
10133 : 6353017 : if (streaming_p ())
10134 : : {
10135 : : /* We know the ordering of the 5 id tags. */
10136 : 1282694 : static const char *const kinds[] =
10137 : : {"", "conv_op ", "anon ", "lambda ", "internal "};
10138 : 1282694 : dump (dumper::TREE)
10139 : 1074 : && dump ("Written:%d %sidentifier:%N", tag,
10140 : 1071 : kinds[code - tt_id],
10141 : 3 : code == tt_conv_id ? TREE_TYPE (t) : t);
10142 : : }
10143 : 6353017 : goto done;
10144 : : }
10145 : :
10146 : 44969408 : if (TREE_CODE (t) == TREE_BINFO)
10147 : : {
10148 : : /* A BINFO -> tt_binfo.
10149 : : We must do this by reference. We stream the binfo tree
10150 : : itself when streaming its owning RECORD_TYPE. That we got
10151 : : here means the dominating type is not in this SCC. */
10152 : 52108 : if (streaming_p ())
10153 : 1370 : i (tt_binfo);
10154 : 52108 : binfo_mergeable (t);
10155 : 52108 : gcc_checking_assert (!TREE_VISITED (t));
10156 : 52108 : int tag = insert (t);
10157 : 52108 : if (streaming_p ())
10158 : 1370 : dump (dumper::TREE) && dump ("Inserting binfo:%d %N", tag, t);
10159 : 52108 : goto done;
10160 : : }
10161 : :
10162 : 44917300 : if (TREE_CODE (t) == INTEGER_CST
10163 : 3334870 : && !TREE_OVERFLOW (t)
10164 : 48252170 : && TREE_CODE (TREE_TYPE (t)) == ENUMERAL_TYPE)
10165 : : {
10166 : : /* An integral constant of enumeral type. See if it matches one
10167 : : of the enumeration values. */
10168 : 28414 : for (tree values = TYPE_VALUES (TREE_TYPE (t));
10169 : 753192 : values; values = TREE_CHAIN (values))
10170 : : {
10171 : 751266 : tree decl = TREE_VALUE (values);
10172 : 751266 : if (tree_int_cst_equal (DECL_INITIAL (decl), t))
10173 : : {
10174 : 26488 : if (streaming_p ())
10175 : 7348 : u (tt_enum_value);
10176 : 26488 : tree_node (decl);
10177 : 26530 : dump (dumper::TREE) && dump ("Written enum value %N", decl);
10178 : 26488 : goto done;
10179 : : }
10180 : : }
10181 : : /* It didn't match. We'll write it a an explicit INTEGER_CST
10182 : : node. */
10183 : : }
10184 : :
10185 : 44890812 : if (TYPE_P (t))
10186 : : {
10187 : 8589734 : type_node (t);
10188 : 8589734 : goto done;
10189 : : }
10190 : :
10191 : 36301078 : if (DECL_P (t))
10192 : : {
10193 : 11216307 : if (DECL_TEMPLATE_PARM_P (t))
10194 : : {
10195 : 1784835 : tpl_parm_value (t);
10196 : 1784835 : goto done;
10197 : : }
10198 : :
10199 : 9431472 : if (!DECL_CONTEXT (t))
10200 : : {
10201 : : /* There are a few cases of decls with no context. We'll write
10202 : : these by value, but first assert they are cases we expect. */
10203 : 22118 : gcc_checking_assert (ref == WK_normal);
10204 : 22118 : switch (TREE_CODE (t))
10205 : : {
10206 : 0 : default: gcc_unreachable ();
10207 : :
10208 : 7308 : case LABEL_DECL:
10209 : : /* CASE_LABEL_EXPRs contain uncontexted LABEL_DECLs. */
10210 : 7308 : gcc_checking_assert (!DECL_NAME (t));
10211 : : break;
10212 : :
10213 : 614 : case VAR_DECL:
10214 : : /* AGGR_INIT_EXPRs cons up anonymous uncontexted VAR_DECLs,
10215 : : and internal vars are created by sanitizers and
10216 : : __builtin_source_location. */
10217 : 614 : gcc_checking_assert ((!DECL_NAME (t)
10218 : : || IDENTIFIER_INTERNAL_P (DECL_NAME (t)))
10219 : : && DECL_ARTIFICIAL (t));
10220 : : break;
10221 : :
10222 : 14168 : case PARM_DECL:
10223 : : /* REQUIRES_EXPRs have a chain of uncontexted PARM_DECLS,
10224 : : and an implicit this parm in an NSDMI has no context. */
10225 : 14168 : gcc_checking_assert (CONSTRAINT_VAR_P (t)
10226 : : || DECL_NAME (t) == this_identifier);
10227 : : break;
10228 : :
10229 : 28 : case TYPE_DECL:
10230 : : /* Some parts of the compiler need internal struct types;
10231 : : these types may not have an appropriate context to use.
10232 : : Walk the whole type (including its definition) by value. */
10233 : 28 : gcc_checking_assert (DECL_ARTIFICIAL (t)
10234 : : && TYPE_ARTIFICIAL (TREE_TYPE (t))
10235 : : && RECORD_OR_UNION_TYPE_P (TREE_TYPE (t))
10236 : : && !CLASS_TYPE_P (TREE_TYPE (t)));
10237 : : break;
10238 : : }
10239 : 22118 : mark_declaration (t, has_definition (t));
10240 : 22118 : goto by_value;
10241 : : }
10242 : : }
10243 : :
10244 : 25084771 : skip_normal:
10245 : 35829600 : if (DECL_P (t) && !decl_node (t, ref))
10246 : 9841356 : goto done;
10247 : :
10248 : : /* Otherwise by value */
10249 : 26010362 : by_value:
10250 : 26010362 : tree_value (t);
10251 : :
10252 : 227292131 : done:
10253 : : /* And, breath out. */
10254 : 227292131 : dump.outdent ();
10255 : 227292131 : }
10256 : :
10257 : : /* Stream in a tree node. */
10258 : :
10259 : : tree
10260 : 84449437 : trees_in::tree_node (bool is_use)
10261 : : {
10262 : 84449437 : if (get_overrun ())
10263 : : return NULL_TREE;
10264 : :
10265 : 84449437 : dump.indent ();
10266 : 84449437 : int tag = i ();
10267 : 84449437 : tree res = NULL_TREE;
10268 : 84449437 : switch (tag)
10269 : : {
10270 : 27810819 : default:
10271 : : /* backref, pull it out of the map. */
10272 : 27810819 : res = back_ref (tag);
10273 : 27810819 : break;
10274 : :
10275 : : case tt_null:
10276 : : /* NULL_TREE. */
10277 : : break;
10278 : :
10279 : 152 : case tt_tu_local:
10280 : 152 : {
10281 : : /* A translation-unit-local entity. */
10282 : 152 : res = make_node (TU_LOCAL_ENTITY);
10283 : 152 : int tag = insert (res);
10284 : :
10285 : 152 : TU_LOCAL_ENTITY_NAME (res) = tree_node ();
10286 : 152 : TU_LOCAL_ENTITY_LOCATION (res) = state->read_location (*this);
10287 : 152 : dump (dumper::TREE) && dump ("Read TU-local entity:%d %N", tag, res);
10288 : : }
10289 : : break;
10290 : :
10291 : 6505536 : case tt_fixed:
10292 : : /* A fixed ref, find it in the fixed_ref array. */
10293 : 6505536 : {
10294 : 6505536 : unsigned fix = u ();
10295 : 6505536 : if (fix < (*fixed_trees).length ())
10296 : : {
10297 : 6505536 : res = (*fixed_trees)[fix];
10298 : 6505536 : dump (dumper::TREE) && dump ("Read fixed:%u %C:%N%S", fix,
10299 : 5046 : TREE_CODE (res), res, res);
10300 : : }
10301 : :
10302 : 6500490 : if (!res)
10303 : 0 : set_overrun ();
10304 : : }
10305 : : break;
10306 : :
10307 : 65796 : case tt_parm:
10308 : 65796 : {
10309 : 65796 : tree fn = tree_node ();
10310 : 65796 : if (fn && TREE_CODE (fn) == FUNCTION_DECL)
10311 : 65796 : res = tree_node ();
10312 : 65796 : if (res)
10313 : 65796 : dump (dumper::TREE)
10314 : 21 : && dump ("Read %s reference %N",
10315 : 21 : TREE_CODE (res) == PARM_DECL ? "parameter" : "result",
10316 : : res);
10317 : : }
10318 : : break;
10319 : :
10320 : 10266138 : case tt_node:
10321 : : /* A new node. Stream it in. */
10322 : 10266138 : res = tree_value ();
10323 : 10266138 : break;
10324 : :
10325 : 1077604 : case tt_decl:
10326 : : /* A new decl. Stream it in. */
10327 : 1077604 : res = decl_value ();
10328 : 1077604 : break;
10329 : :
10330 : 366196 : case tt_tpl_parm:
10331 : : /* A template parameter. Stream it in. */
10332 : 366196 : res = tpl_parm_value ();
10333 : 366196 : break;
10334 : :
10335 : 1078970 : case tt_id:
10336 : : /* An identifier node. */
10337 : 1078970 : {
10338 : 1078970 : size_t l;
10339 : 1078970 : const char *chars = str (&l);
10340 : 1078970 : res = get_identifier_with_length (chars, l);
10341 : 1078970 : int tag = insert (res);
10342 : 1078970 : dump (dumper::TREE)
10343 : 1488 : && dump ("Read identifier:%d %N", tag, res);
10344 : : }
10345 : 1078970 : break;
10346 : :
10347 : 2738 : case tt_conv_id:
10348 : : /* A conversion operator. Get the type and recreate the
10349 : : identifier. */
10350 : 2738 : {
10351 : 2738 : tree type = tree_node ();
10352 : 2738 : if (!get_overrun ())
10353 : : {
10354 : 2738 : res = type ? make_conv_op_name (type) : conv_op_identifier;
10355 : 2738 : int tag = insert (res);
10356 : 2738 : dump (dumper::TREE)
10357 : 27 : && dump ("Created conv_op:%d %S for %N", tag, res, type);
10358 : : }
10359 : : }
10360 : : break;
10361 : :
10362 : 6234 : case tt_anon_id:
10363 : 6234 : case tt_lambda_id:
10364 : : /* An anonymous or lambda id. */
10365 : 6234 : {
10366 : 6234 : res = make_anon_name ();
10367 : 6234 : if (tag == tt_lambda_id)
10368 : 3693 : IDENTIFIER_LAMBDA_P (res) = true;
10369 : 6234 : int tag = insert (res);
10370 : 6237 : dump (dumper::TREE)
10371 : 3 : && dump ("Read %s identifier:%d %N",
10372 : 3 : IDENTIFIER_LAMBDA_P (res) ? "lambda" : "anon", tag, res);
10373 : : }
10374 : : break;
10375 : :
10376 : 8 : case tt_internal_id:
10377 : : /* An internal label. */
10378 : 8 : {
10379 : 8 : const char *prefix = str ();
10380 : 8 : res = generate_internal_label (prefix);
10381 : 8 : int tag = insert (res);
10382 : 8 : dump (dumper::TREE)
10383 : 1 : && dump ("Read internal identifier:%d %N", tag, res);
10384 : : }
10385 : : break;
10386 : :
10387 : 167381 : case tt_typedef_type:
10388 : 167381 : res = tree_node ();
10389 : 167381 : if (res)
10390 : : {
10391 : 167381 : dump (dumper::TREE)
10392 : 74 : && dump ("Read %stypedef %C:%N",
10393 : 74 : DECL_IMPLICIT_TYPEDEF_P (res) ? "implicit " : "",
10394 : 74 : TREE_CODE (res), res);
10395 : 167381 : if (TREE_CODE (res) != TU_LOCAL_ENTITY)
10396 : 167380 : res = TREE_TYPE (res);
10397 : : }
10398 : : break;
10399 : :
10400 : 1277418 : case tt_derived_type:
10401 : : /* A type derived from some other type. */
10402 : 1277418 : {
10403 : 1277418 : enum tree_code code = tree_code (u ());
10404 : 1277418 : res = tree_node ();
10405 : :
10406 : 1277418 : switch (code)
10407 : : {
10408 : 0 : default:
10409 : 0 : set_overrun ();
10410 : 0 : break;
10411 : :
10412 : 19124 : case ARRAY_TYPE:
10413 : 19124 : {
10414 : 19124 : tree domain = tree_node ();
10415 : 19124 : int dep = u ();
10416 : 19124 : if (!get_overrun ())
10417 : 19124 : res = build_cplus_array_type (res, domain, dep);
10418 : : }
10419 : : break;
10420 : :
10421 : 111 : case COMPLEX_TYPE:
10422 : 111 : if (!get_overrun ())
10423 : 111 : res = build_complex_type (res);
10424 : : break;
10425 : :
10426 : 9 : case BOOLEAN_TYPE:
10427 : 9 : {
10428 : 9 : unsigned precision = u ();
10429 : 9 : if (!get_overrun ())
10430 : 9 : res = build_nonstandard_boolean_type (precision);
10431 : : }
10432 : : break;
10433 : :
10434 : 17356 : case INTEGER_TYPE:
10435 : 17356 : if (res)
10436 : : {
10437 : : /* A range type (representing an array domain). */
10438 : 16571 : tree min = tree_node ();
10439 : 16571 : tree max = tree_node ();
10440 : :
10441 : 16571 : if (!get_overrun ())
10442 : 16571 : res = build_range_type (res, min, max);
10443 : : }
10444 : : else
10445 : : {
10446 : : /* A new integral type (representing a bitfield). */
10447 : 785 : unsigned enc = u ();
10448 : 785 : if (!get_overrun ())
10449 : 785 : res = build_nonstandard_integer_type (enc >> 1, enc & 1);
10450 : : }
10451 : : break;
10452 : :
10453 : 363709 : case FUNCTION_TYPE:
10454 : 363709 : case METHOD_TYPE:
10455 : 363709 : {
10456 : 363709 : tree klass = code == METHOD_TYPE ? tree_node () : NULL_TREE;
10457 : 363709 : tree args = tree_node ();
10458 : 363709 : if (!get_overrun ())
10459 : : {
10460 : 363709 : if (klass)
10461 : 229515 : res = build_method_type_directly (klass, res, args);
10462 : : else
10463 : 134194 : res = cp_build_function_type (res, args);
10464 : : }
10465 : : }
10466 : : break;
10467 : :
10468 : 253 : case OFFSET_TYPE:
10469 : 253 : {
10470 : 253 : tree base = tree_node ();
10471 : 253 : if (!get_overrun ())
10472 : 253 : res = build_offset_type (base, res);
10473 : : }
10474 : : break;
10475 : :
10476 : 173316 : case POINTER_TYPE:
10477 : 173316 : if (!get_overrun ())
10478 : 173316 : res = build_pointer_type (res);
10479 : : break;
10480 : :
10481 : 145463 : case REFERENCE_TYPE:
10482 : 145463 : {
10483 : 145463 : bool rval = bool (u ());
10484 : 145463 : if (!get_overrun ())
10485 : 145463 : res = cp_build_reference_type (res, rval);
10486 : : }
10487 : : break;
10488 : :
10489 : 389144 : case DECLTYPE_TYPE:
10490 : 389144 : case TYPEOF_TYPE:
10491 : 389144 : case DEPENDENT_OPERATOR_TYPE:
10492 : 389144 : {
10493 : 389144 : tree expr = tree_node ();
10494 : 389144 : if (!get_overrun ())
10495 : : {
10496 : 389144 : res = cxx_make_type (code);
10497 : 389144 : TYPE_VALUES_RAW (res) = expr;
10498 : 389144 : if (code == DECLTYPE_TYPE)
10499 : 18885 : tree_node_bools (res);
10500 : 389144 : SET_TYPE_STRUCTURAL_EQUALITY (res);
10501 : : }
10502 : : }
10503 : : break;
10504 : :
10505 : 2236 : case TRAIT_TYPE:
10506 : 2236 : {
10507 : 2236 : tree kind = tree_node ();
10508 : 2236 : tree type1 = tree_node ();
10509 : 2236 : tree type2 = tree_node ();
10510 : 2236 : if (!get_overrun ())
10511 : : {
10512 : 2236 : res = cxx_make_type (TRAIT_TYPE);
10513 : 2236 : TRAIT_TYPE_KIND_RAW (res) = kind;
10514 : 2236 : TRAIT_TYPE_TYPE1 (res) = type1;
10515 : 2236 : TRAIT_TYPE_TYPE2 (res) = type2;
10516 : 2236 : SET_TYPE_STRUCTURAL_EQUALITY (res);
10517 : : }
10518 : : }
10519 : : break;
10520 : :
10521 : 53769 : case TYPE_ARGUMENT_PACK:
10522 : 53769 : if (!get_overrun ())
10523 : : {
10524 : 53769 : tree pack = cxx_make_type (TYPE_ARGUMENT_PACK);
10525 : 53769 : ARGUMENT_PACK_ARGS (pack) = res;
10526 : : res = pack;
10527 : : }
10528 : : break;
10529 : :
10530 : 52949 : case TYPE_PACK_EXPANSION:
10531 : 52949 : {
10532 : 52949 : bool local = u ();
10533 : 52949 : tree param_packs = tree_node ();
10534 : 52949 : tree extra_args = tree_node ();
10535 : 52949 : if (!get_overrun ())
10536 : : {
10537 : 52949 : tree expn = cxx_make_type (TYPE_PACK_EXPANSION);
10538 : 52949 : SET_TYPE_STRUCTURAL_EQUALITY (expn);
10539 : 52949 : PACK_EXPANSION_PATTERN (expn) = res;
10540 : 105898 : PACK_EXPANSION_PARAMETER_PACKS (expn) = param_packs;
10541 : 52949 : PACK_EXPANSION_EXTRA_ARGS (expn) = extra_args;
10542 : 52949 : PACK_EXPANSION_LOCAL_P (expn) = local;
10543 : 52949 : res = expn;
10544 : : }
10545 : : }
10546 : : break;
10547 : :
10548 : 20 : case PACK_INDEX_TYPE:
10549 : 20 : {
10550 : 20 : tree pack = tree_node ();
10551 : 20 : tree index = tree_node ();
10552 : 20 : if (!get_overrun ())
10553 : 20 : res = make_pack_index (pack, index);
10554 : : }
10555 : : break;
10556 : :
10557 : 59879 : case TYPENAME_TYPE:
10558 : 59879 : {
10559 : 59879 : tree ctx = tree_node ();
10560 : 59879 : tree name = tree_node ();
10561 : 59879 : tree fullname = tree_node ();
10562 : 59879 : enum tag_types tag_type = tag_types (u ());
10563 : :
10564 : 59879 : if (!get_overrun ())
10565 : 59879 : res = build_typename_type (ctx, name, fullname, tag_type);
10566 : : }
10567 : : break;
10568 : :
10569 : 50 : case UNBOUND_CLASS_TEMPLATE:
10570 : 50 : {
10571 : 50 : tree ctx = tree_node ();
10572 : 50 : tree name = tree_node ();
10573 : 50 : tree parms = tree_node ();
10574 : :
10575 : 50 : if (!get_overrun ())
10576 : 50 : res = make_unbound_class_template_raw (ctx, name, parms);
10577 : : }
10578 : : break;
10579 : :
10580 : : case VECTOR_TYPE:
10581 : : {
10582 : : poly_uint64 nunits;
10583 : 60 : for (unsigned ix = 0; ix != NUM_POLY_INT_COEFFS; ix++)
10584 : 30 : nunits.coeffs[ix] = wu ();
10585 : 30 : if (!get_overrun ())
10586 : 30 : res = build_vector_type (res, nunits);
10587 : : }
10588 : : break;
10589 : : }
10590 : :
10591 : : /* In the exporting TU, a derived type with attributes was built by
10592 : : build_type_attribute_variant as a distinct copy, with itself as
10593 : : TYPE_MAIN_VARIANT. We repeat that on import to get the version
10594 : : without attributes as TYPE_CANONICAL. */
10595 : 1277418 : if (tree attribs = tree_node ())
10596 : 14942 : res = cp_build_type_attribute_variant (res, attribs);
10597 : :
10598 : 1277418 : int tag = i ();
10599 : 1277418 : if (!tag)
10600 : : {
10601 : 1092290 : tag = insert (res);
10602 : 1092290 : if (res)
10603 : 1092290 : dump (dumper::TREE)
10604 : 678 : && dump ("Created:%d derived type %C", tag, code);
10605 : : }
10606 : : else
10607 : 185128 : res = back_ref (tag);
10608 : : }
10609 : : break;
10610 : :
10611 : 370972 : case tt_variant_type:
10612 : : /* Variant of some type. */
10613 : 370972 : {
10614 : 370972 : res = tree_node ();
10615 : 370972 : int flags = i ();
10616 : 370972 : if (get_overrun ())
10617 : : ;
10618 : 370972 : else if (flags < 0)
10619 : : /* No change. */;
10620 : 179824 : else if (TREE_CODE (res) == FUNCTION_TYPE
10621 : 179824 : || TREE_CODE (res) == METHOD_TYPE)
10622 : : {
10623 : 178297 : cp_ref_qualifier rqual = cp_ref_qualifier (flags & 3);
10624 : 178297 : bool late = (flags >> 2) & 1;
10625 : 178297 : cp_cv_quals quals = cp_cv_quals (flags >> 3);
10626 : :
10627 : 178297 : tree raises = tree_node ();
10628 : 178297 : if (raises == error_mark_node)
10629 : 6072 : raises = TYPE_RAISES_EXCEPTIONS (res);
10630 : :
10631 : 178297 : res = build_cp_fntype_variant (res, rqual, raises, late);
10632 : 178297 : if (TREE_CODE (res) == FUNCTION_TYPE)
10633 : 64179 : res = apply_memfn_quals (res, quals, rqual);
10634 : : }
10635 : : else
10636 : : {
10637 : 1527 : res = build_aligned_type (res, (1u << flags) >> 1);
10638 : 1527 : TYPE_USER_ALIGN (res) = true;
10639 : : }
10640 : :
10641 : 370972 : int quals = i ();
10642 : 370972 : if (quals >= 0 && !get_overrun ())
10643 : 192277 : res = cp_build_qualified_type (res, quals);
10644 : :
10645 : 370972 : int tag = i ();
10646 : 370972 : if (!tag)
10647 : : {
10648 : 370972 : tag = insert (res);
10649 : 370972 : if (res)
10650 : 370972 : dump (dumper::TREE)
10651 : 292 : && dump ("Created:%d variant type %C", tag, TREE_CODE (res));
10652 : : }
10653 : : else
10654 : 0 : res = back_ref (tag);
10655 : : }
10656 : : break;
10657 : :
10658 : 4405 : case tt_tinfo_var:
10659 : 4405 : case tt_tinfo_typedef:
10660 : : /* A tinfo var or typedef. */
10661 : 4405 : {
10662 : 4405 : bool is_var = tag == tt_tinfo_var;
10663 : 4405 : unsigned ix = u ();
10664 : 4405 : tree type = NULL_TREE;
10665 : :
10666 : 4405 : if (is_var)
10667 : : {
10668 : 2663 : tree name = tree_node ();
10669 : 2663 : type = tree_node ();
10670 : :
10671 : 2663 : if (!get_overrun ())
10672 : 2663 : res = get_tinfo_decl_direct (type, name, int (ix));
10673 : : }
10674 : : else
10675 : : {
10676 : 1742 : if (!get_overrun ())
10677 : : {
10678 : 1742 : type = get_pseudo_tinfo_type (ix);
10679 : 1742 : res = TYPE_NAME (type);
10680 : : }
10681 : : }
10682 : 4405 : if (res)
10683 : : {
10684 : 4405 : int tag = insert (res);
10685 : 4405 : dump (dumper::TREE)
10686 : 36 : && dump ("Created tinfo_%s:%d %S:%u for %N",
10687 : : is_var ? "var" : "decl", tag, res, ix, type);
10688 : 4405 : if (!is_var)
10689 : : {
10690 : 1742 : tag = insert (type);
10691 : 1742 : dump (dumper::TREE)
10692 : 12 : && dump ("Created tinfo_type:%d %u %N", tag, ix, type);
10693 : : }
10694 : : }
10695 : : }
10696 : : break;
10697 : :
10698 : 1052 : case tt_ptrmem_type:
10699 : : /* A pointer to member function. */
10700 : 1052 : {
10701 : 1052 : tree type = tree_node ();
10702 : 1052 : if (type && TREE_CODE (type) == POINTER_TYPE
10703 : 2104 : && TREE_CODE (TREE_TYPE (type)) == METHOD_TYPE)
10704 : : {
10705 : 1052 : res = build_ptrmemfunc_type (type);
10706 : 1052 : int tag = insert (res);
10707 : 1055 : dump (dumper::TREE) && dump ("Created:%d ptrmem type", tag);
10708 : : }
10709 : : else
10710 : 0 : set_overrun ();
10711 : : }
10712 : : break;
10713 : :
10714 : 6 : case tt_nttp_var:
10715 : : /* An NTTP object. */
10716 : 6 : {
10717 : 6 : tree init = tree_node ();
10718 : 6 : tree name = tree_node ();
10719 : 6 : if (!get_overrun ())
10720 : : {
10721 : : /* We don't want to check the initializer as that may require
10722 : : name lookup, which could recursively start lazy loading.
10723 : : Instead we know that INIT is already valid so we can just
10724 : : apply that directly. */
10725 : 6 : res = get_template_parm_object (init, name, /*check_init=*/false);
10726 : 6 : int tag = insert (res);
10727 : 6 : dump (dumper::TREE)
10728 : 0 : && dump ("Created nttp object:%d %N", tag, name);
10729 : : }
10730 : : }
10731 : : break;
10732 : :
10733 : 6475 : case tt_enum_value:
10734 : : /* An enum const value. */
10735 : 6475 : {
10736 : 6475 : if (tree decl = tree_node ())
10737 : : {
10738 : 6493 : dump (dumper::TREE) && dump ("Read enum value %N", decl);
10739 : 6475 : res = DECL_INITIAL (decl);
10740 : : }
10741 : :
10742 : 6475 : if (!res)
10743 : 0 : set_overrun ();
10744 : : }
10745 : : break;
10746 : :
10747 : 12440 : case tt_enum_decl:
10748 : : /* An enum decl. */
10749 : 12440 : {
10750 : 12440 : tree ctx = tree_node ();
10751 : 12440 : tree name = tree_node ();
10752 : :
10753 : 12440 : if (!get_overrun ()
10754 : 12440 : && TREE_CODE (ctx) == ENUMERAL_TYPE)
10755 : 12440 : res = find_enum_member (ctx, name);
10756 : :
10757 : 12440 : if (!res)
10758 : 0 : set_overrun ();
10759 : : else
10760 : : {
10761 : 12440 : int tag = insert (res);
10762 : 12440 : dump (dumper::TREE)
10763 : 18 : && dump ("Read enum decl:%d %C:%N", tag, TREE_CODE (res), res);
10764 : : }
10765 : : }
10766 : : break;
10767 : :
10768 : 7282 : case tt_data_member:
10769 : : /* A data member. */
10770 : 7282 : {
10771 : 7282 : tree ctx = tree_node ();
10772 : 7282 : tree name = tree_node ();
10773 : :
10774 : 7282 : if (!get_overrun ()
10775 : 7282 : && RECORD_OR_UNION_TYPE_P (ctx))
10776 : : {
10777 : 7282 : if (name)
10778 : 6393 : res = lookup_class_binding (ctx, name);
10779 : : else
10780 : 889 : res = lookup_field_ident (ctx, u ());
10781 : :
10782 : 7282 : if (!res
10783 : 7282 : || (TREE_CODE (res) != FIELD_DECL
10784 : 7282 : && TREE_CODE (res) != USING_DECL)
10785 : 14564 : || DECL_CONTEXT (res) != ctx)
10786 : : res = NULL_TREE;
10787 : : }
10788 : :
10789 : 7282 : if (!res)
10790 : 0 : set_overrun ();
10791 : : else
10792 : : {
10793 : 7282 : int tag = insert (res);
10794 : 7282 : dump (dumper::TREE)
10795 : 26 : && dump ("Read member:%d %C:%N", tag, TREE_CODE (res), res);
10796 : : }
10797 : : }
10798 : : break;
10799 : :
10800 : 1359 : case tt_binfo:
10801 : : /* A BINFO. Walk the tree of the dominating type. */
10802 : 1359 : {
10803 : 1359 : tree type;
10804 : 1359 : unsigned ix = binfo_mergeable (&type);
10805 : 1359 : if (type)
10806 : : {
10807 : 1359 : res = TYPE_BINFO (type);
10808 : 1422 : for (; ix && res; res = TREE_CHAIN (res))
10809 : 63 : ix--;
10810 : 1359 : if (!res)
10811 : 0 : set_overrun ();
10812 : : }
10813 : :
10814 : 1359 : if (get_overrun ())
10815 : : break;
10816 : :
10817 : : /* Insert binfo into backreferences. */
10818 : 1359 : tag = insert (res);
10819 : 1359 : dump (dumper::TREE) && dump ("Read binfo:%d %N", tag, res);
10820 : : }
10821 : 1359 : break;
10822 : :
10823 : 65 : case tt_vtable:
10824 : 65 : {
10825 : 65 : unsigned ix = u ();
10826 : 65 : tree ctx = tree_node ();
10827 : 65 : dump (dumper::TREE) && dump ("Reading vtable %N[%u]", ctx, ix);
10828 : 65 : if (TREE_CODE (ctx) == RECORD_TYPE && TYPE_LANG_SPECIFIC (ctx))
10829 : 77 : for (res = CLASSTYPE_VTABLES (ctx); res; res = DECL_CHAIN (res))
10830 : 77 : if (!ix--)
10831 : : break;
10832 : 65 : if (!res)
10833 : 0 : set_overrun ();
10834 : : }
10835 : : break;
10836 : :
10837 : 0 : case tt_thunk:
10838 : 0 : {
10839 : 0 : int fixed = i ();
10840 : 0 : tree target = tree_node ();
10841 : 0 : tree virt = tree_node ();
10842 : :
10843 : 0 : for (tree thunk = DECL_THUNKS (target);
10844 : 0 : thunk; thunk = DECL_CHAIN (thunk))
10845 : 0 : if (THUNK_FIXED_OFFSET (thunk) == fixed
10846 : 0 : && !THUNK_VIRTUAL_OFFSET (thunk) == !virt
10847 : 0 : && (!virt
10848 : 0 : || tree_int_cst_equal (virt, THUNK_VIRTUAL_OFFSET (thunk))))
10849 : : {
10850 : : res = thunk;
10851 : : break;
10852 : : }
10853 : :
10854 : 0 : int tag = insert (res);
10855 : 0 : if (res)
10856 : 0 : dump (dumper::TREE)
10857 : 0 : && dump ("Read:%d thunk %N to %N", tag, DECL_NAME (res), target);
10858 : : else
10859 : 0 : set_overrun ();
10860 : : }
10861 : : break;
10862 : :
10863 : 133477 : case tt_clone_ref:
10864 : 133477 : {
10865 : 133477 : tree target = tree_node ();
10866 : 133477 : tree name = tree_node ();
10867 : :
10868 : 133477 : if (DECL_P (target) && DECL_MAYBE_IN_CHARGE_CDTOR_P (target))
10869 : : {
10870 : 133477 : tree clone;
10871 : 207718 : FOR_EVERY_CLONE (clone, target)
10872 : 207718 : if (DECL_NAME (clone) == name)
10873 : : {
10874 : : res = clone;
10875 : : break;
10876 : : }
10877 : : }
10878 : :
10879 : : /* A clone might have a different vtable entry. */
10880 : 133477 : if (res && DECL_VIRTUAL_P (res))
10881 : 7639 : DECL_VINDEX (res) = tree_node ();
10882 : :
10883 : 133477 : if (!res)
10884 : 0 : set_overrun ();
10885 : 133477 : int tag = insert (res);
10886 : 133477 : if (res)
10887 : 133477 : dump (dumper::TREE)
10888 : 230 : && dump ("Read:%d clone %N of %N", tag, DECL_NAME (res), target);
10889 : : else
10890 : 0 : set_overrun ();
10891 : : }
10892 : : break;
10893 : :
10894 : 939063 : case tt_entity:
10895 : : /* Index into the entity table. Perhaps not loaded yet! */
10896 : 939063 : {
10897 : 939063 : unsigned origin = state->slurp->remap_module (u ());
10898 : 939063 : unsigned ident = u ();
10899 : 939063 : module_state *from = (*modules)[origin];
10900 : :
10901 : 939063 : if (!origin || ident >= from->entity_num)
10902 : 0 : set_overrun ();
10903 : 939063 : if (!get_overrun ())
10904 : : {
10905 : 939063 : binding_slot *slot = &(*entity_ary)[from->entity_lwm + ident];
10906 : 939063 : if (slot->is_lazy ())
10907 : 42161 : if (!from->lazy_load (ident, slot))
10908 : 0 : set_overrun ();
10909 : 939063 : res = *slot;
10910 : : }
10911 : :
10912 : 939063 : if (res)
10913 : : {
10914 : 939063 : const char *kind = (origin != state->mod ? "Imported" : "Named");
10915 : 939063 : int tag = insert (res);
10916 : 939063 : dump (dumper::TREE)
10917 : 605 : && dump ("%s:%d %C:%N@%M", kind, tag, TREE_CODE (res),
10918 : 605 : res, (*modules)[origin]);
10919 : :
10920 : 939063 : if (!add_indirects (res))
10921 : : {
10922 : 0 : set_overrun ();
10923 : 0 : res = NULL_TREE;
10924 : : }
10925 : : }
10926 : : }
10927 : : break;
10928 : :
10929 : 2815 : case tt_template:
10930 : : /* A template. */
10931 : 2815 : if (tree tpl = tree_node ())
10932 : : {
10933 : 2815 : res = (TREE_CODE (tpl) == TU_LOCAL_ENTITY ?
10934 : 2815 : tpl : DECL_TEMPLATE_RESULT (tpl));
10935 : 2815 : dump (dumper::TREE)
10936 : 9 : && dump ("Read template %C:%N", TREE_CODE (res), res);
10937 : : }
10938 : : break;
10939 : : }
10940 : :
10941 : 84449437 : if (is_use && !unused && res && DECL_P (res) && !TREE_USED (res))
10942 : : {
10943 : : /* Mark decl used as mark_used does -- we cannot call
10944 : : mark_used in the middle of streaming, we only need a subset
10945 : : of its functionality. */
10946 : 634945 : TREE_USED (res) = true;
10947 : :
10948 : : /* And for structured bindings also the underlying decl. */
10949 : 634945 : if (DECL_DECOMPOSITION_P (res) && !DECL_DECOMP_IS_BASE (res))
10950 : 1138 : TREE_USED (DECL_DECOMP_BASE (res)) = true;
10951 : :
10952 : 634945 : if (DECL_CLONED_FUNCTION_P (res))
10953 : 5935 : TREE_USED (DECL_CLONED_FUNCTION (res)) = true;
10954 : : }
10955 : :
10956 : 84449437 : dump.outdent ();
10957 : 84449437 : return res;
10958 : : }
10959 : :
10960 : : void
10961 : 1509434 : trees_out::tpl_parms (tree parms, unsigned &tpl_levels)
10962 : : {
10963 : 1509434 : if (!parms)
10964 : : return;
10965 : :
10966 : 1015232 : if (TREE_VISITED (parms))
10967 : : {
10968 : 424951 : ref_node (parms);
10969 : 424951 : return;
10970 : : }
10971 : :
10972 : 590281 : tpl_parms (TREE_CHAIN (parms), tpl_levels);
10973 : :
10974 : 590281 : tree vec = TREE_VALUE (parms);
10975 : 590281 : unsigned len = TREE_VEC_LENGTH (vec);
10976 : : /* Depth. */
10977 : 590281 : int tag = insert (parms);
10978 : 590281 : if (streaming_p ())
10979 : : {
10980 : 159201 : i (len + 1);
10981 : 159267 : dump (dumper::TREE)
10982 : 66 : && dump ("Writing template parms:%d level:%N length:%d",
10983 : 66 : tag, TREE_PURPOSE (parms), len);
10984 : : }
10985 : 590281 : tree_node (TREE_PURPOSE (parms));
10986 : :
10987 : 1615908 : for (unsigned ix = 0; ix != len; ix++)
10988 : : {
10989 : 1025627 : tree parm = TREE_VEC_ELT (vec, ix);
10990 : 1025627 : tree decl = TREE_VALUE (parm);
10991 : :
10992 : 1025627 : gcc_checking_assert (DECL_TEMPLATE_PARM_P (decl));
10993 : 1025627 : if (CHECKING_P)
10994 : 1025627 : switch (TREE_CODE (decl))
10995 : : {
10996 : 0 : default: gcc_unreachable ();
10997 : :
10998 : 3021 : case TEMPLATE_DECL:
10999 : 3021 : gcc_assert ((TREE_CODE (TREE_TYPE (decl)) == TEMPLATE_TEMPLATE_PARM)
11000 : : && (TREE_CODE (DECL_TEMPLATE_RESULT (decl)) == TYPE_DECL)
11001 : : && (TYPE_NAME (TREE_TYPE (decl)) == decl));
11002 : : break;
11003 : :
11004 : 958675 : case TYPE_DECL:
11005 : 958675 : gcc_assert ((TREE_CODE (TREE_TYPE (decl)) == TEMPLATE_TYPE_PARM)
11006 : : && (TYPE_NAME (TREE_TYPE (decl)) == decl));
11007 : : break;
11008 : :
11009 : 63931 : case PARM_DECL:
11010 : 63931 : gcc_assert ((TREE_CODE (DECL_INITIAL (decl)) == TEMPLATE_PARM_INDEX)
11011 : : && (TREE_CODE (TEMPLATE_PARM_DECL (DECL_INITIAL (decl)))
11012 : : == CONST_DECL)
11013 : : && (DECL_TEMPLATE_PARM_P
11014 : : (TEMPLATE_PARM_DECL (DECL_INITIAL (decl)))));
11015 : : break;
11016 : : }
11017 : :
11018 : 1025627 : tree_node (decl);
11019 : 1025627 : tree_node (TEMPLATE_PARM_CONSTRAINTS (parm));
11020 : : }
11021 : :
11022 : 590281 : tpl_levels++;
11023 : : }
11024 : :
11025 : : tree
11026 : 292162 : trees_in::tpl_parms (unsigned &tpl_levels)
11027 : : {
11028 : 292162 : tree parms = NULL_TREE;
11029 : :
11030 : 614742 : while (int len = i ())
11031 : : {
11032 : 322580 : if (len < 0)
11033 : : {
11034 : 175469 : parms = back_ref (len);
11035 : 175469 : continue;
11036 : : }
11037 : :
11038 : 147111 : len -= 1;
11039 : 147111 : parms = tree_cons (NULL_TREE, NULL_TREE, parms);
11040 : 147111 : int tag = insert (parms);
11041 : 147111 : TREE_PURPOSE (parms) = tree_node ();
11042 : :
11043 : 147111 : dump (dumper::TREE)
11044 : 105 : && dump ("Reading template parms:%d level:%N length:%d",
11045 : 105 : tag, TREE_PURPOSE (parms), len);
11046 : :
11047 : 147111 : tree vec = make_tree_vec (len);
11048 : 388391 : for (int ix = 0; ix != len; ix++)
11049 : : {
11050 : 241280 : tree decl = tree_node ();
11051 : 241280 : if (!decl)
11052 : : return NULL_TREE;
11053 : :
11054 : 241280 : tree parm = build_tree_list (NULL, decl);
11055 : 241280 : TEMPLATE_PARM_CONSTRAINTS (parm) = tree_node ();
11056 : :
11057 : 241280 : TREE_VEC_ELT (vec, ix) = parm;
11058 : : }
11059 : :
11060 : 147111 : TREE_VALUE (parms) = vec;
11061 : 147111 : tpl_levels++;
11062 : : }
11063 : :
11064 : : return parms;
11065 : : }
11066 : :
11067 : : void
11068 : 919153 : trees_out::tpl_parms_fini (tree tmpl, unsigned tpl_levels)
11069 : : {
11070 : 919153 : for (tree parms = DECL_TEMPLATE_PARMS (tmpl);
11071 : 1509434 : tpl_levels--; parms = TREE_CHAIN (parms))
11072 : : {
11073 : 590281 : tree vec = TREE_VALUE (parms);
11074 : :
11075 : 590281 : tree_node (TREE_TYPE (vec));
11076 : 1615908 : for (unsigned ix = TREE_VEC_LENGTH (vec); ix--;)
11077 : : {
11078 : 1025627 : tree parm = TREE_VEC_ELT (vec, ix);
11079 : 1025627 : tree dflt = TREE_PURPOSE (parm);
11080 : 1025627 : tree_node (dflt);
11081 : :
11082 : : /* Template template parameters need a context of their owning
11083 : : template. This is quite tricky to infer correctly on stream-in
11084 : : (see PR c++/98881) so we'll just provide it directly. */
11085 : 1025627 : tree decl = TREE_VALUE (parm);
11086 : 1025627 : if (TREE_CODE (decl) == TEMPLATE_DECL)
11087 : 3021 : tree_node (DECL_CONTEXT (decl));
11088 : : }
11089 : : }
11090 : 919153 : }
11091 : :
11092 : : bool
11093 : 292162 : trees_in::tpl_parms_fini (tree tmpl, unsigned tpl_levels)
11094 : : {
11095 : 292162 : for (tree parms = DECL_TEMPLATE_PARMS (tmpl);
11096 : 439273 : tpl_levels--; parms = TREE_CHAIN (parms))
11097 : : {
11098 : 147111 : tree vec = TREE_VALUE (parms);
11099 : :
11100 : 147111 : TREE_TYPE (vec) = tree_node ();
11101 : 388391 : for (unsigned ix = TREE_VEC_LENGTH (vec); ix--;)
11102 : : {
11103 : 241280 : tree parm = TREE_VEC_ELT (vec, ix);
11104 : 241280 : tree dflt = tree_node ();
11105 : 241280 : TREE_PURPOSE (parm) = dflt;
11106 : :
11107 : 241280 : tree decl = TREE_VALUE (parm);
11108 : 241280 : if (TREE_CODE (decl) == TEMPLATE_DECL)
11109 : 750 : DECL_CONTEXT (decl) = tree_node ();
11110 : :
11111 : 241280 : if (get_overrun ())
11112 : : return false;
11113 : : }
11114 : : }
11115 : : return true;
11116 : : }
11117 : :
11118 : : /* PARMS is a LIST, one node per level.
11119 : : TREE_VALUE is a TREE_VEC of parm info for that level.
11120 : : each ELT is a TREE_LIST
11121 : : TREE_VALUE is PARM_DECL, TYPE_DECL or TEMPLATE_DECL
11122 : : TREE_PURPOSE is the default value. */
11123 : :
11124 : : void
11125 : 919153 : trees_out::tpl_header (tree tpl, unsigned *tpl_levels)
11126 : : {
11127 : 919153 : tree parms = DECL_TEMPLATE_PARMS (tpl);
11128 : 919153 : tpl_parms (parms, *tpl_levels);
11129 : :
11130 : : /* Mark end. */
11131 : 919153 : if (streaming_p ())
11132 : 305970 : u (0);
11133 : :
11134 : 919153 : if (*tpl_levels)
11135 : 555910 : tree_node (TEMPLATE_PARMS_CONSTRAINTS (parms));
11136 : 919153 : }
11137 : :
11138 : : bool
11139 : 292162 : trees_in::tpl_header (tree tpl, unsigned *tpl_levels)
11140 : : {
11141 : 292162 : tree parms = tpl_parms (*tpl_levels);
11142 : 292162 : if (!parms)
11143 : : return false;
11144 : :
11145 : 292162 : DECL_TEMPLATE_PARMS (tpl) = parms;
11146 : :
11147 : 292162 : if (*tpl_levels)
11148 : 145213 : TEMPLATE_PARMS_CONSTRAINTS (parms) = tree_node ();
11149 : :
11150 : : return true;
11151 : : }
11152 : :
11153 : : /* Stream skeleton parm nodes, with their flags, type & parm indices.
11154 : : All the parms will have consecutive tags. */
11155 : :
11156 : : void
11157 : 1249345 : trees_out::fn_parms_init (tree fn)
11158 : : {
11159 : : /* First init them. */
11160 : 1249345 : int base_tag = ref_num - 1;
11161 : 1249345 : int ix = 0;
11162 : 1249345 : for (tree parm = DECL_ARGUMENTS (fn);
11163 : 3748289 : parm; parm = DECL_CHAIN (parm), ix++)
11164 : : {
11165 : 2498944 : if (streaming_p ())
11166 : : {
11167 : 832950 : start (parm);
11168 : 832950 : tree_node_bools (parm);
11169 : : }
11170 : 2498944 : int tag = insert (parm);
11171 : 2498944 : gcc_checking_assert (base_tag - ix == tag);
11172 : : }
11173 : : /* Mark the end. */
11174 : 1249345 : if (streaming_p ())
11175 : 416623 : u (0);
11176 : :
11177 : : /* Now stream their contents. */
11178 : 1249345 : ix = 0;
11179 : 1249345 : for (tree parm = DECL_ARGUMENTS (fn);
11180 : 3748289 : parm; parm = DECL_CHAIN (parm), ix++)
11181 : : {
11182 : 2498944 : if (streaming_p ())
11183 : 832950 : dump (dumper::TREE)
11184 : 222 : && dump ("Writing parm:%d %u (%N) of %N",
11185 : : base_tag - ix, ix, parm, fn);
11186 : 2498944 : tree_node_vals (parm);
11187 : : }
11188 : :
11189 : 1249345 : if (!streaming_p ())
11190 : : {
11191 : : /* We must walk contract attrs so the dependency graph is complete. */
11192 : 832722 : for (tree contract = DECL_CONTRACTS (fn);
11193 : 832830 : contract;
11194 : 108 : contract = CONTRACT_CHAIN (contract))
11195 : 108 : tree_node (contract);
11196 : : }
11197 : :
11198 : : /* Write a reference to contracts pre/post functions, if any, to avoid
11199 : : regenerating them in importers. */
11200 : 1249345 : tree_node (DECL_PRE_FN (fn));
11201 : 1249345 : tree_node (DECL_POST_FN (fn));
11202 : 1249345 : }
11203 : :
11204 : : /* Build skeleton parm nodes, read their flags, type & parm indices. */
11205 : :
11206 : : int
11207 : 406715 : trees_in::fn_parms_init (tree fn)
11208 : : {
11209 : 406715 : int base_tag = ~(int)back_refs.length ();
11210 : :
11211 : 406715 : tree *parm_ptr = &DECL_ARGUMENTS (fn);
11212 : 406715 : int ix = 0;
11213 : 1220120 : for (; int code = u (); ix++)
11214 : : {
11215 : 813405 : tree parm = start (code);
11216 : 813405 : if (!tree_node_bools (parm))
11217 : : return 0;
11218 : :
11219 : 813405 : int tag = insert (parm);
11220 : 813405 : gcc_checking_assert (base_tag - ix == tag);
11221 : 813405 : *parm_ptr = parm;
11222 : 813405 : parm_ptr = &DECL_CHAIN (parm);
11223 : 813405 : }
11224 : :
11225 : 406715 : ix = 0;
11226 : 406715 : for (tree parm = DECL_ARGUMENTS (fn);
11227 : 1220120 : parm; parm = DECL_CHAIN (parm), ix++)
11228 : : {
11229 : 813405 : dump (dumper::TREE)
11230 : 362 : && dump ("Reading parm:%d %u (%N) of %N",
11231 : : base_tag - ix, ix, parm, fn);
11232 : 813405 : if (!tree_node_vals (parm))
11233 : : return 0;
11234 : : }
11235 : :
11236 : : /* Reload references to contract functions, if any. */
11237 : 406715 : tree pre_fn = tree_node ();
11238 : 406715 : tree post_fn = tree_node ();
11239 : 406715 : set_contract_functions (fn, pre_fn, post_fn);
11240 : :
11241 : 406715 : return base_tag;
11242 : : }
11243 : :
11244 : : /* Read the remaining parm node data. Replace with existing (if
11245 : : non-null) in the map. */
11246 : :
11247 : : void
11248 : 406715 : trees_in::fn_parms_fini (int tag, tree fn, tree existing, bool is_defn)
11249 : : {
11250 : 597300 : tree existing_parm = existing ? DECL_ARGUMENTS (existing) : NULL_TREE;
11251 : 406715 : tree parms = DECL_ARGUMENTS (fn);
11252 : 1220120 : for (tree parm = parms; parm; parm = DECL_CHAIN (parm))
11253 : : {
11254 : 813405 : if (existing_parm)
11255 : : {
11256 : 555922 : if (is_defn && !DECL_SAVED_TREE (existing))
11257 : : {
11258 : : /* If we're about to become the definition, set the
11259 : : names of the parms from us. */
11260 : 13944 : DECL_NAME (existing_parm) = DECL_NAME (parm);
11261 : 13944 : DECL_SOURCE_LOCATION (existing_parm) = DECL_SOURCE_LOCATION (parm);
11262 : :
11263 : : /* And some other flags important for codegen are only set
11264 : : by the definition. */
11265 : 13944 : TREE_ADDRESSABLE (existing_parm) = TREE_ADDRESSABLE (parm);
11266 : 13944 : DECL_BY_REFERENCE (existing_parm) = DECL_BY_REFERENCE (parm);
11267 : 13944 : DECL_NONLOCAL (existing_parm) = DECL_NONLOCAL (parm);
11268 : 13944 : DECL_ARG_TYPE (existing_parm) = DECL_ARG_TYPE (parm);
11269 : :
11270 : : /* Invisiref parms had their types adjusted by cp_genericize. */
11271 : 13944 : if (DECL_BY_REFERENCE (parm))
11272 : : {
11273 : 6 : TREE_TYPE (existing_parm) = TREE_TYPE (parm);
11274 : 6 : relayout_decl (existing_parm);
11275 : : }
11276 : : }
11277 : :
11278 : 376356 : back_refs[~tag] = existing_parm;
11279 : 376356 : existing_parm = DECL_CHAIN (existing_parm);
11280 : : }
11281 : 813405 : tag--;
11282 : : }
11283 : 406715 : }
11284 : :
11285 : : /* Encode into KEY the position of the local type (class or enum)
11286 : : declaration DECL within FN. The position is encoded as the
11287 : : index of the innermost BLOCK (numbered in BFS order) along with
11288 : : the index within its BLOCK_VARS list. */
11289 : :
11290 : : void
11291 : 12429 : trees_out::key_local_type (merge_key& key, tree decl, tree fn)
11292 : : {
11293 : 12429 : auto_vec<tree, 4> blocks;
11294 : 12429 : blocks.quick_push (DECL_INITIAL (fn));
11295 : 12429 : unsigned block_ix = 0;
11296 : 56049 : while (block_ix != blocks.length ())
11297 : : {
11298 : 21810 : tree block = blocks[block_ix];
11299 : 21810 : unsigned decl_ix = 0;
11300 : 61800 : for (tree var = BLOCK_VARS (block); var; var = DECL_CHAIN (var))
11301 : : {
11302 : 52419 : if (TREE_CODE (var) != TYPE_DECL)
11303 : 31188 : continue;
11304 : 21231 : if (var == decl)
11305 : : {
11306 : 12429 : key.index = (block_ix << 10) | decl_ix;
11307 : 12429 : return;
11308 : : }
11309 : 8802 : ++decl_ix;
11310 : : }
11311 : 19551 : for (tree sub = BLOCK_SUBBLOCKS (block); sub; sub = BLOCK_CHAIN (sub))
11312 : 10170 : blocks.safe_push (sub);
11313 : 9381 : ++block_ix;
11314 : : }
11315 : :
11316 : : /* Not-found value. */
11317 : 0 : key.index = 1023;
11318 : 12429 : }
11319 : :
11320 : : /* Look up the local type corresponding at the position encoded by
11321 : : KEY within FN and named NAME. */
11322 : :
11323 : : tree
11324 : 3992 : trees_in::key_local_type (const merge_key& key, tree fn, tree name)
11325 : : {
11326 : 3992 : if (!DECL_INITIAL (fn))
11327 : : return NULL_TREE;
11328 : :
11329 : 1967 : const unsigned block_pos = key.index >> 10;
11330 : 1967 : const unsigned decl_pos = key.index & 1023;
11331 : :
11332 : 1967 : if (decl_pos == 1023)
11333 : : return NULL_TREE;
11334 : :
11335 : 1967 : auto_vec<tree, 4> blocks;
11336 : 1967 : blocks.quick_push (DECL_INITIAL (fn));
11337 : 1967 : unsigned block_ix = 0;
11338 : 8849 : while (block_ix != blocks.length ())
11339 : : {
11340 : 3441 : tree block = blocks[block_ix];
11341 : 3441 : if (block_ix == block_pos)
11342 : : {
11343 : 1967 : unsigned decl_ix = 0;
11344 : 5077 : for (tree var = BLOCK_VARS (block); var; var = DECL_CHAIN (var))
11345 : : {
11346 : 5077 : if (TREE_CODE (var) != TYPE_DECL)
11347 : 2179 : continue;
11348 : : /* Prefer using the identifier as the key for more robustness
11349 : : to ODR violations, except for anonymous types since their
11350 : : compiler-generated identifiers aren't stable. */
11351 : 5796 : if (IDENTIFIER_ANON_P (name)
11352 : 2898 : ? decl_ix == decl_pos
11353 : 302 : : DECL_NAME (var) == name)
11354 : : return var;
11355 : 931 : ++decl_ix;
11356 : : }
11357 : : return NULL_TREE;
11358 : : }
11359 : 3057 : for (tree sub = BLOCK_SUBBLOCKS (block); sub; sub = BLOCK_CHAIN (sub))
11360 : 1583 : blocks.safe_push (sub);
11361 : 1474 : ++block_ix;
11362 : : }
11363 : :
11364 : : return NULL_TREE;
11365 : 1967 : }
11366 : :
11367 : : /* DEP is the depset of some decl we're streaming by value. Determine
11368 : : the merging behaviour. */
11369 : :
11370 : : merge_kind
11371 : 3176551 : trees_out::get_merge_kind (tree decl, depset *dep)
11372 : : {
11373 : 3176551 : if (!dep)
11374 : : {
11375 : 601829 : if (VAR_OR_FUNCTION_DECL_P (decl))
11376 : : {
11377 : : /* Any var or function with template info should have DEP. */
11378 : 318436 : gcc_checking_assert (!DECL_LANG_SPECIFIC (decl)
11379 : : || !DECL_TEMPLATE_INFO (decl));
11380 : 318436 : if (DECL_LOCAL_DECL_P (decl))
11381 : : return MK_unique;
11382 : : }
11383 : :
11384 : : /* Either unique, or some member of a class that cannot have an
11385 : : out-of-class definition. For instance a FIELD_DECL. */
11386 : 601577 : tree ctx = CP_DECL_CONTEXT (decl);
11387 : 601577 : if (TREE_CODE (ctx) == FUNCTION_DECL)
11388 : : {
11389 : : /* USING_DECLs and NAMESPACE_DECLs cannot have DECL_TEMPLATE_INFO --
11390 : : this isn't permitting them to have one. */
11391 : 361551 : gcc_checking_assert (TREE_CODE (decl) == USING_DECL
11392 : : || TREE_CODE (decl) == NAMESPACE_DECL
11393 : : || !DECL_LANG_SPECIFIC (decl)
11394 : : || !DECL_TEMPLATE_INFO (decl));
11395 : :
11396 : : return MK_unique;
11397 : : }
11398 : :
11399 : 240026 : if (TREE_CODE (decl) == TEMPLATE_DECL
11400 : 240026 : && DECL_UNINSTANTIATED_TEMPLATE_FRIEND_P (decl))
11401 : : return MK_local_friend;
11402 : :
11403 : 240026 : gcc_checking_assert (TYPE_P (ctx));
11404 : :
11405 : : /* Internal-only types will not need to dedup their members. */
11406 : 240026 : if (!DECL_CONTEXT (TYPE_NAME (ctx)))
11407 : : return MK_unique;
11408 : :
11409 : 239970 : if (TREE_CODE (decl) == USING_DECL)
11410 : : return MK_field;
11411 : :
11412 : 154117 : if (TREE_CODE (decl) == FIELD_DECL)
11413 : : {
11414 : 108125 : if (DECL_NAME (decl))
11415 : : {
11416 : : /* Anonymous FIELD_DECLs have a NULL name. */
11417 : 85214 : gcc_checking_assert (!IDENTIFIER_ANON_P (DECL_NAME (decl)));
11418 : : return MK_named;
11419 : : }
11420 : :
11421 : 22911 : if (walking_bit_field_unit)
11422 : : {
11423 : : /* The underlying storage unit for a bitfield. We do not
11424 : : need to dedup it, because it's only reachable through
11425 : : the bitfields it represents. And those are deduped. */
11426 : : // FIXME: Is that assertion correct -- do we ever fish it
11427 : : // out and put it in an expr?
11428 : 378 : gcc_checking_assert (!DECL_NAME (decl)
11429 : : && !RECORD_OR_UNION_TYPE_P (TREE_TYPE (decl))
11430 : : && !DECL_BIT_FIELD_REPRESENTATIVE (decl));
11431 : 378 : gcc_checking_assert ((TREE_CODE (TREE_TYPE (decl)) == ARRAY_TYPE
11432 : : ? TREE_CODE (TREE_TYPE (TREE_TYPE (decl)))
11433 : : : TREE_CODE (TREE_TYPE (decl)))
11434 : : == INTEGER_TYPE);
11435 : : return MK_unique;
11436 : : }
11437 : :
11438 : : return MK_field;
11439 : : }
11440 : :
11441 : 45992 : if (TREE_CODE (decl) == CONST_DECL)
11442 : : return MK_named;
11443 : :
11444 : 7190 : if (TREE_CODE (decl) == VAR_DECL
11445 : 7190 : && DECL_VTABLE_OR_VTT_P (decl))
11446 : : return MK_vtable;
11447 : :
11448 : 1176 : if (DECL_THUNK_P (decl))
11449 : : /* Thunks are unique-enough, because they're only referenced
11450 : : from the vtable. And that's either new (so we want the
11451 : : thunks), or it's a duplicate (so it will be dropped). */
11452 : : return MK_unique;
11453 : :
11454 : : /* There should be no other cases. */
11455 : 0 : gcc_unreachable ();
11456 : : }
11457 : :
11458 : 2574722 : gcc_checking_assert (TREE_CODE (decl) != FIELD_DECL
11459 : : && TREE_CODE (decl) != USING_DECL
11460 : : && TREE_CODE (decl) != CONST_DECL);
11461 : :
11462 : 2574722 : if (is_key_order ())
11463 : : {
11464 : : /* When doing the mergeablilty graph, there's an indirection to
11465 : : the actual depset. */
11466 : 858105 : gcc_assert (dep->is_special ());
11467 : 858105 : dep = dep->deps[0];
11468 : : }
11469 : :
11470 : 2574722 : gcc_checking_assert (decl == dep->get_entity ());
11471 : :
11472 : 2574722 : merge_kind mk = MK_named;
11473 : 2574722 : switch (dep->get_entity_kind ())
11474 : : {
11475 : 0 : default:
11476 : 0 : gcc_unreachable ();
11477 : :
11478 : : case depset::EK_PARTIAL:
11479 : : mk = MK_partial;
11480 : : break;
11481 : :
11482 : 1504094 : case depset::EK_DECL:
11483 : 1504094 : {
11484 : 1504094 : tree ctx = CP_DECL_CONTEXT (decl);
11485 : :
11486 : 1504094 : switch (TREE_CODE (ctx))
11487 : : {
11488 : 0 : default:
11489 : 0 : gcc_unreachable ();
11490 : :
11491 : 12429 : case FUNCTION_DECL:
11492 : 12429 : gcc_checking_assert
11493 : : (DECL_IMPLICIT_TYPEDEF_P (STRIP_TEMPLATE (decl)));
11494 : :
11495 : : mk = MK_local_type;
11496 : : break;
11497 : :
11498 : 1491665 : case RECORD_TYPE:
11499 : 1491665 : case UNION_TYPE:
11500 : 1491665 : case NAMESPACE_DECL:
11501 : 1491665 : if (DECL_NAME (decl) == as_base_identifier)
11502 : : {
11503 : : mk = MK_as_base;
11504 : : break;
11505 : : }
11506 : :
11507 : : /* A lambda may have a class as its context, even though it
11508 : : isn't a member in the traditional sense; see the test
11509 : : g++.dg/modules/lambda-6_a.C. */
11510 : 1880276 : if (DECL_IMPLICIT_TYPEDEF_P (STRIP_TEMPLATE (decl))
11511 : 1630103 : && LAMBDA_TYPE_P (TREE_TYPE (decl)))
11512 : : {
11513 : 598 : if (get_keyed_decl_scope (decl))
11514 : : mk = MK_keyed;
11515 : : else
11516 : : /* Lambdas not attached to any mangling scope are TU-local
11517 : : and so cannot be deduplicated. */
11518 : 363908 : mk = MK_unique;
11519 : : break;
11520 : : }
11521 : :
11522 : 1422052 : if (TREE_CODE (decl) == TEMPLATE_DECL
11523 : 1422052 : ? DECL_UNINSTANTIATED_TEMPLATE_FRIEND_P (decl)
11524 : 639012 : : decl_specialization_friend_p (decl))
11525 : : {
11526 : : mk = MK_local_friend;
11527 : : break;
11528 : : }
11529 : :
11530 : 1405072 : if (DECL_DECOMPOSITION_P (decl))
11531 : : {
11532 : : mk = MK_unique;
11533 : : break;
11534 : : }
11535 : :
11536 : 1404595 : if (IDENTIFIER_ANON_P (DECL_NAME (decl)))
11537 : : {
11538 : 23672 : if (RECORD_OR_UNION_TYPE_P (ctx))
11539 : : mk = MK_field;
11540 : 1114 : else if (DECL_IMPLICIT_TYPEDEF_P (decl)
11541 : 1114 : && UNSCOPED_ENUM_P (TREE_TYPE (decl))
11542 : 2228 : && TYPE_VALUES (TREE_TYPE (decl)))
11543 : : /* Keyed by first enum value, and underlying type. */
11544 : : mk = MK_enum;
11545 : : else
11546 : : /* No way to merge it, it is an ODR land-mine. */
11547 : : mk = MK_unique;
11548 : : }
11549 : : }
11550 : : }
11551 : : break;
11552 : :
11553 : 1025025 : case depset::EK_SPECIALIZATION:
11554 : 1025025 : {
11555 : 1025025 : gcc_checking_assert (dep->is_special ());
11556 : :
11557 : 1025025 : if (TREE_CODE (DECL_CONTEXT (decl)) == FUNCTION_DECL)
11558 : : /* An block-scope classes of templates are themselves
11559 : : templates. */
11560 : 4284 : gcc_checking_assert (DECL_IMPLICIT_TYPEDEF_P (decl));
11561 : :
11562 : 1025025 : if (dep->is_friend_spec ())
11563 : : mk = MK_friend_spec;
11564 : 1025025 : else if (dep->is_type_spec ())
11565 : : mk = MK_type_spec;
11566 : : else
11567 : 711096 : mk = MK_decl_spec;
11568 : :
11569 : 1025025 : if (TREE_CODE (decl) == TEMPLATE_DECL)
11570 : : {
11571 : 74142 : spec_entry *entry = reinterpret_cast <spec_entry *> (dep->deps[0]);
11572 : 74142 : if (TREE_CODE (entry->spec) != TEMPLATE_DECL)
11573 : 7902 : mk = merge_kind (mk | MK_tmpl_tmpl_mask);
11574 : : }
11575 : : }
11576 : : break;
11577 : : }
11578 : :
11579 : : return mk;
11580 : : }
11581 : :
11582 : :
11583 : : /* The container of DECL -- not necessarily its context! */
11584 : :
11585 : : tree
11586 : 3176551 : trees_out::decl_container (tree decl)
11587 : : {
11588 : 3176551 : int use_tpl;
11589 : 3176551 : tree tpl = NULL_TREE;
11590 : 3176551 : if (tree template_info = node_template_info (decl, use_tpl))
11591 : 1077391 : tpl = TI_TEMPLATE (template_info);
11592 : 3176551 : if (tpl == decl)
11593 : 0 : tpl = nullptr;
11594 : :
11595 : : /* Stream the template we're instantiated from. */
11596 : 3176551 : tree_node (tpl);
11597 : :
11598 : 3176551 : tree container = NULL_TREE;
11599 : 3176551 : if (TREE_CODE (decl) == TEMPLATE_DECL
11600 : 3176551 : ? DECL_UNINSTANTIATED_TEMPLATE_FRIEND_P (decl)
11601 : 2262786 : : decl_specialization_friend_p (decl))
11602 : 16980 : container = DECL_CHAIN (decl);
11603 : : else
11604 : 3159571 : container = CP_DECL_CONTEXT (decl);
11605 : :
11606 : 3176551 : if (TYPE_P (container))
11607 : 1867828 : container = TYPE_NAME (container);
11608 : :
11609 : 3176551 : tree_node (container);
11610 : :
11611 : 3176551 : return container;
11612 : : }
11613 : :
11614 : : tree
11615 : 1077604 : trees_in::decl_container ()
11616 : : {
11617 : : /* The maybe-template. */
11618 : 1077604 : (void)tree_node ();
11619 : :
11620 : 1077604 : tree container = tree_node ();
11621 : :
11622 : 1077604 : return container;
11623 : : }
11624 : :
11625 : : /* Write out key information about a mergeable DEP. Does not write
11626 : : the contents of DEP itself. The context has already been
11627 : : written. The container has already been streamed. */
11628 : :
11629 : : void
11630 : 3176551 : trees_out::key_mergeable (int tag, merge_kind mk, tree decl, tree inner,
11631 : : tree container, depset *dep)
11632 : : {
11633 : 3176551 : if (dep && is_key_order ())
11634 : : {
11635 : 858105 : gcc_checking_assert (dep->is_special ());
11636 : 858105 : dep = dep->deps[0];
11637 : : }
11638 : :
11639 : 3176551 : if (streaming_p ())
11640 : 1153025 : dump (dumper::MERGE)
11641 : 798 : && dump ("Writing:%d's %s merge key (%s) %C:%N", tag, merge_kind_name[mk],
11642 : 723 : dep ? dep->entity_kind_name () : "contained",
11643 : 798 : TREE_CODE (decl), decl);
11644 : :
11645 : : /* Now write the locating information. */
11646 : 3176551 : if (mk & MK_template_mask)
11647 : : {
11648 : : /* Specializations are located via their originating template,
11649 : : and the set of template args they specialize. */
11650 : 1025025 : gcc_checking_assert (dep && dep->is_special ());
11651 : 1025025 : spec_entry *entry = reinterpret_cast <spec_entry *> (dep->deps[0]);
11652 : :
11653 : 1025025 : tree_node (entry->tmpl);
11654 : 1025025 : tree_node (entry->args);
11655 : 1025025 : if (mk & MK_tmpl_decl_mask)
11656 : 711096 : if (flag_concepts && TREE_CODE (inner) == VAR_DECL)
11657 : : {
11658 : : /* Variable template partial specializations might need
11659 : : constraints (see spec_hasher::equal). It's simpler to
11660 : : write NULL when we don't need them. */
11661 : 15168 : tree constraints = NULL_TREE;
11662 : :
11663 : 15168 : if (uses_template_parms (entry->args))
11664 : 465 : constraints = get_constraints (inner);
11665 : 15168 : tree_node (constraints);
11666 : : }
11667 : :
11668 : 1025025 : if (CHECKING_P)
11669 : : {
11670 : : /* Make sure we can locate the decl. */
11671 : 1025025 : tree existing = match_mergeable_specialization
11672 : 1025025 : (bool (mk & MK_tmpl_decl_mask), entry);
11673 : :
11674 : 1025025 : gcc_assert (existing);
11675 : 1025025 : if (mk & MK_tmpl_decl_mask)
11676 : : {
11677 : 711096 : if (mk & MK_tmpl_tmpl_mask)
11678 : 6273 : existing = DECL_TI_TEMPLATE (existing);
11679 : : }
11680 : : else
11681 : : {
11682 : 313929 : if (mk & MK_tmpl_tmpl_mask)
11683 : 1629 : existing = CLASSTYPE_TI_TEMPLATE (existing);
11684 : : else
11685 : 312300 : existing = TYPE_NAME (existing);
11686 : : }
11687 : :
11688 : : /* The walkabout should have found ourselves. */
11689 : 1025025 : gcc_checking_assert (TREE_CODE (decl) == TYPE_DECL
11690 : : ? same_type_p (TREE_TYPE (decl),
11691 : : TREE_TYPE (existing))
11692 : : : existing == decl);
11693 : : }
11694 : : }
11695 : 2151526 : else if (mk != MK_unique)
11696 : : {
11697 : 1787618 : merge_key key;
11698 : 1787618 : tree name = DECL_NAME (decl);
11699 : :
11700 : 1787618 : switch (mk)
11701 : : {
11702 : 0 : default:
11703 : 0 : gcc_unreachable ();
11704 : :
11705 : 1504939 : case MK_named:
11706 : 1504939 : case MK_friend_spec:
11707 : 1504939 : if (IDENTIFIER_CONV_OP_P (name))
11708 : 5008 : name = conv_op_identifier;
11709 : :
11710 : 1504939 : if (TREE_CODE (inner) == FUNCTION_DECL)
11711 : : {
11712 : : /* Functions are distinguished by parameter types. */
11713 : 800455 : tree fn_type = TREE_TYPE (inner);
11714 : :
11715 : 800455 : key.ref_q = type_memfn_rqual (fn_type);
11716 : 800455 : key.args = TYPE_ARG_TYPES (fn_type);
11717 : :
11718 : 800455 : if (tree reqs = get_constraints (inner))
11719 : : {
11720 : 55692 : if (cxx_dialect < cxx20)
11721 : 48 : reqs = CI_ASSOCIATED_CONSTRAINTS (reqs);
11722 : : else
11723 : 111336 : reqs = CI_DECLARATOR_REQS (reqs);
11724 : 55692 : key.constraints = reqs;
11725 : : }
11726 : :
11727 : 800455 : if (IDENTIFIER_CONV_OP_P (name)
11728 : 800455 : || (decl != inner
11729 : 420825 : && !(name == fun_identifier
11730 : : /* In case the user names something _FUN */
11731 : 84 : && LAMBDA_TYPE_P (DECL_CONTEXT (inner)))))
11732 : : /* And a function template, or conversion operator needs
11733 : : the return type. Except for the _FUN thunk of a
11734 : : generic lambda, which has a recursive decl_type'd
11735 : : return type. */
11736 : : // FIXME: What if the return type is a voldemort?
11737 : 425791 : key.ret = fndecl_declared_return_type (inner);
11738 : : }
11739 : : break;
11740 : :
11741 : 130944 : case MK_field:
11742 : 130944 : {
11743 : 130944 : unsigned ix = 0;
11744 : 130944 : if (TREE_CODE (inner) != FIELD_DECL)
11745 : : name = NULL_TREE;
11746 : : else
11747 : 22533 : gcc_checking_assert (!name || !IDENTIFIER_ANON_P (name));
11748 : :
11749 : 130944 : for (tree field = TYPE_FIELDS (TREE_TYPE (container));
11750 : 3139352 : ; field = DECL_CHAIN (field))
11751 : : {
11752 : 3270296 : tree finner = STRIP_TEMPLATE (field);
11753 : 3270296 : if (TREE_CODE (finner) == TREE_CODE (inner))
11754 : : {
11755 : 1108852 : if (finner == inner)
11756 : : break;
11757 : 977908 : ix++;
11758 : : }
11759 : 3139352 : }
11760 : 130944 : key.index = ix;
11761 : : }
11762 : 130944 : break;
11763 : :
11764 : 6014 : case MK_vtable:
11765 : 6014 : {
11766 : 6014 : tree vtable = CLASSTYPE_VTABLES (TREE_TYPE (container));
11767 : 7670 : for (unsigned ix = 0; ; vtable = DECL_CHAIN (vtable), ix++)
11768 : 7670 : if (vtable == decl)
11769 : : {
11770 : 6014 : key.index = ix;
11771 : 6014 : break;
11772 : : }
11773 : 6014 : name = NULL_TREE;
11774 : : }
11775 : 6014 : break;
11776 : :
11777 : 69015 : case MK_as_base:
11778 : 69015 : gcc_checking_assert
11779 : : (decl == TYPE_NAME (CLASSTYPE_AS_BASE (TREE_TYPE (container))));
11780 : : break;
11781 : :
11782 : 16980 : case MK_local_friend:
11783 : 16980 : {
11784 : : /* Find by index on the class's DECL_LIST. We set TREE_CHAIN to
11785 : : point to the class in push_template_decl or grokfndecl. */
11786 : 16980 : unsigned ix = 0;
11787 : 16980 : for (tree decls = CLASSTYPE_DECL_LIST (TREE_CHAIN (decl));
11788 : 468438 : decls; decls = TREE_CHAIN (decls))
11789 : 468438 : if (!TREE_PURPOSE (decls))
11790 : : {
11791 : 66753 : tree frnd = friend_from_decl_list (TREE_VALUE (decls));
11792 : 66753 : if (frnd == decl)
11793 : : break;
11794 : 49773 : ix++;
11795 : : }
11796 : 16980 : key.index = ix;
11797 : 16980 : name = NULL_TREE;
11798 : : }
11799 : 16980 : break;
11800 : :
11801 : 12429 : case MK_local_type:
11802 : 12429 : key_local_type (key, STRIP_TEMPLATE (decl), container);
11803 : 12429 : break;
11804 : :
11805 : 1114 : case MK_enum:
11806 : 1114 : {
11807 : : /* Anonymous enums are located by their first identifier,
11808 : : and underlying type. */
11809 : 1114 : tree type = TREE_TYPE (decl);
11810 : :
11811 : 1114 : gcc_checking_assert (UNSCOPED_ENUM_P (type));
11812 : : /* Using the type name drops the bit precision we might
11813 : : have been using on the enum. */
11814 : 1114 : key.ret = TYPE_NAME (ENUM_UNDERLYING_TYPE (type));
11815 : 1114 : if (tree values = TYPE_VALUES (type))
11816 : 1114 : name = DECL_NAME (TREE_VALUE (values));
11817 : : }
11818 : : break;
11819 : :
11820 : 580 : case MK_keyed:
11821 : 580 : {
11822 : 580 : tree scope = get_keyed_decl_scope (inner);
11823 : 580 : gcc_checking_assert (scope);
11824 : :
11825 : 580 : auto *root = keyed_table->get (scope);
11826 : 580 : unsigned ix = root->length ();
11827 : : /* If we don't find it, we'll write a really big number
11828 : : that the reader will ignore. */
11829 : 614 : while (ix--)
11830 : 614 : if ((*root)[ix] == inner)
11831 : : break;
11832 : :
11833 : : /* Use the keyed-to decl as the 'name'. */
11834 : 580 : name = scope;
11835 : 580 : key.index = ix;
11836 : : }
11837 : 580 : break;
11838 : :
11839 : 45603 : case MK_partial:
11840 : 45603 : {
11841 : 45603 : tree ti = get_template_info (inner);
11842 : 45603 : key.constraints = get_constraints (inner);
11843 : 45603 : key.ret = TI_TEMPLATE (ti);
11844 : 45603 : key.args = TI_ARGS (ti);
11845 : : }
11846 : 45603 : break;
11847 : : }
11848 : :
11849 : 1787618 : tree_node (name);
11850 : 1787618 : if (streaming_p ())
11851 : : {
11852 : 635460 : unsigned code = (key.ref_q << 0) | (key.index << 2);
11853 : 635460 : u (code);
11854 : : }
11855 : :
11856 : 1787618 : if (mk == MK_enum)
11857 : 1114 : tree_node (key.ret);
11858 : 1786504 : else if (mk == MK_partial
11859 : 1740901 : || (mk == MK_named && inner
11860 : 1504939 : && TREE_CODE (inner) == FUNCTION_DECL))
11861 : : {
11862 : 846058 : tree_node (key.ret);
11863 : 846058 : tree arg = key.args;
11864 : 846058 : if (mk == MK_named)
11865 : 2300979 : while (arg && arg != void_list_node)
11866 : : {
11867 : 1500524 : tree_node (TREE_VALUE (arg));
11868 : 1500524 : arg = TREE_CHAIN (arg);
11869 : : }
11870 : 846058 : tree_node (arg);
11871 : 846058 : tree_node (key.constraints);
11872 : : }
11873 : : }
11874 : 3176551 : }
11875 : :
11876 : : /* DECL is a new declaration that may be duplicated in OVL. Use RET &
11877 : : ARGS to find its clone, or NULL. If DECL's DECL_NAME is NULL, this
11878 : : has been found by a proxy. It will be an enum type located by its
11879 : : first member.
11880 : :
11881 : : We're conservative with matches, so ambiguous decls will be
11882 : : registered as different, then lead to a lookup error if the two
11883 : : modules are both visible. Perhaps we want to do something similar
11884 : : to duplicate decls to get ODR errors on loading? We already have
11885 : : some special casing for namespaces. */
11886 : :
11887 : : static tree
11888 : 284480 : check_mergeable_decl (merge_kind mk, tree decl, tree ovl, merge_key const &key)
11889 : : {
11890 : 284480 : tree found = NULL_TREE;
11891 : 1296470 : for (ovl_iterator iter (ovl); !found && iter; ++iter)
11892 : : {
11893 : 624517 : tree match = *iter;
11894 : :
11895 : 624517 : tree d_inner = decl;
11896 : 624517 : tree m_inner = match;
11897 : :
11898 : 721926 : again:
11899 : 721926 : if (TREE_CODE (d_inner) != TREE_CODE (m_inner))
11900 : : {
11901 : 108428 : if (TREE_CODE (match) == NAMESPACE_DECL
11902 : 108428 : && !DECL_NAMESPACE_ALIAS (match))
11903 : : /* Namespaces are never overloaded. */
11904 : : found = match;
11905 : :
11906 : 108428 : continue;
11907 : : }
11908 : :
11909 : 613498 : switch (TREE_CODE (d_inner))
11910 : : {
11911 : 222635 : case TEMPLATE_DECL:
11912 : 222635 : if (template_heads_equivalent_p (d_inner, m_inner))
11913 : : {
11914 : 97409 : d_inner = DECL_TEMPLATE_RESULT (d_inner);
11915 : 97409 : m_inner = DECL_TEMPLATE_RESULT (m_inner);
11916 : 97409 : if (d_inner == error_mark_node
11917 : 97409 : && TYPE_DECL_ALIAS_P (m_inner))
11918 : : {
11919 : : found = match;
11920 : : break;
11921 : : }
11922 : 97409 : goto again;
11923 : : }
11924 : : break;
11925 : :
11926 : 292642 : case FUNCTION_DECL:
11927 : 292642 : if (tree m_type = TREE_TYPE (m_inner))
11928 : 292642 : if ((!key.ret
11929 : 146200 : || same_type_p (key.ret, fndecl_declared_return_type (m_inner)))
11930 : 269663 : && type_memfn_rqual (m_type) == key.ref_q
11931 : 269423 : && compparms (key.args, TYPE_ARG_TYPES (m_type))
11932 : : /* Reject if old is a "C" builtin and new is not "C".
11933 : : Matches decls_match behaviour. */
11934 : 126269 : && (!DECL_IS_UNDECLARED_BUILTIN (m_inner)
11935 : 6814 : || !DECL_EXTERN_C_P (m_inner)
11936 : 6650 : || DECL_EXTERN_C_P (d_inner))
11937 : : /* Reject if one is a different member of a
11938 : : guarded/pre/post fn set. */
11939 : 126254 : && (!flag_contracts
11940 : 352 : || (DECL_IS_PRE_FN_P (d_inner)
11941 : 176 : == DECL_IS_PRE_FN_P (m_inner)))
11942 : 418896 : && (!flag_contracts
11943 : 352 : || (DECL_IS_POST_FN_P (d_inner)
11944 : 176 : == DECL_IS_POST_FN_P (m_inner))))
11945 : : {
11946 : 126254 : tree m_reqs = get_constraints (m_inner);
11947 : 126254 : if (m_reqs)
11948 : : {
11949 : 9133 : if (cxx_dialect < cxx20)
11950 : 8 : m_reqs = CI_ASSOCIATED_CONSTRAINTS (m_reqs);
11951 : : else
11952 : 18258 : m_reqs = CI_DECLARATOR_REQS (m_reqs);
11953 : : }
11954 : :
11955 : 126254 : if (cp_tree_equal (key.constraints, m_reqs))
11956 : 164507 : found = match;
11957 : : }
11958 : : break;
11959 : :
11960 : 59911 : case TYPE_DECL:
11961 : 119822 : if (DECL_IMPLICIT_TYPEDEF_P (d_inner)
11962 : 59911 : == DECL_IMPLICIT_TYPEDEF_P (m_inner))
11963 : : {
11964 : 59885 : if (!IDENTIFIER_ANON_P (DECL_NAME (m_inner)))
11965 : 59757 : return match;
11966 : 128 : else if (mk == MK_enum
11967 : 128 : && (TYPE_NAME (ENUM_UNDERLYING_TYPE (TREE_TYPE (m_inner)))
11968 : 128 : == key.ret))
11969 : : found = match;
11970 : : }
11971 : : break;
11972 : :
11973 : : default:
11974 : : found = match;
11975 : : break;
11976 : : }
11977 : : }
11978 : :
11979 : 224723 : return found;
11980 : : }
11981 : :
11982 : : /* DECL, INNER & TYPE are a skeleton set of nodes for a decl. Only
11983 : : the bools have been filled in. Read its merging key and merge it.
11984 : : Returns the existing decl if there is one. */
11985 : :
11986 : : tree
11987 : 1077604 : trees_in::key_mergeable (int tag, merge_kind mk, tree decl, tree inner,
11988 : : tree type, tree container, bool is_attached,
11989 : : bool is_imported_temploid_friend)
11990 : : {
11991 : 1077604 : const char *kind = "new";
11992 : 1077604 : tree existing = NULL_TREE;
11993 : :
11994 : 1077604 : if (mk & MK_template_mask)
11995 : : {
11996 : : // FIXME: We could stream the specialization hash?
11997 : 330683 : spec_entry spec;
11998 : 330683 : spec.tmpl = tree_node ();
11999 : 330683 : spec.args = tree_node ();
12000 : :
12001 : 330683 : if (get_overrun ())
12002 : 0 : return error_mark_node;
12003 : :
12004 : 330683 : DECL_NAME (decl) = DECL_NAME (spec.tmpl);
12005 : 330683 : DECL_CONTEXT (decl) = DECL_CONTEXT (spec.tmpl);
12006 : 330683 : DECL_NAME (inner) = DECL_NAME (decl);
12007 : 330683 : DECL_CONTEXT (inner) = DECL_CONTEXT (decl);
12008 : :
12009 : 330683 : tree constr = NULL_TREE;
12010 : 330683 : bool is_decl = mk & MK_tmpl_decl_mask;
12011 : 330683 : if (is_decl)
12012 : : {
12013 : 234243 : if (flag_concepts && TREE_CODE (inner) == VAR_DECL)
12014 : : {
12015 : 4826 : constr = tree_node ();
12016 : 4826 : if (constr)
12017 : 0 : set_constraints (inner, constr);
12018 : : }
12019 : 466103 : spec.spec = (mk & MK_tmpl_tmpl_mask) ? inner : decl;
12020 : : }
12021 : : else
12022 : 96440 : spec.spec = type;
12023 : 330683 : existing = match_mergeable_specialization (is_decl, &spec);
12024 : 330683 : if (constr)
12025 : : /* We'll add these back later, if this is the new decl. */
12026 : 0 : remove_constraints (inner);
12027 : :
12028 : 330683 : if (!existing)
12029 : : ; /* We'll add to the table once read. */
12030 : 144188 : else if (mk & MK_tmpl_decl_mask)
12031 : : {
12032 : : /* A declaration specialization. */
12033 : 98892 : if (mk & MK_tmpl_tmpl_mask)
12034 : 1025 : existing = DECL_TI_TEMPLATE (existing);
12035 : : }
12036 : : else
12037 : : {
12038 : : /* A type specialization. */
12039 : 45296 : if (mk & MK_tmpl_tmpl_mask)
12040 : 234 : existing = CLASSTYPE_TI_TEMPLATE (existing);
12041 : : else
12042 : 45062 : existing = TYPE_NAME (existing);
12043 : : }
12044 : : }
12045 : 746921 : else if (mk == MK_unique)
12046 : : kind = "unique";
12047 : : else
12048 : : {
12049 : 563485 : tree name = tree_node ();
12050 : :
12051 : 563485 : merge_key key;
12052 : 563485 : unsigned code = u ();
12053 : 563485 : key.ref_q = cp_ref_qualifier ((code >> 0) & 3);
12054 : 563485 : key.index = code >> 2;
12055 : :
12056 : 563485 : if (mk == MK_enum)
12057 : 228 : key.ret = tree_node ();
12058 : 563257 : else if (mk == MK_partial
12059 : 552829 : || ((mk == MK_named || mk == MK_friend_spec)
12060 : 467463 : && TREE_CODE (inner) == FUNCTION_DECL))
12061 : : {
12062 : 265518 : key.ret = tree_node ();
12063 : 265518 : tree arg, *arg_ptr = &key.args;
12064 : 265518 : while ((arg = tree_node ())
12065 : 746078 : && arg != void_list_node
12066 : 1239938 : && mk != MK_partial)
12067 : : {
12068 : 481996 : *arg_ptr = tree_cons (NULL_TREE, arg, NULL_TREE);
12069 : 481996 : arg_ptr = &TREE_CHAIN (*arg_ptr);
12070 : : }
12071 : 265518 : *arg_ptr = arg;
12072 : 265518 : key.constraints = tree_node ();
12073 : : }
12074 : :
12075 : 563485 : if (get_overrun ())
12076 : 0 : return error_mark_node;
12077 : :
12078 : 563485 : if (mk < MK_indirect_lwm)
12079 : : {
12080 : 552615 : DECL_NAME (decl) = name;
12081 : 552615 : DECL_CONTEXT (decl) = FROB_CONTEXT (container);
12082 : : }
12083 : 563485 : DECL_NAME (inner) = DECL_NAME (decl);
12084 : 563485 : DECL_CONTEXT (inner) = DECL_CONTEXT (decl);
12085 : :
12086 : 563485 : if (mk == MK_partial)
12087 : : {
12088 : 10428 : for (tree spec = DECL_TEMPLATE_SPECIALIZATIONS (key.ret);
12089 : 53683 : spec; spec = TREE_CHAIN (spec))
12090 : : {
12091 : 49353 : tree tmpl = TREE_VALUE (spec);
12092 : 49353 : tree ti = get_template_info (tmpl);
12093 : 49353 : if (template_args_equal (key.args, TI_ARGS (ti))
12094 : 56225 : && cp_tree_equal (key.constraints,
12095 : : get_constraints
12096 : 6872 : (DECL_TEMPLATE_RESULT (tmpl))))
12097 : : {
12098 : : existing = tmpl;
12099 : : break;
12100 : : }
12101 : : }
12102 : : }
12103 : 553057 : else if (mk == MK_keyed
12104 : 276 : && DECL_LANG_SPECIFIC (name)
12105 : 553333 : && DECL_MODULE_KEYED_DECLS_P (name))
12106 : : {
12107 : 276 : gcc_checking_assert (TREE_CODE (container) == NAMESPACE_DECL
12108 : : || TREE_CODE (container) == TYPE_DECL);
12109 : 276 : if (auto *set = keyed_table->get (name))
12110 : 563616 : if (key.index < set->length ())
12111 : : {
12112 : 131 : existing = (*set)[key.index];
12113 : 131 : if (existing)
12114 : : {
12115 : 131 : gcc_checking_assert
12116 : : (DECL_IMPLICIT_TYPEDEF_P (existing));
12117 : 131 : if (inner != decl)
12118 : 88 : existing
12119 : 88 : = CLASSTYPE_TI_TEMPLATE (TREE_TYPE (existing));
12120 : : }
12121 : : }
12122 : : }
12123 : : else
12124 : 552781 : switch (TREE_CODE (container))
12125 : : {
12126 : 0 : default:
12127 : 0 : gcc_unreachable ();
12128 : :
12129 : 135972 : case NAMESPACE_DECL:
12130 : 135972 : if (is_attached
12131 : 135972 : && !is_imported_temploid_friend
12132 : 135972 : && !(state->is_module () || state->is_partition ()))
12133 : : kind = "unique";
12134 : : else
12135 : : {
12136 : 134026 : gcc_checking_assert (mk == MK_named || mk == MK_enum);
12137 : 134026 : tree mvec;
12138 : 134026 : tree *vslot = mergeable_namespace_slots (container, name,
12139 : : is_attached, &mvec);
12140 : 134026 : existing = check_mergeable_decl (mk, decl, *vslot, key);
12141 : 134026 : if (!existing)
12142 : 59737 : add_mergeable_namespace_entity (vslot, decl);
12143 : : else
12144 : : {
12145 : : /* Note that we now have duplicates to deal with in
12146 : : name lookup. */
12147 : 74289 : if (is_attached)
12148 : 36 : BINDING_VECTOR_PARTITION_DUPS_P (mvec) = true;
12149 : : else
12150 : 74253 : BINDING_VECTOR_GLOBAL_DUPS_P (mvec) = true;
12151 : : }
12152 : : }
12153 : : break;
12154 : :
12155 : 3992 : case FUNCTION_DECL:
12156 : 3992 : gcc_checking_assert (mk == MK_local_type);
12157 : 3992 : existing = key_local_type (key, container, name);
12158 : 3992 : if (existing && inner != decl)
12159 : 3616 : existing = TYPE_TI_TEMPLATE (TREE_TYPE (existing));
12160 : : break;
12161 : :
12162 : 412817 : case TYPE_DECL:
12163 : 412817 : gcc_checking_assert (!is_imported_temploid_friend);
12164 : 4530 : if (is_attached && !(state->is_module () || state->is_partition ())
12165 : : /* Implicit member functions can come from
12166 : : anywhere. */
12167 : 416418 : && !(DECL_ARTIFICIAL (decl)
12168 : 2101 : && TREE_CODE (decl) == FUNCTION_DECL
12169 : 664 : && !DECL_THUNK_P (decl)))
12170 : : kind = "unique";
12171 : : else
12172 : : {
12173 : 409880 : tree ctx = TREE_TYPE (container);
12174 : :
12175 : : /* For some reason templated enumeral types are not marked
12176 : : as COMPLETE_TYPE_P, even though they have members.
12177 : : This may well be a bug elsewhere. */
12178 : 409880 : if (TREE_CODE (ctx) == ENUMERAL_TYPE)
12179 : 14220 : existing = find_enum_member (ctx, name);
12180 : 395660 : else if (COMPLETE_TYPE_P (ctx))
12181 : : {
12182 : 189640 : switch (mk)
12183 : : {
12184 : 0 : default:
12185 : 0 : gcc_unreachable ();
12186 : :
12187 : 150861 : case MK_named:
12188 : 150861 : existing = lookup_class_binding (ctx, name);
12189 : 150861 : if (existing)
12190 : : {
12191 : 150454 : tree inner = decl;
12192 : 150454 : if (TREE_CODE (inner) == TEMPLATE_DECL
12193 : 150454 : && !DECL_MEMBER_TEMPLATE_P (inner))
12194 : 67568 : inner = DECL_TEMPLATE_RESULT (inner);
12195 : :
12196 : 150454 : existing = check_mergeable_decl
12197 : 150454 : (mk, inner, existing, key);
12198 : :
12199 : 150454 : if (!existing && DECL_ALIAS_TEMPLATE_P (decl))
12200 : : {} // FIXME: Insert into specialization
12201 : : // tables, we'll need the arguments for that!
12202 : : }
12203 : : break;
12204 : :
12205 : 26680 : case MK_field:
12206 : 26680 : {
12207 : 26680 : unsigned ix = key.index;
12208 : 26680 : for (tree field = TYPE_FIELDS (ctx);
12209 : 890298 : field; field = DECL_CHAIN (field))
12210 : : {
12211 : 890298 : tree finner = STRIP_TEMPLATE (field);
12212 : 890298 : if (TREE_CODE (finner) == TREE_CODE (inner))
12213 : 299111 : if (!ix--)
12214 : : {
12215 : : existing = field;
12216 : : break;
12217 : : }
12218 : : }
12219 : : }
12220 : : break;
12221 : :
12222 : 1404 : case MK_vtable:
12223 : 1404 : {
12224 : 1404 : unsigned ix = key.index;
12225 : 1404 : for (tree vtable = CLASSTYPE_VTABLES (ctx);
12226 : 1733 : vtable; vtable = DECL_CHAIN (vtable))
12227 : 1508 : if (!ix--)
12228 : : {
12229 : : existing = vtable;
12230 : : break;
12231 : : }
12232 : : }
12233 : : break;
12234 : :
12235 : 7734 : case MK_as_base:
12236 : 7734 : {
12237 : 7734 : tree as_base = CLASSTYPE_AS_BASE (ctx);
12238 : 7734 : if (as_base && as_base != ctx)
12239 : 7734 : existing = TYPE_NAME (as_base);
12240 : : }
12241 : : break;
12242 : :
12243 : 2961 : case MK_local_friend:
12244 : 2961 : {
12245 : 2961 : unsigned ix = key.index;
12246 : 2961 : for (tree decls = CLASSTYPE_DECL_LIST (ctx);
12247 : 82766 : decls; decls = TREE_CHAIN (decls))
12248 : 82766 : if (!TREE_PURPOSE (decls) && !ix--)
12249 : : {
12250 : 2961 : existing
12251 : 2961 : = friend_from_decl_list (TREE_VALUE (decls));
12252 : 2961 : break;
12253 : : }
12254 : : }
12255 : : break;
12256 : : }
12257 : :
12258 : 189640 : if (existing && mk < MK_indirect_lwm && mk != MK_partial
12259 : 185568 : && TREE_CODE (decl) == TEMPLATE_DECL
12260 : 275615 : && !DECL_MEMBER_TEMPLATE_P (decl))
12261 : : {
12262 : 69645 : tree ti;
12263 : 69645 : if (DECL_IMPLICIT_TYPEDEF_P (existing))
12264 : 890 : ti = TYPE_TEMPLATE_INFO (TREE_TYPE (existing));
12265 : : else
12266 : 68755 : ti = DECL_TEMPLATE_INFO (existing);
12267 : 69645 : existing = TI_TEMPLATE (ti);
12268 : : }
12269 : : }
12270 : : }
12271 : : }
12272 : : }
12273 : :
12274 : 1077604 : dump (dumper::MERGE)
12275 : 2401 : && dump ("Read:%d's %s merge key (%s) %C:%N", tag, merge_kind_name[mk],
12276 : 2401 : existing ? "matched" : kind, TREE_CODE (decl), decl);
12277 : :
12278 : : return existing;
12279 : : }
12280 : :
12281 : : void
12282 : 260320 : trees_out::binfo_mergeable (tree binfo)
12283 : : {
12284 : 260320 : tree dom = binfo;
12285 : 331735 : while (tree parent = BINFO_INHERITANCE_CHAIN (dom))
12286 : : dom = parent;
12287 : 260320 : tree type = BINFO_TYPE (dom);
12288 : 260320 : gcc_checking_assert (TYPE_BINFO (type) == dom);
12289 : 260320 : tree_node (type);
12290 : 260320 : if (streaming_p ())
12291 : : {
12292 : : unsigned ix = 0;
12293 : 142514 : for (; dom != binfo; dom = TREE_CHAIN (dom))
12294 : 37058 : ix++;
12295 : 105456 : u (ix);
12296 : : }
12297 : 260320 : }
12298 : :
12299 : : unsigned
12300 : 85880 : trees_in::binfo_mergeable (tree *type)
12301 : : {
12302 : 85880 : *type = tree_node ();
12303 : 85880 : return u ();
12304 : : }
12305 : :
12306 : : /* DECL is a just streamed declaration with attributes DATTR that should
12307 : : have matching ABI tags as EXISTING's attributes EATTR. Check that the
12308 : : ABI tags match, and report an error if not. */
12309 : :
12310 : : void
12311 : 286114 : trees_in::check_abi_tags (tree existing, tree decl, tree &eattr, tree &dattr)
12312 : : {
12313 : 286114 : tree etags = lookup_attribute ("abi_tag", eattr);
12314 : 286114 : tree dtags = lookup_attribute ("abi_tag", dattr);
12315 : 286114 : if ((etags == nullptr) != (dtags == nullptr)
12316 : 286114 : || (etags && !attribute_value_equal (etags, dtags)))
12317 : : {
12318 : 30 : if (etags)
12319 : 21 : etags = TREE_VALUE (etags);
12320 : 30 : if (dtags)
12321 : 24 : dtags = TREE_VALUE (dtags);
12322 : :
12323 : : /* We only error if mangling wouldn't consider the tags equivalent. */
12324 : 30 : if (!equal_abi_tags (etags, dtags))
12325 : : {
12326 : 21 : auto_diagnostic_group d;
12327 : 21 : if (dtags)
12328 : 15 : error_at (DECL_SOURCE_LOCATION (decl),
12329 : : "mismatching abi tags for %qD with tags %qE",
12330 : : decl, dtags);
12331 : : else
12332 : 6 : error_at (DECL_SOURCE_LOCATION (decl),
12333 : : "mismatching abi tags for %qD with no tags", decl);
12334 : 21 : if (etags)
12335 : 15 : inform (DECL_SOURCE_LOCATION (existing),
12336 : : "existing declaration here with tags %qE", etags);
12337 : : else
12338 : 6 : inform (DECL_SOURCE_LOCATION (existing),
12339 : : "existing declaration here with no tags");
12340 : 21 : }
12341 : :
12342 : : /* Always use the existing abi_tags as the canonical set so that
12343 : : later processing doesn't get confused. */
12344 : 30 : if (dtags)
12345 : 24 : dattr = remove_attribute ("abi_tag", dattr);
12346 : 30 : if (etags)
12347 : 21 : duplicate_one_attribute (&dattr, eattr, "abi_tag");
12348 : : }
12349 : 286114 : }
12350 : :
12351 : : /* DECL is a just streamed mergeable decl that should match EXISTING. Check
12352 : : it does and issue an appropriate diagnostic if not. Merge any
12353 : : bits from DECL to EXISTING. This is stricter matching than
12354 : : decls_match, because we can rely on ODR-sameness, and we cannot use
12355 : : decls_match because it can cause instantiations of constraints. */
12356 : :
12357 : : bool
12358 : 421937 : trees_in::is_matching_decl (tree existing, tree decl, bool is_typedef)
12359 : : {
12360 : : // FIXME: We should probably do some duplicate decl-like stuff here
12361 : : // (beware, default parms should be the same?) Can we just call
12362 : : // duplicate_decls and teach it how to handle the module-specific
12363 : : // permitted/required duplications?
12364 : :
12365 : : // We know at this point that the decls have matched by key, so we
12366 : : // can elide some of the checking
12367 : 421937 : gcc_checking_assert (TREE_CODE (existing) == TREE_CODE (decl));
12368 : :
12369 : 421937 : tree d_inner = decl;
12370 : 421937 : tree e_inner = existing;
12371 : 421937 : if (TREE_CODE (decl) == TEMPLATE_DECL)
12372 : : {
12373 : 142179 : d_inner = DECL_TEMPLATE_RESULT (d_inner);
12374 : 142179 : e_inner = DECL_TEMPLATE_RESULT (e_inner);
12375 : 142179 : gcc_checking_assert (TREE_CODE (e_inner) == TREE_CODE (d_inner));
12376 : : }
12377 : :
12378 : : // FIXME: do more precise errors at point of mismatch
12379 : 421937 : const char *mismatch_msg = nullptr;
12380 : :
12381 : 421937 : if (VAR_OR_FUNCTION_DECL_P (d_inner)
12382 : 421937 : && DECL_EXTERN_C_P (d_inner) != DECL_EXTERN_C_P (e_inner))
12383 : : {
12384 : 6 : mismatch_msg = G_("conflicting language linkage for imported "
12385 : : "declaration %#qD");
12386 : 6 : goto mismatch;
12387 : : }
12388 : 421931 : else if (TREE_CODE (d_inner) == FUNCTION_DECL)
12389 : : {
12390 : 190585 : tree e_ret = fndecl_declared_return_type (existing);
12391 : 190585 : tree d_ret = fndecl_declared_return_type (decl);
12392 : :
12393 : 82738 : if (decl != d_inner && DECL_NAME (d_inner) == fun_identifier
12394 : 190609 : && LAMBDA_TYPE_P (DECL_CONTEXT (d_inner)))
12395 : : /* This has a recursive type that will compare different. */;
12396 : 190573 : else if (!same_type_p (d_ret, e_ret))
12397 : : {
12398 : 7 : mismatch_msg = G_("conflicting type for imported declaration %#qD");
12399 : 7 : goto mismatch;
12400 : : }
12401 : :
12402 : 190578 : tree e_type = TREE_TYPE (e_inner);
12403 : 190578 : tree d_type = TREE_TYPE (d_inner);
12404 : :
12405 : 190578 : for (tree e_args = TYPE_ARG_TYPES (e_type),
12406 : 190578 : d_args = TYPE_ARG_TYPES (d_type);
12407 : 325273 : e_args != d_args && (e_args || d_args);
12408 : 134695 : e_args = TREE_CHAIN (e_args), d_args = TREE_CHAIN (d_args))
12409 : : {
12410 : 134695 : if (!(e_args && d_args))
12411 : : {
12412 : 0 : mismatch_msg = G_("conflicting argument list for imported "
12413 : : "declaration %#qD");
12414 : 0 : goto mismatch;
12415 : : }
12416 : :
12417 : 134695 : if (!same_type_p (TREE_VALUE (d_args), TREE_VALUE (e_args)))
12418 : : {
12419 : 0 : mismatch_msg = G_("conflicting argument types for imported "
12420 : : "declaration %#qD");
12421 : 0 : goto mismatch;
12422 : : }
12423 : : }
12424 : :
12425 : : /* If EXISTING has an undeduced or uninstantiated exception
12426 : : specification, but DECL does not, propagate the exception
12427 : : specification. Otherwise we end up asserting or trying to
12428 : : instantiate it in the middle of loading. */
12429 : 190578 : tree e_spec = TYPE_RAISES_EXCEPTIONS (e_type);
12430 : 190578 : tree d_spec = TYPE_RAISES_EXCEPTIONS (d_type);
12431 : 279690 : if (DECL_MAYBE_DELETED (e_inner) || DEFERRED_NOEXCEPT_SPEC_P (e_spec))
12432 : : {
12433 : 15211 : if (!DEFERRED_NOEXCEPT_SPEC_P (d_spec)
12434 : 45265 : || (UNEVALUATED_NOEXCEPT_SPEC_P (e_spec)
12435 : 12257 : && !UNEVALUATED_NOEXCEPT_SPEC_P (d_spec)))
12436 : : {
12437 : 259 : dump (dumper::MERGE)
12438 : 3 : && dump ("Propagating instantiated noexcept to %N", existing);
12439 : 259 : TREE_TYPE (existing) = d_type;
12440 : :
12441 : : /* Propagate to existing clones. */
12442 : 259 : tree clone;
12443 : 533 : FOR_EACH_CLONE (clone, existing)
12444 : : {
12445 : 274 : if (TREE_TYPE (clone) == e_type)
12446 : 274 : TREE_TYPE (clone) = d_type;
12447 : : else
12448 : 0 : TREE_TYPE (clone)
12449 : 0 : = build_exception_variant (TREE_TYPE (clone), d_spec);
12450 : : }
12451 : : }
12452 : : }
12453 : 175317 : else if (!DECL_MAYBE_DELETED (d_inner)
12454 : 250475 : && !DEFERRED_NOEXCEPT_SPEC_P (d_spec)
12455 : 350633 : && !comp_except_specs (d_spec, e_spec, ce_type))
12456 : : {
12457 : 1251 : mismatch_msg = G_("conflicting %<noexcept%> specifier for "
12458 : : "imported declaration %#qD");
12459 : 1251 : goto mismatch;
12460 : : }
12461 : :
12462 : : /* Similarly if EXISTING has an undeduced return type, but DECL's
12463 : : is already deduced. */
12464 : 189327 : if (undeduced_auto_decl (existing) && !undeduced_auto_decl (decl))
12465 : : {
12466 : 13 : dump (dumper::MERGE)
12467 : 0 : && dump ("Propagating deduced return type to %N", existing);
12468 : 13 : gcc_checking_assert (existing == e_inner);
12469 : 13 : FNDECL_USED_AUTO (existing) = true;
12470 : 13 : DECL_SAVED_AUTO_RETURN_TYPE (existing) = TREE_TYPE (e_type);
12471 : 13 : TREE_TYPE (existing) = change_return_type (TREE_TYPE (d_type), e_type);
12472 : : }
12473 : 189314 : else if (type_uses_auto (d_ret)
12474 : 189314 : && !same_type_p (TREE_TYPE (d_type), TREE_TYPE (e_type)))
12475 : : {
12476 : 3 : mismatch_msg = G_("conflicting deduced return type for "
12477 : : "imported declaration %#qD");
12478 : 3 : goto mismatch;
12479 : : }
12480 : :
12481 : : /* Similarly if EXISTING has undeduced constexpr, but DECL's
12482 : : is already deduced. */
12483 : 189324 : if (DECL_DECLARED_CONSTEXPR_P (e_inner)
12484 : 189324 : == DECL_DECLARED_CONSTEXPR_P (d_inner))
12485 : : /* Already matches. */;
12486 : 2 : else if (DECL_DECLARED_CONSTEXPR_P (d_inner)
12487 : 2 : && (DECL_MAYBE_DELETED (e_inner)
12488 : 0 : || decl_implicit_constexpr_p (d_inner)))
12489 : : /* DECL was deduced, copy to EXISTING. */
12490 : : {
12491 : 1 : DECL_DECLARED_CONSTEXPR_P (e_inner) = true;
12492 : 1 : if (decl_implicit_constexpr_p (d_inner))
12493 : 0 : DECL_LANG_SPECIFIC (e_inner)->u.fn.implicit_constexpr = true;
12494 : : }
12495 : 1 : else if (DECL_DECLARED_CONSTEXPR_P (e_inner)
12496 : 1 : && (DECL_MAYBE_DELETED (d_inner)
12497 : 0 : || decl_implicit_constexpr_p (e_inner)))
12498 : : /* EXISTING was deduced, leave it alone. */;
12499 : : else
12500 : : {
12501 : 0 : mismatch_msg = G_("conflicting %<constexpr%> for imported "
12502 : : "declaration %#qD");
12503 : 0 : goto mismatch;
12504 : : }
12505 : :
12506 : : /* Don't synthesize a defaulted function if we're importing one
12507 : : we've already determined. */
12508 : 189324 : if (!DECL_MAYBE_DELETED (d_inner))
12509 : 189214 : DECL_MAYBE_DELETED (e_inner) = false;
12510 : : }
12511 : 231346 : else if (is_typedef)
12512 : : {
12513 : 82553 : if (!DECL_ORIGINAL_TYPE (e_inner)
12514 : 82553 : || !same_type_p (DECL_ORIGINAL_TYPE (d_inner),
12515 : : DECL_ORIGINAL_TYPE (e_inner)))
12516 : : {
12517 : 3 : mismatch_msg = G_("conflicting imported declaration %q#D");
12518 : 3 : goto mismatch;
12519 : : }
12520 : : }
12521 : : /* Using cp_tree_equal because we can meet TYPE_ARGUMENT_PACKs
12522 : : here. I suspect the entities that directly do that are things
12523 : : that shouldn't go to duplicate_decls (FIELD_DECLs etc). */
12524 : 148793 : else if (!cp_tree_equal (TREE_TYPE (decl), TREE_TYPE (existing)))
12525 : : {
12526 : : mismatch_msg = G_("conflicting type for imported declaration %#qD");
12527 : 1945 : mismatch:
12528 : 1945 : if (DECL_IS_UNDECLARED_BUILTIN (existing))
12529 : : /* Just like duplicate_decls, presum the user knows what
12530 : : they're doing in overriding a builtin. */
12531 : 1255 : TREE_TYPE (existing) = TREE_TYPE (decl);
12532 : 690 : else if (decl_function_context (decl))
12533 : : /* The type of a mergeable local entity (such as a function scope
12534 : : capturing lambda's closure type fields) can depend on an
12535 : : unmergeable local entity (such as a local variable), so type
12536 : : equality isn't feasible in general for local entities. */;
12537 : : else
12538 : : {
12539 : 21 : gcc_checking_assert (mismatch_msg);
12540 : 21 : auto_diagnostic_group d;
12541 : 21 : error_at (DECL_SOURCE_LOCATION (decl), mismatch_msg, decl);
12542 : 21 : inform (DECL_SOURCE_LOCATION (existing),
12543 : : "existing declaration %#qD", existing);
12544 : 21 : return false;
12545 : 21 : }
12546 : : }
12547 : :
12548 : 421916 : if (DECL_IS_UNDECLARED_BUILTIN (existing)
12549 : 421916 : && !DECL_IS_UNDECLARED_BUILTIN (decl))
12550 : : {
12551 : : /* We're matching a builtin that the user has yet to declare.
12552 : : We are the one! This is very much duplicate-decl
12553 : : shenanigans. */
12554 : 1487 : DECL_SOURCE_LOCATION (existing) = DECL_SOURCE_LOCATION (decl);
12555 : 1487 : if (TREE_CODE (decl) != TYPE_DECL)
12556 : : {
12557 : : /* Propagate exceptions etc. */
12558 : 1473 : TREE_TYPE (existing) = TREE_TYPE (decl);
12559 : 1473 : TREE_NOTHROW (existing) = TREE_NOTHROW (decl);
12560 : : }
12561 : : /* This is actually an import! */
12562 : 1487 : DECL_MODULE_IMPORT_P (existing) = true;
12563 : :
12564 : : /* Yay, sliced! */
12565 : 1487 : existing->base = decl->base;
12566 : :
12567 : 1487 : if (TREE_CODE (decl) == FUNCTION_DECL)
12568 : : {
12569 : : /* Ew :( */
12570 : 1473 : memcpy (&existing->decl_common.size,
12571 : : &decl->decl_common.size,
12572 : : (offsetof (tree_decl_common, pt_uid)
12573 : : - offsetof (tree_decl_common, size)));
12574 : 1473 : auto bltin_class = DECL_BUILT_IN_CLASS (decl);
12575 : 1473 : existing->function_decl.built_in_class = bltin_class;
12576 : 1473 : auto fncode = DECL_UNCHECKED_FUNCTION_CODE (decl);
12577 : 1473 : DECL_UNCHECKED_FUNCTION_CODE (existing) = fncode;
12578 : 1473 : if (existing->function_decl.built_in_class == BUILT_IN_NORMAL)
12579 : : {
12580 : 1317 : if (builtin_decl_explicit_p (built_in_function (fncode)))
12581 : 1317 : switch (fncode)
12582 : : {
12583 : 0 : case BUILT_IN_STPCPY:
12584 : 0 : set_builtin_decl_implicit_p
12585 : 0 : (built_in_function (fncode), true);
12586 : 0 : break;
12587 : 1317 : default:
12588 : 1317 : set_builtin_decl_declared_p
12589 : 1317 : (built_in_function (fncode), true);
12590 : 1317 : break;
12591 : : }
12592 : 1317 : copy_attributes_to_builtin (decl);
12593 : : }
12594 : : }
12595 : : }
12596 : :
12597 : 421916 : if (VAR_OR_FUNCTION_DECL_P (decl)
12598 : 421916 : && DECL_TEMPLATE_INSTANTIATED (decl))
12599 : : /* Don't instantiate again! */
12600 : 8364 : DECL_TEMPLATE_INSTANTIATED (existing) = true;
12601 : :
12602 : 421916 : if (TREE_CODE (d_inner) == FUNCTION_DECL
12603 : 421916 : && DECL_DECLARED_INLINE_P (d_inner))
12604 : : {
12605 : 153788 : DECL_DECLARED_INLINE_P (e_inner) = true;
12606 : 153788 : if (!DECL_SAVED_TREE (e_inner)
12607 : 75237 : && lookup_attribute ("gnu_inline", DECL_ATTRIBUTES (d_inner))
12608 : 153798 : && !lookup_attribute ("gnu_inline", DECL_ATTRIBUTES (e_inner)))
12609 : : {
12610 : 30 : DECL_INTERFACE_KNOWN (e_inner)
12611 : 10 : |= DECL_INTERFACE_KNOWN (d_inner);
12612 : 10 : DECL_DISREGARD_INLINE_LIMITS (e_inner)
12613 : 10 : |= DECL_DISREGARD_INLINE_LIMITS (d_inner);
12614 : : // TODO: we will eventually want to merge all decl attributes
12615 : 10 : duplicate_one_attribute (&DECL_ATTRIBUTES (e_inner),
12616 : 10 : DECL_ATTRIBUTES (d_inner), "gnu_inline");
12617 : : }
12618 : : }
12619 : 421916 : if (!DECL_EXTERNAL (d_inner))
12620 : 200142 : DECL_EXTERNAL (e_inner) = false;
12621 : :
12622 : 421916 : if (VAR_OR_FUNCTION_DECL_P (d_inner))
12623 : 420288 : check_abi_tags (existing, decl,
12624 : 210144 : DECL_ATTRIBUTES (e_inner), DECL_ATTRIBUTES (d_inner));
12625 : :
12626 : 421916 : if (TREE_CODE (decl) == TEMPLATE_DECL)
12627 : : {
12628 : : /* Merge default template arguments. */
12629 : 142177 : tree d_parms = DECL_INNERMOST_TEMPLATE_PARMS (decl);
12630 : 142177 : tree e_parms = DECL_INNERMOST_TEMPLATE_PARMS (existing);
12631 : 142177 : gcc_checking_assert (TREE_VEC_LENGTH (d_parms)
12632 : : == TREE_VEC_LENGTH (e_parms));
12633 : 408239 : for (int i = 0; i < TREE_VEC_LENGTH (d_parms); ++i)
12634 : : {
12635 : 266071 : tree d_default = TREE_PURPOSE (TREE_VEC_ELT (d_parms, i));
12636 : 266071 : tree& e_default = TREE_PURPOSE (TREE_VEC_ELT (e_parms, i));
12637 : 266071 : if (e_default == NULL_TREE)
12638 : 225921 : e_default = d_default;
12639 : 40150 : else if (d_default != NULL_TREE
12640 : 40150 : && !cp_tree_equal (d_default, e_default))
12641 : : {
12642 : 9 : auto_diagnostic_group d;
12643 : 9 : tree d_parm = TREE_VALUE (TREE_VEC_ELT (d_parms, i));
12644 : 9 : tree e_parm = TREE_VALUE (TREE_VEC_ELT (e_parms, i));
12645 : 9 : error_at (DECL_SOURCE_LOCATION (d_parm),
12646 : : "conflicting default argument for %#qD", d_parm);
12647 : 9 : inform (DECL_SOURCE_LOCATION (e_parm),
12648 : : "existing default declared here");
12649 : 9 : return false;
12650 : 9 : }
12651 : : }
12652 : : }
12653 : :
12654 : 421907 : if (TREE_CODE (d_inner) == FUNCTION_DECL)
12655 : : {
12656 : : /* Merge default function arguments. */
12657 : 190576 : tree d_parm = FUNCTION_FIRST_USER_PARMTYPE (d_inner);
12658 : 190576 : tree e_parm = FUNCTION_FIRST_USER_PARMTYPE (e_inner);
12659 : 190576 : int i = 0;
12660 : 452779 : for (; d_parm && d_parm != void_list_node;
12661 : 262203 : d_parm = TREE_CHAIN (d_parm), e_parm = TREE_CHAIN (e_parm), ++i)
12662 : : {
12663 : 262215 : tree d_default = TREE_PURPOSE (d_parm);
12664 : 262215 : tree& e_default = TREE_PURPOSE (e_parm);
12665 : 262215 : if (e_default == NULL_TREE)
12666 : 247255 : e_default = d_default;
12667 : 14960 : else if (d_default != NULL_TREE
12668 : 14960 : && !cp_tree_equal (d_default, e_default))
12669 : : {
12670 : 12 : auto_diagnostic_group d;
12671 : 12 : error_at (get_fndecl_argument_location (d_inner, i),
12672 : : "conflicting default argument for parameter %P of %#qD",
12673 : : i, decl);
12674 : 12 : inform (get_fndecl_argument_location (e_inner, i),
12675 : : "existing default declared here");
12676 : 12 : return false;
12677 : 12 : }
12678 : : }
12679 : : }
12680 : :
12681 : : return true;
12682 : : }
12683 : :
12684 : : /* FN is an implicit member function that we've discovered is new to
12685 : : the class. Add it to the TYPE_FIELDS chain and the method vector.
12686 : : Reset the appropriate classtype lazy flag. */
12687 : :
12688 : : bool
12689 : 886 : trees_in::install_implicit_member (tree fn)
12690 : : {
12691 : 886 : tree ctx = DECL_CONTEXT (fn);
12692 : 886 : tree name = DECL_NAME (fn);
12693 : : /* We know these are synthesized, so the set of expected prototypes
12694 : : is quite restricted. We're not validating correctness, just
12695 : : distinguishing beteeen the small set of possibilities. */
12696 : 886 : tree parm_type = TREE_VALUE (FUNCTION_FIRST_USER_PARMTYPE (fn));
12697 : 886 : if (IDENTIFIER_CTOR_P (name))
12698 : : {
12699 : 602 : if (CLASSTYPE_LAZY_DEFAULT_CTOR (ctx)
12700 : 602 : && VOID_TYPE_P (parm_type))
12701 : 154 : CLASSTYPE_LAZY_DEFAULT_CTOR (ctx) = false;
12702 : 448 : else if (!TYPE_REF_P (parm_type))
12703 : : return false;
12704 : 448 : else if (CLASSTYPE_LAZY_COPY_CTOR (ctx)
12705 : 448 : && !TYPE_REF_IS_RVALUE (parm_type))
12706 : 227 : CLASSTYPE_LAZY_COPY_CTOR (ctx) = false;
12707 : 221 : else if (CLASSTYPE_LAZY_MOVE_CTOR (ctx))
12708 : 221 : CLASSTYPE_LAZY_MOVE_CTOR (ctx) = false;
12709 : : else
12710 : : return false;
12711 : : }
12712 : 284 : else if (IDENTIFIER_DTOR_P (name))
12713 : : {
12714 : 222 : if (CLASSTYPE_LAZY_DESTRUCTOR (ctx))
12715 : 222 : CLASSTYPE_LAZY_DESTRUCTOR (ctx) = false;
12716 : : else
12717 : : return false;
12718 : 222 : if (DECL_VIRTUAL_P (fn))
12719 : : /* A virtual dtor should have been created when the class
12720 : : became complete. */
12721 : : return false;
12722 : : }
12723 : 62 : else if (name == assign_op_identifier)
12724 : : {
12725 : 62 : if (!TYPE_REF_P (parm_type))
12726 : : return false;
12727 : 62 : else if (CLASSTYPE_LAZY_COPY_ASSIGN (ctx)
12728 : 62 : && !TYPE_REF_IS_RVALUE (parm_type))
12729 : 31 : CLASSTYPE_LAZY_COPY_ASSIGN (ctx) = false;
12730 : 31 : else if (CLASSTYPE_LAZY_MOVE_ASSIGN (ctx))
12731 : 31 : CLASSTYPE_LAZY_MOVE_ASSIGN (ctx) = false;
12732 : : else
12733 : : return false;
12734 : : }
12735 : : else
12736 : : return false;
12737 : :
12738 : 916 : dump (dumper::MERGE) && dump ("Adding implicit member %N", fn);
12739 : :
12740 : 886 : DECL_CHAIN (fn) = TYPE_FIELDS (ctx);
12741 : 886 : TYPE_FIELDS (ctx) = fn;
12742 : :
12743 : 886 : add_method (ctx, fn, false);
12744 : :
12745 : : /* Propagate TYPE_FIELDS. */
12746 : 886 : fixup_type_variants (ctx);
12747 : :
12748 : 886 : return true;
12749 : : }
12750 : :
12751 : : /* Return non-zero if DECL has a definition that would be interesting to
12752 : : write out. */
12753 : :
12754 : : static bool
12755 : 1458629 : has_definition (tree decl)
12756 : : {
12757 : 1458629 : bool is_tmpl = TREE_CODE (decl) == TEMPLATE_DECL;
12758 : 1458629 : if (is_tmpl)
12759 : 324006 : decl = DECL_TEMPLATE_RESULT (decl);
12760 : :
12761 : 1458629 : switch (TREE_CODE (decl))
12762 : : {
12763 : : default:
12764 : : break;
12765 : :
12766 : 514631 : case FUNCTION_DECL:
12767 : 514631 : if (!DECL_SAVED_TREE (decl))
12768 : : /* Not defined. */
12769 : : break;
12770 : :
12771 : 220470 : if (DECL_DECLARED_INLINE_P (decl))
12772 : : return true;
12773 : :
12774 : 13009 : if (header_module_p ())
12775 : : /* We always need to write definitions in header modules,
12776 : : since there's no TU to emit them in otherwise. */
12777 : : return true;
12778 : :
12779 : 4182 : if (DECL_TEMPLATE_INFO (decl))
12780 : : {
12781 : 3483 : int use_tpl = DECL_USE_TEMPLATE (decl);
12782 : :
12783 : : // FIXME: Partial specializations have definitions too.
12784 : 3483 : if (use_tpl < 2)
12785 : : return true;
12786 : : }
12787 : : break;
12788 : :
12789 : 626782 : case TYPE_DECL:
12790 : 626782 : {
12791 : 626782 : tree type = TREE_TYPE (decl);
12792 : 626782 : if (type == TYPE_MAIN_VARIANT (type)
12793 : 291358 : && decl == TYPE_NAME (type)
12794 : 918140 : && (TREE_CODE (type) == ENUMERAL_TYPE
12795 : 291358 : ? TYPE_VALUES (type) : TYPE_FIELDS (type)))
12796 : : return true;
12797 : : }
12798 : : break;
12799 : :
12800 : 83996 : case VAR_DECL:
12801 : : /* DECL_INITIALIZED_P might not be set on a dependent VAR_DECL. */
12802 : 83996 : if (DECL_LANG_SPECIFIC (decl)
12803 : 83301 : && DECL_TEMPLATE_INFO (decl)
12804 : 132612 : && DECL_INITIAL (decl))
12805 : : return true;
12806 : : else
12807 : : {
12808 : 39206 : if (!DECL_INITIALIZED_P (decl))
12809 : : /* Not defined. */
12810 : : return false;
12811 : :
12812 : 33500 : if (header_module_p ())
12813 : : /* We always need to write definitions in header modules,
12814 : : since there's no TU to emit them in otherwise. */
12815 : : return true;
12816 : :
12817 : 10303 : if (decl_maybe_constant_var_p (decl))
12818 : : /* We might need its constant value. */
12819 : : return true;
12820 : :
12821 : 373 : if (vague_linkage_p (decl))
12822 : : /* These are emitted as needed. */
12823 : : return true;
12824 : :
12825 : : return false;
12826 : : }
12827 : 6231 : break;
12828 : :
12829 : 6231 : case CONCEPT_DECL:
12830 : 6231 : if (DECL_INITIAL (decl))
12831 : : return true;
12832 : :
12833 : : break;
12834 : : }
12835 : :
12836 : : return false;
12837 : : }
12838 : :
12839 : : uintptr_t *
12840 : 577866 : trees_in::find_duplicate (tree existing)
12841 : : {
12842 : 273582 : if (!duplicates)
12843 : : return NULL;
12844 : :
12845 : 365347 : return duplicates->get (existing);
12846 : : }
12847 : :
12848 : : /* We're starting to read a duplicate DECL. EXISTING is the already
12849 : : known node. */
12850 : :
12851 : : void
12852 : 604950 : trees_in::register_duplicate (tree decl, tree existing)
12853 : : {
12854 : 604950 : if (!duplicates)
12855 : 97741 : duplicates = new duplicate_hash_map (40);
12856 : :
12857 : 604950 : bool existed;
12858 : 604950 : uintptr_t &slot = duplicates->get_or_insert (existing, &existed);
12859 : 604950 : gcc_checking_assert (!existed);
12860 : 604950 : slot = reinterpret_cast<uintptr_t> (decl);
12861 : :
12862 : 604950 : if (TREE_CODE (decl) == TEMPLATE_DECL)
12863 : : /* Also register the DECL_TEMPLATE_RESULT as a duplicate so
12864 : : that passing decl's _RESULT to maybe_duplicate naturally
12865 : : gives us existing's _RESULT back. */
12866 : 284358 : register_duplicate (DECL_TEMPLATE_RESULT (decl),
12867 : 142179 : DECL_TEMPLATE_RESULT (existing));
12868 : 604950 : }
12869 : :
12870 : : /* We've read a definition of MAYBE_EXISTING. If not a duplicate,
12871 : : return MAYBE_EXISTING (into which the definition should be
12872 : : installed). Otherwise return NULL if already known bad, or the
12873 : : duplicate we read (for ODR checking, or extracting additional merge
12874 : : information). */
12875 : :
12876 : : tree
12877 : 304284 : trees_in::odr_duplicate (tree maybe_existing, bool has_defn)
12878 : : {
12879 : 304284 : tree res = NULL_TREE;
12880 : :
12881 : 464319 : if (uintptr_t *dup = find_duplicate (maybe_existing))
12882 : : {
12883 : 142116 : if (!(*dup & 1))
12884 : 142113 : res = reinterpret_cast<tree> (*dup);
12885 : : }
12886 : : else
12887 : : res = maybe_existing;
12888 : :
12889 : 304284 : assert_definition (maybe_existing, res && !has_defn);
12890 : :
12891 : : // FIXME: We probably need to return the template, so that the
12892 : : // template header can be checked?
12893 : 304284 : return res ? STRIP_TEMPLATE (res) : NULL_TREE;
12894 : : }
12895 : :
12896 : : /* The following writer functions rely on the current behaviour of
12897 : : depset::hash::add_dependency making the decl and defn depset nodes
12898 : : depend on eachother. That way we don't have to worry about seeding
12899 : : the tree map with named decls that cannot be looked up by name (I.e
12900 : : template and function parms). We know the decl and definition will
12901 : : be in the same cluster, which is what we want. */
12902 : :
12903 : : void
12904 : 398973 : trees_out::write_function_def (tree decl)
12905 : : {
12906 : 398973 : tree_node (DECL_RESULT (decl));
12907 : :
12908 : 398973 : {
12909 : : /* The function body for a non-inline function or function template
12910 : : is ignored for determining exposures. This should only matter
12911 : : for templates (we don't emit the bodies of non-inline functions
12912 : : to begin with). */
12913 : 398973 : auto ovr = make_temp_override (dep_hash->ignore_tu_local,
12914 : 398973 : !DECL_DECLARED_INLINE_P (decl));
12915 : 398973 : tree_node (DECL_INITIAL (decl));
12916 : 398973 : tree_node (DECL_SAVED_TREE (decl));
12917 : 398973 : }
12918 : :
12919 : 849376 : tree_node (DECL_FRIEND_CONTEXT (decl));
12920 : :
12921 : 398973 : constexpr_fundef *cexpr = retrieve_constexpr_fundef (decl);
12922 : :
12923 : 398973 : if (streaming_p ())
12924 : 199448 : u (cexpr != nullptr);
12925 : 398973 : if (cexpr)
12926 : : {
12927 : 94538 : chained_decls (cexpr->parms);
12928 : 94538 : tree_node (cexpr->result);
12929 : 94538 : tree_node (cexpr->body);
12930 : : }
12931 : :
12932 : 398973 : function* f = DECL_STRUCT_FUNCTION (decl);
12933 : :
12934 : 398973 : if (streaming_p ())
12935 : : {
12936 : 199448 : unsigned flags = 0;
12937 : :
12938 : : /* Whether the importer should emit this definition, if used. */
12939 : 199448 : flags |= 1 * (DECL_NOT_REALLY_EXTERN (decl)
12940 : 199448 : && (get_importer_interface (decl)
12941 : : != importer_interface::external));
12942 : :
12943 : : /* Make sure DECL_REALLY_EXTERN and DECL_INTERFACE_KNOWN are consistent
12944 : : on non-templates or we'll crash later in import_export_decl. */
12945 : 130187 : gcc_checking_assert (flags || DECL_INTERFACE_KNOWN (decl)
12946 : : || (DECL_LANG_SPECIFIC (decl)
12947 : : && DECL_LOCAL_DECL_P (decl)
12948 : : && DECL_OMP_DECLARE_REDUCTION_P (decl))
12949 : : || (DECL_LANG_SPECIFIC (decl)
12950 : : && DECL_TEMPLATE_INFO (decl)
12951 : : && uses_template_parms (DECL_TI_ARGS (decl))));
12952 : :
12953 : 199448 : if (f)
12954 : : {
12955 : 198749 : flags |= 2;
12956 : : /* These flags are needed in tsubst_lambda_expr. */
12957 : 198749 : flags |= 4 * f->language->returns_value;
12958 : 198749 : flags |= 8 * f->language->returns_null;
12959 : 198749 : flags |= 16 * f->language->returns_abnormally;
12960 : 198749 : flags |= 32 * f->language->infinite_loop;
12961 : : }
12962 : :
12963 : 199448 : u (flags);
12964 : : }
12965 : :
12966 : 398973 : if (state && f)
12967 : : {
12968 : 397575 : state->write_location (*this, f->function_start_locus);
12969 : 397575 : state->write_location (*this, f->function_end_locus);
12970 : : }
12971 : 398973 : }
12972 : :
12973 : : void
12974 : 0 : trees_out::mark_function_def (tree)
12975 : : {
12976 : 0 : }
12977 : :
12978 : : bool
12979 : 197799 : trees_in::read_function_def (tree decl, tree maybe_template)
12980 : : {
12981 : 198244 : dump () && dump ("Reading function definition %N", decl);
12982 : 197799 : tree result = tree_node ();
12983 : 197799 : tree initial = tree_node ();
12984 : 197799 : tree saved = tree_node ();
12985 : 197799 : tree context = tree_node ();
12986 : 197799 : constexpr_fundef cexpr;
12987 : 197799 : post_process_data pdata {};
12988 : 197799 : pdata.decl = maybe_template;
12989 : :
12990 : 197799 : tree maybe_dup = odr_duplicate (maybe_template, DECL_SAVED_TREE (decl));
12991 : 395595 : bool installing = maybe_dup && !DECL_SAVED_TREE (decl);
12992 : :
12993 : 197799 : if (u ())
12994 : : {
12995 : 43134 : cexpr.parms = chained_decls ();
12996 : 43134 : cexpr.result = tree_node ();
12997 : 43134 : cexpr.body = tree_node ();
12998 : 43134 : cexpr.decl = decl;
12999 : : }
13000 : : else
13001 : 154665 : cexpr.decl = NULL_TREE;
13002 : :
13003 : 197799 : unsigned flags = u ();
13004 : :
13005 : 197799 : if (flags & 2)
13006 : : {
13007 : 197025 : pdata.start_locus = state->read_location (*this);
13008 : 197025 : pdata.end_locus = state->read_location (*this);
13009 : 197025 : pdata.returns_value = flags & 4;
13010 : 197025 : pdata.returns_null = flags & 8;
13011 : 197025 : pdata.returns_abnormally = flags & 16;
13012 : 197025 : pdata.infinite_loop = flags & 32;
13013 : : }
13014 : :
13015 : 197799 : if (get_overrun ())
13016 : : return NULL_TREE;
13017 : :
13018 : 197799 : if (installing)
13019 : : {
13020 : 114583 : DECL_NOT_REALLY_EXTERN (decl) = flags & 1;
13021 : 114583 : DECL_RESULT (decl) = result;
13022 : 114583 : DECL_INITIAL (decl) = initial;
13023 : 114583 : DECL_SAVED_TREE (decl) = saved;
13024 : :
13025 : 114583 : if (context)
13026 : 5358 : SET_DECL_FRIEND_CONTEXT (decl, context);
13027 : 114583 : if (cexpr.decl)
13028 : 30013 : register_constexpr_fundef (cexpr);
13029 : :
13030 : 114583 : if (DECL_LOCAL_DECL_P (decl))
13031 : : /* Block-scope OMP UDRs aren't real functions, and don't need a
13032 : : function structure to be allocated or to be expanded. */
13033 : 3 : gcc_checking_assert (DECL_OMP_DECLARE_REDUCTION_P (decl));
13034 : : else
13035 : 114580 : post_process (pdata);
13036 : : }
13037 : : else if (maybe_dup)
13038 : : {
13039 : : // FIXME:QOI Check matching defn
13040 : : }
13041 : :
13042 : : return true;
13043 : : }
13044 : :
13045 : : /* Also for CONCEPT_DECLs. */
13046 : :
13047 : : void
13048 : 113458 : trees_out::write_var_def (tree decl)
13049 : : {
13050 : : /* The initializer of a non-inline variable or variable template is
13051 : : ignored for determining exposures. */
13052 : 113458 : auto ovr = make_temp_override (dep_hash->ignore_tu_local,
13053 : 113458 : VAR_P (decl) && !DECL_INLINE_VAR_P (decl));
13054 : :
13055 : 113458 : tree init = DECL_INITIAL (decl);
13056 : 113458 : tree_node (init);
13057 : 113458 : if (!init)
13058 : : {
13059 : 1340 : tree dyn_init = NULL_TREE;
13060 : :
13061 : : /* We only need to write initializers in header modules. */
13062 : 2502 : if (header_module_p () && DECL_NONTRIVIALLY_INITIALIZED_P (decl))
13063 : : {
13064 : 450 : dyn_init = value_member (decl,
13065 : 450 : CP_DECL_THREAD_LOCAL_P (decl)
13066 : : ? tls_aggregates : static_aggregates);
13067 : 450 : gcc_checking_assert (dyn_init);
13068 : : /* Mark it so write_inits knows this is needed. */
13069 : 450 : TREE_LANG_FLAG_0 (dyn_init) = true;
13070 : 450 : dyn_init = TREE_PURPOSE (dyn_init);
13071 : : }
13072 : 1340 : tree_node (dyn_init);
13073 : : }
13074 : 113458 : }
13075 : :
13076 : : void
13077 : 0 : trees_out::mark_var_def (tree)
13078 : : {
13079 : 0 : }
13080 : :
13081 : : bool
13082 : 41386 : trees_in::read_var_def (tree decl, tree maybe_template)
13083 : : {
13084 : : /* Do not mark the virtual table entries as used. */
13085 : 41386 : bool vtable = VAR_P (decl) && DECL_VTABLE_OR_VTT_P (decl);
13086 : 41386 : unused += vtable;
13087 : 41386 : tree init = tree_node ();
13088 : 41386 : tree dyn_init = init ? NULL_TREE : tree_node ();
13089 : 41386 : unused -= vtable;
13090 : :
13091 : 41386 : if (get_overrun ())
13092 : : return false;
13093 : :
13094 : 41386 : bool initialized = (VAR_P (decl) ? bool (DECL_INITIALIZED_P (decl))
13095 : 41386 : : bool (DECL_INITIAL (decl)));
13096 : 41386 : tree maybe_dup = odr_duplicate (maybe_template, initialized);
13097 : 41386 : bool installing = maybe_dup && !initialized;
13098 : 41386 : if (installing)
13099 : : {
13100 : 25327 : DECL_INITIAL (decl) = init;
13101 : 25327 : if (DECL_EXTERNAL (decl))
13102 : 3147 : DECL_NOT_REALLY_EXTERN (decl) = true;
13103 : 25327 : if (VAR_P (decl))
13104 : : {
13105 : 22498 : DECL_INITIALIZED_P (decl) = true;
13106 : 22498 : if (maybe_dup && DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (maybe_dup))
13107 : 22103 : DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl) = true;
13108 : 22498 : tentative_decl_linkage (decl);
13109 : 22498 : if (DECL_EXPLICIT_INSTANTIATION (decl)
13110 : 22498 : && !DECL_EXTERNAL (decl))
13111 : 9 : setup_explicit_instantiation_definition_linkage (decl);
13112 : : /* Class non-template static members are handled in read_class_def.
13113 : : But still handle specialisations of member templates. */
13114 : 44996 : if ((!DECL_CLASS_SCOPE_P (decl)
13115 : 14769 : || primary_template_specialization_p (decl))
13116 : 30346 : && (DECL_IMPLICIT_INSTANTIATION (decl)
13117 : 7771 : || (DECL_EXPLICIT_INSTANTIATION (decl)
13118 : 18 : && !DECL_EXTERNAL (decl))))
13119 : 86 : note_vague_linkage_variable (decl);
13120 : : }
13121 : 25327 : if (!dyn_init)
13122 : : ;
13123 : 216 : else if (CP_DECL_THREAD_LOCAL_P (decl))
13124 : 96 : tls_aggregates = tree_cons (dyn_init, decl, tls_aggregates);
13125 : : else
13126 : 120 : static_aggregates = tree_cons (dyn_init, decl, static_aggregates);
13127 : : }
13128 : : else if (maybe_dup)
13129 : : {
13130 : : // FIXME:QOI Check matching defn
13131 : : }
13132 : :
13133 : : return true;
13134 : : }
13135 : :
13136 : : /* If MEMBER doesn't have an independent life outside the class,
13137 : : return it (or its TEMPLATE_DECL). Otherwise NULL. */
13138 : :
13139 : : static tree
13140 : 200892 : member_owned_by_class (tree member)
13141 : : {
13142 : 200892 : gcc_assert (DECL_P (member));
13143 : :
13144 : : /* Clones are owned by their origin. */
13145 : 200892 : if (DECL_CLONED_FUNCTION_P (member))
13146 : : return NULL;
13147 : :
13148 : 200892 : if (TREE_CODE (member) == FIELD_DECL)
13149 : : /* FIELD_DECLS can have template info in some cases. We always
13150 : : want the FIELD_DECL though, as there's never a TEMPLATE_DECL
13151 : : wrapping them. */
13152 : : return member;
13153 : :
13154 : 93089 : int use_tpl = -1;
13155 : 93089 : if (tree ti = node_template_info (member, use_tpl))
13156 : : {
13157 : : // FIXME: Don't bail on things that CANNOT have their own
13158 : : // template header. No, make sure they're in the same cluster.
13159 : 0 : if (use_tpl > 0)
13160 : : return NULL_TREE;
13161 : :
13162 : 0 : if (DECL_TEMPLATE_RESULT (TI_TEMPLATE (ti)) == member)
13163 : 200892 : member = TI_TEMPLATE (ti);
13164 : : }
13165 : : return member;
13166 : : }
13167 : :
13168 : : void
13169 : 152070 : trees_out::write_class_def (tree defn)
13170 : : {
13171 : 152070 : gcc_assert (DECL_P (defn));
13172 : 152070 : if (streaming_p ())
13173 : 76320 : dump () && dump ("Writing class definition %N", defn);
13174 : :
13175 : 152070 : tree type = TREE_TYPE (defn);
13176 : 152070 : tree_node (TYPE_SIZE (type));
13177 : 152070 : tree_node (TYPE_SIZE_UNIT (type));
13178 : 152070 : tree_node (TYPE_VFIELD (type));
13179 : 152070 : tree_node (TYPE_BINFO (type));
13180 : :
13181 : 152070 : vec_chained_decls (TYPE_FIELDS (type));
13182 : :
13183 : : /* Every class but __as_base has a type-specific. */
13184 : 301680 : gcc_checking_assert (!TYPE_LANG_SPECIFIC (type) == IS_FAKE_BASE_TYPE (type));
13185 : :
13186 : 152070 : if (TYPE_LANG_SPECIFIC (type))
13187 : : {
13188 : 149610 : {
13189 : 149610 : vec<tree, va_gc> *v = CLASSTYPE_MEMBER_VEC (type);
13190 : 149610 : if (!v)
13191 : : {
13192 : 39232 : gcc_checking_assert (!streaming_p ());
13193 : : /* Force a class vector. */
13194 : 39232 : v = set_class_bindings (type, -1);
13195 : 39232 : gcc_checking_assert (v);
13196 : : }
13197 : :
13198 : 149610 : unsigned len = v->length ();
13199 : 149610 : if (streaming_p ())
13200 : 74785 : u (len);
13201 : 1190258 : for (unsigned ix = 0; ix != len; ix++)
13202 : : {
13203 : 1040648 : tree m = (*v)[ix];
13204 : 1040648 : if (TREE_CODE (m) == TYPE_DECL
13205 : 314457 : && DECL_ARTIFICIAL (m)
13206 : 1202047 : && TYPE_STUB_DECL (TREE_TYPE (m)) == m)
13207 : : /* This is a using-decl for a type, or an anonymous
13208 : : struct (maybe with a typedef name). Write the type. */
13209 : 10966 : m = TREE_TYPE (m);
13210 : 1040648 : tree_node (m);
13211 : : }
13212 : : }
13213 : 149610 : tree_node (CLASSTYPE_LAMBDA_EXPR (type));
13214 : :
13215 : : /* TYPE_CONTAINS_VPTR_P looks at the vbase vector, which the
13216 : : reader won't know at this point. */
13217 : 149610 : int has_vptr = TYPE_CONTAINS_VPTR_P (type);
13218 : :
13219 : 149610 : if (streaming_p ())
13220 : : {
13221 : 74785 : unsigned nvbases = vec_safe_length (CLASSTYPE_VBASECLASSES (type));
13222 : 74785 : u (nvbases);
13223 : 74785 : i (has_vptr);
13224 : : }
13225 : :
13226 : 149610 : if (has_vptr)
13227 : : {
13228 : 5152 : tree_vec (CLASSTYPE_PURE_VIRTUALS (type));
13229 : 5152 : tree_pair_vec (CLASSTYPE_VCALL_INDICES (type));
13230 : 5152 : tree_node (CLASSTYPE_KEY_METHOD (type));
13231 : : }
13232 : : }
13233 : :
13234 : 152070 : if (TYPE_LANG_SPECIFIC (type))
13235 : : {
13236 : 149610 : tree_node (CLASSTYPE_PRIMARY_BINFO (type));
13237 : :
13238 : 149610 : tree as_base = CLASSTYPE_AS_BASE (type);
13239 : 149610 : if (as_base)
13240 : 76691 : as_base = TYPE_NAME (as_base);
13241 : 149610 : tree_node (as_base);
13242 : :
13243 : : /* Write the vtables. */
13244 : 149610 : tree vtables = CLASSTYPE_VTABLES (type);
13245 : 149610 : vec_chained_decls (vtables);
13246 : 305234 : for (; vtables; vtables = TREE_CHAIN (vtables))
13247 : 6014 : write_definition (vtables);
13248 : :
13249 : 149610 : {
13250 : : /* Friend declarations in class definitions are ignored when
13251 : : determining exposures. */
13252 : 149610 : auto ovr = make_temp_override (dep_hash->ignore_tu_local, true);
13253 : :
13254 : : /* Write the friend classes. */
13255 : 149610 : tree_list (CLASSTYPE_FRIEND_CLASSES (type), false);
13256 : :
13257 : : /* Write the friend functions. */
13258 : 149610 : for (tree friends = DECL_FRIENDLIST (defn);
13259 : 170084 : friends; friends = TREE_CHAIN (friends))
13260 : : {
13261 : 20474 : tree_node (FRIEND_NAME (friends));
13262 : 20474 : tree_list (FRIEND_DECLS (friends), false);
13263 : : }
13264 : : /* End of friend fns. */
13265 : 149610 : tree_node (NULL_TREE);
13266 : 149610 : }
13267 : :
13268 : : /* Write the decl list. We don't need to ignore exposures of friend
13269 : : decls here as any such decls should already have been added and
13270 : : ignored above. */
13271 : 149610 : tree_list (CLASSTYPE_DECL_LIST (type), true);
13272 : :
13273 : 149610 : if (TYPE_CONTAINS_VPTR_P (type))
13274 : : {
13275 : : /* Write the thunks. */
13276 : 5152 : for (tree decls = TYPE_FIELDS (type);
13277 : 145480 : decls; decls = DECL_CHAIN (decls))
13278 : 140328 : if (TREE_CODE (decls) == FUNCTION_DECL
13279 : 101618 : && DECL_VIRTUAL_P (decls)
13280 : 166888 : && DECL_THUNKS (decls))
13281 : : {
13282 : 888 : tree_node (decls);
13283 : : /* Thunks are always unique, so chaining is ok. */
13284 : 888 : chained_decls (DECL_THUNKS (decls));
13285 : : }
13286 : 5152 : tree_node (NULL_TREE);
13287 : : }
13288 : : }
13289 : 152070 : }
13290 : :
13291 : : void
13292 : 200892 : trees_out::mark_class_member (tree member, bool do_defn)
13293 : : {
13294 : 200892 : gcc_assert (DECL_P (member));
13295 : :
13296 : 200892 : member = member_owned_by_class (member);
13297 : 200892 : if (member)
13298 : 401784 : mark_declaration (member, do_defn && has_definition (member));
13299 : 200892 : }
13300 : :
13301 : : void
13302 : 152098 : trees_out::mark_class_def (tree defn)
13303 : : {
13304 : 152098 : gcc_assert (DECL_P (defn));
13305 : 152098 : tree type = TREE_TYPE (defn);
13306 : : /* Mark the class members that are not type-decls and cannot have
13307 : : independent definitions. */
13308 : 1609804 : for (tree member = TYPE_FIELDS (type); member; member = DECL_CHAIN (member))
13309 : 1457706 : if (TREE_CODE (member) == FIELD_DECL
13310 : 1457706 : || TREE_CODE (member) == USING_DECL
13311 : : /* A cloned enum-decl from 'using enum unrelated;' */
13312 : 1457706 : || (TREE_CODE (member) == CONST_DECL
13313 : 14390 : && DECL_CONTEXT (member) == type))
13314 : : {
13315 : 200892 : mark_class_member (member);
13316 : 200892 : if (TREE_CODE (member) == FIELD_DECL)
13317 : 107803 : if (tree repr = DECL_BIT_FIELD_REPRESENTATIVE (member))
13318 : : /* If we're marking a class template definition, then
13319 : : this'll contain the width (as set by grokbitfield)
13320 : : instead of a decl. */
13321 : 2216 : if (DECL_P (repr))
13322 : 1774 : mark_declaration (repr, false);
13323 : : }
13324 : :
13325 : : /* Mark the binfo hierarchy. */
13326 : 360310 : for (tree child = TYPE_BINFO (type); child; child = TREE_CHAIN (child))
13327 : 208212 : mark_by_value (child);
13328 : :
13329 : 152098 : if (TYPE_LANG_SPECIFIC (type))
13330 : : {
13331 : 149614 : for (tree vtable = CLASSTYPE_VTABLES (type);
13332 : 155628 : vtable; vtable = TREE_CHAIN (vtable))
13333 : 6014 : mark_declaration (vtable, true);
13334 : :
13335 : 149614 : if (TYPE_CONTAINS_VPTR_P (type))
13336 : : /* Mark the thunks, they belong to the class definition,
13337 : : /not/ the thunked-to function. */
13338 : 5152 : for (tree decls = TYPE_FIELDS (type);
13339 : 145480 : decls; decls = DECL_CHAIN (decls))
13340 : 140328 : if (TREE_CODE (decls) == FUNCTION_DECL)
13341 : 101618 : for (tree thunks = DECL_THUNKS (decls);
13342 : 102794 : thunks; thunks = DECL_CHAIN (thunks))
13343 : 1176 : mark_declaration (thunks, false);
13344 : : }
13345 : 152098 : }
13346 : :
13347 : : /* Nop sorting, needed for resorting the member vec. */
13348 : :
13349 : : static void
13350 : 9367490 : nop (void *, void *, void *)
13351 : : {
13352 : 9367490 : }
13353 : :
13354 : : bool
13355 : 62300 : trees_in::read_class_def (tree defn, tree maybe_template)
13356 : : {
13357 : 62300 : gcc_assert (DECL_P (defn));
13358 : 62837 : dump () && dump ("Reading class definition %N", defn);
13359 : 62300 : tree type = TREE_TYPE (defn);
13360 : 62300 : tree size = tree_node ();
13361 : 62300 : tree size_unit = tree_node ();
13362 : 62300 : tree vfield = tree_node ();
13363 : 62300 : tree binfo = tree_node ();
13364 : 62300 : vec<tree, va_gc> *vbase_vec = NULL;
13365 : 62300 : vec<tree, va_gc> *member_vec = NULL;
13366 : 62300 : vec<tree, va_gc> *pure_virts = NULL;
13367 : 62300 : vec<tree_pair_s, va_gc> *vcall_indices = NULL;
13368 : 62300 : tree key_method = NULL_TREE;
13369 : 62300 : tree lambda = NULL_TREE;
13370 : :
13371 : : /* Read the fields. */
13372 : 62300 : vec<tree, va_heap> *fields = vec_chained_decls ();
13373 : :
13374 : 62300 : if (TYPE_LANG_SPECIFIC (type))
13375 : : {
13376 : 61016 : if (unsigned len = u ())
13377 : : {
13378 : 61016 : vec_alloc (member_vec, len);
13379 : 537597 : for (unsigned ix = 0; ix != len; ix++)
13380 : : {
13381 : 476581 : tree m = tree_node ();
13382 : 476581 : if (get_overrun ())
13383 : : break;
13384 : 476581 : if (TYPE_P (m))
13385 : 4872 : m = TYPE_STUB_DECL (m);
13386 : 476581 : member_vec->quick_push (m);
13387 : : }
13388 : : }
13389 : 61016 : lambda = tree_node ();
13390 : :
13391 : 61016 : if (!get_overrun ())
13392 : : {
13393 : 61016 : unsigned nvbases = u ();
13394 : 61016 : if (nvbases)
13395 : : {
13396 : 266 : vec_alloc (vbase_vec, nvbases);
13397 : 1211 : for (tree child = binfo; child; child = TREE_CHAIN (child))
13398 : 945 : if (BINFO_VIRTUAL_P (child))
13399 : 266 : vbase_vec->quick_push (child);
13400 : : }
13401 : : }
13402 : :
13403 : 61016 : if (!get_overrun ())
13404 : : {
13405 : 61016 : int has_vptr = i ();
13406 : 61016 : if (has_vptr)
13407 : : {
13408 : 2431 : pure_virts = tree_vec ();
13409 : 2431 : vcall_indices = tree_pair_vec ();
13410 : 2431 : key_method = tree_node ();
13411 : : }
13412 : : }
13413 : : }
13414 : :
13415 : 62300 : tree maybe_dup = odr_duplicate (maybe_template, TYPE_SIZE (type));
13416 : 62300 : bool installing = maybe_dup && !TYPE_SIZE (type);
13417 : 32851 : if (installing)
13418 : : {
13419 : 32851 : if (maybe_dup != defn)
13420 : : {
13421 : : // FIXME: This is needed on other defns too, almost
13422 : : // duplicate-decl like? See is_matching_decl too.
13423 : : /* Copy flags from the duplicate. */
13424 : 235 : tree type_dup = TREE_TYPE (maybe_dup);
13425 : :
13426 : : /* Core pieces. */
13427 : 235 : TYPE_MODE_RAW (type) = TYPE_MODE_RAW (type_dup);
13428 : 235 : TYPE_ALIGN_RAW (type) = TYPE_ALIGN_RAW (type_dup);
13429 : 470 : TYPE_WARN_IF_NOT_ALIGN_RAW (type)
13430 : 235 : = TYPE_WARN_IF_NOT_ALIGN_RAW (type_dup);
13431 : 235 : TYPE_USER_ALIGN (type) = TYPE_USER_ALIGN (type_dup);
13432 : :
13433 : 235 : SET_DECL_MODE (defn, DECL_MODE (maybe_dup));
13434 : 235 : DECL_SIZE (defn) = DECL_SIZE (maybe_dup);
13435 : 235 : DECL_SIZE_UNIT (defn) = DECL_SIZE_UNIT (maybe_dup);
13436 : 235 : DECL_ALIGN_RAW (defn) = DECL_ALIGN_RAW (maybe_dup);
13437 : 470 : DECL_WARN_IF_NOT_ALIGN_RAW (defn)
13438 : 235 : = DECL_WARN_IF_NOT_ALIGN_RAW (maybe_dup);
13439 : 235 : DECL_USER_ALIGN (defn) = DECL_USER_ALIGN (maybe_dup);
13440 : :
13441 : 235 : TYPE_TYPELESS_STORAGE (type) = TYPE_TYPELESS_STORAGE (type_dup);
13442 : 235 : TYPE_CXX_ODR_P (type) = TYPE_CXX_ODR_P (type_dup);
13443 : 235 : TYPE_NO_FORCE_BLK (type) = TYPE_NO_FORCE_BLK (type_dup);
13444 : 235 : TYPE_TRANSPARENT_AGGR (type) = TYPE_TRANSPARENT_AGGR (type_dup);
13445 : 470 : TYPE_CONTAINS_PLACEHOLDER_INTERNAL (type)
13446 : 235 : = TYPE_CONTAINS_PLACEHOLDER_INTERNAL (type_dup);
13447 : :
13448 : 235 : TYPE_EMPTY_P (type) = TYPE_EMPTY_P (type_dup);
13449 : 235 : TREE_ADDRESSABLE (type) = TREE_ADDRESSABLE (type_dup);
13450 : :
13451 : : /* C++ pieces. */
13452 : 235 : TYPE_POLYMORPHIC_P (type) = TYPE_POLYMORPHIC_P (type_dup);
13453 : 235 : CLASSTYPE_FINAL (type) = CLASSTYPE_FINAL (type_dup);
13454 : :
13455 : 470 : TYPE_HAS_USER_CONSTRUCTOR (type)
13456 : 235 : = TYPE_HAS_USER_CONSTRUCTOR (type_dup);
13457 : 470 : TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type)
13458 : 235 : = TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type_dup);
13459 : 470 : TYPE_NEEDS_CONSTRUCTING (type)
13460 : 235 : = TYPE_NEEDS_CONSTRUCTING (type_dup);
13461 : :
13462 : 235 : if (auto ls = TYPE_LANG_SPECIFIC (type_dup))
13463 : : {
13464 : 235 : if (TYPE_LANG_SPECIFIC (type))
13465 : : {
13466 : 705 : CLASSTYPE_BEFRIENDING_CLASSES (type_dup)
13467 : 235 : = CLASSTYPE_BEFRIENDING_CLASSES (type);
13468 : 235 : if (!ANON_AGGR_TYPE_P (type))
13469 : 705 : CLASSTYPE_TYPEINFO_VAR (type_dup)
13470 : 235 : = CLASSTYPE_TYPEINFO_VAR (type);
13471 : : }
13472 : 1091 : for (tree v = type; v; v = TYPE_NEXT_VARIANT (v))
13473 : 856 : TYPE_LANG_SPECIFIC (v) = ls;
13474 : : }
13475 : : }
13476 : :
13477 : 32851 : TYPE_SIZE (type) = size;
13478 : 32851 : TYPE_SIZE_UNIT (type) = size_unit;
13479 : :
13480 : 32851 : if (fields)
13481 : : {
13482 : 32851 : tree *chain = &TYPE_FIELDS (type);
13483 : 32851 : unsigned len = fields->length ();
13484 : 409604 : for (unsigned ix = 0; ix != len; ix++)
13485 : : {
13486 : 376753 : tree decl = (*fields)[ix];
13487 : :
13488 : 376753 : if (!decl)
13489 : : {
13490 : : /* An anonymous struct with typedef name. */
13491 : 3 : tree tdef = (*fields)[ix+1];
13492 : 3 : decl = TYPE_STUB_DECL (TREE_TYPE (tdef));
13493 : 3 : gcc_checking_assert (IDENTIFIER_ANON_P (DECL_NAME (decl))
13494 : : && decl != tdef);
13495 : : }
13496 : :
13497 : 681745 : gcc_checking_assert (!*chain == !DECL_CLONED_FUNCTION_P (decl));
13498 : 376753 : *chain = decl;
13499 : 376753 : chain = &DECL_CHAIN (decl);
13500 : :
13501 : 376753 : if (TREE_CODE (decl) == FIELD_DECL
13502 : 376753 : && ANON_AGGR_TYPE_P (TREE_TYPE (decl)))
13503 : : {
13504 : 230 : tree anon_type = TYPE_MAIN_VARIANT (TREE_TYPE (decl));
13505 : 230 : if (DECL_NAME (defn) == as_base_identifier)
13506 : : /* ANON_AGGR_TYPE_FIELD should already point to the
13507 : : original FIELD_DECL; don't overwrite it to point
13508 : : to the as-base FIELD_DECL copy. */
13509 : 13 : gcc_checking_assert (ANON_AGGR_TYPE_FIELD (anon_type));
13510 : : else
13511 : 217 : ANON_AGGR_TYPE_FIELD (anon_type) = decl;
13512 : : }
13513 : :
13514 : 376753 : if (TREE_CODE (decl) == USING_DECL
13515 : 376753 : && TREE_CODE (USING_DECL_SCOPE (decl)) == RECORD_TYPE)
13516 : : {
13517 : : /* Reconstruct DECL_ACCESS. */
13518 : 16248 : tree decls = USING_DECL_DECLS (decl);
13519 : 16248 : tree access = declared_access (decl);
13520 : :
13521 : 19035 : for (ovl_iterator iter (decls); iter; ++iter)
13522 : : {
13523 : 1809 : tree d = *iter;
13524 : :
13525 : 1809 : retrofit_lang_decl (d);
13526 : 1809 : tree list = DECL_ACCESS (d);
13527 : :
13528 : 1809 : if (!purpose_member (type, list))
13529 : 2156 : DECL_ACCESS (d) = tree_cons (type, access, list);
13530 : : }
13531 : : }
13532 : :
13533 : 376753 : if (TREE_CODE (decl) == VAR_DECL
13534 : 12458 : && TREE_CODE (maybe_template) != TEMPLATE_DECL)
13535 : 10801 : note_vague_linkage_variable (decl);
13536 : : }
13537 : : }
13538 : :
13539 : 32851 : TYPE_VFIELD (type) = vfield;
13540 : 32851 : TYPE_BINFO (type) = binfo;
13541 : :
13542 : 32851 : if (TYPE_LANG_SPECIFIC (type))
13543 : : {
13544 : 32075 : if (!TYPE_POLYMORPHIC_P (type))
13545 : 30694 : SET_CLASSTYPE_LAMBDA_EXPR (type, lambda);
13546 : : else
13547 : 1381 : gcc_checking_assert (lambda == NULL_TREE);
13548 : :
13549 : 32075 : CLASSTYPE_MEMBER_VEC (type) = member_vec;
13550 : 32075 : CLASSTYPE_PURE_VIRTUALS (type) = pure_virts;
13551 : 32075 : CLASSTYPE_VCALL_INDICES (type) = vcall_indices;
13552 : :
13553 : 32075 : if (TYPE_POLYMORPHIC_P (type))
13554 : 1381 : SET_CLASSTYPE_KEY_METHOD (type, key_method);
13555 : : else
13556 : 30694 : gcc_checking_assert (key_method == NULL_TREE);
13557 : :
13558 : 32075 : CLASSTYPE_VBASECLASSES (type) = vbase_vec;
13559 : :
13560 : : /* Resort the member vector. */
13561 : 32075 : resort_type_member_vec (member_vec, NULL, nop, NULL);
13562 : : }
13563 : : }
13564 : : else if (maybe_dup)
13565 : : {
13566 : : // FIXME:QOI Check matching defn
13567 : : }
13568 : :
13569 : 62300 : if (TYPE_LANG_SPECIFIC (type))
13570 : : {
13571 : 61016 : tree primary = tree_node ();
13572 : 61016 : tree as_base = tree_node ();
13573 : :
13574 : 61016 : if (as_base)
13575 : 30969 : as_base = TREE_TYPE (as_base);
13576 : :
13577 : : /* Read the vtables. */
13578 : 61016 : vec<tree, va_heap> *vtables = vec_chained_decls ();
13579 : 61016 : if (vtables)
13580 : : {
13581 : 2393 : unsigned len = vtables->length ();
13582 : 5235 : for (unsigned ix = 0; ix != len; ix++)
13583 : : {
13584 : 2842 : tree vtable = (*vtables)[ix];
13585 : 2842 : read_var_def (vtable, vtable);
13586 : : }
13587 : : }
13588 : :
13589 : 61016 : tree friend_classes = tree_list (false);
13590 : 61016 : tree friend_functions = NULL_TREE;
13591 : 61016 : for (tree *chain = &friend_functions;
13592 : 72099 : tree name = tree_node (); chain = &TREE_CHAIN (*chain))
13593 : : {
13594 : 11083 : tree val = tree_list (false);
13595 : 11083 : *chain = build_tree_list (name, val);
13596 : 11083 : }
13597 : 61016 : tree decl_list = tree_list (true);
13598 : :
13599 : 61016 : if (installing)
13600 : : {
13601 : 32075 : CLASSTYPE_PRIMARY_BINFO (type) = primary;
13602 : 32075 : CLASSTYPE_AS_BASE (type) = as_base;
13603 : :
13604 : 32075 : if (vtables)
13605 : : {
13606 : 1393 : if ((!CLASSTYPE_KEY_METHOD (type)
13607 : : /* Sneaky user may have defined it inline
13608 : : out-of-class. */
13609 : 806 : || DECL_DECLARED_INLINE_P (CLASSTYPE_KEY_METHOD (type)))
13610 : : /* An imported non-template class attached to a module
13611 : : doesn't need to have its vtables emitted here. */
13612 : 1399 : && (CLASSTYPE_USE_TEMPLATE (type)
13613 : 177 : || !DECL_MODULE_ATTACH_P (defn)))
13614 : 948 : vec_safe_push (keyed_classes, type);
13615 : 1393 : unsigned len = vtables->length ();
13616 : 1393 : tree *chain = &CLASSTYPE_VTABLES (type);
13617 : 3056 : for (unsigned ix = 0; ix != len; ix++)
13618 : : {
13619 : 1663 : tree vtable = (*vtables)[ix];
13620 : 1663 : gcc_checking_assert (!*chain);
13621 : 1663 : *chain = vtable;
13622 : 1663 : chain = &DECL_CHAIN (vtable);
13623 : : }
13624 : : }
13625 : 32075 : CLASSTYPE_FRIEND_CLASSES (type) = friend_classes;
13626 : 32075 : DECL_FRIENDLIST (defn) = friend_functions;
13627 : 32075 : CLASSTYPE_DECL_LIST (type) = decl_list;
13628 : :
13629 : 34648 : for (; friend_classes; friend_classes = TREE_CHAIN (friend_classes))
13630 : : {
13631 : 2573 : tree f = TREE_VALUE (friend_classes);
13632 : 2573 : if (TREE_CODE (f) == TEMPLATE_DECL)
13633 : 1041 : f = TREE_TYPE (f);
13634 : :
13635 : 2573 : if (CLASS_TYPE_P (f))
13636 : : {
13637 : 2535 : CLASSTYPE_BEFRIENDING_CLASSES (f)
13638 : 5070 : = tree_cons (NULL_TREE, type,
13639 : 2535 : CLASSTYPE_BEFRIENDING_CLASSES (f));
13640 : 2579 : dump () && dump ("Class %N befriending %C:%N",
13641 : 6 : type, TREE_CODE (f), f);
13642 : : }
13643 : : }
13644 : :
13645 : 38394 : for (; friend_functions;
13646 : 6319 : friend_functions = TREE_CHAIN (friend_functions))
13647 : 6319 : for (tree friend_decls = TREE_VALUE (friend_functions);
13648 : 14472 : friend_decls; friend_decls = TREE_CHAIN (friend_decls))
13649 : : {
13650 : 8153 : tree f = TREE_VALUE (friend_decls);
13651 : 8153 : if (TREE_CODE (f) == TU_LOCAL_ENTITY)
13652 : 36 : continue;
13653 : :
13654 : 8117 : DECL_BEFRIENDING_CLASSES (f)
13655 : 8117 : = tree_cons (NULL_TREE, type, DECL_BEFRIENDING_CLASSES (f));
13656 : 8180 : dump () && dump ("Class %N befriending %C:%N",
13657 : 27 : type, TREE_CODE (f), f);
13658 : : }
13659 : : }
13660 : :
13661 : 61016 : if (TYPE_CONTAINS_VPTR_P (type))
13662 : : /* Read and install the thunks. */
13663 : 2839 : while (tree vfunc = tree_node ())
13664 : : {
13665 : 408 : tree thunks = chained_decls ();
13666 : 408 : if (installing)
13667 : 242 : SET_DECL_THUNKS (vfunc, thunks);
13668 : : }
13669 : :
13670 : 61016 : vec_free (vtables);
13671 : : }
13672 : :
13673 : : /* Propagate to all variants. */
13674 : 62300 : if (installing)
13675 : 32851 : fixup_type_variants (type);
13676 : :
13677 : : /* IS_FAKE_BASE_TYPE is inaccurate at this point, because if this is
13678 : : the fake base, we've not hooked it into the containing class's
13679 : : data structure yet. Fortunately it has a unique name. */
13680 : 32851 : if (installing
13681 : 32851 : && DECL_NAME (defn) != as_base_identifier
13682 : 32075 : && (!CLASSTYPE_TEMPLATE_INFO (type)
13683 : 27281 : || !uses_template_parms (TI_ARGS (CLASSTYPE_TEMPLATE_INFO (type)))))
13684 : : /* Emit debug info. It'd be nice to know if the interface TU
13685 : : already emitted this. */
13686 : 17811 : rest_of_type_compilation (type, !LOCAL_CLASS_P (type));
13687 : :
13688 : 62300 : vec_free (fields);
13689 : :
13690 : 62300 : return !get_overrun ();
13691 : : }
13692 : :
13693 : : void
13694 : 7762 : trees_out::write_enum_def (tree decl)
13695 : : {
13696 : 7762 : tree type = TREE_TYPE (decl);
13697 : :
13698 : 7762 : tree_node (TYPE_VALUES (type));
13699 : : /* Note that we stream TYPE_MIN/MAX_VALUE directly as part of the
13700 : : ENUMERAL_TYPE. */
13701 : 7762 : }
13702 : :
13703 : : void
13704 : 7762 : trees_out::mark_enum_def (tree decl)
13705 : : {
13706 : 7762 : tree type = TREE_TYPE (decl);
13707 : :
13708 : 39328 : for (tree values = TYPE_VALUES (type); values; values = TREE_CHAIN (values))
13709 : : {
13710 : 31566 : tree cst = TREE_VALUE (values);
13711 : 31566 : mark_by_value (cst);
13712 : : /* We must mark the init to avoid circularity in tt_enum_int. */
13713 : 31566 : if (tree init = DECL_INITIAL (cst))
13714 : 31322 : if (TREE_CODE (init) == INTEGER_CST)
13715 : 30746 : mark_by_value (init);
13716 : : }
13717 : 7762 : }
13718 : :
13719 : : bool
13720 : 2799 : trees_in::read_enum_def (tree defn, tree maybe_template)
13721 : : {
13722 : 2799 : tree type = TREE_TYPE (defn);
13723 : 2799 : tree values = tree_node ();
13724 : :
13725 : 2799 : if (get_overrun ())
13726 : : return false;
13727 : :
13728 : 2799 : tree maybe_dup = odr_duplicate (maybe_template, TYPE_VALUES (type));
13729 : 5598 : bool installing = maybe_dup && !TYPE_VALUES (type);
13730 : :
13731 : 2799 : if (installing)
13732 : : {
13733 : 1335 : TYPE_VALUES (type) = values;
13734 : : /* Note that we stream TYPE_MIN/MAX_VALUE directly as part of the
13735 : : ENUMERAL_TYPE. */
13736 : :
13737 : 2164 : rest_of_type_compilation (type, DECL_NAMESPACE_SCOPE_P (defn));
13738 : : }
13739 : 1464 : else if (maybe_dup)
13740 : : {
13741 : 1464 : tree known = TYPE_VALUES (type);
13742 : 8196 : for (; known && values;
13743 : 6732 : known = TREE_CHAIN (known), values = TREE_CHAIN (values))
13744 : : {
13745 : 6741 : tree known_decl = TREE_VALUE (known);
13746 : 6741 : tree new_decl = TREE_VALUE (values);
13747 : :
13748 : 6741 : if (DECL_NAME (known_decl) != DECL_NAME (new_decl))
13749 : : break;
13750 : :
13751 : 6735 : new_decl = maybe_duplicate (new_decl);
13752 : :
13753 : 6735 : if (!cp_tree_equal (DECL_INITIAL (known_decl),
13754 : 6735 : DECL_INITIAL (new_decl)))
13755 : : break;
13756 : : }
13757 : :
13758 : 1464 : if (known || values)
13759 : : {
13760 : 12 : auto_diagnostic_group d;
13761 : 12 : error_at (DECL_SOURCE_LOCATION (maybe_dup),
13762 : : "definition of %qD does not match", maybe_dup);
13763 : 12 : inform (DECL_SOURCE_LOCATION (defn),
13764 : : "existing definition %qD", defn);
13765 : :
13766 : 12 : tree known_decl = NULL_TREE, new_decl = NULL_TREE;
13767 : :
13768 : 12 : if (known)
13769 : 9 : known_decl = TREE_VALUE (known);
13770 : 12 : if (values)
13771 : 12 : new_decl = maybe_duplicate (TREE_VALUE (values));
13772 : :
13773 : 12 : if (known_decl && new_decl)
13774 : : {
13775 : 9 : inform (DECL_SOURCE_LOCATION (new_decl),
13776 : : "enumerator %qD does not match ...", new_decl);
13777 : 9 : inform (DECL_SOURCE_LOCATION (known_decl),
13778 : : "... this enumerator %qD", known_decl);
13779 : : }
13780 : 3 : else if (known_decl || new_decl)
13781 : : {
13782 : 3 : tree extra = known_decl ? known_decl : new_decl;
13783 : 3 : inform (DECL_SOURCE_LOCATION (extra),
13784 : : "additional enumerators beginning with %qD", extra);
13785 : : }
13786 : : else
13787 : 0 : inform (DECL_SOURCE_LOCATION (maybe_dup),
13788 : : "enumeration range differs");
13789 : :
13790 : : /* Mark it bad. */
13791 : 12 : unmatched_duplicate (maybe_template);
13792 : 12 : }
13793 : : }
13794 : :
13795 : : return true;
13796 : : }
13797 : :
13798 : : /* Write out the body of DECL. See above circularity note. */
13799 : :
13800 : : void
13801 : 672263 : trees_out::write_definition (tree decl, bool refs_tu_local)
13802 : : {
13803 : 672263 : auto ovr = make_temp_override (writing_local_entities,
13804 : 672263 : writing_local_entities || refs_tu_local);
13805 : :
13806 : 672263 : if (streaming_p ())
13807 : : {
13808 : 336047 : assert_definition (decl);
13809 : 336047 : dump ()
13810 : 641 : && dump ("Writing definition %C:%N", TREE_CODE (decl), decl);
13811 : : }
13812 : : else
13813 : 336216 : dump (dumper::DEPEND)
13814 : 96 : && dump ("Depending definition %C:%N", TREE_CODE (decl), decl);
13815 : :
13816 : 1040219 : again:
13817 : 1040219 : switch (TREE_CODE (decl))
13818 : : {
13819 : 0 : default:
13820 : 0 : gcc_unreachable ();
13821 : :
13822 : 367956 : case TEMPLATE_DECL:
13823 : 367956 : decl = DECL_TEMPLATE_RESULT (decl);
13824 : 367956 : goto again;
13825 : :
13826 : 398973 : case FUNCTION_DECL:
13827 : 398973 : write_function_def (decl);
13828 : 398973 : break;
13829 : :
13830 : 159832 : case TYPE_DECL:
13831 : 159832 : {
13832 : 159832 : tree type = TREE_TYPE (decl);
13833 : 159832 : gcc_assert (TYPE_MAIN_VARIANT (type) == type
13834 : : && TYPE_NAME (type) == decl);
13835 : 159832 : if (TREE_CODE (type) == ENUMERAL_TYPE)
13836 : 7762 : write_enum_def (decl);
13837 : : else
13838 : 152070 : write_class_def (decl);
13839 : : }
13840 : : break;
13841 : :
13842 : 113458 : case VAR_DECL:
13843 : 113458 : case CONCEPT_DECL:
13844 : 113458 : write_var_def (decl);
13845 : 113458 : break;
13846 : : }
13847 : 672263 : }
13848 : :
13849 : : /* Mark a declaration for by-value walking. If DO_DEFN is true, mark
13850 : : its body too. */
13851 : :
13852 : : void
13853 : 2806861 : trees_out::mark_declaration (tree decl, bool do_defn)
13854 : : {
13855 : 2806861 : mark_by_value (decl);
13856 : :
13857 : 2806861 : if (TREE_CODE (decl) == TEMPLATE_DECL)
13858 : 913788 : decl = DECL_TEMPLATE_RESULT (decl);
13859 : :
13860 : 2806861 : if (!do_defn)
13861 : : return;
13862 : :
13863 : 672285 : switch (TREE_CODE (decl))
13864 : : {
13865 : 0 : default:
13866 : 0 : gcc_unreachable ();
13867 : :
13868 : : case FUNCTION_DECL:
13869 : : mark_function_def (decl);
13870 : : break;
13871 : :
13872 : 159860 : case TYPE_DECL:
13873 : 159860 : {
13874 : 159860 : tree type = TREE_TYPE (decl);
13875 : 159860 : gcc_assert (TYPE_MAIN_VARIANT (type) == type
13876 : : && TYPE_NAME (type) == decl);
13877 : 159860 : if (TREE_CODE (type) == ENUMERAL_TYPE)
13878 : 7762 : mark_enum_def (decl);
13879 : : else
13880 : 152098 : mark_class_def (decl);
13881 : : }
13882 : : break;
13883 : :
13884 : : case VAR_DECL:
13885 : : case CONCEPT_DECL:
13886 : : mark_var_def (decl);
13887 : : break;
13888 : : }
13889 : : }
13890 : :
13891 : : /* Read in the body of DECL. See above circularity note. */
13892 : :
13893 : : bool
13894 : 301442 : trees_in::read_definition (tree decl)
13895 : : {
13896 : 302541 : dump () && dump ("Reading definition %C %N", TREE_CODE (decl), decl);
13897 : :
13898 : : tree maybe_template = decl;
13899 : :
13900 : 301442 : again:
13901 : 480408 : switch (TREE_CODE (decl))
13902 : : {
13903 : : default:
13904 : : break;
13905 : :
13906 : 178966 : case TEMPLATE_DECL:
13907 : 178966 : decl = DECL_TEMPLATE_RESULT (decl);
13908 : 178966 : goto again;
13909 : :
13910 : 197799 : case FUNCTION_DECL:
13911 : 197799 : return read_function_def (decl, maybe_template);
13912 : :
13913 : 65099 : case TYPE_DECL:
13914 : 65099 : {
13915 : 65099 : tree type = TREE_TYPE (decl);
13916 : 65099 : gcc_assert (TYPE_MAIN_VARIANT (type) == type
13917 : : && TYPE_NAME (type) == decl);
13918 : 65099 : if (TREE_CODE (type) == ENUMERAL_TYPE)
13919 : 2799 : return read_enum_def (decl, maybe_template);
13920 : : else
13921 : 62300 : return read_class_def (decl, maybe_template);
13922 : : }
13923 : 38544 : break;
13924 : :
13925 : 38544 : case VAR_DECL:
13926 : 38544 : case CONCEPT_DECL:
13927 : 38544 : return read_var_def (decl, maybe_template);
13928 : : }
13929 : :
13930 : : return false;
13931 : : }
13932 : :
13933 : : /* Lookup an maybe insert a slot for depset for KEY. */
13934 : :
13935 : : depset **
13936 : 13134794 : depset::hash::entity_slot (tree entity, bool insert)
13937 : : {
13938 : 13134794 : traits::compare_type key (entity, NULL);
13939 : 19833705 : depset **slot = find_slot_with_hash (key, traits::hash (key),
13940 : : insert ? INSERT : NO_INSERT);
13941 : :
13942 : 13134794 : return slot;
13943 : : }
13944 : :
13945 : : depset **
13946 : 168176 : depset::hash::binding_slot (tree ctx, tree name, bool insert)
13947 : : {
13948 : 168176 : traits::compare_type key (ctx, name);
13949 : 205240 : depset **slot = find_slot_with_hash (key, traits::hash (key),
13950 : : insert ? INSERT : NO_INSERT);
13951 : :
13952 : 168176 : return slot;
13953 : : }
13954 : :
13955 : : depset *
13956 : 6374905 : depset::hash::find_dependency (tree decl)
13957 : : {
13958 : 6374905 : depset **slot = entity_slot (decl, false);
13959 : :
13960 : 6374905 : return slot ? *slot : NULL;
13961 : : }
13962 : :
13963 : : depset *
13964 : 37064 : depset::hash::find_binding (tree ctx, tree name)
13965 : : {
13966 : 37064 : depset **slot = binding_slot (ctx, name, false);
13967 : :
13968 : 37064 : return slot ? *slot : NULL;
13969 : : }
13970 : :
13971 : : static bool is_tu_local_entity (tree decl, bool explain = false);
13972 : : static bool is_tu_local_value (tree decl, tree expr, bool explain = false);
13973 : : static bool has_tu_local_tmpl_arg (tree decl, tree args, bool explain);
13974 : :
13975 : : /* Returns true if DECL is a TU-local entity, as defined by [basic.link].
13976 : : If EXPLAIN is true, emit an informative note about why DECL is TU-local. */
13977 : :
13978 : : static bool
13979 : 2687850 : is_tu_local_entity (tree decl, bool explain/*=false*/)
13980 : : {
13981 : 2687850 : gcc_checking_assert (DECL_P (decl));
13982 : 2687850 : location_t loc = DECL_SOURCE_LOCATION (decl);
13983 : 2687850 : tree type = TREE_TYPE (decl);
13984 : :
13985 : : /* Only types, functions, variables, and template (specialisations)
13986 : : can be TU-local. */
13987 : 2687850 : if (TREE_CODE (decl) != TYPE_DECL
13988 : : && TREE_CODE (decl) != FUNCTION_DECL
13989 : : && TREE_CODE (decl) != VAR_DECL
13990 : : && TREE_CODE (decl) != TEMPLATE_DECL)
13991 : : return false;
13992 : :
13993 : : /* An explicit type alias is not an entity; we don't want to stream
13994 : : such aliases if they refer to TU-local entities, so propagate this
13995 : : from the original type. The built-in declarations of 'int' and such
13996 : : are never TU-local. */
13997 : 2684927 : if (TREE_CODE (decl) == TYPE_DECL
13998 : 820293 : && !DECL_SELF_REFERENCE_P (decl)
13999 : 3472725 : && !DECL_IMPLICIT_TYPEDEF_P (decl))
14000 : : {
14001 : 431016 : tree orig = DECL_ORIGINAL_TYPE (decl);
14002 : 431016 : if (orig && TYPE_NAME (orig))
14003 : : {
14004 : 88023 : if (explain)
14005 : 11 : inform (loc, "%qD is an alias of TU-local type %qT", decl, orig);
14006 : 88023 : return is_tu_local_entity (TYPE_NAME (orig), explain);
14007 : : }
14008 : : else
14009 : : return false;
14010 : : }
14011 : :
14012 : : /* Check specializations first for slightly better explanations. */
14013 : 2253911 : int use_tpl = -1;
14014 : 2253911 : tree ti = node_template_info (decl, use_tpl);
14015 : 2705014 : if (use_tpl > 0 && TREE_CODE (TI_TEMPLATE (ti)) == TEMPLATE_DECL)
14016 : : {
14017 : : /* A specialization of a TU-local template. */
14018 : 450981 : tree tmpl = TI_TEMPLATE (ti);
14019 : 450981 : if (is_tu_local_entity (tmpl))
14020 : : {
14021 : 72 : if (explain)
14022 : : {
14023 : 18 : inform (loc, "%qD is a specialization of TU-local template %qD",
14024 : : decl, tmpl);
14025 : 18 : is_tu_local_entity (tmpl, /*explain=*/true);
14026 : : }
14027 : 72 : return true;
14028 : : }
14029 : :
14030 : : /* A specialization of a template with any TU-local template argument. */
14031 : 450909 : if (has_tu_local_tmpl_arg (decl, TI_ARGS (ti), explain))
14032 : : return true;
14033 : :
14034 : : /* FIXME A specialization of a template whose (possibly instantiated)
14035 : : declaration is an exposure. This should always be covered by the
14036 : : above cases?? */
14037 : : }
14038 : :
14039 : : /* A type, function, variable, or template with internal linkage. */
14040 : 2253807 : linkage_kind kind = decl_linkage (decl);
14041 : 2253807 : if (kind == lk_internal
14042 : : /* But although weakrefs are marked static, don't consider them
14043 : : to be TU-local. */
14044 : 2253807 : && !lookup_attribute ("weakref", DECL_ATTRIBUTES (decl)))
14045 : : {
14046 : 790 : if (explain)
14047 : 165 : inform (loc, "%qD declared with internal linkage", decl);
14048 : 790 : return true;
14049 : : }
14050 : :
14051 : : /* Does not have a name with linkage and is declared, or introduced by a
14052 : : lambda-expression, within the definition of a TU-local entity. */
14053 : 2253017 : if (kind == lk_none)
14054 : : {
14055 : 250981 : tree ctx = CP_DECL_CONTEXT (decl);
14056 : 313899 : if (LAMBDA_TYPE_P (type))
14057 : 42018 : if (tree extra = LAMBDA_TYPE_EXTRA_SCOPE (type))
14058 : 250981 : ctx = extra;
14059 : :
14060 : 250981 : if (TREE_CODE (ctx) == NAMESPACE_DECL)
14061 : : {
14062 : 31 : if (!TREE_PUBLIC (ctx))
14063 : : {
14064 : 0 : if (explain)
14065 : 0 : inform (loc, "%qD has no linkage and is declared in an "
14066 : : "anonymous namespace", decl);
14067 : 0 : return true;
14068 : : }
14069 : : }
14070 : 250950 : else if (TYPE_P (ctx))
14071 : : {
14072 : 26901 : tree ctx_decl = TYPE_MAIN_DECL (ctx);
14073 : 26901 : if (is_tu_local_entity (ctx_decl))
14074 : : {
14075 : 6 : if (explain)
14076 : : {
14077 : 0 : inform (loc, "%qD has no linkage and is declared within "
14078 : : "TU-local entity %qT", decl, ctx);
14079 : 0 : is_tu_local_entity (ctx_decl, /*explain=*/true);
14080 : : }
14081 : 6 : return true;
14082 : : }
14083 : : }
14084 : 224049 : else if (is_tu_local_entity (ctx))
14085 : : {
14086 : 33 : if (explain)
14087 : : {
14088 : 6 : inform (loc, "%qD has no linkage and is declared within "
14089 : : "TU-local entity %qD", decl, ctx);
14090 : 6 : is_tu_local_entity (ctx, /*explain=*/true);
14091 : : }
14092 : 33 : return true;
14093 : : }
14094 : : }
14095 : :
14096 : : /* A type with no name that is defined outside a class-specifier, function
14097 : : body, or initializer; or is introduced by a defining-type-specifier that
14098 : : is used to declare only TU-local entities.
14099 : :
14100 : : We consider types with names for linkage purposes as having names, since
14101 : : these aren't really TU-local. */
14102 : 2252978 : tree inner = STRIP_TEMPLATE (decl);
14103 : 925082 : if (inner
14104 : 2252978 : && TREE_CODE (inner) == TYPE_DECL
14105 : 1487203 : && TYPE_ANON_P (type)
14106 : 23068 : && !DECL_SELF_REFERENCE_P (inner)
14107 : : /* An enum with an enumerator name for linkage. */
14108 : 947019 : && !(UNSCOPED_ENUM_P (type) && TYPE_VALUES (type)))
14109 : : {
14110 : 20491 : tree main_decl = TYPE_MAIN_DECL (type);
14111 : 40721 : if (LAMBDA_TYPE_P (type))
14112 : : {
14113 : : /* A lambda expression is, in practice, TU-local iff it has no
14114 : : mangling scope. This currently doesn't line up exactly with
14115 : : the standard's definition due to some ABI issues, but it's
14116 : : pretty close, and avoids other issues down the line. */
14117 : 40398 : if (!LAMBDA_TYPE_EXTRA_SCOPE (type))
14118 : : {
14119 : 4 : if (explain)
14120 : 2 : inform (loc, "%qT has no name and cannot be differentiated "
14121 : : "from similar lambdas in other TUs", type);
14122 : 4 : return true;
14123 : : }
14124 : : }
14125 : 584 : else if (!DECL_CLASS_SCOPE_P (main_decl)
14126 : 322 : && !decl_function_context (main_decl))
14127 : : {
14128 : 30 : if (explain)
14129 : 12 : inform (loc, "%qT has no name and is not defined within a class, "
14130 : : "function, or initializer", type);
14131 : 30 : return true;
14132 : : }
14133 : :
14134 : : // FIXME introduced by a defining-type-specifier only declaring TU-local
14135 : : // entities; does this refer to e.g. 'static struct {} a;"? I can't
14136 : : // think of any cases where this isn't covered by earlier cases. */
14137 : : }
14138 : :
14139 : : return false;
14140 : : }
14141 : :
14142 : : /* Helper for is_tu_local_entity. Returns true if one of the ARGS of
14143 : : DECL is TU-local. Emits an explanation if EXPLAIN is true. */
14144 : :
14145 : : static bool
14146 : 507333 : has_tu_local_tmpl_arg (tree decl, tree args, bool explain)
14147 : : {
14148 : 507333 : if (!args || TREE_CODE (args) != TREE_VEC)
14149 : : return false;
14150 : :
14151 : 1359795 : for (tree a : tree_vec_range (args))
14152 : : {
14153 : 852494 : if (TREE_CODE (a) == TREE_VEC)
14154 : : {
14155 : 56424 : if (has_tu_local_tmpl_arg (decl, a, explain))
14156 : 32 : return true;
14157 : : }
14158 : : else if (!WILDCARD_TYPE_P (a))
14159 : : {
14160 : 721520 : if (DECL_P (a) && is_tu_local_entity (a))
14161 : : {
14162 : 0 : if (explain)
14163 : : {
14164 : 0 : inform (DECL_SOURCE_LOCATION (decl),
14165 : : "%qD has TU-local template argument %qD",
14166 : : decl, a);
14167 : 0 : is_tu_local_entity (a, /*explain=*/true);
14168 : : }
14169 : 0 : return true;
14170 : : }
14171 : :
14172 : 721520 : if (TYPE_P (a) && TYPE_NAME (a) && is_tu_local_entity (TYPE_NAME (a)))
14173 : : {
14174 : 17 : if (explain)
14175 : : {
14176 : 1 : inform (DECL_SOURCE_LOCATION (decl),
14177 : : "%qD has TU-local template argument %qT",
14178 : : decl, a);
14179 : 1 : is_tu_local_entity (TYPE_NAME (a), /*explain=*/true);
14180 : : }
14181 : 17 : return true;
14182 : : }
14183 : :
14184 : 721503 : if (EXPR_P (a) && is_tu_local_value (decl, a, explain))
14185 : : return true;
14186 : : }
14187 : : }
14188 : :
14189 : 507301 : return false;
14190 : : }
14191 : :
14192 : : /* Returns true if EXPR (part of the initializer for DECL) is a TU-local value
14193 : : or object. Emits an explanation if EXPLAIN is true. */
14194 : :
14195 : : static bool
14196 : 50618 : is_tu_local_value (tree decl, tree expr, bool explain/*=false*/)
14197 : : {
14198 : 50618 : if (!expr)
14199 : : return false;
14200 : :
14201 : 49397 : tree e = expr;
14202 : 49397 : STRIP_ANY_LOCATION_WRAPPER (e);
14203 : 49397 : STRIP_NOPS (e);
14204 : 49397 : if (TREE_CODE (e) == TARGET_EXPR)
14205 : 0 : e = TARGET_EXPR_INITIAL (e);
14206 : 0 : if (!e)
14207 : : return false;
14208 : :
14209 : : /* It is, or is a pointer to, a TU-local function or the object associated
14210 : : with a TU-local variable. */
14211 : 49397 : tree object = NULL_TREE;
14212 : 49397 : if (TREE_CODE (e) == ADDR_EXPR)
14213 : 1894 : object = TREE_OPERAND (e, 0);
14214 : 47503 : else if (TREE_CODE (e) == PTRMEM_CST)
14215 : 0 : object = PTRMEM_CST_MEMBER (e);
14216 : 47503 : else if (VAR_OR_FUNCTION_DECL_P (e))
14217 : : object = e;
14218 : :
14219 : 1894 : if (object
14220 : 2269 : && VAR_OR_FUNCTION_DECL_P (object)
14221 : 2359 : && is_tu_local_entity (object))
14222 : : {
14223 : 54 : if (explain)
14224 : : {
14225 : : /* We've lost a lot of location information by the time we get here,
14226 : : so let's just do our best effort. */
14227 : 18 : auto loc = cp_expr_loc_or_loc (expr, DECL_SOURCE_LOCATION (decl));
14228 : 18 : if (VAR_P (object))
14229 : 9 : inform (loc, "%qD refers to TU-local object %qD", decl, object);
14230 : : else
14231 : 9 : inform (loc, "%qD refers to TU-local function %qD", decl, object);
14232 : 18 : is_tu_local_entity (object, true);
14233 : : }
14234 : 54 : return true;
14235 : : }
14236 : :
14237 : : /* It is an object of class or array type and any of its subobjects or
14238 : : any of the objects or functions to which its non-static data members
14239 : : of reference type refer is TU-local and is usable in constant
14240 : : expressions. */
14241 : 49343 : if (TREE_CODE (e) == CONSTRUCTOR && AGGREGATE_TYPE_P (TREE_TYPE (e)))
14242 : 21585 : for (auto &f : CONSTRUCTOR_ELTS (e))
14243 : 14967 : if (is_tu_local_value (decl, f.value, explain))
14244 : : return true;
14245 : :
14246 : : return false;
14247 : : }
14248 : :
14249 : : /* Complains if DECL is a TU-local entity imported from a named module.
14250 : : Returns TRUE if instantiation should fail. */
14251 : :
14252 : : bool
14253 : 7580562727 : instantiating_tu_local_entity (tree decl)
14254 : : {
14255 : 7580562727 : if (!modules_p ())
14256 : : return false;
14257 : :
14258 : 23515540 : if (TREE_CODE (decl) == TU_LOCAL_ENTITY)
14259 : : {
14260 : 86 : auto_diagnostic_group d;
14261 : 86 : error ("instantiation exposes TU-local entity %qD",
14262 : 86 : TU_LOCAL_ENTITY_NAME (decl));
14263 : 86 : inform (TU_LOCAL_ENTITY_LOCATION (decl), "declared here");
14264 : 86 : return true;
14265 : 86 : }
14266 : :
14267 : : /* Currently, only TU-local variables and functions, or possibly
14268 : : templates thereof, will be emitted from named modules. */
14269 : 23515454 : tree inner = STRIP_TEMPLATE (decl);
14270 : 23515454 : if (!VAR_OR_FUNCTION_DECL_P (inner))
14271 : : return false;
14272 : :
14273 : : /* From this point we will only be emitting warnings; if we're not
14274 : : warning about this case then there's no need to check further. */
14275 : 968954 : if (!warn_expose_global_module_tu_local
14276 : 1937908 : || !warning_enabled_at (DECL_SOURCE_LOCATION (decl),
14277 : 968954 : OPT_Wexpose_global_module_tu_local))
14278 : 7132 : return false;
14279 : :
14280 : 961822 : if (!is_tu_local_entity (decl))
14281 : : return false;
14282 : :
14283 : 62 : if (!DECL_LANG_SPECIFIC (inner)
14284 : 114 : || !DECL_MODULE_IMPORT_P (inner))
14285 : : return false;
14286 : :
14287 : : /* Referencing TU-local entities from a header is generally OK.
14288 : : We don't have an easy way to detect if this declaration came
14289 : : from a header via a separate named module, but we can just
14290 : : ignore that case for warning purposes. */
14291 : 9 : unsigned index = import_entity_index (decl);
14292 : 9 : module_state *mod = import_entity_module (index);
14293 : 9 : if (mod->is_header ())
14294 : : return false;
14295 : :
14296 : 9 : auto_diagnostic_group d;
14297 : 9 : warning (OPT_Wexpose_global_module_tu_local,
14298 : : "instantiation exposes TU-local entity %qD", decl);
14299 : 9 : inform (DECL_SOURCE_LOCATION (decl), "declared here");
14300 : :
14301 : : /* We treat TU-local entities from the GMF as not actually being
14302 : : TU-local as an extension, so allow instantation to proceed. */
14303 : 9 : return false;
14304 : 9 : }
14305 : :
14306 : : /* DECL is a newly discovered dependency. Create the depset, if it
14307 : : doesn't already exist. Add it to the worklist if so.
14308 : :
14309 : : DECL will be an OVL_USING_P OVERLOAD, if it's from a binding that's
14310 : : a using decl.
14311 : :
14312 : : We do not have to worry about adding the same dependency more than
14313 : : once. First it's harmless, but secondly the TREE_VISITED marking
14314 : : prevents us wanting to do it anyway. */
14315 : :
14316 : : depset *
14317 : 5577778 : depset::hash::make_dependency (tree decl, entity_kind ek)
14318 : : {
14319 : : /* Make sure we're being told consistent information. */
14320 : 10375249 : gcc_checking_assert ((ek == EK_NAMESPACE)
14321 : : == (TREE_CODE (decl) == NAMESPACE_DECL
14322 : : && !DECL_NAMESPACE_ALIAS (decl)));
14323 : 5577778 : gcc_checking_assert (ek != EK_BINDING && ek != EK_REDIRECT);
14324 : 5577778 : gcc_checking_assert (TREE_CODE (decl) != FIELD_DECL
14325 : : && (TREE_CODE (decl) != USING_DECL
14326 : : || TREE_CODE (DECL_CONTEXT (decl)) == FUNCTION_DECL));
14327 : 5577778 : gcc_checking_assert (!is_key_order ());
14328 : 5577778 : if (ek == EK_USING)
14329 : 20627 : gcc_checking_assert (TREE_CODE (decl) == OVERLOAD);
14330 : 5577778 : if (ek == EK_TU_LOCAL)
14331 : 87 : gcc_checking_assert (DECL_DECLARES_FUNCTION_P (decl));
14332 : :
14333 : 5577778 : if (TREE_CODE (decl) == TEMPLATE_DECL)
14334 : : /* The template should have copied these from its result decl. */
14335 : 2009220 : gcc_checking_assert (DECL_MODULE_EXPORT_P (decl)
14336 : : == DECL_MODULE_EXPORT_P (DECL_TEMPLATE_RESULT (decl)));
14337 : :
14338 : 5577778 : depset **slot = entity_slot (decl, true);
14339 : 5577778 : depset *dep = *slot;
14340 : 5577778 : bool for_binding = ek == EK_FOR_BINDING;
14341 : :
14342 : 5577778 : if (!dep)
14343 : : {
14344 : 518778 : if ((DECL_IMPLICIT_TYPEDEF_P (decl)
14345 : : /* ... not an enum, for instance. */
14346 : 261289 : && RECORD_OR_UNION_TYPE_P (TREE_TYPE (decl))
14347 : 257190 : && TYPE_LANG_SPECIFIC (TREE_TYPE (decl))
14348 : 234152 : && CLASSTYPE_USE_TEMPLATE (TREE_TYPE (decl)) == 2)
14349 : 1761433 : || (VAR_P (decl)
14350 : 73599 : && DECL_LANG_SPECIFIC (decl)
14351 : 73518 : && DECL_USE_TEMPLATE (decl) == 2))
14352 : : {
14353 : : /* A partial or explicit specialization. Partial
14354 : : specializations might not be in the hash table, because
14355 : : there can be multiple differently-constrained variants.
14356 : :
14357 : : template<typename T> class silly;
14358 : : template<typename T> requires true class silly {};
14359 : :
14360 : : We need to find them, insert their TEMPLATE_DECL in the
14361 : : dep_hash, and then convert the dep we just found into a
14362 : : redirect. */
14363 : :
14364 : 37202 : tree ti = get_template_info (decl);
14365 : 37202 : tree tmpl = TI_TEMPLATE (ti);
14366 : 37202 : tree partial = NULL_TREE;
14367 : 37202 : for (tree spec = DECL_TEMPLATE_SPECIALIZATIONS (tmpl);
14368 : 122313 : spec; spec = TREE_CHAIN (spec))
14369 : 104835 : if (DECL_TEMPLATE_RESULT (TREE_VALUE (spec)) == decl)
14370 : : {
14371 : : partial = TREE_VALUE (spec);
14372 : : break;
14373 : : }
14374 : :
14375 : 37202 : if (partial)
14376 : : {
14377 : : /* Eagerly create an empty redirect. The following
14378 : : make_dependency call could cause hash reallocation,
14379 : : and invalidate slot's value. */
14380 : 19724 : depset *redirect = make_entity (decl, EK_REDIRECT);
14381 : :
14382 : : /* Redirects are never reached -- always snap to their target. */
14383 : 19724 : redirect->set_flag_bit<DB_UNREACHED_BIT> ();
14384 : :
14385 : 19724 : *slot = redirect;
14386 : :
14387 : 19724 : depset *tmpl_dep = make_dependency (partial, EK_PARTIAL);
14388 : 19724 : gcc_checking_assert (tmpl_dep->get_entity_kind () == EK_PARTIAL);
14389 : :
14390 : 19724 : redirect->deps.safe_push (tmpl_dep);
14391 : :
14392 : 19724 : return redirect;
14393 : : }
14394 : : }
14395 : :
14396 : 1255064 : bool has_def = ek != EK_USING && has_definition (decl);
14397 : 1234437 : if (ek > EK_BINDING)
14398 : 144020 : ek = EK_DECL;
14399 : :
14400 : : /* The only OVERLOADS we should see are USING decls from
14401 : : bindings. */
14402 : 1255064 : *slot = dep = make_entity (decl, ek, has_def);
14403 : :
14404 : 1255064 : if (CHECKING_P && TREE_CODE (decl) == TEMPLATE_DECL)
14405 : : /* The template_result should otherwise not be in the
14406 : : table, or be an empty redirect (created above). */
14407 : 324006 : if (auto *eslot = entity_slot (DECL_TEMPLATE_RESULT (decl), false))
14408 : 19724 : gcc_checking_assert ((*eslot)->get_entity_kind () == EK_REDIRECT
14409 : : && !(*eslot)->deps.length ());
14410 : :
14411 : 1255064 : if (ek != EK_USING)
14412 : : {
14413 : 1234437 : tree not_tmpl = STRIP_TEMPLATE (decl);
14414 : 1234437 : bool imported_from_module_p = false;
14415 : :
14416 : 1234437 : if (DECL_LANG_SPECIFIC (not_tmpl)
14417 : 2399318 : && DECL_MODULE_IMPORT_P (not_tmpl))
14418 : : {
14419 : : /* Store the module number and index in cluster/section,
14420 : : so we don't have to look them up again. */
14421 : 54644 : unsigned index = import_entity_index (decl);
14422 : 54644 : module_state *from = import_entity_module (index);
14423 : : /* Remap will be zero for imports from partitions, which
14424 : : we want to treat as-if declared in this TU. */
14425 : 54644 : if (from->remap)
14426 : : {
14427 : 54169 : dep->cluster = index - from->entity_lwm;
14428 : 54169 : dep->section = from->remap;
14429 : 54169 : dep->set_flag_bit<DB_IMPORTED_BIT> ();
14430 : :
14431 : 54169 : if (!from->is_header ())
14432 : 1234437 : imported_from_module_p = true;
14433 : : }
14434 : : }
14435 : :
14436 : : /* Check for TU-local entities. This is unnecessary in header
14437 : : units because we can export internal-linkage decls, and
14438 : : no declarations are exposures. Similarly, if the decl was
14439 : : imported from a non-header module we know it cannot have
14440 : : been TU-local. */
14441 : 1234437 : if (!header_module_p () && !imported_from_module_p)
14442 : : {
14443 : 392602 : if (is_tu_local_entity (decl))
14444 : 300 : dep->set_flag_bit<DB_TU_LOCAL_BIT> ();
14445 : :
14446 : 392602 : if (VAR_P (decl)
14447 : 25415 : && decl_maybe_constant_var_p (decl)
14448 : 417246 : && is_tu_local_value (decl, DECL_INITIAL (decl)))
14449 : : {
14450 : : /* A potentially-constant variable initialized to a TU-local
14451 : : value is not usable in constant expressions within other
14452 : : translation units. We can achieve this by simply not
14453 : : streaming the definition in such cases. */
14454 : 24 : dep->clear_flag_bit<DB_DEFN_BIT> ();
14455 : :
14456 : 24 : if (DECL_DECLARED_CONSTEXPR_P (decl)
14457 : 39 : || DECL_INLINE_VAR_P (decl))
14458 : : /* A constexpr variable initialized to a TU-local value,
14459 : : or an inline value (PR c++/119996), is an exposure.
14460 : :
14461 : : For simplicity, we don't support "non-strict" TU-local
14462 : : values: even if the TU-local entity we refer to in the
14463 : : initialiser is in the GMF, we still won't consider this
14464 : : valid in constant expressions in other TUs, and so
14465 : : complain accordingly. */
14466 : 15 : dep->set_flag_bit<DB_EXPOSE_PURVIEW_BIT> ();
14467 : : }
14468 : : }
14469 : :
14470 : : /* A namespace-scope type may be declared in one module unit
14471 : : and defined in another; make sure that we're found when
14472 : : completing the class. */
14473 : 1234437 : if (ek == EK_DECL
14474 : 507773 : && !dep->is_import ()
14475 : 501749 : && dep->has_defn ()
14476 : 259507 : && DECL_NAMESPACE_SCOPE_P (not_tmpl)
14477 : 94977 : && DECL_IMPLICIT_TYPEDEF_P (not_tmpl)
14478 : : /* Anonymous types can't be forward-declared. */
14479 : 1261191 : && !IDENTIFIER_ANON_P (DECL_NAME (not_tmpl)))
14480 : 26308 : dep->set_flag_bit<DB_IS_PENDING_BIT> ();
14481 : :
14482 : : /* Namespace-scope functions can be found by ADL by template
14483 : : instantiations in this module. We need to create bindings
14484 : : for them so that name lookup recognises they exist, if they
14485 : : won't be discarded. add_binding_entity is too early to do
14486 : : this for GM functions, because if nobody ends up using them
14487 : : we'll have leftover bindings laying around, and it's tricky
14488 : : to delete them and any namespaces they've implicitly created
14489 : : deps on. The downside is this means we don't pick up on
14490 : : using-decls, but by [module.global.frag] p3.6 we don't have
14491 : : to. */
14492 : 1234437 : if (ek == EK_DECL
14493 : 1234437 : && !for_binding
14494 : 363765 : && !dep->is_import ()
14495 : 357747 : && !dep->is_tu_local ()
14496 : 357634 : && DECL_NAMESPACE_SCOPE_P (decl)
14497 : 21984 : && DECL_DECLARES_FUNCTION_P (decl)
14498 : : /* Compiler-generated functions won't participate in ADL. */
14499 : 14451 : && !DECL_ARTIFICIAL (decl)
14500 : : /* A hidden friend doesn't need a binding. */
14501 : 1243512 : && !(DECL_LANG_SPECIFIC (not_tmpl)
14502 : 9075 : && DECL_UNIQUE_FRIEND_P (not_tmpl)))
14503 : : {
14504 : : /* This will only affect GM functions. */
14505 : 6234 : gcc_checking_assert (!DECL_LANG_SPECIFIC (not_tmpl)
14506 : : || !DECL_MODULE_PURVIEW_P (not_tmpl));
14507 : : /* We shouldn't see any instantiations or specialisations. */
14508 : 3117 : gcc_checking_assert (!DECL_LANG_SPECIFIC (decl)
14509 : : || !DECL_USE_TEMPLATE (decl));
14510 : :
14511 : 3117 : tree ns = CP_DECL_CONTEXT (decl);
14512 : 3117 : tree name = DECL_NAME (decl);
14513 : 3117 : depset *binding = find_binding (ns, name);
14514 : 3117 : if (!binding)
14515 : : {
14516 : 1510 : binding = make_binding (ns, name);
14517 : 1510 : add_namespace_context (binding, ns);
14518 : :
14519 : 1510 : depset **slot = binding_slot (ns, name, /*insert=*/true);
14520 : 1510 : *slot = binding;
14521 : : }
14522 : :
14523 : 3117 : binding->deps.safe_push (dep);
14524 : 3117 : dep->deps.safe_push (binding);
14525 : 3126 : dump (dumper::DEPEND)
14526 : 9 : && dump ("Built ADL binding for %C:%N",
14527 : 9 : TREE_CODE (decl), decl);
14528 : : }
14529 : : }
14530 : :
14531 : 1255064 : if (!dep->is_import ())
14532 : 1200895 : worklist.safe_push (dep);
14533 : : }
14534 : :
14535 : 5558054 : dump (dumper::DEPEND)
14536 : 37067 : && dump ("%s on %s %C:%N found",
14537 : : ek == EK_REDIRECT ? "Redirect"
14538 : 37067 : : (for_binding || ek == EK_TU_LOCAL) ? "Binding"
14539 : : : "Dependency",
14540 : 37067 : dep->entity_kind_name (), TREE_CODE (decl), decl);
14541 : :
14542 : 5558054 : return dep;
14543 : : }
14544 : :
14545 : : /* Whether REF is an exposure of a member type of SOURCE.
14546 : :
14547 : : This comes up with exposures of class-scope lambdas, that we currently
14548 : : treat as TU-local due to ABI reasons. In such a case the type of the
14549 : : lambda will be exposed in two places, first by the class type it is in
14550 : : the TYPE_FIELDS list of, and second by the actual member declaring that
14551 : : lambda. We only want the second case to warn. */
14552 : :
14553 : : static bool
14554 : 250 : is_exposure_of_member_type (depset *source, depset *ref)
14555 : : {
14556 : 250 : gcc_checking_assert (source->refs_tu_local (/*strict=*/true)
14557 : : && ref->is_tu_local (/*strict=*/true));
14558 : 250 : tree source_entity = STRIP_TEMPLATE (source->get_entity ());
14559 : 250 : tree ref_entity = STRIP_TEMPLATE (ref->get_entity ());
14560 : :
14561 : 250 : if (!source->is_tu_local (/*strict=*/true)
14562 : 238 : && source_entity
14563 : 238 : && ref_entity
14564 : 238 : && DECL_IMPLICIT_TYPEDEF_P (source_entity)
14565 : 2 : && DECL_IMPLICIT_TYPEDEF_P (ref_entity)
14566 : 2 : && DECL_CLASS_SCOPE_P (ref_entity)
14567 : 252 : && DECL_CONTEXT (ref_entity) == TREE_TYPE (source_entity))
14568 : : {
14569 : 4 : gcc_checking_assert (LAMBDA_TYPE_P (TREE_TYPE (ref_entity)));
14570 : : return true;
14571 : : }
14572 : : else
14573 : : return false;
14574 : : }
14575 : :
14576 : : /* DEP is a newly discovered dependency. Append it to current's
14577 : : depset. */
14578 : :
14579 : : void
14580 : 4090196 : depset::hash::add_dependency (depset *dep)
14581 : : {
14582 : 4090196 : gcc_checking_assert (current && !is_key_order ());
14583 : 4090196 : current->deps.safe_push (dep);
14584 : :
14585 : 4090196 : if (dep->is_tu_local (/*strict=*/true))
14586 : : {
14587 : 326 : if (dep->is_tu_local ())
14588 : 269 : current->set_flag_bit<DB_REF_PURVIEW_BIT> ();
14589 : : else
14590 : 57 : current->set_flag_bit<DB_REF_GLOBAL_BIT> ();
14591 : :
14592 : 326 : if (!ignore_tu_local && !is_exposure_of_member_type (current, dep))
14593 : : {
14594 : 130 : if (dep->is_tu_local ())
14595 : 94 : current->set_flag_bit<DB_EXPOSE_PURVIEW_BIT> ();
14596 : : else
14597 : 36 : current->set_flag_bit<DB_EXPOSE_GLOBAL_BIT> ();
14598 : : }
14599 : : }
14600 : :
14601 : 4090196 : if (current->get_entity_kind () == EK_USING
14602 : 20627 : && DECL_IMPLICIT_TYPEDEF_P (dep->get_entity ())
14603 : 4094950 : && TREE_CODE (TREE_TYPE (dep->get_entity ())) == ENUMERAL_TYPE)
14604 : : {
14605 : : /* CURRENT is an unwrapped using-decl and DECL is an enum's
14606 : : implicit typedef. Is CURRENT a member of the enum? */
14607 : 4456 : tree c_decl = OVL_FUNCTION (current->get_entity ());
14608 : :
14609 : 4456 : if (TREE_CODE (c_decl) == CONST_DECL
14610 : 8875 : && (current->deps[0]->get_entity ()
14611 : 4419 : == CP_DECL_CONTEXT (dep->get_entity ())))
14612 : : /* Make DECL depend on CURRENT. */
14613 : 4365 : dep->deps.safe_push (current);
14614 : : }
14615 : :
14616 : : /* If two dependencies recursively depend on each other existing within
14617 : : their own merge keys, we must ensure that the first dep we saw while
14618 : : walking is written first in this cluster. See sort_cluster for more
14619 : : details. */
14620 : 4090196 : if (writing_merge_key)
14621 : : {
14622 : 722047 : dep->set_flag_bit<DB_MAYBE_RECURSIVE_BIT> ();
14623 : 722047 : if (!current->is_maybe_recursive ())
14624 : 680610 : current->set_flag_bit<DB_ENTRY_BIT> ();
14625 : : }
14626 : :
14627 : 4090196 : if (dep->is_unreached ())
14628 : : {
14629 : : /* The dependency is reachable now. */
14630 : 308045 : reached_unreached = true;
14631 : 308045 : dep->clear_flag_bit<DB_UNREACHED_BIT> ();
14632 : 308045 : dump (dumper::DEPEND)
14633 : 30 : && dump ("Reaching unreached %s %C:%N", dep->entity_kind_name (),
14634 : 30 : TREE_CODE (dep->get_entity ()), dep->get_entity ());
14635 : : }
14636 : 4090196 : }
14637 : :
14638 : : depset *
14639 : 6115266 : depset::hash::add_dependency (tree decl, entity_kind ek)
14640 : : {
14641 : 6115266 : depset *dep;
14642 : :
14643 : 6115266 : if (is_key_order ())
14644 : : {
14645 : 1980174 : dep = find_dependency (decl);
14646 : 1980174 : if (dep)
14647 : : {
14648 : 986984 : current->deps.safe_push (dep);
14649 : 986984 : dump (dumper::MERGE)
14650 : 471 : && dump ("Key dependency on %s %C:%N found",
14651 : 471 : dep->entity_kind_name (), TREE_CODE (decl), decl);
14652 : : }
14653 : : else
14654 : : {
14655 : : /* It's not a mergeable decl, look for it in the original
14656 : : table. */
14657 : 993190 : dep = chain->find_dependency (decl);
14658 : 993190 : gcc_checking_assert (dep);
14659 : : }
14660 : : }
14661 : : else
14662 : : {
14663 : 4135092 : dep = make_dependency (decl, ek);
14664 : 4135092 : if (dep->get_entity_kind () != EK_REDIRECT)
14665 : 4090196 : add_dependency (dep);
14666 : : }
14667 : :
14668 : 6115266 : return dep;
14669 : : }
14670 : :
14671 : : void
14672 : 516585 : depset::hash::add_namespace_context (depset *dep, tree ns)
14673 : : {
14674 : 516585 : depset *ns_dep = make_dependency (ns, depset::EK_NAMESPACE);
14675 : 516585 : dep->deps.safe_push (ns_dep);
14676 : :
14677 : : /* Mark it as special if imported so we don't walk connect when
14678 : : SCCing. */
14679 : 516585 : if (!dep->is_binding () && ns_dep->is_import ())
14680 : 0 : dep->set_special ();
14681 : 516585 : }
14682 : :
14683 : : struct add_binding_data
14684 : : {
14685 : : tree ns;
14686 : : bitmap partitions;
14687 : : depset *binding;
14688 : : depset::hash *hash;
14689 : : bool met_namespace;
14690 : : };
14691 : :
14692 : : /* Return true if we are, or contain something that is exported. */
14693 : :
14694 : : bool
14695 : 5385646 : depset::hash::add_binding_entity (tree decl, WMB_Flags flags, void *data_)
14696 : : {
14697 : 5385646 : auto data = static_cast <add_binding_data *> (data_);
14698 : 5385646 : decl = strip_using_decl (decl);
14699 : :
14700 : 5385646 : if (!(TREE_CODE (decl) == NAMESPACE_DECL && !DECL_NAMESPACE_ALIAS (decl)))
14701 : : {
14702 : 5378000 : tree inner = decl;
14703 : :
14704 : 5378000 : if (TREE_CODE (inner) == CONST_DECL
14705 : 5645 : && TREE_CODE (DECL_CONTEXT (inner)) == ENUMERAL_TYPE
14706 : : /* A using-decl could make a CONST_DECL purview for a non-purview
14707 : : enumeration. */
14708 : 5383645 : && (!DECL_LANG_SPECIFIC (inner) || !DECL_MODULE_PURVIEW_P (inner)))
14709 : 5604 : inner = TYPE_NAME (DECL_CONTEXT (inner));
14710 : 5372396 : else if (TREE_CODE (inner) == TEMPLATE_DECL)
14711 : 103166 : inner = DECL_TEMPLATE_RESULT (inner);
14712 : :
14713 : 10591958 : if ((!DECL_LANG_SPECIFIC (inner) || !DECL_MODULE_PURVIEW_P (inner))
14714 : 10432943 : && !((flags & WMB_Using) && (flags & WMB_Purview)))
14715 : : /* Ignore entities not within the module purview. We'll need to
14716 : : create bindings for any non-discarded function calls for ADL,
14717 : : but it's simpler to handle that at the point of use rather
14718 : : than trying to clear out bindings after the fact. */
14719 : : return false;
14720 : :
14721 : 164761 : bool internal_decl = false;
14722 : 164761 : if (!header_module_p () && is_tu_local_entity (decl))
14723 : : {
14724 : : /* A TU-local entity. For ADL we still need to create bindings
14725 : : for internal-linkage functions attached to a named module. */
14726 : 120 : if (DECL_DECLARES_FUNCTION_P (inner)
14727 : 96 : && DECL_LANG_SPECIFIC (inner)
14728 : 312 : && DECL_MODULE_ATTACH_P (inner))
14729 : : {
14730 : 87 : gcc_checking_assert (!DECL_MODULE_EXPORT_P (inner));
14731 : : internal_decl = true;
14732 : : }
14733 : : else
14734 : : return false;
14735 : : }
14736 : :
14737 : 164632 : if ((TREE_CODE (decl) == VAR_DECL
14738 : 164632 : || TREE_CODE (decl) == TYPE_DECL)
14739 : 164632 : && DECL_TINFO_P (decl))
14740 : : /* Ignore TINFO things. */
14741 : : return false;
14742 : :
14743 : 164632 : if (TREE_CODE (decl) == VAR_DECL && DECL_NTTP_OBJECT_P (decl))
14744 : : /* Ignore NTTP objects. */
14745 : : return false;
14746 : :
14747 : 164632 : if (deduction_guide_p (decl))
14748 : : {
14749 : : /* Ignore deduction guides, bindings for them will be created within
14750 : : find_dependencies for their class template. But still build a dep
14751 : : for them so that we don't discard them. */
14752 : 1569 : data->hash->make_dependency (decl, EK_FOR_BINDING);
14753 : 1569 : return false;
14754 : : }
14755 : :
14756 : 163063 : if (!(flags & WMB_Using) && CP_DECL_CONTEXT (decl) != data->ns)
14757 : : {
14758 : : /* An unscoped enum constant implicitly brought into the containing
14759 : : namespace. We treat this like a using-decl. */
14760 : 3725 : gcc_checking_assert (TREE_CODE (decl) == CONST_DECL);
14761 : :
14762 : 3725 : flags = WMB_Flags (flags | WMB_Using);
14763 : 3725 : if (DECL_MODULE_EXPORT_P (TYPE_NAME (TREE_TYPE (decl)))
14764 : : /* A using-decl can make an enum constant exported for a
14765 : : non-exported enumeration. */
14766 : 3725 : || (DECL_LANG_SPECIFIC (decl) && DECL_MODULE_EXPORT_P (decl)))
14767 : 3503 : flags = WMB_Flags (flags | WMB_Export);
14768 : : }
14769 : :
14770 : 163063 : if (!data->binding)
14771 : : /* No binding to check. */;
14772 : 34126 : else if (flags & WMB_Using)
14773 : : {
14774 : : /* Look in the binding to see if we already have this
14775 : : using. */
14776 : 47248 : for (unsigned ix = data->binding->deps.length (); --ix;)
14777 : : {
14778 : 37693 : depset *d = data->binding->deps[ix];
14779 : 75386 : if (d->get_entity_kind () == EK_USING
14780 : 37693 : && OVL_FUNCTION (d->get_entity ()) == decl)
14781 : : {
14782 : 1 : if (!(flags & WMB_Hidden))
14783 : 1 : d->clear_hidden_binding ();
14784 : 1 : OVL_PURVIEW_P (d->get_entity ()) = true;
14785 : 1 : if (flags & WMB_Export)
14786 : 1 : OVL_EXPORT_P (d->get_entity ()) = true;
14787 : 1 : return bool (flags & WMB_Export);
14788 : : }
14789 : : }
14790 : : }
14791 : 29348 : else if (flags & WMB_Dups)
14792 : : {
14793 : : /* Look in the binding to see if we already have this decl. */
14794 : 78 : for (unsigned ix = data->binding->deps.length (); --ix;)
14795 : : {
14796 : 39 : depset *d = data->binding->deps[ix];
14797 : 39 : if (d->get_entity () == decl)
14798 : : {
14799 : 33 : if (!(flags & WMB_Hidden))
14800 : 30 : d->clear_hidden_binding ();
14801 : 33 : return false;
14802 : : }
14803 : : }
14804 : : }
14805 : :
14806 : : /* We're adding something. */
14807 : 163029 : if (!data->binding)
14808 : : {
14809 : 128937 : data->binding = make_binding (data->ns, DECL_NAME (decl));
14810 : 128937 : data->hash->add_namespace_context (data->binding, data->ns);
14811 : :
14812 : 128937 : depset **slot = data->hash->binding_slot (data->ns,
14813 : 128937 : DECL_NAME (decl), true);
14814 : 128937 : gcc_checking_assert (!*slot);
14815 : 128937 : *slot = data->binding;
14816 : : }
14817 : :
14818 : : /* Make sure nobody left a tree visited lying about. */
14819 : 163029 : gcc_checking_assert (!TREE_VISITED (decl));
14820 : :
14821 : 163029 : if (flags & WMB_Using)
14822 : : {
14823 : 20627 : decl = ovl_make (decl, NULL_TREE);
14824 : 20627 : OVL_USING_P (decl) = true;
14825 : 20627 : OVL_PURVIEW_P (decl) = true;
14826 : 20627 : if (flags & WMB_Export)
14827 : 19767 : OVL_EXPORT_P (decl) = true;
14828 : : }
14829 : :
14830 : 163029 : entity_kind ek = EK_FOR_BINDING;
14831 : 163029 : if (internal_decl)
14832 : : ek = EK_TU_LOCAL;
14833 : 162942 : else if (flags & WMB_Using)
14834 : 20627 : ek = EK_USING;
14835 : :
14836 : 163029 : depset *dep = data->hash->make_dependency (decl, ek);
14837 : 163029 : if (flags & WMB_Hidden)
14838 : 5187 : dep->set_hidden_binding ();
14839 : 163029 : data->binding->deps.safe_push (dep);
14840 : : /* Binding and contents are mutually dependent. */
14841 : 163029 : dep->deps.safe_push (data->binding);
14842 : :
14843 : 163029 : return (flags & WMB_Using
14844 : 163029 : ? flags & WMB_Export : DECL_MODULE_EXPORT_P (decl));
14845 : : }
14846 : 7646 : else if (!data->met_namespace)
14847 : : {
14848 : : /* Namespace, walk exactly once. */
14849 : 7637 : data->met_namespace = true;
14850 : 7637 : if (data->hash->add_namespace_entities (decl, data->partitions))
14851 : : {
14852 : : /* It contains an exported thing, so it is exported. */
14853 : 1519 : gcc_checking_assert (DECL_MODULE_PURVIEW_P (decl));
14854 : 1519 : gcc_checking_assert (TREE_PUBLIC (decl) || header_module_p ());
14855 : 1519 : DECL_MODULE_EXPORT_P (decl) = true;
14856 : : }
14857 : :
14858 : 7637 : if (DECL_MODULE_PURVIEW_P (decl))
14859 : : {
14860 : 1856 : data->hash->make_dependency (decl, depset::EK_NAMESPACE);
14861 : :
14862 : 1856 : return DECL_MODULE_EXPORT_P (decl);
14863 : : }
14864 : : }
14865 : :
14866 : : return false;
14867 : : }
14868 : :
14869 : : /* Recursively find all the namespace bindings of NS. Add a depset
14870 : : for every binding that contains an export or module-linkage entity.
14871 : : Add a defining depset for every such decl that we need to write a
14872 : : definition. Such defining depsets depend on the binding depset.
14873 : : Returns true if we contain something exported. */
14874 : :
14875 : : bool
14876 : 10246 : depset::hash::add_namespace_entities (tree ns, bitmap partitions)
14877 : : {
14878 : 11330 : dump () && dump ("Looking for writables in %N", ns);
14879 : 10246 : dump.indent ();
14880 : :
14881 : 10246 : unsigned count = 0;
14882 : 10246 : add_binding_data data;
14883 : 10246 : data.ns = ns;
14884 : 10246 : data.partitions = partitions;
14885 : 10246 : data.hash = this;
14886 : :
14887 : 14113510 : for (tree binding : *DECL_NAMESPACE_BINDINGS (ns))
14888 : : {
14889 : 7051632 : data.binding = nullptr;
14890 : 7051632 : data.met_namespace = false;
14891 : 7051632 : if (walk_module_binding (binding, partitions, add_binding_entity, &data))
14892 : 121404 : count++;
14893 : : }
14894 : :
14895 : : /* Seed any using-directives so that we emit the relevant namespaces. */
14896 : 10822 : for (tree udir : NAMESPACE_LEVEL (ns)->using_directives)
14897 : 198 : if (TREE_CODE (udir) == USING_DECL && DECL_MODULE_PURVIEW_P (udir))
14898 : : {
14899 : 162 : make_dependency (USING_DECL_DECLS (udir), depset::EK_NAMESPACE);
14900 : 162 : if (DECL_MODULE_EXPORT_P (udir))
14901 : 94 : count++;
14902 : : }
14903 : :
14904 : 10246 : if (count)
14905 : 3709 : dump () && dump ("Found %u entries", count);
14906 : 10246 : dump.outdent ();
14907 : :
14908 : 10246 : return count != 0;
14909 : : }
14910 : :
14911 : : void
14912 : 203 : depset::hash::add_partial_entities (vec<tree, va_gc> *partial_classes)
14913 : : {
14914 : 17041 : for (unsigned ix = 0; ix != partial_classes->length (); ix++)
14915 : : {
14916 : 16838 : tree inner = (*partial_classes)[ix];
14917 : :
14918 : 16838 : depset *dep = make_dependency (inner, depset::EK_DECL);
14919 : :
14920 : 16838 : if (dep->get_entity_kind () == depset::EK_REDIRECT)
14921 : : {
14922 : 16838 : dep = dep->deps[0];
14923 : : /* We should have recorded the template as a partial
14924 : : specialization. */
14925 : 16838 : gcc_checking_assert (dep->get_entity_kind ()
14926 : : == depset::EK_PARTIAL);
14927 : :
14928 : : /* Only emit GM entities if reached. */
14929 : 16838 : if (!DECL_LANG_SPECIFIC (inner)
14930 : 28739 : || !DECL_MODULE_PURVIEW_P (inner))
14931 : 5541 : dep->set_flag_bit<DB_UNREACHED_BIT> ();
14932 : : }
14933 : : else
14934 : : {
14935 : : /* It was an explicit specialization, not a partial one.
14936 : : We should have already added this. */
14937 : 0 : gcc_checking_assert (dep->get_entity_kind ()
14938 : : == depset::EK_SPECIALIZATION);
14939 : 0 : gcc_checking_assert (dep->is_special ());
14940 : : }
14941 : : }
14942 : 203 : }
14943 : :
14944 : : /* Add the members of imported classes that we defined in this TU.
14945 : : This will also include lazily created implicit member function
14946 : : declarations. (All others will be definitions.) */
14947 : :
14948 : : void
14949 : 12 : depset::hash::add_class_entities (vec<tree, va_gc> *class_members)
14950 : : {
14951 : 24 : for (unsigned ix = 0; ix != class_members->length (); ix++)
14952 : : {
14953 : 12 : tree defn = (*class_members)[ix];
14954 : 12 : depset *dep = make_dependency (defn, EK_INNER_DECL);
14955 : :
14956 : 12 : if (dep->get_entity_kind () == EK_REDIRECT)
14957 : 0 : dep = dep->deps[0];
14958 : :
14959 : : /* Only non-instantiations need marking as pendings. */
14960 : 24 : if (dep->get_entity_kind () == EK_DECL)
14961 : 12 : dep->set_flag_bit <DB_IS_PENDING_BIT> ();
14962 : : }
14963 : 12 : }
14964 : :
14965 : : /* We add the partial & explicit specializations, and the explicit
14966 : : instantiations. */
14967 : :
14968 : : static void
14969 : 724589 : specialization_add (bool decl_p, spec_entry *entry, void *data_)
14970 : : {
14971 : 724589 : vec<spec_entry *> *data = reinterpret_cast <vec<spec_entry *> *> (data_);
14972 : :
14973 : 724589 : if (!decl_p)
14974 : : {
14975 : : /* We exclusively use decls to locate things. Make sure there's
14976 : : no mismatch between the two specialization tables we keep.
14977 : : pt.cc optimizes instantiation lookup using a complicated
14978 : : heuristic. We don't attempt to replicate that algorithm, but
14979 : : observe its behaviour and reproduce it upon read back. */
14980 : :
14981 : 225092 : gcc_checking_assert (TREE_CODE (entry->spec) == ENUMERAL_TYPE
14982 : : || DECL_CLASS_TEMPLATE_P (entry->tmpl));
14983 : :
14984 : 225092 : gcc_checking_assert (!match_mergeable_specialization (true, entry));
14985 : : }
14986 : 499497 : else if (VAR_OR_FUNCTION_DECL_P (entry->spec))
14987 : 245447 : gcc_checking_assert (!DECL_LOCAL_DECL_P (entry->spec));
14988 : :
14989 : 724589 : data->safe_push (entry);
14990 : 724589 : }
14991 : :
14992 : : /* Arbitrary stable comparison. */
14993 : :
14994 : : static int
14995 : 43340001 : specialization_cmp (const void *a_, const void *b_)
14996 : : {
14997 : 43340001 : const spec_entry *ea = *reinterpret_cast<const spec_entry *const *> (a_);
14998 : 43340001 : const spec_entry *eb = *reinterpret_cast<const spec_entry *const *> (b_);
14999 : :
15000 : 43340001 : if (ea == eb)
15001 : : return 0;
15002 : :
15003 : 43340001 : tree a = ea->spec;
15004 : 43340001 : tree b = eb->spec;
15005 : 43340001 : if (TYPE_P (a))
15006 : : {
15007 : 12778720 : a = TYPE_NAME (a);
15008 : 12778720 : b = TYPE_NAME (b);
15009 : : }
15010 : :
15011 : 43340001 : if (a == b)
15012 : : /* This can happen with friend specializations. Just order by
15013 : : entry address. See note in depset_cmp. */
15014 : 170 : return ea < eb ? -1 : +1;
15015 : :
15016 : 43339880 : return DECL_UID (a) < DECL_UID (b) ? -1 : +1;
15017 : : }
15018 : :
15019 : : /* We add all kinds of specialializations. Implicit specializations
15020 : : should only streamed and walked if they are reachable from
15021 : : elsewhere. Hence the UNREACHED flag. This is making the
15022 : : assumption that it is cheaper to reinstantiate them on demand
15023 : : elsewhere, rather than stream them in when we instantiate their
15024 : : general template. Also, if we do stream them, we can only do that
15025 : : if they are not internal (which they can become if they themselves
15026 : : touch an internal entity?). */
15027 : :
15028 : : void
15029 : 5218 : depset::hash::add_specializations (bool decl_p)
15030 : : {
15031 : 5218 : vec<spec_entry *> data;
15032 : 5218 : data.create (100);
15033 : 5218 : walk_specializations (decl_p, specialization_add, &data);
15034 : 5218 : data.qsort (specialization_cmp);
15035 : 729807 : while (data.length ())
15036 : : {
15037 : 724589 : spec_entry *entry = data.pop ();
15038 : 724589 : tree spec = entry->spec;
15039 : 724589 : int use_tpl = 0;
15040 : 724589 : bool is_friend = false;
15041 : :
15042 : 724589 : if (decl_p && DECL_UNINSTANTIATED_TEMPLATE_FRIEND_P (entry->tmpl))
15043 : : /* A friend of a template. This is keyed to the
15044 : : instantiation. */
15045 : : is_friend = true;
15046 : :
15047 : 724589 : if (decl_p)
15048 : : {
15049 : 499497 : if (tree ti = DECL_TEMPLATE_INFO (spec))
15050 : : {
15051 : 499497 : tree tmpl = TI_TEMPLATE (ti);
15052 : :
15053 : 499497 : use_tpl = DECL_USE_TEMPLATE (spec);
15054 : 499497 : if (spec == DECL_TEMPLATE_RESULT (tmpl))
15055 : : {
15056 : 3562 : spec = tmpl;
15057 : 3562 : gcc_checking_assert (DECL_USE_TEMPLATE (spec) == use_tpl);
15058 : : }
15059 : 495935 : else if (is_friend)
15060 : : {
15061 : 3371 : if (TI_TEMPLATE (ti) != entry->tmpl
15062 : 3371 : || !template_args_equal (TI_ARGS (ti), entry->tmpl))
15063 : 3371 : goto template_friend;
15064 : : }
15065 : : }
15066 : : else
15067 : : {
15068 : 0 : template_friend:;
15069 : 3371 : gcc_checking_assert (is_friend);
15070 : : /* This is a friend of a template class, but not the one
15071 : : that generated entry->spec itself (i.e. it's an
15072 : : equivalent clone). We do not need to record
15073 : : this. */
15074 : 3371 : continue;
15075 : : }
15076 : : }
15077 : : else
15078 : : {
15079 : 225092 : if (TREE_CODE (spec) == ENUMERAL_TYPE)
15080 : : {
15081 : 1135 : tree ctx = DECL_CONTEXT (TYPE_NAME (spec));
15082 : :
15083 : 1135 : if (TYPE_P (ctx))
15084 : 1129 : use_tpl = CLASSTYPE_USE_TEMPLATE (ctx);
15085 : : else
15086 : 6 : use_tpl = DECL_USE_TEMPLATE (ctx);
15087 : : }
15088 : : else
15089 : 223957 : use_tpl = CLASSTYPE_USE_TEMPLATE (spec);
15090 : :
15091 : 225092 : tree ti = TYPE_TEMPLATE_INFO (spec);
15092 : 225092 : tree tmpl = TI_TEMPLATE (ti);
15093 : :
15094 : 225092 : spec = TYPE_NAME (spec);
15095 : 225092 : if (spec == DECL_TEMPLATE_RESULT (tmpl))
15096 : : {
15097 : 1017 : spec = tmpl;
15098 : 1017 : use_tpl = DECL_USE_TEMPLATE (spec);
15099 : : }
15100 : : }
15101 : :
15102 : 721218 : bool needs_reaching = false;
15103 : 721218 : if (use_tpl == 1)
15104 : : /* Implicit instantiations only walked if we reach them. */
15105 : : needs_reaching = true;
15106 : 64772 : else if (!DECL_LANG_SPECIFIC (STRIP_TEMPLATE (spec))
15107 : 118780 : || !DECL_MODULE_PURVIEW_P (STRIP_TEMPLATE (spec)))
15108 : : /* Likewise, GMF explicit or partial specializations. */
15109 : : needs_reaching = true;
15110 : :
15111 : : #if false && CHECKING_P
15112 : : /* The instantiation isn't always on
15113 : : DECL_TEMPLATE_INSTANTIATIONS, */
15114 : : // FIXME: we probably need to remember this information?
15115 : : /* Verify the specialization is on the
15116 : : DECL_TEMPLATE_INSTANTIATIONS of the template. */
15117 : : for (tree cons = DECL_TEMPLATE_INSTANTIATIONS (entry->tmpl);
15118 : : cons; cons = TREE_CHAIN (cons))
15119 : : if (TREE_VALUE (cons) == entry->spec)
15120 : : {
15121 : : gcc_assert (entry->args == TREE_PURPOSE (cons));
15122 : : goto have_spec;
15123 : : }
15124 : : gcc_unreachable ();
15125 : : have_spec:;
15126 : : #endif
15127 : :
15128 : : /* Make sure nobody left a tree visited lying about. */
15129 : 721218 : gcc_checking_assert (!TREE_VISITED (spec));
15130 : 721218 : depset *dep = make_dependency (spec, depset::EK_SPECIALIZATION);
15131 : 721218 : if (dep->is_special ())
15132 : 0 : gcc_unreachable ();
15133 : : else
15134 : : {
15135 : 721218 : if (dep->get_entity_kind () == depset::EK_REDIRECT)
15136 : 18960 : dep = dep->deps[0];
15137 : 702258 : else if (dep->get_entity_kind () == depset::EK_SPECIALIZATION)
15138 : : {
15139 : 702258 : dep->set_special ();
15140 : 702258 : dep->deps.safe_push (reinterpret_cast<depset *> (entry));
15141 : 702258 : if (!decl_p)
15142 : 209018 : dep->set_flag_bit<DB_TYPE_SPEC_BIT> ();
15143 : : }
15144 : :
15145 : 721218 : if (needs_reaching)
15146 : 677982 : dep->set_flag_bit<DB_UNREACHED_BIT> ();
15147 : 721218 : if (is_friend)
15148 : 0 : dep->set_flag_bit<DB_FRIEND_SPEC_BIT> ();
15149 : : }
15150 : : }
15151 : 5218 : data.release ();
15152 : 5218 : }
15153 : :
15154 : : /* Add a depset into the mergeable hash. */
15155 : :
15156 : : void
15157 : 858105 : depset::hash::add_mergeable (depset *mergeable)
15158 : : {
15159 : 858105 : gcc_checking_assert (is_key_order ());
15160 : 858105 : entity_kind ek = mergeable->get_entity_kind ();
15161 : 858105 : tree decl = mergeable->get_entity ();
15162 : 858105 : gcc_checking_assert (ek < EK_DIRECT_HWM);
15163 : :
15164 : 858105 : depset **slot = entity_slot (decl, true);
15165 : 858105 : gcc_checking_assert (!*slot);
15166 : 858105 : depset *dep = make_entity (decl, ek);
15167 : 858105 : *slot = dep;
15168 : :
15169 : 858105 : worklist.safe_push (dep);
15170 : :
15171 : : /* So we can locate the mergeable depset this depset refers to,
15172 : : mark the first dep. */
15173 : 858105 : dep->set_special ();
15174 : 858105 : dep->deps.safe_push (mergeable);
15175 : 858105 : }
15176 : :
15177 : : /* Find the innermost-namespace scope of DECL, and that
15178 : : namespace-scope decl. */
15179 : :
15180 : : tree
15181 : 27754086 : find_pending_key (tree decl, tree *decl_p = nullptr)
15182 : : {
15183 : 27754086 : tree ns = decl;
15184 : 33280185 : do
15185 : : {
15186 : 33280185 : decl = ns;
15187 : 33280185 : ns = CP_DECL_CONTEXT (ns);
15188 : 33280185 : if (TYPE_P (ns))
15189 : 3482343 : ns = TYPE_NAME (ns);
15190 : : }
15191 : 33280185 : while (TREE_CODE (ns) != NAMESPACE_DECL);
15192 : :
15193 : 27754086 : if (decl_p)
15194 : 27370838 : *decl_p = decl;
15195 : :
15196 : 27754086 : return ns;
15197 : : }
15198 : :
15199 : : /* Creates bindings and dependencies for all deduction guides of
15200 : : the given class template DECL as needed. */
15201 : :
15202 : : void
15203 : 42902 : depset::hash::add_deduction_guides (tree decl)
15204 : : {
15205 : : /* Alias templates never have deduction guides. */
15206 : 42902 : if (DECL_ALIAS_TEMPLATE_P (decl))
15207 : 42231 : return;
15208 : :
15209 : : /* We don't need to do anything for class-scope deduction guides,
15210 : : as they will be added as members anyway. */
15211 : 42902 : if (!DECL_NAMESPACE_SCOPE_P (decl))
15212 : : return;
15213 : :
15214 : 33947 : tree ns = CP_DECL_CONTEXT (decl);
15215 : 33947 : tree name = dguide_name (decl);
15216 : :
15217 : : /* We always add all deduction guides with a given name at once,
15218 : : so if there's already a binding there's nothing to do. */
15219 : 33947 : if (find_binding (ns, name))
15220 : : return;
15221 : :
15222 : 32080 : tree guides = lookup_qualified_name (ns, name, LOOK_want::NORMAL,
15223 : : /*complain=*/false);
15224 : 32080 : if (guides == error_mark_node)
15225 : : return;
15226 : :
15227 : 671 : depset *binding = nullptr;
15228 : 3035 : for (tree t : lkp_range (guides))
15229 : : {
15230 : 1693 : gcc_checking_assert (!TREE_VISITED (t));
15231 : 1693 : depset *dep = make_dependency (t, EK_FOR_BINDING);
15232 : :
15233 : : /* We don't want to create bindings for imported deduction guides, as
15234 : : this would potentially cause name lookup to return duplicates. */
15235 : 1693 : if (dep->is_import ())
15236 : 6 : continue;
15237 : :
15238 : 1687 : if (!binding)
15239 : : {
15240 : : /* We have bindings to add. */
15241 : 665 : binding = make_binding (ns, name);
15242 : 665 : add_namespace_context (binding, ns);
15243 : :
15244 : 665 : depset **slot = binding_slot (ns, name, /*insert=*/true);
15245 : 665 : *slot = binding;
15246 : : }
15247 : :
15248 : 1687 : binding->deps.safe_push (dep);
15249 : 1687 : dep->deps.safe_push (binding);
15250 : 1687 : dump (dumper::DEPEND)
15251 : 0 : && dump ("Built binding for deduction guide %C:%N",
15252 : 0 : TREE_CODE (decl), decl);
15253 : : }
15254 : : }
15255 : :
15256 : : /* Iteratively find dependencies. During the walk we may find more
15257 : : entries on the same binding that need walking. */
15258 : :
15259 : : void
15260 : 243563 : depset::hash::find_dependencies (module_state *module)
15261 : : {
15262 : 243563 : trees_out walker (NULL, module, *this);
15263 : 243563 : vec<depset *> unreached;
15264 : 487126 : unreached.create (worklist.length ());
15265 : :
15266 : 1084 : for (;;)
15267 : : {
15268 : 244647 : reached_unreached = false;
15269 : 3779033 : while (worklist.length ())
15270 : : {
15271 : 3534386 : depset *item = worklist.pop ();
15272 : :
15273 : 3534386 : gcc_checking_assert (!item->is_binding ());
15274 : 3534386 : if (item->is_unreached ())
15275 : 1792295 : unreached.quick_push (item);
15276 : : else
15277 : : {
15278 : 1742091 : current = item;
15279 : 1742091 : tree decl = current->get_entity ();
15280 : 1742091 : dump (is_key_order () ? dumper::MERGE : dumper::DEPEND)
15281 : 1743144 : && dump ("Dependencies of %s %C:%N",
15282 : 1053 : is_key_order () ? "key-order"
15283 : 1053 : : current->entity_kind_name (), TREE_CODE (decl), decl);
15284 : 1742091 : dump.indent ();
15285 : 1742091 : walker.begin ();
15286 : 1742091 : if (current->get_entity_kind () == EK_USING)
15287 : 20627 : walker.tree_node (OVL_FUNCTION (decl));
15288 : 1721464 : else if (current->get_entity_kind () == EK_TU_LOCAL)
15289 : : /* We only stream its name and location. */
15290 : 87 : module->note_location (DECL_SOURCE_LOCATION (decl));
15291 : 1721377 : else if (TREE_VISITED (decl))
15292 : : /* A global tree. */;
15293 : 1719007 : else if (current->get_entity_kind () == EK_NAMESPACE)
15294 : : {
15295 : 2225 : module->note_location (DECL_SOURCE_LOCATION (decl));
15296 : 2225 : add_namespace_context (current, CP_DECL_CONTEXT (decl));
15297 : : }
15298 : : else
15299 : : {
15300 : 1716782 : walker.mark_declaration (decl, current->has_defn ());
15301 : :
15302 : 1716782 : if (!is_key_order ()
15303 : 1716782 : && item->is_pending_entity ())
15304 : : {
15305 : 383248 : tree ns = find_pending_key (decl, nullptr);
15306 : 383248 : add_namespace_context (item, ns);
15307 : : }
15308 : :
15309 : 1716782 : walker.decl_value (decl, current);
15310 : 1716782 : if (current->has_defn ())
15311 : 333206 : walker.write_definition (decl, current->refs_tu_local ());
15312 : : }
15313 : 1742091 : walker.end ();
15314 : :
15315 : : /* If we see either a class template or a deduction guide, make
15316 : : sure to add all visible deduction guides. We need to check
15317 : : both in case they have been added in separate modules, or
15318 : : one is in the GMF and would have otherwise been discarded. */
15319 : 1742091 : if (!is_key_order ()
15320 : 1742091 : && DECL_CLASS_TEMPLATE_P (decl))
15321 : 41209 : add_deduction_guides (decl);
15322 : 1742091 : if (!is_key_order ()
15323 : 1742091 : && deduction_guide_p (decl))
15324 : 1693 : add_deduction_guides (TYPE_NAME (TREE_TYPE (TREE_TYPE (decl))));
15325 : :
15326 : 1742091 : if (!is_key_order ()
15327 : 883986 : && TREE_CODE (decl) == TEMPLATE_DECL
15328 : 2046747 : && !DECL_UNINSTANTIATED_TEMPLATE_FRIEND_P (decl))
15329 : : {
15330 : : /* Mark all the explicit & partial specializations as
15331 : : reachable. We search both specialization lists as some
15332 : : constrained partial specializations for class types are
15333 : : only found in DECL_TEMPLATE_SPECIALIZATIONS. */
15334 : 864513 : auto mark_reached = [this](tree spec)
15335 : : {
15336 : 565490 : if (TYPE_P (spec))
15337 : 171067 : spec = TYPE_NAME (spec);
15338 : 565490 : int use_tpl;
15339 : 565490 : node_template_info (spec, use_tpl);
15340 : 565490 : if (use_tpl & 2)
15341 : : {
15342 : 64275 : depset *spec_dep = find_dependency (spec);
15343 : 64275 : if (spec_dep->get_entity_kind () == EK_REDIRECT)
15344 : 14548 : spec_dep = spec_dep->deps[0];
15345 : 64275 : if (spec_dep->is_unreached ())
15346 : : {
15347 : 5357 : reached_unreached = true;
15348 : 5357 : spec_dep->clear_flag_bit<DB_UNREACHED_BIT> ();
15349 : 5357 : dump (dumper::DEPEND)
15350 : 0 : && dump ("Reaching unreached specialization"
15351 : 0 : " %C:%N", TREE_CODE (spec), spec);
15352 : : }
15353 : : }
15354 : 864513 : };
15355 : :
15356 : 299023 : for (tree cons = DECL_TEMPLATE_INSTANTIATIONS (decl);
15357 : 849307 : cons; cons = TREE_CHAIN (cons))
15358 : 550284 : mark_reached (TREE_VALUE (cons));
15359 : 299023 : for (tree cons = DECL_TEMPLATE_SPECIALIZATIONS (decl);
15360 : 314229 : cons; cons = TREE_CHAIN (cons))
15361 : 15206 : mark_reached (TREE_VALUE (cons));
15362 : : }
15363 : :
15364 : 1742091 : dump.outdent ();
15365 : 1742091 : current = NULL;
15366 : : }
15367 : : }
15368 : :
15369 : 244647 : if (!reached_unreached)
15370 : : break;
15371 : :
15372 : : /* It's possible the we reached the unreached before we
15373 : : processed it in the above loop, so we'll be doing this an
15374 : : extra time. However, to avoid that we have to do some
15375 : : bit shuffling that also involves a scan of the list.
15376 : : Swings & roundabouts I guess. */
15377 : 1084 : std::swap (worklist, unreached);
15378 : : }
15379 : :
15380 : 243563 : unreached.release ();
15381 : 243563 : }
15382 : :
15383 : : /* Compare two entries of a single binding. TYPE_DECL before
15384 : : non-exported before exported. */
15385 : :
15386 : : static int
15387 : 530042 : binding_cmp (const void *a_, const void *b_)
15388 : : {
15389 : 530042 : depset *a = *(depset *const *)a_;
15390 : 530042 : depset *b = *(depset *const *)b_;
15391 : :
15392 : 530042 : tree a_ent = a->get_entity ();
15393 : 530042 : tree b_ent = b->get_entity ();
15394 : 530042 : gcc_checking_assert (a_ent != b_ent
15395 : : && !a->is_binding ()
15396 : : && !b->is_binding ());
15397 : :
15398 : : /* Implicit typedefs come first. */
15399 : 530042 : bool a_implicit = DECL_IMPLICIT_TYPEDEF_P (a_ent);
15400 : 530042 : bool b_implicit = DECL_IMPLICIT_TYPEDEF_P (b_ent);
15401 : 529924 : if (a_implicit || b_implicit)
15402 : : {
15403 : : /* A binding with two implicit type decls? That's unpossible! */
15404 : 236 : gcc_checking_assert (!(a_implicit && b_implicit));
15405 : 354 : return a_implicit ? -1 : +1; /* Implicit first. */
15406 : : }
15407 : :
15408 : : /* TU-local before non-TU-local. */
15409 : 529806 : bool a_internal = a->get_entity_kind () == depset::EK_TU_LOCAL;
15410 : 529806 : bool b_internal = b->get_entity_kind () == depset::EK_TU_LOCAL;
15411 : 529806 : if (a_internal != b_internal)
15412 : 0 : return a_internal ? -1 : +1; /* Internal first. */
15413 : :
15414 : : /* Hidden before non-hidden. */
15415 : 529806 : bool a_hidden = a->is_hidden ();
15416 : 529806 : bool b_hidden = b->is_hidden ();
15417 : 529806 : if (a_hidden != b_hidden)
15418 : 54266 : return a_hidden ? -1 : +1;
15419 : :
15420 : 494236 : bool a_using = a->get_entity_kind () == depset::EK_USING;
15421 : 494236 : bool a_export;
15422 : 494236 : if (a_using)
15423 : : {
15424 : 70456 : a_export = OVL_EXPORT_P (a_ent);
15425 : 70456 : a_ent = OVL_FUNCTION (a_ent);
15426 : : }
15427 : 423780 : else if (TREE_CODE (a_ent) == CONST_DECL
15428 : 0 : && DECL_LANG_SPECIFIC (a_ent)
15429 : 423780 : && DECL_MODULE_EXPORT_P (a_ent))
15430 : : a_export = true;
15431 : : else
15432 : 423780 : a_export = DECL_MODULE_EXPORT_P (TREE_CODE (a_ent) == CONST_DECL
15433 : : ? TYPE_NAME (TREE_TYPE (a_ent))
15434 : : : STRIP_TEMPLATE (a_ent));
15435 : :
15436 : 494236 : bool b_using = b->get_entity_kind () == depset::EK_USING;
15437 : 494236 : bool b_export;
15438 : 494236 : if (b_using)
15439 : : {
15440 : 66644 : b_export = OVL_EXPORT_P (b_ent);
15441 : 66644 : b_ent = OVL_FUNCTION (b_ent);
15442 : : }
15443 : 427592 : else if (TREE_CODE (b_ent) == CONST_DECL
15444 : 0 : && DECL_LANG_SPECIFIC (b_ent)
15445 : 427592 : && DECL_MODULE_EXPORT_P (b_ent))
15446 : : b_export = true;
15447 : : else
15448 : 427592 : b_export = DECL_MODULE_EXPORT_P (TREE_CODE (b_ent) == CONST_DECL
15449 : : ? TYPE_NAME (TREE_TYPE (b_ent))
15450 : : : STRIP_TEMPLATE (b_ent));
15451 : :
15452 : : /* Non-exports before exports. */
15453 : 494236 : if (a_export != b_export)
15454 : 360 : return a_export ? +1 : -1;
15455 : :
15456 : : /* At this point we don't care, but want a stable sort. */
15457 : :
15458 : 494026 : if (a_using != b_using)
15459 : : /* using first. */
15460 : 23996 : return a_using? -1 : +1;
15461 : :
15462 : 476731 : return DECL_UID (a_ent) < DECL_UID (b_ent) ? -1 : +1;
15463 : : }
15464 : :
15465 : : /* True iff TMPL has an explicit instantiation definition.
15466 : :
15467 : : This is local to module.cc because register_specialization skips adding most
15468 : : instantiations unless module_maybe_has_cmi_p. */
15469 : :
15470 : : static bool
15471 : 73 : template_has_explicit_inst (tree tmpl)
15472 : : {
15473 : 85 : for (tree t = DECL_TEMPLATE_INSTANTIATIONS (tmpl); t; t = TREE_CHAIN (t))
15474 : : {
15475 : 24 : tree spec = TREE_VALUE (t);
15476 : 24 : if (DECL_EXPLICIT_INSTANTIATION (spec)
15477 : 24 : && !DECL_REALLY_EXTERN (spec))
15478 : : return true;
15479 : : }
15480 : : return false;
15481 : : }
15482 : :
15483 : : /* Complain about DEP that exposes a TU-local entity.
15484 : :
15485 : : If STRICT, DEP only referenced entities from the GMF. Returns TRUE
15486 : : if we explained anything. */
15487 : :
15488 : : bool
15489 : 127 : depset::hash::diagnose_bad_internal_ref (depset *dep, bool strict)
15490 : : {
15491 : 127 : tree decl = dep->get_entity ();
15492 : :
15493 : : /* Don't need to walk if we're not going to be emitting
15494 : : any diagnostics anyway. */
15495 : 148 : if (strict && !warning_enabled_at (DECL_SOURCE_LOCATION (decl),
15496 : 21 : OPT_Wexpose_global_module_tu_local))
15497 : : return false;
15498 : :
15499 : 520 : for (depset *rdep : dep->deps)
15500 : 132 : if (!rdep->is_binding () && rdep->is_tu_local (strict)
15501 : 366 : && !is_exposure_of_member_type (dep, rdep))
15502 : : {
15503 : : // FIXME:QOI Better location information? We're
15504 : : // losing, so it doesn't matter about efficiency.
15505 : 118 : tree exposed = rdep->get_entity ();
15506 : 118 : auto_diagnostic_group d;
15507 : 118 : if (strict)
15508 : : {
15509 : : /* Allow suppressing the warning from the point of declaration
15510 : : of the otherwise-exposed decl, for cases we know that
15511 : : exposures will never be 'bad'. */
15512 : 27 : if (warning_enabled_at (DECL_SOURCE_LOCATION (exposed),
15513 : 27 : OPT_Wexpose_global_module_tu_local)
15514 : 45 : && pedwarn (DECL_SOURCE_LOCATION (decl),
15515 : 18 : OPT_Wexpose_global_module_tu_local,
15516 : : "%qD exposes TU-local entity %qD", decl, exposed))
15517 : : {
15518 : 18 : bool informed = is_tu_local_entity (exposed, /*explain=*/true);
15519 : 18 : gcc_checking_assert (informed);
15520 : : return true;
15521 : : }
15522 : : }
15523 : : else
15524 : : {
15525 : 91 : error_at (DECL_SOURCE_LOCATION (decl),
15526 : : "%qD exposes TU-local entity %qD", decl, exposed);
15527 : 91 : bool informed = is_tu_local_entity (exposed, /*explain=*/true);
15528 : 91 : gcc_checking_assert (informed);
15529 : 91 : if (dep->is_tu_local (/*strict=*/true))
15530 : 3 : inform (DECL_SOURCE_LOCATION (decl),
15531 : : "%qD is also TU-local but has been exposed elsewhere",
15532 : : decl);
15533 : 91 : return true;
15534 : : }
15535 : 118 : }
15536 : :
15537 : : return false;
15538 : : }
15539 : :
15540 : : /* Warn about a template DEP that references a TU-local entity.
15541 : :
15542 : : If STRICT, DEP only referenced entities from the GMF. Returns TRUE
15543 : : if we explained anything. */
15544 : :
15545 : : bool
15546 : 91 : depset::hash::diagnose_template_names_tu_local (depset *dep, bool strict)
15547 : : {
15548 : 91 : tree decl = dep->get_entity ();
15549 : :
15550 : : /* Don't bother walking if we know we won't be emitting anything. */
15551 : 91 : if (!warning_enabled_at (DECL_SOURCE_LOCATION (decl),
15552 : 91 : OPT_Wtemplate_names_tu_local)
15553 : : /* Only warn strictly if users haven't silenced this warning here. */
15554 : 118 : || (strict && !warning_enabled_at (DECL_SOURCE_LOCATION (decl),
15555 : 27 : OPT_Wexpose_global_module_tu_local)))
15556 : 0 : return false;
15557 : :
15558 : : /* Friend decls in a class body are ignored, but this is harmless:
15559 : : it should not impact any consumers. */
15560 : 91 : if (RECORD_OR_UNION_TYPE_P (TREE_TYPE (decl)))
15561 : : return false;
15562 : :
15563 : : /* We should now only be warning about templates. */
15564 : 73 : gcc_checking_assert
15565 : : (TREE_CODE (decl) == TEMPLATE_DECL
15566 : : && VAR_OR_FUNCTION_DECL_P (DECL_TEMPLATE_RESULT (decl)));
15567 : :
15568 : : /* Don't warn if we've seen any explicit instantiation definitions,
15569 : : the intent might be for importers to only use those. */
15570 : 73 : if (template_has_explicit_inst (decl))
15571 : : return false;
15572 : :
15573 : 256 : for (depset *rdep : dep->deps)
15574 : 128 : if (!rdep->is_binding () && rdep->is_tu_local (strict))
15575 : : {
15576 : 64 : tree ref = rdep->get_entity ();
15577 : 64 : auto_diagnostic_group d;
15578 : 64 : if (strict)
15579 : : {
15580 : 15 : if (warning_enabled_at (DECL_SOURCE_LOCATION (ref),
15581 : 15 : OPT_Wexpose_global_module_tu_local)
15582 : 21 : && warning_at (DECL_SOURCE_LOCATION (decl),
15583 : 6 : OPT_Wtemplate_names_tu_local,
15584 : : "%qD refers to TU-local entity %qD, which may "
15585 : : "cause issues when instantiating in other TUs",
15586 : : decl, ref))
15587 : : {
15588 : 6 : is_tu_local_entity (ref, /*explain=*/true);
15589 : 6 : return true;
15590 : : }
15591 : : }
15592 : 49 : else if (warning_at (DECL_SOURCE_LOCATION (decl),
15593 : 49 : OPT_Wtemplate_names_tu_local,
15594 : : "%qD refers to TU-local entity %qD and cannot "
15595 : : "be instantiated in other TUs", decl, ref))
15596 : : {
15597 : 49 : is_tu_local_entity (ref, /*explain=*/true);
15598 : 49 : return true;
15599 : : }
15600 : 64 : }
15601 : :
15602 : : return false;
15603 : : }
15604 : :
15605 : : /* Sort the bindings, issue errors about bad internal refs. */
15606 : :
15607 : : bool
15608 : 2609 : depset::hash::finalize_dependencies ()
15609 : : {
15610 : 2609 : bool ok = true;
15611 : 2814409 : for (depset *dep : *this)
15612 : : {
15613 : 1405900 : if (dep->is_binding ())
15614 : : {
15615 : : /* Keep the containing namespace dep first. */
15616 : 131112 : gcc_checking_assert (dep->deps.length () > 1
15617 : : && (dep->deps[0]->get_entity_kind ()
15618 : : == EK_NAMESPACE)
15619 : : && (dep->deps[0]->get_entity ()
15620 : : == dep->get_entity ()));
15621 : 131112 : if (dep->deps.length () > 2)
15622 : 10137 : gcc_qsort (&dep->deps[1], dep->deps.length () - 1,
15623 : : sizeof (dep->deps[1]), binding_cmp);
15624 : :
15625 : : /* Bindings shouldn't refer to imported entities. */
15626 : 131112 : if (CHECKING_P)
15627 : 692281 : for (depset *entity : dep->deps)
15628 : 298945 : gcc_checking_assert (!entity->is_import ());
15629 : 131112 : continue;
15630 : 131112 : }
15631 : :
15632 : : /* Otherwise, we'll check for bad internal refs.
15633 : : Don't complain about any references from TU-local entities. */
15634 : 1274788 : if (dep->is_tu_local ())
15635 : 267 : continue;
15636 : :
15637 : 1274521 : if (dep->is_exposure ())
15638 : : {
15639 : 106 : bool explained = diagnose_bad_internal_ref (dep);
15640 : :
15641 : : /* A TU-local variable will always be considered an exposure,
15642 : : so we don't have to worry about strict-only handling. */
15643 : 106 : tree decl = dep->get_entity ();
15644 : 106 : if (!explained
15645 : 15 : && VAR_P (decl)
15646 : 121 : && (DECL_DECLARED_CONSTEXPR_P (decl)
15647 : 6 : || DECL_INLINE_VAR_P (decl)))
15648 : : {
15649 : 15 : auto_diagnostic_group d;
15650 : 15 : if (DECL_DECLARED_CONSTEXPR_P (decl))
15651 : 9 : error_at (DECL_SOURCE_LOCATION (decl),
15652 : : "%qD is declared %<constexpr%> and is initialized to "
15653 : : "a TU-local value", decl);
15654 : : else
15655 : : {
15656 : : /* This can only occur with references. */
15657 : 6 : gcc_checking_assert (TYPE_REF_P (TREE_TYPE (decl)));
15658 : 6 : error_at (DECL_SOURCE_LOCATION (decl),
15659 : : "%qD is a reference declared %<inline%> and is "
15660 : : "constant-initialized to a TU-local value", decl);
15661 : : }
15662 : 15 : bool informed = is_tu_local_value (decl, DECL_INITIAL (decl),
15663 : : /*explain=*/true);
15664 : 15 : gcc_checking_assert (informed);
15665 : 15 : explained = true;
15666 : 15 : }
15667 : :
15668 : : /* We should have emitted an error above, unless the warning was
15669 : : silenced. */
15670 : 106 : gcc_checking_assert (explained);
15671 : 106 : ok = false;
15672 : 106 : continue;
15673 : 106 : }
15674 : :
15675 : : /* In all other cases, we're just warning (rather than erroring).
15676 : : We don't want to do too much warning, so let's just bail after
15677 : : the first warning we successfully emit. */
15678 : 1274433 : if (warn_expose_global_module_tu_local
15679 : 1274415 : && !dep->is_tu_local (/*strict=*/true)
15680 : 1274385 : && dep->is_exposure (/*strict=*/true)
15681 : 1274436 : && diagnose_bad_internal_ref (dep, /*strict=*/true))
15682 : 18 : continue;
15683 : :
15684 : 1274446 : if (warn_template_names_tu_local
15685 : 84812 : && dep->refs_tu_local ()
15686 : 1274461 : && diagnose_template_names_tu_local (dep))
15687 : 49 : continue;
15688 : :
15689 : 1274348 : if (warn_template_names_tu_local
15690 : 84763 : && warn_expose_global_module_tu_local
15691 : 84763 : && !dep->is_tu_local (/*strict=*/true)
15692 : 84739 : && dep->refs_tu_local (/*strict=*/true)
15693 : 30 : && !dep->is_exposure (/*strict=*/true)
15694 : 1274375 : && diagnose_template_names_tu_local (dep, /*strict=*/true))
15695 : : continue;
15696 : : }
15697 : :
15698 : 2609 : return ok;
15699 : : }
15700 : :
15701 : : /* Core of TARJAN's algorithm to find Strongly Connected Components
15702 : : within a graph. See https://en.wikipedia.org/wiki/
15703 : : Tarjan%27s_strongly_connected_components_algorithm for details.
15704 : :
15705 : : We use depset::section as lowlink. Completed nodes have
15706 : : depset::cluster containing the cluster number, with the top
15707 : : bit set.
15708 : :
15709 : : A useful property is that the output vector is a reverse
15710 : : topological sort of the resulting DAG. In our case that means
15711 : : dependent SCCs are found before their dependers. We make use of
15712 : : that property. */
15713 : :
15714 : : void
15715 : 1872234 : depset::tarjan::connect (depset *v)
15716 : : {
15717 : 1872234 : gcc_checking_assert (v->is_binding ()
15718 : : || !(v->is_tu_local ()
15719 : : || v->is_unreached ()
15720 : : || v->is_import ()));
15721 : :
15722 : 1872234 : v->cluster = v->section = ++index;
15723 : 1872234 : stack.safe_push (v);
15724 : :
15725 : : /* Walk all our dependencies, ignore a first marked slot */
15726 : 15601212 : for (unsigned ix = v->is_special (); ix != v->deps.length (); ix++)
15727 : : {
15728 : 5932314 : depset *dep = v->deps[ix];
15729 : :
15730 : 5932314 : if (dep->is_binding ()
15731 : 11697094 : || !(dep->is_import () || dep->is_tu_local ()))
15732 : : {
15733 : 5923223 : unsigned lwm = dep->cluster;
15734 : :
15735 : 5923223 : if (!dep->cluster)
15736 : : {
15737 : : /* A new node. Connect it. */
15738 : 1035818 : connect (dep);
15739 : 1035818 : lwm = dep->section;
15740 : : }
15741 : :
15742 : 5923223 : if (dep->section && v->section > lwm)
15743 : 946482 : v->section = lwm;
15744 : : }
15745 : : }
15746 : :
15747 : 1872234 : if (v->section == v->cluster)
15748 : : {
15749 : : /* Root of a new SCC. Push all the members onto the result list. */
15750 : : unsigned num = v->cluster;
15751 : 1872234 : depset *p;
15752 : 1872234 : do
15753 : : {
15754 : 1872234 : p = stack.pop ();
15755 : 1872234 : p->cluster = num;
15756 : 1872234 : p->section = 0;
15757 : 1872234 : result.quick_push (p);
15758 : : }
15759 : 1872234 : while (p != v);
15760 : : }
15761 : 1872234 : }
15762 : :
15763 : : /* Compare two depsets. The specific ordering is unimportant, we're
15764 : : just trying to get consistency. */
15765 : :
15766 : : static int
15767 : 88120678 : depset_cmp (const void *a_, const void *b_)
15768 : : {
15769 : 88120678 : depset *a = *(depset *const *)a_;
15770 : 88120678 : depset *b = *(depset *const *)b_;
15771 : :
15772 : 88120678 : depset::entity_kind a_kind = a->get_entity_kind ();
15773 : 88120678 : depset::entity_kind b_kind = b->get_entity_kind ();
15774 : :
15775 : 88120678 : if (a_kind != b_kind)
15776 : : /* Different entity kinds, order by that. */
15777 : 4479204 : return a_kind < b_kind ? -1 : +1;
15778 : :
15779 : 84940371 : tree a_decl = a->get_entity ();
15780 : 84940371 : tree b_decl = b->get_entity ();
15781 : 84940371 : if (a_kind == depset::EK_USING)
15782 : : {
15783 : : /* If one is a using, the other must be too. */
15784 : 1159213 : a_decl = OVL_FUNCTION (a_decl);
15785 : 1159213 : b_decl = OVL_FUNCTION (b_decl);
15786 : : }
15787 : :
15788 : 84940371 : if (a_decl != b_decl)
15789 : : /* Different entities, order by their UID. */
15790 : 78501956 : return DECL_UID (a_decl) < DECL_UID (b_decl) ? -1 : +1;
15791 : :
15792 : 6438415 : if (a_kind == depset::EK_BINDING)
15793 : : {
15794 : : /* Both are bindings. Order by identifier hash. */
15795 : 6435389 : gcc_checking_assert (a->get_name () != b->get_name ());
15796 : 6435389 : hashval_t ah = IDENTIFIER_HASH_VALUE (a->get_name ());
15797 : 6435389 : hashval_t bh = IDENTIFIER_HASH_VALUE (b->get_name ());
15798 : 9587724 : return (ah == bh ? 0 : ah < bh ? -1 : +1);
15799 : : }
15800 : :
15801 : : /* They are the same decl. This can happen with two using decls
15802 : : pointing to the same target. The best we can aim for is
15803 : : consistently telling qsort how to order them. Hopefully we'll
15804 : : never have to debug a case that depends on this. Oh, who am I
15805 : : kidding? Good luck. */
15806 : 3026 : gcc_checking_assert (a_kind == depset::EK_USING);
15807 : :
15808 : : /* Order by depset address. Not the best, but it is something. */
15809 : 3026 : return a < b ? -1 : +1;
15810 : : }
15811 : :
15812 : : /* Sort the clusters in SCC such that those that depend on one another
15813 : : are placed later. */
15814 : :
15815 : : // FIXME: I am not convinced this is needed and, if needed,
15816 : : // sufficient. We emit the decls in this order but that emission
15817 : : // could walk into later decls (from the body of the decl, or default
15818 : : // arg-like things). Why doesn't that walk do the right thing? And
15819 : : // if it DTRT why do we need to sort here -- won't things naturally
15820 : : // work? I think part of the issue is that when we're going to refer
15821 : : // to an entity by name, and that entity is in the same cluster as us,
15822 : : // we need to actually walk that entity, if we've not already walked
15823 : : // it.
15824 : : static void
15825 : 240954 : sort_cluster (depset::hash *original, depset *scc[], unsigned size)
15826 : : {
15827 : 240954 : depset::hash table (size, original);
15828 : :
15829 : 240954 : dump.indent ();
15830 : :
15831 : : /* Place bindings last, usings before that. It's not strictly
15832 : : necessary, but it does make things neater. Says Mr OCD. */
15833 : : unsigned bind_lwm = size;
15834 : : unsigned use_lwm = size;
15835 : 1250541 : for (unsigned ix = 0; ix != use_lwm;)
15836 : : {
15837 : 1009587 : depset *dep = scc[ix];
15838 : 1009587 : switch (dep->get_entity_kind ())
15839 : : {
15840 : 130858 : case depset::EK_BINDING:
15841 : : /* Move to end. No increment. Notice this could be moving
15842 : : a using decl, which we'll then move again. */
15843 : 130858 : if (--bind_lwm != ix)
15844 : : {
15845 : 70248 : scc[ix] = scc[bind_lwm];
15846 : 70248 : scc[bind_lwm] = dep;
15847 : : }
15848 : 130858 : if (use_lwm > bind_lwm)
15849 : : {
15850 : 107057 : use_lwm--;
15851 : 107057 : break;
15852 : : }
15853 : : /* We must have copied a using or TU-local, so move it too. */
15854 : 23801 : dep = scc[ix];
15855 : 23801 : gcc_checking_assert
15856 : : (dep->get_entity_kind () == depset::EK_USING
15857 : : || dep->get_entity_kind () == depset::EK_TU_LOCAL);
15858 : : /* FALLTHROUGH */
15859 : :
15860 : 44425 : case depset::EK_USING:
15861 : 44425 : case depset::EK_TU_LOCAL:
15862 : 44425 : if (--use_lwm != ix)
15863 : : {
15864 : 31529 : scc[ix] = scc[use_lwm];
15865 : 31529 : scc[use_lwm] = dep;
15866 : : }
15867 : : break;
15868 : :
15869 : 858105 : case depset::EK_DECL:
15870 : 858105 : case depset::EK_SPECIALIZATION:
15871 : 858105 : case depset::EK_PARTIAL:
15872 : 858105 : table.add_mergeable (dep);
15873 : 858105 : ix++;
15874 : 858105 : break;
15875 : :
15876 : 0 : default:
15877 : 0 : gcc_unreachable ();
15878 : : }
15879 : : }
15880 : :
15881 : 240954 : gcc_checking_assert (use_lwm <= bind_lwm);
15882 : 241218 : dump (dumper::MERGE) && dump ("Ordering %u/%u depsets", use_lwm, size);
15883 : :
15884 : 240954 : table.find_dependencies (nullptr);
15885 : :
15886 : 481908 : auto_vec<depset *> order = table.connect ();
15887 : 481908 : gcc_checking_assert (order.length () == use_lwm);
15888 : :
15889 : : /* Now rewrite entries [0,lwm), in the dependency order we
15890 : : discovered. Usually each entity is in its own cluster. Rarely,
15891 : : we can get multi-entity clusters, in which case all but one must
15892 : : only be reached from within the cluster. This happens for
15893 : : something like:
15894 : :
15895 : : template<typename T>
15896 : : auto Foo (const T &arg) -> TPL<decltype (arg)>;
15897 : :
15898 : : The instantiation of TPL will be in the specialization table, and
15899 : : refer to Foo via arg. But we can only get to that specialization
15900 : : from Foo's declaration, so we only need to treat Foo as mergable
15901 : : (We'll do structural comparison of TPL<decltype (arg)>).
15902 : :
15903 : : We approximate finding the single cluster entry dep by checking for
15904 : : entities recursively depending on a dep first seen when streaming
15905 : : its own merge key; the first dep we see in such a cluster should be
15906 : : the first one streamed. */
15907 : : unsigned entry_pos = ~0u;
15908 : : unsigned cluster = ~0u;
15909 : 2198118 : for (unsigned ix = 0; ix != order.length (); ix++)
15910 : : {
15911 : 858105 : gcc_checking_assert (order[ix]->is_special ());
15912 : 858105 : bool tight = order[ix]->cluster == cluster;
15913 : 858105 : depset *dep = order[ix]->deps[0];
15914 : 858828 : dump (dumper::MERGE)
15915 : 1443 : && dump ("Mergeable %u is %N%s%s", ix, dep->get_entity (),
15916 : 723 : tight ? " (tight)" : "", dep->is_entry () ? " (entry)" : "");
15917 : 858105 : scc[ix] = dep;
15918 : 858105 : if (tight)
15919 : : {
15920 : 96 : gcc_checking_assert (dep->is_maybe_recursive ());
15921 : 96 : if (dep->is_entry ())
15922 : : {
15923 : : /* There should only be one entry dep in a cluster. */
15924 : 6 : gcc_checking_assert (!scc[entry_pos]->is_entry ());
15925 : 6 : gcc_checking_assert (scc[entry_pos]->is_maybe_recursive ());
15926 : 6 : scc[ix] = scc[entry_pos];
15927 : 6 : scc[entry_pos] = dep;
15928 : : }
15929 : : }
15930 : : else
15931 : : entry_pos = ix;
15932 : 858105 : cluster = order[ix]->cluster;
15933 : : }
15934 : :
15935 : 241218 : dump (dumper::MERGE) && dump ("Ordered %u keys", order.length ());
15936 : 240954 : dump.outdent ();
15937 : 240954 : }
15938 : :
15939 : : /* Reduce graph to SCCS clusters. SCCS will be populated with the
15940 : : depsets in dependency order. Each depset's CLUSTER field contains
15941 : : its cluster number. Each SCC has a unique cluster number, and are
15942 : : contiguous in SCCS. Cluster numbers are otherwise arbitrary. */
15943 : :
15944 : : vec<depset *>
15945 : 243534 : depset::hash::connect ()
15946 : : {
15947 : 243534 : tarjan connector (size ());
15948 : 243534 : vec<depset *> deps;
15949 : 243534 : deps.create (size ());
15950 : 4769782 : for (depset *item : *this)
15951 : : {
15952 : 2263124 : entity_kind kind = item->get_entity_kind ();
15953 : 2132266 : if (kind == EK_BINDING
15954 : 2132266 : || !(kind == EK_REDIRECT
15955 : 2112542 : || item->is_tu_local ()
15956 : 2112417 : || item->is_unreached ()
15957 : 1747571 : || item->is_import ()))
15958 : 1872234 : deps.quick_push (item);
15959 : : }
15960 : :
15961 : : /* Iteration over the hash table is an unspecified ordering. While
15962 : : that has advantages, it causes 2 problems. Firstly repeatable
15963 : : builds are tricky. Secondly creating testcases that check
15964 : : dependencies are correct by making sure a bad ordering would
15965 : : happen if that was wrong. */
15966 : 1323484 : deps.qsort (depset_cmp);
15967 : :
15968 : 2115768 : while (deps.length ())
15969 : : {
15970 : 1872234 : depset *v = deps.pop ();
15971 : 1872234 : dump (dumper::CLUSTER) &&
15972 : 1800 : (v->is_binding ()
15973 : 210 : ? dump ("Connecting binding %P", v->get_entity (), v->get_name ())
15974 : 1590 : : dump ("Connecting %s %s %C:%N",
15975 : 1590 : is_key_order () ? "key-order"
15976 : 870 : : !v->has_defn () ? "declaration" : "definition",
15977 : 1590 : v->entity_kind_name (), TREE_CODE (v->get_entity ()),
15978 : : v->get_entity ()));
15979 : 1872234 : if (!v->cluster)
15980 : 836416 : connector.connect (v);
15981 : : }
15982 : :
15983 : 243534 : deps.release ();
15984 : 487068 : return connector.result;
15985 : 243534 : }
15986 : :
15987 : : /* Initialize location spans. */
15988 : :
15989 : : void
15990 : 4650 : loc_spans::init (const line_maps *lmaps, const line_map_ordinary *map)
15991 : : {
15992 : 4650 : gcc_checking_assert (!init_p ());
15993 : 4650 : spans = new vec<span> ();
15994 : 4650 : spans->reserve (20);
15995 : :
15996 : 4650 : span interval;
15997 : 4650 : interval.ordinary.first = 0;
15998 : 4650 : interval.macro.second = MAX_LOCATION_T + 1;
15999 : 4650 : interval.ordinary_delta = interval.macro_delta = 0;
16000 : :
16001 : : /* A span for reserved fixed locs. */
16002 : 4650 : interval.ordinary.second
16003 : 4650 : = MAP_START_LOCATION (LINEMAPS_ORDINARY_MAP_AT (line_table, 0));
16004 : 4650 : interval.macro.first = interval.macro.second;
16005 : 4650 : dump (dumper::LOCATION)
16006 : 42 : && dump ("Fixed span %u ordinary:[%K,%K) macro:[%K,%K)", spans->length (),
16007 : : interval.ordinary.first, interval.ordinary.second,
16008 : : interval.macro.first, interval.macro.second);
16009 : 4650 : spans->quick_push (interval);
16010 : :
16011 : : /* A span for command line & forced headers. */
16012 : 4650 : interval.ordinary.first = interval.ordinary.second;
16013 : 4650 : interval.macro.second = interval.macro.first;
16014 : 4650 : if (map)
16015 : : {
16016 : 4644 : interval.ordinary.second = map->start_location;
16017 : 4644 : interval.macro.first = LINEMAPS_MACRO_LOWEST_LOCATION (lmaps);
16018 : : }
16019 : 4650 : dump (dumper::LOCATION)
16020 : 21 : && dump ("Pre span %u ordinary:[%K,%K) macro:[%K,%K)", spans->length (),
16021 : : interval.ordinary.first, interval.ordinary.second,
16022 : : interval.macro.first, interval.macro.second);
16023 : 4650 : spans->quick_push (interval);
16024 : :
16025 : : /* Start an interval for the main file. */
16026 : 4650 : interval.ordinary.first = interval.ordinary.second;
16027 : 4650 : interval.macro.second = interval.macro.first;
16028 : 4650 : dump (dumper::LOCATION)
16029 : 21 : && dump ("Main span %u ordinary:[%K,*) macro:[*,%K)", spans->length (),
16030 : : interval.ordinary.first, interval.macro.second);
16031 : 4650 : spans->quick_push (interval);
16032 : 4650 : }
16033 : :
16034 : : /* Reopen the span, if we want the about-to-be-inserted set of maps to
16035 : : be propagated in our own location table. I.e. we are the primary
16036 : : interface and we're importing a partition. */
16037 : :
16038 : : bool
16039 : 2886 : loc_spans::maybe_propagate (module_state *import, location_t hwm)
16040 : : {
16041 : 2886 : bool opened = (module_interface_p () && !module_partition_p ()
16042 : 3283 : && import->is_partition ());
16043 : 157 : if (opened)
16044 : 157 : open (hwm);
16045 : 2886 : return opened;
16046 : : }
16047 : :
16048 : : /* Open a new linemap interval. The just-created ordinary map is the
16049 : : first map of the interval. */
16050 : :
16051 : : void
16052 : 1033 : loc_spans::open (location_t hwm)
16053 : : {
16054 : 1033 : span interval;
16055 : 1033 : interval.ordinary.first = interval.ordinary.second = hwm;
16056 : 2066 : interval.macro.first = interval.macro.second
16057 : 1033 : = LINEMAPS_MACRO_LOWEST_LOCATION (line_table);
16058 : 1033 : interval.ordinary_delta = interval.macro_delta = 0;
16059 : 1033 : dump (dumper::LOCATION)
16060 : 0 : && dump ("Opening span %u ordinary:[%K,... macro:...,%K)",
16061 : 0 : spans->length (), interval.ordinary.first,
16062 : : interval.macro.second);
16063 : 1033 : if (spans->length ())
16064 : : {
16065 : : /* No overlapping! */
16066 : 1033 : auto &last = spans->last ();
16067 : 1033 : gcc_checking_assert (interval.ordinary.first >= last.ordinary.second);
16068 : 1033 : gcc_checking_assert (interval.macro.second <= last.macro.first);
16069 : : }
16070 : 1033 : spans->safe_push (interval);
16071 : 1033 : }
16072 : :
16073 : : /* Close out the current linemap interval. The last maps are within
16074 : : the interval. */
16075 : :
16076 : : void
16077 : 5680 : loc_spans::close ()
16078 : : {
16079 : 5680 : span &interval = spans->last ();
16080 : :
16081 : 5680 : interval.ordinary.second
16082 : 5680 : = ((line_table->highest_location
16083 : 5680 : + (loc_one << line_table->default_range_bits))
16084 : 5680 : & ~((loc_one << line_table->default_range_bits) - 1));
16085 : 5680 : interval.macro.first = LINEMAPS_MACRO_LOWEST_LOCATION (line_table);
16086 : 5680 : dump (dumper::LOCATION)
16087 : 21 : && dump ("Closing span %u ordinary:[%K,%K) macro:[%K,%K)",
16088 : 21 : spans->length () - 1,
16089 : : interval.ordinary.first,interval.ordinary.second,
16090 : : interval.macro.first, interval.macro.second);
16091 : 5680 : }
16092 : :
16093 : : /* Given an ordinary location LOC, return the lmap_interval it resides
16094 : : in. NULL if it is not in an interval. */
16095 : :
16096 : : const loc_spans::span *
16097 : 26644647 : loc_spans::ordinary (location_t loc)
16098 : : {
16099 : 26644647 : unsigned len = spans->length ();
16100 : 53161966 : unsigned pos = 0;
16101 : 53171769 : while (len)
16102 : : {
16103 : 53158293 : unsigned half = len / 2;
16104 : 53158293 : const span &probe = (*spans)[pos + half];
16105 : 53158293 : if (loc < probe.ordinary.first)
16106 : : len = half;
16107 : 53148490 : else if (loc < probe.ordinary.second)
16108 : : return &probe;
16109 : : else
16110 : : {
16111 : 26517319 : pos += half + 1;
16112 : 26517319 : len = len - (half + 1);
16113 : : }
16114 : : }
16115 : : return NULL;
16116 : : }
16117 : :
16118 : : /* Likewise, given a macro location LOC, return the lmap interval it
16119 : : resides in. */
16120 : :
16121 : : const loc_spans::span *
16122 : 2349435 : loc_spans::macro (location_t loc)
16123 : : {
16124 : 2349435 : unsigned len = spans->length ();
16125 : 4695812 : unsigned pos = 0;
16126 : 4695830 : while (len)
16127 : : {
16128 : 4695800 : unsigned half = len / 2;
16129 : 4695800 : const span &probe = (*spans)[pos + half];
16130 : 4695800 : if (loc >= probe.macro.second)
16131 : : len = half;
16132 : 4695782 : else if (loc >= probe.macro.first)
16133 : : return &probe;
16134 : : else
16135 : : {
16136 : 2346377 : pos += half + 1;
16137 : 2346377 : len = len - (half + 1);
16138 : : }
16139 : : }
16140 : : return NULL;
16141 : : }
16142 : :
16143 : : /* Return the ordinary location closest to FROM. */
16144 : :
16145 : : static location_t
16146 : 6500 : ordinary_loc_of (line_maps *lmaps, location_t from)
16147 : : {
16148 : 13003 : while (!IS_ORDINARY_LOC (from))
16149 : : {
16150 : 3 : if (IS_ADHOC_LOC (from))
16151 : 3 : from = get_location_from_adhoc_loc (lmaps, from);
16152 : 3 : if (from >= LINEMAPS_MACRO_LOWEST_LOCATION (lmaps))
16153 : : {
16154 : : /* Find the ordinary location nearest FROM. */
16155 : 0 : const line_map *map = linemap_lookup (lmaps, from);
16156 : 0 : const line_map_macro *mac_map = linemap_check_macro (map);
16157 : 0 : from = mac_map->get_expansion_point_location ();
16158 : : }
16159 : : }
16160 : 6500 : return from;
16161 : : }
16162 : :
16163 : : static module_state **
16164 : 11553 : get_module_slot (tree name, module_state *parent, bool partition, bool insert)
16165 : : {
16166 : 11553 : module_state_hash::compare_type ct (name, uintptr_t (parent) | partition);
16167 : 11553 : hashval_t hv = module_state_hash::hash (ct);
16168 : :
16169 : 11553 : return modules_hash->find_slot_with_hash (ct, hv, insert ? INSERT : NO_INSERT);
16170 : : }
16171 : :
16172 : : static module_state *
16173 : 93520 : get_primary (module_state *parent)
16174 : : {
16175 : 97178 : while (parent->is_partition ())
16176 : 488 : parent = parent->parent;
16177 : :
16178 : 96690 : if (!parent->name)
16179 : : // Implementation unit has null name
16180 : 48727 : parent = parent->parent;
16181 : :
16182 : 92611 : return parent;
16183 : : }
16184 : :
16185 : : /* Find or create module NAME & PARENT in the hash table. */
16186 : :
16187 : : module_state *
16188 : 11553 : get_module (tree name, module_state *parent, bool partition)
16189 : : {
16190 : : /* We might be given an empty NAME if preprocessing fails to handle
16191 : : a header-name token. */
16192 : 11553 : if (name && TREE_CODE (name) == STRING_CST
16193 : 14354 : && TREE_STRING_LENGTH (name) == 0)
16194 : : return nullptr;
16195 : :
16196 : 11553 : if (partition)
16197 : : {
16198 : 964 : if (!parent)
16199 : 205 : parent = get_primary (this_module ());
16200 : :
16201 : 964 : if (!parent->is_partition () && !parent->flatname)
16202 : 226 : parent->set_flatname ();
16203 : : }
16204 : :
16205 : 11553 : module_state **slot = get_module_slot (name, parent, partition, true);
16206 : 11553 : module_state *state = *slot;
16207 : 11553 : if (!state)
16208 : : {
16209 : 6276 : state = (new (ggc_alloc<module_state> ())
16210 : 6276 : module_state (name, parent, partition));
16211 : 6276 : *slot = state;
16212 : : }
16213 : : return state;
16214 : : }
16215 : :
16216 : : /* Process string name PTR into a module_state. */
16217 : :
16218 : : static module_state *
16219 : 410 : get_module (const char *ptr)
16220 : : {
16221 : : /* On DOS based file systems, there is an ambiguity with A:B which can be
16222 : : interpreted as a module Module:Partition or Drive:PATH. Interpret strings
16223 : : which clearly starts as pathnames as header-names and everything else is
16224 : : treated as a (possibly malformed) named moduled. */
16225 : 410 : if (IS_DIR_SEPARATOR (ptr[ptr[0] == '.']) // ./FOO or /FOO
16226 : : #if HAVE_DOS_BASED_FILE_SYSTEM
16227 : : || (HAS_DRIVE_SPEC (ptr) && IS_DIR_SEPARATOR (ptr[2])) // A:/FOO
16228 : : #endif
16229 : : || false)
16230 : : /* A header name. */
16231 : 110 : return get_module (build_string (strlen (ptr), ptr));
16232 : :
16233 : : bool partition = false;
16234 : : module_state *mod = NULL;
16235 : :
16236 : 1210 : for (const char *probe = ptr;; probe++)
16237 : 1510 : if (!*probe || *probe == '.' || *probe == ':')
16238 : : {
16239 : 381 : if (probe == ptr)
16240 : : return NULL;
16241 : :
16242 : 381 : mod = get_module (get_identifier_with_length (ptr, probe - ptr),
16243 : : mod, partition);
16244 : 381 : ptr = probe;
16245 : 381 : if (*ptr == ':')
16246 : : {
16247 : 78 : if (partition)
16248 : : return NULL;
16249 : : partition = true;
16250 : : }
16251 : :
16252 : 381 : if (!*ptr++)
16253 : : break;
16254 : : }
16255 : 1129 : else if (!(ISALPHA (*probe) || *probe == '_'
16256 : 18 : || (probe != ptr && ISDIGIT (*probe))))
16257 : : return NULL;
16258 : :
16259 : : return mod;
16260 : : }
16261 : :
16262 : : /* Create a new mapper connecting to OPTION. */
16263 : :
16264 : : module_client *
16265 : 4650 : make_mapper (location_t loc, class mkdeps *deps)
16266 : : {
16267 : 4650 : timevar_start (TV_MODULE_MAPPER);
16268 : 4650 : const char *option = module_mapper_name;
16269 : 4650 : if (!option)
16270 : 4608 : option = getenv ("CXX_MODULE_MAPPER");
16271 : :
16272 : 9300 : mapper = module_client::open_module_client
16273 : 4650 : (loc, option, deps, &set_cmi_repo,
16274 : 4650 : (save_decoded_options[0].opt_index == OPT_SPECIAL_program_name)
16275 : 4650 : && save_decoded_options[0].arg != progname
16276 : : ? save_decoded_options[0].arg : nullptr);
16277 : :
16278 : 4650 : timevar_stop (TV_MODULE_MAPPER);
16279 : :
16280 : 4650 : return mapper;
16281 : : }
16282 : :
16283 : : static unsigned lazy_snum;
16284 : :
16285 : : static bool
16286 : 10536 : recursive_lazy (unsigned snum = ~0u)
16287 : : {
16288 : 10536 : if (lazy_snum)
16289 : : {
16290 : 0 : error_at (input_location, "recursive lazy load");
16291 : 0 : return true;
16292 : : }
16293 : :
16294 : 10536 : lazy_snum = snum;
16295 : 10536 : return false;
16296 : : }
16297 : :
16298 : : /* If THIS has an interface dependency on itself, report an error and
16299 : : return false. */
16300 : :
16301 : : bool
16302 : 2725 : module_state::check_circular_import (location_t from)
16303 : : {
16304 : 2725 : if (this == this_module ())
16305 : : {
16306 : : /* Cannot import the current module. */
16307 : 9 : auto_diagnostic_group d;
16308 : 9 : error_at (from, "module %qs depends on itself", get_flatname ());
16309 : 9 : if (!header_module_p ())
16310 : 6 : inform (loc, "module %qs declared here", get_flatname ());
16311 : 9 : return false;
16312 : 9 : }
16313 : : return true;
16314 : : }
16315 : :
16316 : : /* Module name substitutions. */
16317 : : static vec<module_state *,va_heap> substs;
16318 : :
16319 : : void
16320 : 7926 : module_state::mangle (bool include_partition)
16321 : : {
16322 : 7926 : if (subst)
16323 : 245 : mangle_module_substitution (subst);
16324 : : else
16325 : : {
16326 : 7681 : if (parent)
16327 : 736 : parent->mangle (include_partition);
16328 : 7681 : if (include_partition || !is_partition ())
16329 : : {
16330 : : // Partitions are significant for global initializer
16331 : : // functions
16332 : 7583 : bool partition = is_partition () && !parent->is_partition ();
16333 : 7583 : subst = mangle_module_component (name, partition);
16334 : 7583 : substs.safe_push (this);
16335 : : }
16336 : : }
16337 : 7926 : }
16338 : :
16339 : : void
16340 : 7190 : mangle_module (int mod, bool include_partition)
16341 : : {
16342 : 7190 : module_state *imp = (*modules)[mod];
16343 : :
16344 : 7190 : gcc_checking_assert (!imp->is_header ());
16345 : :
16346 : 7190 : if (!imp->name)
16347 : : /* Set when importing the primary module interface. */
16348 : 241 : imp = imp->parent;
16349 : :
16350 : : /* Ensure this is actually a module unit. */
16351 : 241 : gcc_checking_assert (imp);
16352 : :
16353 : 7190 : imp->mangle (include_partition);
16354 : 7190 : }
16355 : :
16356 : : /* Clean up substitutions. */
16357 : : void
16358 : 6902 : mangle_module_fini ()
16359 : : {
16360 : 14485 : while (substs.length ())
16361 : 7583 : substs.pop ()->subst = 0;
16362 : 6902 : }
16363 : :
16364 : : /* Announce WHAT about the module. */
16365 : :
16366 : : void
16367 : 11929 : module_state::announce (const char *what) const
16368 : : {
16369 : 11929 : if (noisy_p ())
16370 : : {
16371 : 0 : fprintf (stderr, " %s:%s", what, get_flatname ());
16372 : 0 : fflush (stderr);
16373 : : }
16374 : 11929 : }
16375 : :
16376 : : /* A human-readable README section. The contents of this section to
16377 : : not contribute to the CRC, so the contents can change per
16378 : : compilation. That allows us to embed CWD, hostname, build time and
16379 : : what not. It is a STRTAB that may be extracted with:
16380 : : readelf -pgnu.c++.README $(module).gcm */
16381 : :
16382 : : void
16383 : 2580 : module_state::write_readme (elf_out *to, cpp_reader *reader, const char *dialect)
16384 : : {
16385 : 2580 : bytes_out readme (to);
16386 : :
16387 : 2580 : readme.begin (false);
16388 : :
16389 : 2580 : readme.printf ("GNU C++ %s",
16390 : 2580 : is_header () ? "header unit"
16391 : 1703 : : !is_partition () ? "primary interface"
16392 : 178 : : is_interface () ? "interface partition"
16393 : : : "internal partition");
16394 : :
16395 : : /* Compiler's version. */
16396 : 2580 : readme.printf ("compiler: %s", version_string);
16397 : :
16398 : : /* Module format version. */
16399 : 2580 : verstr_t string;
16400 : 2580 : version2string (MODULE_VERSION, string);
16401 : 2580 : readme.printf ("version: %s", string);
16402 : :
16403 : : /* Module information. */
16404 : 2580 : readme.printf ("module: %s", get_flatname ());
16405 : 2580 : readme.printf ("source: %s", main_input_filename);
16406 : 2580 : readme.printf ("dialect: %s", dialect);
16407 : 2580 : if (extensions)
16408 : 30 : readme.printf ("extensions: %s%s%s",
16409 : : extensions & SE_OPENMP ? "-fopenmp"
16410 : 6 : : extensions & SE_OPENMP_SIMD ? "-fopenmp-simd" : "",
16411 : : (extensions & SE_OPENACC)
16412 : 3 : && (extensions & (SE_OPENMP | SE_OPENMP_SIMD))
16413 : : ? " " : "",
16414 : 12 : extensions & SE_OPENACC ? "-fopenacc" : "");
16415 : :
16416 : : /* The following fields could be expected to change between
16417 : : otherwise identical compilations. Consider a distributed build
16418 : : system. We should have a way of overriding that. */
16419 : 2580 : if (char *cwd = getcwd (NULL, 0))
16420 : : {
16421 : 2580 : readme.printf ("cwd: %s", cwd);
16422 : 2580 : free (cwd);
16423 : : }
16424 : 5160 : readme.printf ("repository: %s", cmi_repo ? cmi_repo : ".");
16425 : : #if NETWORKING
16426 : : {
16427 : : char hostname[64];
16428 : : if (!gethostname (hostname, sizeof (hostname)))
16429 : : readme.printf ("host: %s", hostname);
16430 : : }
16431 : : #endif
16432 : 2580 : {
16433 : : /* This of course will change! */
16434 : 2580 : time_t stampy;
16435 : 2580 : auto kind = cpp_get_date (reader, &stampy);
16436 : 2580 : if (kind != CPP_time_kind::UNKNOWN)
16437 : : {
16438 : 2580 : struct tm *time;
16439 : :
16440 : 2580 : time = gmtime (&stampy);
16441 : 2580 : readme.print_time ("build", time, "UTC");
16442 : :
16443 : 2580 : if (kind == CPP_time_kind::DYNAMIC)
16444 : : {
16445 : 2580 : time = localtime (&stampy);
16446 : 2580 : readme.print_time ("local", time,
16447 : : #if defined (__USE_MISC) || defined (__USE_BSD) /* Is there a better way? */
16448 : : time->tm_zone
16449 : : #else
16450 : : ""
16451 : : #endif
16452 : : );
16453 : : }
16454 : : }
16455 : : }
16456 : :
16457 : : /* Its direct imports. */
16458 : 3190 : for (unsigned ix = 1; ix < modules->length (); ix++)
16459 : : {
16460 : 610 : module_state *state = (*modules)[ix];
16461 : :
16462 : 610 : if (state->is_direct ())
16463 : 906 : readme.printf ("%s: %s %s", state->exported_p ? "export" : "import",
16464 : : state->get_flatname (), state->filename);
16465 : : }
16466 : :
16467 : 2580 : readme.end (to, to->name (MOD_SNAME_PFX ".README"), NULL);
16468 : 2580 : }
16469 : :
16470 : : /* Sort environment var names in reverse order. */
16471 : :
16472 : : static int
16473 : 0 : env_var_cmp (const void *a_, const void *b_)
16474 : : {
16475 : 0 : const unsigned char *a = *(const unsigned char *const *)a_;
16476 : 0 : const unsigned char *b = *(const unsigned char *const *)b_;
16477 : :
16478 : 0 : for (unsigned ix = 0; ; ix++)
16479 : : {
16480 : 0 : bool a_end = !a[ix] || a[ix] == '=';
16481 : 0 : if (a[ix] == b[ix])
16482 : : {
16483 : 0 : if (a_end)
16484 : : break;
16485 : : }
16486 : : else
16487 : : {
16488 : 0 : bool b_end = !b[ix] || b[ix] == '=';
16489 : :
16490 : 0 : if (!a_end && !b_end)
16491 : 0 : return a[ix] < b[ix] ? +1 : -1;
16492 : 0 : if (a_end && b_end)
16493 : : break;
16494 : 0 : return a_end ? +1 : -1;
16495 : : }
16496 : 0 : }
16497 : :
16498 : : return 0;
16499 : : }
16500 : :
16501 : : /* Write the environment. It is a STRTAB that may be extracted with:
16502 : : readelf -pgnu.c++.ENV $(module).gcm */
16503 : :
16504 : : void
16505 : 0 : module_state::write_env (elf_out *to)
16506 : : {
16507 : 0 : vec<const char *> vars;
16508 : 0 : vars.create (20);
16509 : :
16510 : 0 : extern char **environ;
16511 : 0 : while (const char *var = environ[vars.length ()])
16512 : 0 : vars.safe_push (var);
16513 : 0 : vars.qsort (env_var_cmp);
16514 : :
16515 : 0 : bytes_out env (to);
16516 : 0 : env.begin (false);
16517 : 0 : while (vars.length ())
16518 : 0 : env.printf ("%s", vars.pop ());
16519 : 0 : env.end (to, to->name (MOD_SNAME_PFX ".ENV"), NULL);
16520 : :
16521 : 0 : vars.release ();
16522 : 0 : }
16523 : :
16524 : : /* Write the direct or indirect imports.
16525 : : u:N
16526 : : {
16527 : : u:index
16528 : : s:name
16529 : : u32:crc
16530 : : s:filename (direct)
16531 : : u:exported (direct)
16532 : : } imports[N]
16533 : : */
16534 : :
16535 : : void
16536 : 826 : module_state::write_imports (bytes_out &sec, bool direct)
16537 : : {
16538 : 826 : unsigned count = 0;
16539 : :
16540 : 1744 : for (unsigned ix = 1; ix < modules->length (); ix++)
16541 : : {
16542 : 918 : module_state *imp = (*modules)[ix];
16543 : :
16544 : 918 : if (imp->remap && imp->is_direct () == direct)
16545 : 441 : count++;
16546 : : }
16547 : :
16548 : 826 : gcc_assert (!direct || count);
16549 : :
16550 : 826 : sec.u (count);
16551 : 1744 : for (unsigned ix = 1; ix < modules->length (); ix++)
16552 : : {
16553 : 918 : module_state *imp = (*modules)[ix];
16554 : :
16555 : 918 : if (imp->remap && imp->is_direct () == direct)
16556 : : {
16557 : 597 : dump () && dump ("Writing %simport:%u->%u %M (crc=%x)",
16558 : : !direct ? "indirect "
16559 : 78 : : imp->exported_p ? "exported " : "",
16560 : : ix, imp->remap, imp, imp->crc);
16561 : 441 : sec.u (imp->remap);
16562 : 441 : sec.str (imp->get_flatname ());
16563 : 441 : sec.u32 (imp->crc);
16564 : 441 : if (direct)
16565 : : {
16566 : 432 : write_location (sec, imp->imported_from ());
16567 : 432 : sec.str (imp->filename);
16568 : 432 : int exportedness = 0;
16569 : 432 : if (imp->exported_p)
16570 : : exportedness = +1;
16571 : 264 : else if (!imp->is_purview_direct ())
16572 : 13 : exportedness = -1;
16573 : 432 : sec.i (exportedness);
16574 : : }
16575 : : }
16576 : : }
16577 : 826 : }
16578 : :
16579 : : /* READER, LMAPS != NULL == direct imports,
16580 : : == NUL == indirect imports. */
16581 : :
16582 : : unsigned
16583 : 704 : module_state::read_imports (bytes_in &sec, cpp_reader *reader, line_maps *lmaps)
16584 : : {
16585 : 704 : unsigned count = sec.u ();
16586 : 704 : unsigned loaded = 0;
16587 : :
16588 : 1779 : while (count--)
16589 : : {
16590 : 371 : unsigned ix = sec.u ();
16591 : 371 : if (ix >= slurp->remap->length () || !ix || (*slurp->remap)[ix])
16592 : : {
16593 : 0 : sec.set_overrun ();
16594 : 0 : break;
16595 : : }
16596 : :
16597 : 371 : const char *name = sec.str (NULL);
16598 : 371 : module_state *imp = get_module (name);
16599 : 371 : unsigned crc = sec.u32 ();
16600 : 371 : int exportedness = 0;
16601 : :
16602 : : /* If the import is a partition, it must be the same primary
16603 : : module as this TU. */
16604 : 371 : if (imp && imp->is_partition () &&
16605 : : (!named_module_p ()
16606 : 135 : || (get_primary (this_module ()) != get_primary (imp))))
16607 : : imp = NULL;
16608 : :
16609 : 371 : if (!imp)
16610 : 0 : sec.set_overrun ();
16611 : 371 : if (sec.get_overrun ())
16612 : : break;
16613 : :
16614 : 371 : if (lmaps)
16615 : : {
16616 : : /* A direct import, maybe load it. */
16617 : 367 : location_t floc = read_location (sec);
16618 : 367 : const char *fname = sec.str (NULL);
16619 : 367 : exportedness = sec.i ();
16620 : :
16621 : 367 : if (sec.get_overrun ())
16622 : : break;
16623 : :
16624 : 367 : if (!imp->check_circular_import (floc))
16625 : 3 : continue;
16626 : :
16627 : 364 : if (imp->loadedness == ML_NONE)
16628 : : {
16629 : 288 : imp->loc = floc;
16630 : 288 : imp->crc = crc;
16631 : 288 : if (!imp->get_flatname ())
16632 : 246 : imp->set_flatname ();
16633 : :
16634 : 288 : unsigned n = dump.push (imp);
16635 : :
16636 : 288 : if (!imp->filename && fname)
16637 : 246 : imp->filename = xstrdup (fname);
16638 : :
16639 : 288 : if (imp->is_partition ())
16640 : 33 : dump () && dump ("Importing elided partition %M", imp);
16641 : :
16642 : 288 : if (!imp->do_import (reader, false))
16643 : 3 : imp = NULL;
16644 : 288 : dump.pop (n);
16645 : 288 : if (!imp)
16646 : 3 : continue;
16647 : : }
16648 : :
16649 : 361 : if (is_partition ())
16650 : : {
16651 : 63 : if (!imp->is_direct ())
16652 : 33 : imp->directness = MD_PARTITION_DIRECT;
16653 : 63 : if (exportedness > 0)
16654 : 3 : imp->exported_p = true;
16655 : : }
16656 : : }
16657 : : else
16658 : : {
16659 : : /* An indirect import, find it, it should already be here. */
16660 : 4 : if (imp->loadedness == ML_NONE)
16661 : : {
16662 : 0 : error_at (loc, "indirect import %qs is not already loaded", name);
16663 : 0 : continue;
16664 : : }
16665 : : }
16666 : :
16667 : 365 : if (imp->crc != crc)
16668 : 0 : error_at (loc, "import %qs has CRC mismatch", imp->get_flatname ());
16669 : :
16670 : 365 : (*slurp->remap)[ix] = (imp->mod << 1) | (lmaps != NULL);
16671 : :
16672 : 365 : if (lmaps && exportedness >= 0)
16673 : 347 : set_import (imp, bool (exportedness));
16674 : 527 : dump () && dump ("Found %simport:%u %M->%u", !lmaps ? "indirect "
16675 : 81 : : exportedness > 0 ? "exported "
16676 : 48 : : exportedness < 0 ? "gmf" : "", ix, imp,
16677 : : imp->mod);
16678 : 365 : loaded++;
16679 : : }
16680 : :
16681 : 704 : return loaded;
16682 : : }
16683 : :
16684 : : /* Write the import table to MOD_SNAME_PFX.imp. */
16685 : :
16686 : : void
16687 : 413 : module_state::write_imports (elf_out *to, unsigned *crc_ptr)
16688 : : {
16689 : 488 : dump () && dump ("Writing imports");
16690 : 413 : dump.indent ();
16691 : :
16692 : 413 : bytes_out sec (to);
16693 : 413 : sec.begin ();
16694 : :
16695 : 413 : write_imports (sec, true);
16696 : 413 : write_imports (sec, false);
16697 : :
16698 : 413 : sec.end (to, to->name (MOD_SNAME_PFX ".imp"), crc_ptr);
16699 : 413 : dump.outdent ();
16700 : 413 : }
16701 : :
16702 : : bool
16703 : 352 : module_state::read_imports (cpp_reader *reader, line_maps *lmaps)
16704 : : {
16705 : 352 : bytes_in sec;
16706 : :
16707 : 352 : if (!sec.begin (loc, from (), MOD_SNAME_PFX ".imp"))
16708 : : return false;
16709 : :
16710 : 430 : dump () && dump ("Reading %u imports", slurp->remap->length () - 1);
16711 : 352 : dump.indent ();
16712 : :
16713 : : /* Read the imports. */
16714 : 352 : unsigned direct = read_imports (sec, reader, lmaps);
16715 : 352 : unsigned indirect = read_imports (sec, NULL, NULL);
16716 : 352 : if (direct + indirect + 1 != slurp->remap->length ())
16717 : 6 : from ()->set_error (elf::E_BAD_IMPORT);
16718 : :
16719 : 352 : dump.outdent ();
16720 : 352 : if (!sec.end (from ()))
16721 : : return false;
16722 : : return true;
16723 : 352 : }
16724 : :
16725 : : /* We're the primary module interface, but have partitions. Document
16726 : : them so that non-partition module implementation units know which
16727 : : have already been loaded. */
16728 : :
16729 : : void
16730 : 121 : module_state::write_partitions (elf_out *to, unsigned count, unsigned *crc_ptr)
16731 : : {
16732 : 142 : dump () && dump ("Writing %u elided partitions", count);
16733 : 121 : dump.indent ();
16734 : :
16735 : 121 : bytes_out sec (to);
16736 : 121 : sec.begin ();
16737 : :
16738 : 308 : for (unsigned ix = 1; ix != modules->length (); ix++)
16739 : : {
16740 : 187 : module_state *imp = (*modules)[ix];
16741 : 187 : if (imp->is_partition ())
16742 : : {
16743 : 202 : dump () && dump ("Writing elided partition %M (crc=%x)",
16744 : : imp, imp->crc);
16745 : 169 : sec.str (imp->get_flatname ());
16746 : 169 : sec.u32 (imp->crc);
16747 : 329 : write_location (sec, imp->is_direct ()
16748 : 160 : ? imp->imported_from () : UNKNOWN_LOCATION);
16749 : 169 : sec.str (imp->filename);
16750 : : }
16751 : : }
16752 : :
16753 : 121 : sec.end (to, to->name (MOD_SNAME_PFX ".prt"), crc_ptr);
16754 : 121 : dump.outdent ();
16755 : 121 : }
16756 : :
16757 : : bool
16758 : 21 : module_state::read_partitions (unsigned count)
16759 : : {
16760 : 21 : bytes_in sec;
16761 : 21 : if (!sec.begin (loc, from (), MOD_SNAME_PFX ".prt"))
16762 : : return false;
16763 : :
16764 : 24 : dump () && dump ("Reading %u elided partitions", count);
16765 : 21 : dump.indent ();
16766 : :
16767 : 54 : while (count--)
16768 : : {
16769 : 33 : const char *name = sec.str (NULL);
16770 : 33 : unsigned crc = sec.u32 ();
16771 : 33 : location_t floc = read_location (sec);
16772 : 33 : const char *fname = sec.str (NULL);
16773 : :
16774 : 33 : if (sec.get_overrun ())
16775 : : break;
16776 : :
16777 : 39 : dump () && dump ("Reading elided partition %s (crc=%x)", name, crc);
16778 : :
16779 : 33 : module_state *imp = get_module (name);
16780 : 33 : if (!imp /* Partition should be ... */
16781 : 33 : || !imp->is_partition () /* a partition ... */
16782 : 33 : || imp->loadedness != ML_NONE /* that is not yet loaded ... */
16783 : 66 : || get_primary (imp) != this) /* whose primary is this. */
16784 : : {
16785 : 0 : sec.set_overrun ();
16786 : 0 : break;
16787 : : }
16788 : :
16789 : 33 : if (!imp->has_location ())
16790 : 30 : imp->loc = floc;
16791 : 33 : imp->crc = crc;
16792 : 33 : if (!imp->filename && fname[0])
16793 : 30 : imp->filename = xstrdup (fname);
16794 : : }
16795 : :
16796 : 21 : dump.outdent ();
16797 : 21 : if (!sec.end (from ()))
16798 : : return false;
16799 : : return true;
16800 : 21 : }
16801 : :
16802 : : /* Data for config reading and writing. */
16803 : : struct module_state_config {
16804 : : const char *dialect_str = get_dialect ();
16805 : : line_map_uint_t ordinary_locs = 0;
16806 : : line_map_uint_t macro_locs = 0;
16807 : : unsigned num_imports = 0;
16808 : : unsigned num_partitions = 0;
16809 : : unsigned num_entities = 0;
16810 : : unsigned loc_range_bits = 0;
16811 : : unsigned active_init = 0;
16812 : :
16813 : 95173 : static void release ()
16814 : : {
16815 : 95173 : XDELETEVEC (dialect);
16816 : 95173 : dialect = NULL;
16817 : : }
16818 : :
16819 : : private:
16820 : : static const char *get_dialect ();
16821 : : static char *dialect;
16822 : : };
16823 : :
16824 : : char *module_state_config::dialect;
16825 : :
16826 : : /* Generate a string of the significant compilation options.
16827 : : Generally assume the user knows what they're doing, in the same way
16828 : : that object files can be mixed. */
16829 : :
16830 : : const char *
16831 : 5587 : module_state_config::get_dialect ()
16832 : : {
16833 : 5587 : if (!dialect)
16834 : 8898 : dialect = concat (get_cxx_dialect_name (cxx_dialect),
16835 : : /* C++ implies these, only show if disabled. */
16836 : 4449 : flag_exceptions ? "" : "/no-exceptions",
16837 : 4449 : flag_rtti ? "" : "/no-rtti",
16838 : 4449 : flag_new_inheriting_ctors ? "" : "/old-inheriting-ctors",
16839 : : /* C++ 20 implies concepts and coroutines. */
16840 : 1417 : cxx_dialect < cxx20 && flag_concepts ? "/concepts" : "",
16841 : 4449 : (cxx_dialect < cxx20 && flag_coroutines
16842 : : ? "/coroutines" : ""),
16843 : 4449 : flag_module_implicit_inline ? "/implicit-inline" : "",
16844 : 4449 : flag_contracts ? "/contracts" : "",
16845 : : NULL);
16846 : :
16847 : 5587 : return dialect;
16848 : : }
16849 : :
16850 : : /* Contents of a cluster. */
16851 : : enum cluster_tag {
16852 : : ct_decl, /* A decl. */
16853 : : ct_defn, /* A definition. */
16854 : : ct_bind, /* A binding. */
16855 : : ct_hwm
16856 : : };
16857 : :
16858 : : /* Binding modifiers. */
16859 : : enum ct_bind_flags
16860 : : {
16861 : : cbf_export = 0x1, /* An exported decl. */
16862 : : cbf_hidden = 0x2, /* A hidden (friend) decl. */
16863 : : cbf_using = 0x4, /* A using decl. */
16864 : : cbf_internal = 0x8, /* A TU-local decl. */
16865 : : };
16866 : :
16867 : : /* DEP belongs to a different cluster, seed it to prevent
16868 : : unfortunately timed duplicate import. */
16869 : : // FIXME: QOI For inter-cluster references we could just only pick
16870 : : // one entity from an earlier cluster. Even better track
16871 : : // dependencies between earlier clusters
16872 : :
16873 : : void
16874 : 5693885 : module_state::intercluster_seed (trees_out &sec, unsigned index_hwm, depset *dep)
16875 : : {
16876 : 5693885 : if (dep->is_tu_local ())
16877 : : /* We only stream placeholders for TU-local entities anyway. */;
16878 : 5693685 : else if (dep->is_import () || dep->cluster < index_hwm)
16879 : : {
16880 : 2525824 : tree ent = dep->get_entity ();
16881 : 2525824 : if (!TREE_VISITED (ent))
16882 : : {
16883 : 1084564 : sec.tree_node (ent);
16884 : 1085014 : dump (dumper::CLUSTER)
16885 : 450 : && dump ("Seeded %s %N",
16886 : 450 : dep->is_import () ? "import" : "intercluster", ent);
16887 : : }
16888 : : }
16889 : 5693885 : }
16890 : :
16891 : : /* Write the cluster of depsets in SCC[0-SIZE).
16892 : : dep->section -> section number
16893 : : dep->cluster -> entity number
16894 : : */
16895 : :
16896 : : unsigned
16897 : 240954 : module_state::write_cluster (elf_out *to, depset *scc[], unsigned size,
16898 : : depset::hash &table, unsigned *counts,
16899 : : unsigned *crc_ptr)
16900 : : {
16901 : 242289 : dump () && dump ("Writing section:%u %u depsets", table.section, size);
16902 : 240954 : dump.indent ();
16903 : :
16904 : 240954 : trees_out sec (to, this, table, table.section);
16905 : 240954 : sec.begin ();
16906 : 240954 : unsigned index_lwm = counts[MSC_entities];
16907 : :
16908 : : /* Determine entity numbers, mark for writing. */
16909 : 241263 : dump (dumper::CLUSTER) && dump ("Cluster members:") && (dump.indent (), true);
16910 : 1250541 : for (unsigned ix = 0; ix != size; ix++)
16911 : : {
16912 : 1009587 : depset *b = scc[ix];
16913 : :
16914 : 1009587 : switch (b->get_entity_kind ())
16915 : : {
16916 : 0 : default:
16917 : 0 : gcc_unreachable ();
16918 : :
16919 : 130858 : case depset::EK_BINDING:
16920 : 130858 : {
16921 : 130858 : dump (dumper::CLUSTER)
16922 : 210 : && dump ("[%u]=%s %P", ix, b->entity_kind_name (),
16923 : : b->get_entity (), b->get_name ());
16924 : 130858 : depset *ns_dep = b->deps[0];
16925 : 130858 : gcc_checking_assert (ns_dep->get_entity_kind ()
16926 : : == depset::EK_NAMESPACE
16927 : : && ns_dep->get_entity () == b->get_entity ());
16928 : 298437 : for (unsigned jx = b->deps.length (); --jx;)
16929 : : {
16930 : 167579 : depset *dep = b->deps[jx];
16931 : : // We could be declaring something that is also a
16932 : : // (merged) import
16933 : 188248 : gcc_checking_assert (dep->is_import ()
16934 : : || TREE_VISITED (dep->get_entity ())
16935 : : || (dep->get_entity_kind ()
16936 : : == depset::EK_USING)
16937 : : || (dep->get_entity_kind ()
16938 : : == depset::EK_TU_LOCAL));
16939 : : }
16940 : : }
16941 : : break;
16942 : :
16943 : 858105 : case depset::EK_DECL:
16944 : 858105 : case depset::EK_SPECIALIZATION:
16945 : 858105 : case depset::EK_PARTIAL:
16946 : 858105 : b->cluster = counts[MSC_entities]++;
16947 : 858105 : sec.mark_declaration (b->get_entity (), b->has_defn ());
16948 : : /* FALLTHROUGH */
16949 : :
16950 : 878729 : case depset::EK_USING:
16951 : 878729 : case depset::EK_TU_LOCAL:
16952 : 1757458 : gcc_checking_assert (!b->is_import ()
16953 : : && !b->is_unreached ());
16954 : 1011924 : dump (dumper::CLUSTER)
16955 : 1161 : && dump ("[%u]=%s %s %N", ix, b->entity_kind_name (),
16956 : 729 : b->has_defn () ? "definition" : "declaration",
16957 : : b->get_entity ());
16958 : : break;
16959 : : }
16960 : : }
16961 : 241263 : dump (dumper::CLUSTER) && (dump.outdent (), true);
16962 : :
16963 : : /* Ensure every out-of-cluster decl is referenced before we start
16964 : : streaming. We must do both imports *and* earlier clusters,
16965 : : because the latter could reach into the former and cause a
16966 : : duplicate loop. */
16967 : 240954 : sec.set_importing (+1);
16968 : 1250541 : for (unsigned ix = 0; ix != size; ix++)
16969 : : {
16970 : 1009587 : depset *b = scc[ix];
16971 : 11899889 : for (unsigned jx = b->is_special (); jx != b->deps.length (); jx++)
16972 : : {
16973 : 4943129 : depset *dep = b->deps[jx];
16974 : :
16975 : 4943129 : if (dep->is_binding ())
16976 : : {
16977 : 1085824 : for (unsigned ix = dep->deps.length (); --ix;)
16978 : : {
16979 : 750756 : depset *bind = dep->deps[ix];
16980 : 750756 : if (bind->get_entity_kind () == depset::EK_USING)
16981 : 92308 : bind = bind->deps[1];
16982 : :
16983 : 750756 : intercluster_seed (sec, index_lwm, bind);
16984 : : }
16985 : : /* Also check the namespace itself. */
16986 : 167534 : dep = dep->deps[0];
16987 : : }
16988 : :
16989 : 4943129 : intercluster_seed (sec, index_lwm, dep);
16990 : : }
16991 : : }
16992 : 240954 : sec.tree_node (NULL_TREE);
16993 : : /* We're done importing now. */
16994 : 240954 : sec.set_importing (-1);
16995 : :
16996 : : /* Write non-definitions. */
16997 : 1250541 : for (unsigned ix = 0; ix != size; ix++)
16998 : : {
16999 : 1009587 : depset *b = scc[ix];
17000 : 1009587 : tree decl = b->get_entity ();
17001 : 1009587 : switch (b->get_entity_kind ())
17002 : : {
17003 : 0 : default:
17004 : 0 : gcc_unreachable ();
17005 : 130858 : break;
17006 : :
17007 : 130858 : case depset::EK_BINDING:
17008 : 130858 : {
17009 : 130858 : gcc_assert (TREE_CODE (decl) == NAMESPACE_DECL);
17010 : 131948 : dump () && dump ("Depset:%u binding %C:%P", ix, TREE_CODE (decl),
17011 : : decl, b->get_name ());
17012 : 130858 : sec.u (ct_bind);
17013 : 130858 : sec.tree_node (decl);
17014 : 130858 : sec.tree_node (b->get_name ());
17015 : :
17016 : : /* Write in reverse order, so reading will see the exports
17017 : : first, thus building the overload chain will be
17018 : : optimized. */
17019 : 429295 : for (unsigned jx = b->deps.length (); --jx;)
17020 : : {
17021 : 167579 : depset *dep = b->deps[jx];
17022 : 167579 : tree bound = dep->get_entity ();
17023 : 167579 : unsigned flags = 0;
17024 : 167579 : if (dep->get_entity_kind () == depset::EK_TU_LOCAL)
17025 : : flags |= cbf_internal;
17026 : 167534 : else if (dep->get_entity_kind () == depset::EK_USING)
17027 : : {
17028 : 20624 : tree ovl = bound;
17029 : 20624 : bound = OVL_FUNCTION (bound);
17030 : 20624 : if (!(TREE_CODE (bound) == CONST_DECL
17031 : 4416 : && UNSCOPED_ENUM_P (TREE_TYPE (bound))
17032 : 3758 : && decl == TYPE_NAME (TREE_TYPE (bound))))
17033 : : /* An unscoped enumerator in its enumeration's
17034 : : scope is not a using. */
17035 : : flags |= cbf_using;
17036 : 20624 : if (OVL_EXPORT_P (ovl))
17037 : 19767 : flags |= cbf_export;
17038 : : }
17039 : : else
17040 : : {
17041 : : /* An implicit typedef must be at one. */
17042 : 146910 : gcc_assert (!DECL_IMPLICIT_TYPEDEF_P (bound) || jx == 1);
17043 : 146910 : if (dep->is_hidden ())
17044 : : flags |= cbf_hidden;
17045 : 141723 : else if (DECL_MODULE_EXPORT_P (STRIP_TEMPLATE (bound)))
17046 : 127851 : flags |= cbf_export;
17047 : : }
17048 : :
17049 : 167579 : gcc_checking_assert (DECL_P (bound));
17050 : :
17051 : 167579 : sec.i (flags);
17052 : 167579 : if (flags & cbf_internal)
17053 : : {
17054 : 45 : sec.tree_node (name_for_tu_local_decl (bound));
17055 : 45 : write_location (sec, DECL_SOURCE_LOCATION (bound));
17056 : : }
17057 : : else
17058 : 167534 : sec.tree_node (bound);
17059 : : }
17060 : :
17061 : : /* Terminate the list. */
17062 : 130858 : sec.i (-1);
17063 : : }
17064 : 130858 : break;
17065 : :
17066 : 20624 : case depset::EK_USING:
17067 : 20624 : case depset::EK_TU_LOCAL:
17068 : 20651 : dump () && dump ("Depset:%u %s %C:%N", ix, b->entity_kind_name (),
17069 : 27 : TREE_CODE (decl), decl);
17070 : : break;
17071 : :
17072 : 858105 : case depset::EK_SPECIALIZATION:
17073 : 858105 : case depset::EK_PARTIAL:
17074 : 858105 : case depset::EK_DECL:
17075 : 860415 : dump () && dump ("Depset:%u %s entity:%u %C:%N", ix,
17076 : : b->entity_kind_name (), b->cluster,
17077 : 2310 : TREE_CODE (decl), decl);
17078 : :
17079 : 858105 : sec.u (ct_decl);
17080 : 858105 : sec.tree_node (decl);
17081 : :
17082 : 1011897 : dump () && dump ("Wrote declaration entity:%u %C:%N",
17083 : 2310 : b->cluster, TREE_CODE (decl), decl);
17084 : : break;
17085 : : }
17086 : : }
17087 : :
17088 : : depset *namer = NULL;
17089 : :
17090 : : /* Write out definitions */
17091 : 1250541 : for (unsigned ix = 0; ix != size; ix++)
17092 : : {
17093 : 1009587 : depset *b = scc[ix];
17094 : 1009587 : tree decl = b->get_entity ();
17095 : 1009587 : switch (b->get_entity_kind ())
17096 : : {
17097 : : default:
17098 : : break;
17099 : :
17100 : 858105 : case depset::EK_SPECIALIZATION:
17101 : 858105 : case depset::EK_PARTIAL:
17102 : 858105 : case depset::EK_DECL:
17103 : 858105 : if (!namer)
17104 : 229594 : namer = b;
17105 : :
17106 : 858105 : if (b->has_defn ())
17107 : : {
17108 : 333037 : sec.u (ct_defn);
17109 : 333037 : sec.tree_node (decl);
17110 : 333669 : dump () && dump ("Writing definition %N", decl);
17111 : 333037 : sec.write_definition (decl, b->refs_tu_local ());
17112 : :
17113 : 333037 : if (!namer->has_defn ())
17114 : 1009587 : namer = b;
17115 : : }
17116 : : break;
17117 : : }
17118 : : }
17119 : :
17120 : : /* We don't find the section by name. Use depset's decl's name for
17121 : : human friendliness. */
17122 : 240954 : unsigned name = 0;
17123 : 240954 : tree naming_decl = NULL_TREE;
17124 : 240954 : if (namer)
17125 : : {
17126 : 229594 : naming_decl = namer->get_entity ();
17127 : 229594 : if (namer->get_entity_kind () == depset::EK_USING)
17128 : : /* This unfortunately names the section from the target of the
17129 : : using decl. But the name is only a guide, so Do Not Care. */
17130 : 0 : naming_decl = OVL_FUNCTION (naming_decl);
17131 : 229594 : if (DECL_IMPLICIT_TYPEDEF_P (naming_decl))
17132 : : /* Lose any anonymousness. */
17133 : 78542 : naming_decl = TYPE_NAME (TREE_TYPE (naming_decl));
17134 : 229594 : name = to->qualified_name (naming_decl, namer->has_defn ());
17135 : : }
17136 : :
17137 : 240954 : unsigned bytes = sec.pos;
17138 : 240954 : unsigned snum = sec.end (to, name, crc_ptr);
17139 : :
17140 : 1250541 : for (unsigned ix = size; ix--;)
17141 : 1009587 : gcc_checking_assert (scc[ix]->section == snum);
17142 : :
17143 : 240954 : dump.outdent ();
17144 : 242289 : dump () && dump ("Wrote section:%u named-by:%N", table.section, naming_decl);
17145 : :
17146 : 240954 : return bytes;
17147 : 240954 : }
17148 : :
17149 : : /* Read a cluster from section SNUM. */
17150 : :
17151 : : bool
17152 : 189914 : module_state::read_cluster (unsigned snum)
17153 : : {
17154 : 189914 : trees_in sec (this);
17155 : :
17156 : 189914 : if (!sec.begin (loc, from (), snum))
17157 : : return false;
17158 : :
17159 : 191063 : dump () && dump ("Reading section:%u", snum);
17160 : 189914 : dump.indent ();
17161 : :
17162 : : /* We care about structural equality. */
17163 : 189914 : comparing_dependent_aliases++;
17164 : :
17165 : : /* First seed the imports. */
17166 : 1127137 : while (tree import = sec.tree_node ())
17167 : 2064546 : dump (dumper::CLUSTER) && dump ("Seeded import %N", import);
17168 : :
17169 : 1376343 : while (!sec.get_overrun () && sec.more_p ())
17170 : : {
17171 : 1186429 : unsigned ct = sec.u ();
17172 : 1186429 : switch (ct)
17173 : : {
17174 : 0 : default:
17175 : 0 : sec.set_overrun ();
17176 : 0 : break;
17177 : :
17178 : 101114 : case ct_bind:
17179 : : /* A set of namespace bindings. */
17180 : 101114 : {
17181 : 101114 : tree ns = sec.tree_node ();
17182 : 101114 : tree name = sec.tree_node ();
17183 : 101114 : tree decls = NULL_TREE;
17184 : 101114 : tree visible = NULL_TREE;
17185 : 101114 : tree internal = NULL_TREE;
17186 : 101114 : tree type = NULL_TREE;
17187 : 101114 : bool dedup = false;
17188 : 101114 : bool global_p = is_header ();
17189 : :
17190 : : /* We rely on the bindings being in the reverse order of
17191 : : the resulting overload set. */
17192 : 237034 : for (;;)
17193 : : {
17194 : 237034 : int flags = sec.i ();
17195 : 237034 : if (flags < 0)
17196 : : break;
17197 : :
17198 : 135920 : if ((flags & cbf_hidden)
17199 : 5412 : && (flags & (cbf_using | cbf_export)))
17200 : 0 : sec.set_overrun ();
17201 : 135920 : if ((flags & cbf_internal)
17202 : 15 : && flags != cbf_internal)
17203 : 0 : sec.set_overrun ();
17204 : :
17205 : 0 : if (flags & cbf_internal)
17206 : : {
17207 : 15 : tree name = sec.tree_node ();
17208 : 15 : location_t loc = read_location (sec);
17209 : 15 : if (sec.get_overrun ())
17210 : : break;
17211 : :
17212 : 15 : tree decl = make_node (TU_LOCAL_ENTITY);
17213 : 15 : TU_LOCAL_ENTITY_NAME (decl) = name;
17214 : 15 : TU_LOCAL_ENTITY_LOCATION (decl) = loc;
17215 : 15 : internal = tree_cons (NULL_TREE, decl, internal);
17216 : 15 : continue;
17217 : 15 : }
17218 : :
17219 : 135905 : tree decl = sec.tree_node ();
17220 : 135905 : if (sec.get_overrun ())
17221 : : break;
17222 : :
17223 : 135905 : if (!global_p)
17224 : : {
17225 : : /* Check if the decl could require GM merging. */
17226 : 4799 : tree orig = get_originating_module_decl (decl);
17227 : 4799 : tree inner = STRIP_TEMPLATE (orig);
17228 : 4799 : if (!DECL_LANG_SPECIFIC (inner)
17229 : 9598 : || !DECL_MODULE_ATTACH_P (inner))
17230 : : global_p = true;
17231 : : }
17232 : :
17233 : 135905 : if (decls && TREE_CODE (decl) == TYPE_DECL)
17234 : : {
17235 : : /* Stat hack. */
17236 : 48 : if (type || !DECL_IMPLICIT_TYPEDEF_P (decl))
17237 : 0 : sec.set_overrun ();
17238 : :
17239 : 48 : if (flags & cbf_using)
17240 : : {
17241 : 3 : type = build_lang_decl_loc (UNKNOWN_LOCATION,
17242 : : USING_DECL,
17243 : 3 : DECL_NAME (decl),
17244 : : NULL_TREE);
17245 : 3 : USING_DECL_DECLS (type) = decl;
17246 : 3 : USING_DECL_SCOPE (type) = CP_DECL_CONTEXT (decl);
17247 : 3 : DECL_CONTEXT (type) = ns;
17248 : :
17249 : 3 : DECL_MODULE_PURVIEW_P (type) = true;
17250 : 3 : if (flags & cbf_export)
17251 : 3 : DECL_MODULE_EXPORT_P (type) = true;
17252 : : }
17253 : : else
17254 : : type = decl;
17255 : : }
17256 : : else
17257 : : {
17258 : 135857 : if ((flags & cbf_using) &&
17259 : 12761 : !DECL_DECLARES_FUNCTION_P (decl))
17260 : : {
17261 : : /* We should only see a single non-function using-decl
17262 : : for a binding; more than that would clash. */
17263 : 4451 : if (decls)
17264 : 0 : sec.set_overrun ();
17265 : :
17266 : : /* FIXME: Propagate the location of the using-decl
17267 : : for use in diagnostics. */
17268 : 4451 : decls = build_lang_decl_loc (UNKNOWN_LOCATION,
17269 : : USING_DECL,
17270 : 4451 : DECL_NAME (decl),
17271 : : NULL_TREE);
17272 : 4451 : USING_DECL_DECLS (decls) = decl;
17273 : : /* We don't currently record the actual scope of the
17274 : : using-declaration, but this approximation should
17275 : : generally be good enough. */
17276 : 4451 : USING_DECL_SCOPE (decls) = CP_DECL_CONTEXT (decl);
17277 : 4451 : DECL_CONTEXT (decls) = ns;
17278 : :
17279 : 4451 : DECL_MODULE_PURVIEW_P (decls) = true;
17280 : 4451 : if (flags & cbf_export)
17281 : 4439 : DECL_MODULE_EXPORT_P (decls) = true;
17282 : : }
17283 : 131406 : else if (decls
17284 : 96648 : || (flags & (cbf_hidden | cbf_using))
17285 : 222101 : || DECL_FUNCTION_TEMPLATE_P (decl))
17286 : : {
17287 : 55268 : decls = ovl_make (decl, decls);
17288 : 55268 : if (flags & cbf_using)
17289 : : {
17290 : 8310 : dedup = true;
17291 : 8310 : OVL_USING_P (decls) = true;
17292 : 8310 : OVL_PURVIEW_P (decls) = true;
17293 : 8310 : if (flags & cbf_export)
17294 : 8295 : OVL_EXPORT_P (decls) = true;
17295 : : }
17296 : :
17297 : 55268 : if (flags & cbf_hidden)
17298 : 5412 : OVL_HIDDEN_P (decls) = true;
17299 : 49856 : else if (dedup)
17300 : 8346 : OVL_DEDUP_P (decls) = true;
17301 : : }
17302 : : else
17303 : : decls = decl;
17304 : :
17305 : 135845 : if (flags & cbf_export
17306 : 135857 : || (!(flags & cbf_hidden)
17307 : 3495 : && (is_module () || is_partition ())))
17308 : : visible = decls;
17309 : : }
17310 : : }
17311 : :
17312 : 101114 : if (!decls && !internal)
17313 : 0 : sec.set_overrun ();
17314 : :
17315 : 101114 : if (sec.get_overrun ())
17316 : : break; /* Bail. */
17317 : :
17318 : 101948 : dump () && dump ("Binding of %P", ns, name);
17319 : 101114 : if (!set_module_binding (ns, name, mod, global_p,
17320 : 101114 : is_module () || is_partition (),
17321 : : decls, type, visible, internal))
17322 : 0 : sec.set_overrun ();
17323 : : }
17324 : : break;
17325 : :
17326 : 783876 : case ct_decl:
17327 : : /* A decl. */
17328 : 783876 : {
17329 : 783876 : tree decl = sec.tree_node ();
17330 : 2163233 : dump () && dump ("Read declaration of %N", decl);
17331 : : }
17332 : : break;
17333 : :
17334 : 301439 : case ct_defn:
17335 : 301439 : {
17336 : 301439 : tree decl = sec.tree_node ();
17337 : 302538 : dump () && dump ("Reading definition of %N", decl);
17338 : 301439 : sec.read_definition (decl);
17339 : : }
17340 : 301439 : break;
17341 : : }
17342 : : }
17343 : :
17344 : : /* When lazy loading is in effect, we can be in the middle of
17345 : : parsing or instantiating a function. Save it away.
17346 : : push_function_context does too much work. */
17347 : 189914 : tree old_cfd = current_function_decl;
17348 : 189914 : struct function *old_cfun = cfun;
17349 : 341910 : for (const post_process_data& pdata : sec.post_process ())
17350 : : {
17351 : 114580 : tree decl = pdata.decl;
17352 : :
17353 : 114580 : bool abstract = false;
17354 : 114580 : if (TREE_CODE (decl) == TEMPLATE_DECL)
17355 : : {
17356 : 69902 : abstract = true;
17357 : 69902 : decl = DECL_TEMPLATE_RESULT (decl);
17358 : : }
17359 : :
17360 : 114580 : current_function_decl = decl;
17361 : 114580 : allocate_struct_function (decl, abstract);
17362 : 114580 : cfun->language = ggc_cleared_alloc<language_function> ();
17363 : 114580 : cfun->language->base.x_stmt_tree.stmts_are_full_exprs_p = 1;
17364 : 114580 : cfun->function_start_locus = pdata.start_locus;
17365 : 114580 : cfun->function_end_locus = pdata.end_locus;
17366 : 114580 : cfun->language->returns_value = pdata.returns_value;
17367 : 114580 : cfun->language->returns_null = pdata.returns_null;
17368 : 114580 : cfun->language->returns_abnormally = pdata.returns_abnormally;
17369 : 114580 : cfun->language->infinite_loop = pdata.infinite_loop;
17370 : :
17371 : : /* Make sure we emit explicit instantiations.
17372 : : FIXME do we want to do this in expand_or_defer_fn instead? */
17373 : 114580 : if (DECL_EXPLICIT_INSTANTIATION (decl)
17374 : 114580 : && !DECL_EXTERNAL (decl))
17375 : 24 : setup_explicit_instantiation_definition_linkage (decl);
17376 : :
17377 : 114580 : if (abstract)
17378 : : ;
17379 : 44678 : else if (DECL_MAYBE_IN_CHARGE_CDTOR_P (decl))
17380 : 7409 : vec_safe_push (post_load_decls, decl);
17381 : : else
17382 : : {
17383 : 37269 : bool aggr = aggregate_value_p (DECL_RESULT (decl), decl);
17384 : : #ifdef PCC_STATIC_STRUCT_RETURN
17385 : : cfun->returns_pcc_struct = aggr;
17386 : : #endif
17387 : 37269 : cfun->returns_struct = aggr;
17388 : 37269 : expand_or_defer_fn (decl);
17389 : :
17390 : : /* If we first see this function after at_eof, it doesn't get
17391 : : note_vague_linkage_fn from tentative_decl_linkage, so the loop in
17392 : : c_parse_final_cleanups won't consider it. But with DECL_COMDAT we
17393 : : can just clear DECL_EXTERNAL and let cgraph decide.
17394 : : FIXME handle this outside module.cc after GCC 15. */
17395 : 4716 : if (at_eof && DECL_COMDAT (decl) && DECL_EXTERNAL (decl)
17396 : 39054 : && DECL_NOT_REALLY_EXTERN (decl))
17397 : 1752 : DECL_EXTERNAL (decl) = false;
17398 : : }
17399 : :
17400 : : }
17401 : 189914 : set_cfun (old_cfun);
17402 : 189914 : current_function_decl = old_cfd;
17403 : 189914 : comparing_dependent_aliases--;
17404 : :
17405 : 189914 : dump.outdent ();
17406 : 191063 : dump () && dump ("Read section:%u", snum);
17407 : :
17408 : 189914 : loaded_clusters++;
17409 : :
17410 : 189914 : if (!sec.end (from ()))
17411 : : return false;
17412 : :
17413 : : return true;
17414 : 189914 : }
17415 : :
17416 : : void
17417 : 133347 : module_state::write_namespace (bytes_out &sec, depset *dep)
17418 : : {
17419 : 133347 : unsigned ns_num = dep->cluster;
17420 : 133347 : unsigned ns_import = 0;
17421 : :
17422 : 133347 : if (dep->is_import ())
17423 : 0 : ns_import = dep->section;
17424 : 133347 : else if (dep->get_entity () != global_namespace)
17425 : 83386 : ns_num++;
17426 : :
17427 : 133347 : sec.u (ns_import);
17428 : 133347 : sec.u (ns_num);
17429 : 133347 : }
17430 : :
17431 : : tree
17432 : 178401 : module_state::read_namespace (bytes_in &sec)
17433 : : {
17434 : 178401 : unsigned ns_import = sec.u ();
17435 : 178401 : unsigned ns_num = sec.u ();
17436 : 178401 : tree ns = NULL_TREE;
17437 : :
17438 : 178401 : if (ns_import || ns_num)
17439 : : {
17440 : 110771 : if (!ns_import)
17441 : 110771 : ns_num--;
17442 : :
17443 : 110771 : if (unsigned origin = slurp->remap_module (ns_import))
17444 : : {
17445 : 110771 : module_state *from = (*modules)[origin];
17446 : 110771 : if (ns_num < from->entity_num)
17447 : : {
17448 : 110771 : binding_slot &slot = (*entity_ary)[from->entity_lwm + ns_num];
17449 : :
17450 : 110771 : if (!slot.is_lazy ())
17451 : 110771 : ns = slot;
17452 : : }
17453 : : }
17454 : : else
17455 : 0 : sec.set_overrun ();
17456 : : }
17457 : : else
17458 : 67630 : ns = global_namespace;
17459 : :
17460 : 178401 : return ns;
17461 : : }
17462 : :
17463 : : /* SPACES is a sorted vector of namespaces. Write out the namespaces
17464 : : to MOD_SNAME_PFX.nms section. */
17465 : :
17466 : : void
17467 : 545 : module_state::write_namespaces (elf_out *to, vec<depset *> spaces,
17468 : : unsigned num, unsigned *crc_p)
17469 : : {
17470 : 597 : dump () && dump ("Writing namespaces");
17471 : 545 : dump.indent ();
17472 : :
17473 : 545 : bytes_out sec (to);
17474 : 545 : sec.begin ();
17475 : :
17476 : 2746 : for (unsigned ix = 0; ix != num; ix++)
17477 : : {
17478 : 2201 : depset *b = spaces[ix];
17479 : 2201 : tree ns = b->get_entity ();
17480 : :
17481 : : /* This could be an anonymous namespace even for a named module,
17482 : : since we can still emit no-linkage decls. */
17483 : 2201 : gcc_checking_assert (TREE_CODE (ns) == NAMESPACE_DECL);
17484 : :
17485 : 2201 : unsigned flags = 0;
17486 : 2201 : if (TREE_PUBLIC (ns))
17487 : 2149 : flags |= 1;
17488 : 2201 : if (DECL_NAMESPACE_INLINE_P (ns))
17489 : 399 : flags |= 2;
17490 : 2201 : if (DECL_MODULE_PURVIEW_P (ns))
17491 : 1838 : flags |= 4;
17492 : 2201 : if (DECL_MODULE_EXPORT_P (ns))
17493 : 1647 : flags |= 8;
17494 : 2201 : if (TREE_DEPRECATED (ns))
17495 : 13 : flags |= 16;
17496 : :
17497 : 2322 : dump () && dump ("Writing namespace:%u %N%s%s%s%s",
17498 : : b->cluster, ns,
17499 : 121 : flags & 1 ? ", public" : "",
17500 : 121 : flags & 2 ? ", inline" : "",
17501 : 121 : flags & 4 ? ", purview" : "",
17502 : 121 : flags & 8 ? ", export" : "",
17503 : 121 : flags & 16 ? ", deprecated" : "");
17504 : 2201 : sec.u (b->cluster);
17505 : 2201 : sec.u (to->name (DECL_NAME (ns)));
17506 : 2201 : write_namespace (sec, b->deps[0]);
17507 : :
17508 : 2201 : sec.u (flags);
17509 : 2201 : write_location (sec, DECL_SOURCE_LOCATION (ns));
17510 : :
17511 : 2201 : if (DECL_NAMESPACE_INLINE_P (ns))
17512 : : {
17513 : 399 : if (tree attr = lookup_attribute ("abi_tag", DECL_ATTRIBUTES (ns)))
17514 : : {
17515 : 113 : tree tags = TREE_VALUE (attr);
17516 : 113 : sec.u (list_length (tags));
17517 : 229 : for (tree tag = tags; tag; tag = TREE_CHAIN (tag))
17518 : 116 : sec.str (TREE_STRING_POINTER (TREE_VALUE (tag)));
17519 : : }
17520 : : else
17521 : 286 : sec.u (0);
17522 : : }
17523 : : }
17524 : :
17525 : 545 : sec.end (to, to->name (MOD_SNAME_PFX ".nms"), crc_p);
17526 : 545 : dump.outdent ();
17527 : 545 : }
17528 : :
17529 : : /* Read the namespace hierarchy from MOD_SNAME_PFX.namespace. Fill in
17530 : : SPACES from that data. */
17531 : :
17532 : : bool
17533 : 576 : module_state::read_namespaces (unsigned num)
17534 : : {
17535 : 576 : bytes_in sec;
17536 : :
17537 : 576 : if (!sec.begin (loc, from (), MOD_SNAME_PFX ".nms"))
17538 : : return false;
17539 : :
17540 : 646 : dump () && dump ("Reading namespaces");
17541 : 576 : dump.indent ();
17542 : :
17543 : 3240 : for (unsigned ix = 0; ix != num; ix++)
17544 : : {
17545 : 2664 : unsigned entity_index = sec.u ();
17546 : 2664 : unsigned name = sec.u ();
17547 : :
17548 : 2664 : tree parent = read_namespace (sec);
17549 : :
17550 : : /* See comment in write_namespace about why not bits. */
17551 : 2664 : unsigned flags = sec.u ();
17552 : 2664 : location_t src_loc = read_location (sec);
17553 : 2664 : unsigned tags_count = (flags & 2) ? sec.u () : 0;
17554 : :
17555 : 2664 : if (entity_index >= entity_num
17556 : 2664 : || !parent
17557 : 2664 : || (flags & 0xc) == 0x8)
17558 : 0 : sec.set_overrun ();
17559 : :
17560 : : tree tags = NULL_TREE;
17561 : 2796 : while (tags_count--)
17562 : : {
17563 : 132 : size_t len;
17564 : 132 : const char *str = sec.str (&len);
17565 : 132 : tags = tree_cons (NULL_TREE, build_string (len + 1, str), tags);
17566 : 132 : tags = nreverse (tags);
17567 : : }
17568 : :
17569 : 2664 : if (sec.get_overrun ())
17570 : : break;
17571 : :
17572 : 5276 : tree id = name ? get_identifier (from ()->name (name)) : NULL_TREE;
17573 : :
17574 : 2858 : dump () && dump ("Read namespace:%u %P%s%s%s%s",
17575 : : entity_index, parent, id,
17576 : 97 : flags & 1 ? ", public" : "",
17577 : : flags & 2 ? ", inline" : "",
17578 : 97 : flags & 4 ? ", purview" : "",
17579 : 97 : flags & 8 ? ", export" : "",
17580 : 97 : flags & 16 ? ", deprecated" : "");
17581 : 2664 : bool visible_p = ((flags & 8)
17582 : 2664 : || ((flags & 1)
17583 : 544 : && (flags & 4)
17584 : 95 : && (is_partition () || is_module ())));
17585 : 2664 : tree inner = add_imported_namespace (parent, id, src_loc, mod,
17586 : : bool (flags & 2), visible_p);
17587 : 2664 : if (!inner)
17588 : : {
17589 : 0 : sec.set_overrun ();
17590 : 0 : break;
17591 : : }
17592 : :
17593 : 2664 : if (is_partition ())
17594 : : {
17595 : 48 : if (flags & 4)
17596 : 42 : DECL_MODULE_PURVIEW_P (inner) = true;
17597 : 48 : if (flags & 8)
17598 : 27 : DECL_MODULE_EXPORT_P (inner) = true;
17599 : : }
17600 : :
17601 : 2664 : if (flags & 16)
17602 : 19 : TREE_DEPRECATED (inner) = true;
17603 : :
17604 : 2664 : if (tags)
17605 : 129 : DECL_ATTRIBUTES (inner)
17606 : 258 : = tree_cons (get_identifier ("abi_tag"), tags, DECL_ATTRIBUTES (inner));
17607 : :
17608 : : /* Install the namespace. */
17609 : 2664 : (*entity_ary)[entity_lwm + entity_index] = inner;
17610 : 2664 : if (DECL_MODULE_IMPORT_P (inner))
17611 : : {
17612 : 0 : bool existed;
17613 : 0 : unsigned *slot = &entity_map->get_or_insert
17614 : 0 : (DECL_UID (inner), &existed);
17615 : 0 : if (existed)
17616 : : /* If it existed, it should match. */
17617 : 0 : gcc_checking_assert (inner == (*entity_ary)[*slot]);
17618 : : else
17619 : 0 : *slot = entity_lwm + entity_index;
17620 : : }
17621 : : }
17622 : :
17623 : 576 : dump.outdent ();
17624 : 576 : if (!sec.end (from ()))
17625 : : return false;
17626 : : return true;
17627 : 576 : }
17628 : :
17629 : : unsigned
17630 : 545 : module_state::write_using_directives (elf_out *to, depset::hash &table,
17631 : : vec<depset *> spaces, unsigned *crc_p)
17632 : : {
17633 : 597 : dump () && dump ("Writing using-directives");
17634 : 545 : dump.indent ();
17635 : :
17636 : 545 : bytes_out sec (to);
17637 : 545 : sec.begin ();
17638 : :
17639 : 545 : unsigned num = 0;
17640 : 3291 : auto emit_one_ns = [&](depset *parent_dep)
17641 : : {
17642 : 2746 : tree parent = parent_dep->get_entity ();
17643 : 3235 : for (auto udir : NAMESPACE_LEVEL (parent)->using_directives)
17644 : : {
17645 : 167 : if (TREE_CODE (udir) != USING_DECL || !DECL_MODULE_PURVIEW_P (udir))
17646 : 20 : continue;
17647 : 147 : bool exported = DECL_MODULE_EXPORT_P (udir);
17648 : 147 : tree target = USING_DECL_DECLS (udir);
17649 : 147 : depset *target_dep = table.find_dependency (target);
17650 : :
17651 : : /* An using-directive imported from a different module might not
17652 : : have been walked earlier (PR c++/122915). But importers will
17653 : : be able to just refer to the decl in that module unless it was
17654 : : a partition anyway, so we don't have anything to do here. */
17655 : 147 : if (!target_dep)
17656 : : {
17657 : 3 : gcc_checking_assert (DECL_MODULE_IMPORT_P (udir));
17658 : 3 : continue;
17659 : : }
17660 : :
17661 : 168 : dump () && dump ("Writing using-directive in %N for %N",
17662 : : parent, target);
17663 : 144 : sec.u (exported);
17664 : 144 : write_namespace (sec, parent_dep);
17665 : 144 : write_namespace (sec, target_dep);
17666 : 144 : ++num;
17667 : : }
17668 : 3291 : };
17669 : :
17670 : 545 : emit_one_ns (table.find_dependency (global_namespace));
17671 : 3836 : for (depset *parent_dep : spaces)
17672 : 2201 : emit_one_ns (parent_dep);
17673 : :
17674 : 545 : sec.end (to, to->name (MOD_SNAME_PFX ".udi"), crc_p);
17675 : 545 : dump.outdent ();
17676 : :
17677 : 545 : return num;
17678 : 545 : }
17679 : :
17680 : : bool
17681 : 147 : module_state::read_using_directives (unsigned num)
17682 : : {
17683 : 147 : if (!bitmap_bit_p (this_module ()->imports, mod))
17684 : : {
17685 : 10 : dump () && dump ("Ignoring using-directives because module %M "
17686 : : "is not visible in this TU", this);
17687 : 7 : return true;
17688 : : }
17689 : :
17690 : 140 : bytes_in sec;
17691 : :
17692 : 140 : if (!sec.begin (loc, from (), MOD_SNAME_PFX ".udi"))
17693 : : return false;
17694 : :
17695 : 152 : dump () && dump ("Reading using-directives");
17696 : 140 : dump.indent ();
17697 : :
17698 : 309 : for (unsigned ix = 0; ix != num; ++ix)
17699 : : {
17700 : 169 : bool exported = sec.u ();
17701 : 169 : tree parent = read_namespace (sec);
17702 : 169 : tree target = read_namespace (sec);
17703 : 169 : if (sec.get_overrun ())
17704 : : break;
17705 : :
17706 : 181 : dump () && dump ("Read using-directive in %N for %N", parent, target);
17707 : 169 : if (exported || is_module () || is_partition ())
17708 : 145 : add_imported_using_namespace (parent, target);
17709 : : }
17710 : :
17711 : 140 : dump.outdent ();
17712 : 140 : if (!sec.end (from ()))
17713 : : return false;
17714 : : return true;
17715 : 140 : }
17716 : :
17717 : : /* Write the binding TABLE to MOD_SNAME_PFX.bnd */
17718 : :
17719 : : unsigned
17720 : 2580 : module_state::write_bindings (elf_out *to, vec<depset *> sccs, unsigned *crc_p)
17721 : : {
17722 : 2856 : dump () && dump ("Writing binding table");
17723 : 2580 : dump.indent ();
17724 : :
17725 : 2580 : unsigned num = 0;
17726 : 2580 : bytes_out sec (to);
17727 : 2580 : sec.begin ();
17728 : :
17729 : 2033418 : for (unsigned ix = 0; ix != sccs.length (); ix++)
17730 : : {
17731 : 1014129 : depset *b = sccs[ix];
17732 : 1014129 : if (b->is_binding ())
17733 : : {
17734 : 130858 : tree ns = b->get_entity ();
17735 : 131948 : dump () && dump ("Bindings %P section:%u", ns, b->get_name (),
17736 : : b->section);
17737 : 130858 : sec.u (to->name (b->get_name ()));
17738 : 130858 : write_namespace (sec, b->deps[0]);
17739 : 130858 : sec.u (b->section);
17740 : 130858 : num++;
17741 : : }
17742 : : }
17743 : :
17744 : 2580 : sec.end (to, to->name (MOD_SNAME_PFX ".bnd"), crc_p);
17745 : 2580 : dump.outdent ();
17746 : :
17747 : 2580 : return num;
17748 : 2580 : }
17749 : :
17750 : : /* Read the binding table from MOD_SNAME_PFX.bind. */
17751 : :
17752 : : bool
17753 : 2760 : module_state::read_bindings (unsigned num, unsigned lwm, unsigned hwm)
17754 : : {
17755 : 2760 : bytes_in sec;
17756 : :
17757 : 2760 : if (!sec.begin (loc, from (), MOD_SNAME_PFX ".bnd"))
17758 : : return false;
17759 : :
17760 : 3256 : dump () && dump ("Reading binding table");
17761 : 2760 : dump.indent ();
17762 : 178159 : for (; !sec.get_overrun () && num--;)
17763 : : {
17764 : 175399 : const char *name = from ()->name (sec.u ());
17765 : 175399 : tree ns = read_namespace (sec);
17766 : 175399 : unsigned snum = sec.u ();
17767 : :
17768 : 175399 : if (!ns || !name || (snum - lwm) >= (hwm - lwm))
17769 : 0 : sec.set_overrun ();
17770 : 175399 : if (!sec.get_overrun ())
17771 : : {
17772 : 175399 : tree id = get_identifier (name);
17773 : 176862 : dump () && dump ("Bindings %P section:%u", ns, id, snum);
17774 : 175399 : if (mod && !import_module_binding (ns, id, mod, snum))
17775 : : break;
17776 : : }
17777 : : }
17778 : :
17779 : 2760 : dump.outdent ();
17780 : 2760 : if (!sec.end (from ()))
17781 : : return false;
17782 : : return true;
17783 : 2760 : }
17784 : :
17785 : : /* Write the entity table to MOD_SNAME_PFX.ent
17786 : :
17787 : : Each entry is a section number. */
17788 : :
17789 : : void
17790 : 2335 : module_state::write_entities (elf_out *to, vec<depset *> depsets,
17791 : : unsigned count, unsigned *crc_p)
17792 : : {
17793 : 2561 : dump () && dump ("Writing entities");
17794 : 2335 : dump.indent ();
17795 : :
17796 : 2335 : bytes_out sec (to);
17797 : 2335 : sec.begin ();
17798 : :
17799 : 2335 : unsigned current = 0;
17800 : 1016443 : for (unsigned ix = 0; ix < depsets.length (); ix++)
17801 : : {
17802 : 1014108 : depset *d = depsets[ix];
17803 : :
17804 : 1876749 : switch (d->get_entity_kind ())
17805 : : {
17806 : : default:
17807 : : break;
17808 : :
17809 : 4536 : case depset::EK_NAMESPACE:
17810 : 4536 : if (!d->is_import () && d->get_entity () != global_namespace)
17811 : : {
17812 : 2201 : gcc_checking_assert (d->cluster == current);
17813 : 2201 : current++;
17814 : 2201 : sec.u (0);
17815 : : }
17816 : : break;
17817 : :
17818 : 858105 : case depset::EK_DECL:
17819 : 858105 : case depset::EK_SPECIALIZATION:
17820 : 858105 : case depset::EK_PARTIAL:
17821 : 1716210 : gcc_checking_assert (!d->is_unreached ()
17822 : : && !d->is_import ()
17823 : : && d->cluster == current
17824 : : && d->section);
17825 : 858105 : current++;
17826 : 858105 : sec.u (d->section);
17827 : 858105 : break;
17828 : : }
17829 : : }
17830 : 2335 : gcc_assert (count == current);
17831 : 2335 : sec.end (to, to->name (MOD_SNAME_PFX ".ent"), crc_p);
17832 : 2335 : dump.outdent ();
17833 : 2335 : }
17834 : :
17835 : : bool
17836 : 2555 : module_state::read_entities (unsigned count, unsigned lwm, unsigned hwm)
17837 : : {
17838 : 2555 : trees_in sec (this);
17839 : :
17840 : 2555 : if (!sec.begin (loc, from (), MOD_SNAME_PFX ".ent"))
17841 : : return false;
17842 : :
17843 : 3000 : dump () && dump ("Reading entities");
17844 : 2555 : dump.indent ();
17845 : :
17846 : 1155555 : for (binding_slot *slot = entity_ary->begin () + entity_lwm; count--; slot++)
17847 : : {
17848 : 1153000 : unsigned snum = sec.u ();
17849 : 1153000 : if (snum && (snum - lwm) >= (hwm - lwm))
17850 : 0 : sec.set_overrun ();
17851 : 1153000 : if (sec.get_overrun ())
17852 : : break;
17853 : :
17854 : 1153000 : if (snum)
17855 : 1150336 : slot->set_lazy (snum << 2);
17856 : : }
17857 : :
17858 : 2555 : dump.outdent ();
17859 : 2555 : if (!sec.end (from ()))
17860 : : return false;
17861 : : return true;
17862 : 2555 : }
17863 : :
17864 : : /* Write the pending table to MOD_SNAME_PFX.pnd
17865 : :
17866 : : The pending table holds information about clusters that need to be
17867 : : loaded because they contain information about something that is not
17868 : : found by namespace-scope lookup.
17869 : :
17870 : : The three cases are:
17871 : :
17872 : : (a) Template (maybe-partial) specializations that we have
17873 : : instantiated or defined. When an importer needs to instantiate
17874 : : that template, they /must have/ the partial, explicit & extern
17875 : : specializations available. If they have the other specializations
17876 : : available, they'll have less work to do. Thus, when we're about to
17877 : : instantiate FOO, we have to be able to ask 'are there any
17878 : : specialization of FOO in our imports?'.
17879 : :
17880 : : (b) (Maybe-implicit) member functions definitions. A class could
17881 : : be defined in one header, and an inline member defined in a
17882 : : different header (this occurs in the STL). Similarly, like the
17883 : : specialization case, an implicit member function could have been
17884 : : 'instantiated' in one module, and it'd be nice to not have to
17885 : : reinstantiate it in another.
17886 : :
17887 : : (c) Classes completed elsewhere. A class could be declared in one
17888 : : header and defined in another. We need to know to load the class
17889 : : definition before looking in it. It does highlight an issue --
17890 : : there could be an intermediate import between the outermost containing
17891 : : namespace-scope class and the innermost being-defined class. This is
17892 : : actually possible with all of these cases, so be aware -- we're not
17893 : : just talking of one level of import to get to the innermost namespace.
17894 : :
17895 : : This gets complicated fast, it took me multiple attempts to even
17896 : : get something remotely working. Partially because I focussed on
17897 : : optimizing what I think turns out to be a smaller problem, given
17898 : : the known need to do the more general case *anyway*. I document
17899 : : the smaller problem, because it does appear to be the natural way
17900 : : to do it. It's trap!
17901 : :
17902 : : **** THE TRAP
17903 : :
17904 : : Let's refer to the primary template or the containing class as the
17905 : : KEY. And the specialization or member as the PENDING-ENTITY. (To
17906 : : avoid having to say those mouthfuls all the time.)
17907 : :
17908 : : In either case, we have an entity and we need some way of mapping
17909 : : that to a set of entities that need to be loaded before we can
17910 : : proceed with whatever processing of the entity we were going to do.
17911 : :
17912 : : We need to link the key to the pending-entity in some way. Given a
17913 : : key, tell me the pending-entities I need to have loaded. However
17914 : : we tie the key to the pending-entity must not rely on the key being
17915 : : loaded -- that'd defeat the lazy loading scheme.
17916 : :
17917 : : As the key will be an import in we know its entity number (either
17918 : : because we imported it, or we're writing it out too). Thus we can
17919 : : generate a map of key-indices to pending-entities. The
17920 : : pending-entity indices will be into our span of the entity table,
17921 : : and thus allow them to be lazily loaded. The key index will be
17922 : : into another slot of the entity table. Notice that this checking
17923 : : could be expensive, we don't want to iterate over a bunch of
17924 : : pending-entity indices (across multiple imports), every time we're
17925 : : about do to the thing with the key. We need to quickly determine
17926 : : 'definitely nothing needed'.
17927 : :
17928 : : That's almost good enough, except that key indices are not unique
17929 : : in a couple of cases :( Specifically the Global Module or a module
17930 : : partition can result in multiple modules assigning an entity index
17931 : : for the key. The decl-merging on loading will detect that so we
17932 : : only have one Key loaded, and in the entity hash it'll indicate the
17933 : : entity index of first load. Which might be different to how we
17934 : : know it. Notice this is restricted to GM entities or this-module
17935 : : entities. Foreign imports cannot have this.
17936 : :
17937 : : We can simply resolve this in the direction of how this module
17938 : : referred to the key to how the importer knows it. Look in the
17939 : : entity table slot that we nominate, maybe lazy load it, and then
17940 : : lookup the resultant entity in the entity hash to learn how the
17941 : : importer knows it.
17942 : :
17943 : : But we need to go in the other direction :( Given the key, find all
17944 : : the index-aliases of that key. We can partially solve that by
17945 : : adding an alias hash table. Whenever we load a merged decl, add or
17946 : : augment a mapping from the entity (or its entity-index) to the
17947 : : newly-discovered index. Then when we look for pending entities of
17948 : : a key, we also iterate over this aliases this mapping provides.
17949 : :
17950 : : But that requires the alias to be loaded. And that's not
17951 : : necessarily true.
17952 : :
17953 : : *** THE SIMPLER WAY
17954 : :
17955 : : The remaining fixed thing we have is the innermost namespace
17956 : : containing the ultimate namespace-scope container of the key and
17957 : : the name of that container (which might be the key itself). I.e. a
17958 : : namespace-decl/identifier/module tuple. Let's call this the
17959 : : top-key. We'll discover that the module is not important here,
17960 : : because of cross-module possibilities mentioned in case #c above.
17961 : : We can't markup namespace-binding slots. The best we can do is
17962 : : mark the binding vector with 'there's something here', and have
17963 : : another map from namespace/identifier pairs to a vector of pending
17964 : : entity indices.
17965 : :
17966 : : Maintain a pending-entity map. This is keyed by top-key, and
17967 : : maps to a vector of pending-entity indices. On the binding vector
17968 : : have flags saying whether the pending-name-entity map has contents.
17969 : : (We might want to further extend the key to be GM-vs-Partition and
17970 : : specialization-vs-member, but let's not get ahead of ourselves.)
17971 : :
17972 : : For every key-like entity, find the outermost namespace-scope
17973 : : name. Use that to lookup in the pending-entity map and then make
17974 : : sure the specified entities are loaded.
17975 : :
17976 : : An optimization might be to have a flag in each key-entity saying
17977 : : that its top key might be in the entity table. It's not clear to
17978 : : me how to set that flag cheaply -- cheaper than just looking.
17979 : :
17980 : : FIXME: It'd be nice to have a bit in decls to tell us whether to
17981 : : even try this. We can have a 'already done' flag, that we set when
17982 : : we've done KLASS's lazy pendings. When we import a module that
17983 : : registers pendings on the same top-key as KLASS we need to clear
17984 : : the flag. A recursive walk of the top-key clearing the bit will
17985 : : suffice. Plus we only need to recurse on classes that have the bit
17986 : : set. (That means we need to set the bit on parents of KLASS here,
17987 : : don't forget.) However, first: correctness, second: efficiency. */
17988 : :
17989 : : unsigned
17990 : 2580 : module_state::write_pendings (elf_out *to, vec<depset *> depsets,
17991 : : depset::hash &table, unsigned *crc_p)
17992 : : {
17993 : 2856 : dump () && dump ("Writing pending-entities");
17994 : 2580 : dump.indent ();
17995 : :
17996 : 2580 : trees_out sec (to, this, table);
17997 : 2580 : sec.begin ();
17998 : :
17999 : 2580 : unsigned count = 0;
18000 : 2580 : tree cache_ns = NULL_TREE;
18001 : 2580 : tree cache_id = NULL_TREE;
18002 : 2580 : unsigned cache_section = ~0;
18003 : 1016709 : for (unsigned ix = 0; ix < depsets.length (); ix++)
18004 : : {
18005 : 1014129 : depset *d = depsets[ix];
18006 : :
18007 : 1014129 : if (d->is_binding ())
18008 : 630951 : continue;
18009 : :
18010 : 883271 : if (d->is_import ())
18011 : 0 : continue;
18012 : :
18013 : 883271 : if (!d->is_pending_entity ())
18014 : 500093 : continue;
18015 : :
18016 : 383178 : tree key_decl = nullptr;
18017 : 383178 : tree key_ns = find_pending_key (d->get_entity (), &key_decl);
18018 : 383178 : tree key_name = DECL_NAME (key_decl);
18019 : :
18020 : 383178 : if (IDENTIFIER_ANON_P (key_name))
18021 : : {
18022 : 6 : gcc_checking_assert (IDENTIFIER_LAMBDA_P (key_name));
18023 : 12 : if (tree attached = LAMBDA_TYPE_EXTRA_SCOPE (TREE_TYPE (key_decl)))
18024 : 6 : key_name = DECL_NAME (attached);
18025 : : else
18026 : : {
18027 : : /* There's nothing to attach it to. Must
18028 : : always reinstantiate. */
18029 : 0 : dump ()
18030 : 0 : && dump ("Unattached lambda %N[%u] section:%u",
18031 : 0 : d->get_entity_kind () == depset::EK_DECL
18032 : : ? "Member" : "Specialization", d->get_entity (),
18033 : : d->cluster, d->section);
18034 : 0 : continue;
18035 : : }
18036 : : }
18037 : :
18038 : 383178 : char const *also = "";
18039 : 383178 : if (d->section == cache_section
18040 : 245082 : && key_ns == cache_ns
18041 : 245082 : && key_name == cache_id)
18042 : : /* Same section & key as previous, no need to repeat ourselves. */
18043 : : also = "also ";
18044 : : else
18045 : : {
18046 : 181211 : cache_ns = key_ns;
18047 : 181211 : cache_id = key_name;
18048 : 181211 : cache_section = d->section;
18049 : 181211 : gcc_checking_assert (table.find_dependency (cache_ns));
18050 : 181211 : sec.tree_node (cache_ns);
18051 : 181211 : sec.tree_node (cache_id);
18052 : 181211 : sec.u (d->cluster);
18053 : 181211 : count++;
18054 : : }
18055 : 384338 : dump () && dump ("Pending %s %N entity:%u section:%u %skeyed to %P",
18056 : 580 : d->get_entity_kind () == depset::EK_DECL
18057 : : ? "member" : "specialization", d->get_entity (),
18058 : : d->cluster, cache_section, also, cache_ns, cache_id);
18059 : : }
18060 : 2580 : sec.end (to, to->name (MOD_SNAME_PFX ".pnd"), crc_p);
18061 : 2580 : dump.outdent ();
18062 : :
18063 : 2580 : return count;
18064 : 2580 : }
18065 : :
18066 : : bool
18067 : 1466 : module_state::read_pendings (unsigned count)
18068 : : {
18069 : 1466 : trees_in sec (this);
18070 : :
18071 : 1466 : if (!sec.begin (loc, from (), MOD_SNAME_PFX ".pnd"))
18072 : : return false;
18073 : :
18074 : 1764 : dump () && dump ("Reading %u pendings", count);
18075 : 1466 : dump.indent ();
18076 : :
18077 : 240608 : for (unsigned ix = 0; ix != count; ix++)
18078 : : {
18079 : 239142 : pending_key key;
18080 : 239142 : unsigned index;
18081 : :
18082 : 239142 : key.ns = sec.tree_node ();
18083 : 239142 : key.id = sec.tree_node ();
18084 : 239142 : index = sec.u ();
18085 : :
18086 : 239142 : if (!key.ns || !key.id
18087 : 239142 : || !(TREE_CODE (key.ns) == NAMESPACE_DECL
18088 : 239142 : && !DECL_NAMESPACE_ALIAS (key.ns))
18089 : 239142 : || !identifier_p (key.id)
18090 : 478284 : || index >= entity_num)
18091 : 0 : sec.set_overrun ();
18092 : :
18093 : 239142 : if (sec.get_overrun ())
18094 : : break;
18095 : :
18096 : 239881 : dump () && dump ("Pending:%u keyed to %P", index, key.ns, key.id);
18097 : :
18098 : 239142 : index += entity_lwm;
18099 : 239142 : auto &vec = pending_table->get_or_insert (key);
18100 : 239142 : vec.safe_push (index);
18101 : : }
18102 : :
18103 : 1466 : dump.outdent ();
18104 : 1466 : if (!sec.end (from ()))
18105 : : return false;
18106 : : return true;
18107 : 1466 : }
18108 : :
18109 : : /* Read & write locations. */
18110 : : enum loc_kind {
18111 : : LK_ORDINARY,
18112 : : LK_MACRO,
18113 : : LK_IMPORT_ORDINARY,
18114 : : LK_IMPORT_MACRO,
18115 : : LK_ADHOC,
18116 : : LK_RESERVED,
18117 : : };
18118 : :
18119 : : static const module_state *
18120 : 6734 : module_for_ordinary_loc (location_t loc)
18121 : : {
18122 : 6734 : unsigned pos = 0;
18123 : 13468 : unsigned len = ool->length () - pos;
18124 : :
18125 : 6737 : while (len)
18126 : : {
18127 : 6737 : unsigned half = len / 2;
18128 : 6737 : module_state *probe = (*ool)[pos + half];
18129 : 6737 : if (loc < probe->ordinary_locs.first)
18130 : : len = half;
18131 : 6734 : else if (loc < probe->ordinary_locs.first + probe->ordinary_locs.second)
18132 : : return probe;
18133 : : else
18134 : : {
18135 : 0 : pos += half + 1;
18136 : 0 : len = len - (half + 1);
18137 : : }
18138 : : }
18139 : :
18140 : : return nullptr;
18141 : : }
18142 : :
18143 : : static const module_state *
18144 : 15 : module_for_macro_loc (location_t loc)
18145 : : {
18146 : 15 : unsigned pos = 1;
18147 : 15 : unsigned len = modules->length () - pos;
18148 : :
18149 : 15 : while (len)
18150 : : {
18151 : 15 : unsigned half = len / 2;
18152 : 15 : module_state *probe = (*modules)[pos + half];
18153 : 15 : if (loc < probe->macro_locs.first)
18154 : : {
18155 : 0 : pos += half + 1;
18156 : 0 : len = len - (half + 1);
18157 : : }
18158 : 15 : else if (loc >= probe->macro_locs.first + probe->macro_locs.second)
18159 : : len = half;
18160 : : else
18161 : : return probe;
18162 : : }
18163 : :
18164 : : return NULL;
18165 : : }
18166 : :
18167 : : location_t
18168 : 1191 : module_state::imported_from () const
18169 : : {
18170 : 1191 : location_t from = loc;
18171 : 1191 : line_map_ordinary const *fmap
18172 : 1191 : = linemap_check_ordinary (linemap_lookup (line_table, from));
18173 : :
18174 : 1191 : if (MAP_MODULE_P (fmap))
18175 : 1191 : from = linemap_included_from (fmap);
18176 : :
18177 : 1191 : return from;
18178 : : }
18179 : :
18180 : : /* Note that LOC will need writing. This allows us to prune locations
18181 : : that are not needed. */
18182 : :
18183 : : bool
18184 : 18501544 : module_state::note_location (location_t loc)
18185 : : {
18186 : 18501544 : bool added = false;
18187 : 18501544 : if (!macro_loc_table && !ord_loc_table)
18188 : : ;
18189 : 18501544 : else if (loc < RESERVED_LOCATION_COUNT)
18190 : : ;
18191 : 16847397 : else if (IS_ADHOC_LOC (loc))
18192 : : {
18193 : 2025196 : location_t locus = get_location_from_adhoc_loc (line_table, loc);
18194 : 2025196 : note_location (locus);
18195 : 2025196 : source_range range = get_range_from_loc (line_table, loc);
18196 : 2025196 : if (range.m_start != locus)
18197 : 1959204 : note_location (range.m_start);
18198 : 2025196 : note_location (range.m_finish);
18199 : : }
18200 : 14822201 : else if (loc >= LINEMAPS_MACRO_LOWEST_LOCATION (line_table))
18201 : : {
18202 : 1178184 : if (spans.macro (loc))
18203 : : {
18204 : 1178169 : const line_map *map = linemap_lookup (line_table, loc);
18205 : 1178169 : const line_map_macro *mac_map = linemap_check_macro (map);
18206 : 1178169 : hashval_t hv = macro_loc_traits::hash (mac_map);
18207 : 1178169 : macro_loc_info *slot
18208 : 1178169 : = macro_loc_table->find_slot_with_hash (mac_map, hv, INSERT);
18209 : 1178169 : if (!slot->src)
18210 : : {
18211 : 134799 : slot->src = mac_map;
18212 : 134799 : slot->remap = 0;
18213 : : // Expansion locations could themselves be from a
18214 : : // macro, we need to note them all.
18215 : 134799 : note_location (mac_map->m_expansion);
18216 : 134799 : gcc_checking_assert (mac_map->n_tokens);
18217 : 134799 : location_t tloc = UNKNOWN_LOCATION;
18218 : 4833723 : for (unsigned ix = mac_map->n_tokens * 2; ix--;)
18219 : 4698924 : if (mac_map->macro_locations[ix] != tloc)
18220 : : {
18221 : 2488477 : tloc = mac_map->macro_locations[ix];
18222 : 2488477 : note_location (tloc);
18223 : : }
18224 : : added = true;
18225 : : }
18226 : : }
18227 : : }
18228 : 13644017 : else if (IS_ORDINARY_LOC (loc))
18229 : : {
18230 : 13644017 : if (spans.ordinary (loc))
18231 : : {
18232 : 13637275 : const line_map *map = linemap_lookup (line_table, loc);
18233 : 13637275 : const line_map_ordinary *ord_map = linemap_check_ordinary (map);
18234 : 13637275 : ord_loc_info lkup;
18235 : 13637275 : lkup.src = ord_map;
18236 : 13637275 : lkup.span = loc_one << ord_map->m_column_and_range_bits;
18237 : 13637275 : lkup.offset = (loc - MAP_START_LOCATION (ord_map)) & ~(lkup.span - 1);
18238 : 13637275 : lkup.remap = 0;
18239 : 13637275 : ord_loc_info *slot = (ord_loc_table->find_slot_with_hash
18240 : 13637275 : (lkup, ord_loc_traits::hash (lkup), INSERT));
18241 : 13637275 : if (!slot->src)
18242 : : {
18243 : 1637793 : *slot = lkup;
18244 : 1637793 : added = true;
18245 : : }
18246 : : }
18247 : : }
18248 : : else
18249 : 0 : gcc_unreachable ();
18250 : 18501544 : return added;
18251 : : }
18252 : :
18253 : : /* If we're not streaming, record that we need location LOC.
18254 : : Otherwise stream it. */
18255 : :
18256 : : void
18257 : 27444338 : module_state::write_location (bytes_out &sec, location_t loc)
18258 : : {
18259 : 27444338 : if (!sec.streaming_p ())
18260 : : {
18261 : 9606904 : note_location (loc);
18262 : 9606904 : return;
18263 : : }
18264 : :
18265 : 17837434 : if (loc < RESERVED_LOCATION_COUNT)
18266 : : {
18267 : 1693266 : dump (dumper::LOCATION) && dump ("Reserved location %K", loc);
18268 : 1693248 : sec.loc (LK_RESERVED + loc);
18269 : : }
18270 : 16144186 : else if (IS_ADHOC_LOC (loc))
18271 : : {
18272 : 1972308 : dump (dumper::LOCATION) && dump ("Adhoc location");
18273 : 1972305 : sec.u (LK_ADHOC);
18274 : 1972305 : location_t locus = get_location_from_adhoc_loc (line_table, loc);
18275 : 1972305 : write_location (sec, locus);
18276 : 1972305 : source_range range = get_range_from_loc (line_table, loc);
18277 : 1972305 : if (range.m_start == locus)
18278 : : /* Compress. */
18279 : 61743 : range.m_start = UNKNOWN_LOCATION;
18280 : 1972305 : write_location (sec, range.m_start);
18281 : 1972305 : write_location (sec, range.m_finish);
18282 : 1972305 : unsigned discriminator = get_discriminator_from_adhoc_loc (line_table, loc);
18283 : 1972305 : sec.u (discriminator);
18284 : : }
18285 : 14171881 : else if (loc >= LINEMAPS_MACRO_LOWEST_LOCATION (line_table))
18286 : : {
18287 : 1171251 : const macro_loc_info *info = nullptr;
18288 : 1171251 : line_map_uint_t offset = 0;
18289 : 1171251 : if (unsigned hwm = macro_loc_remap->length ())
18290 : : {
18291 : 1171245 : info = macro_loc_remap->begin ();
18292 : 17361622 : while (hwm != 1)
18293 : : {
18294 : 15019132 : unsigned mid = hwm / 2;
18295 : 15019132 : if (MAP_START_LOCATION (info[mid].src) <= loc)
18296 : : {
18297 : 7601120 : info += mid;
18298 : 7601120 : hwm -= mid;
18299 : : }
18300 : : else
18301 : : hwm = mid;
18302 : : }
18303 : 1171245 : offset = loc - MAP_START_LOCATION (info->src);
18304 : 1171245 : if (offset > info->src->n_tokens)
18305 : 9 : info = nullptr;
18306 : : }
18307 : :
18308 : 1171251 : gcc_checking_assert (bool (info) == bool (spans.macro (loc)));
18309 : :
18310 : 1171251 : if (info)
18311 : : {
18312 : 1171236 : offset += info->remap;
18313 : 1171236 : sec.u (LK_MACRO);
18314 : 1171236 : sec.loc (offset);
18315 : 1171236 : dump (dumper::LOCATION)
18316 : 9 : && dump ("Macro location %K output %K", loc, offset);
18317 : : }
18318 : 15 : else if (const module_state *import = module_for_macro_loc (loc))
18319 : : {
18320 : 15 : auto off = loc - import->macro_locs.first;
18321 : 15 : sec.u (LK_IMPORT_MACRO);
18322 : 15 : sec.u (import->remap);
18323 : 15 : sec.loc (off);
18324 : 15 : dump (dumper::LOCATION)
18325 : 0 : && dump ("Imported macro location %K output %u:%K",
18326 : 0 : loc, import->remap, off);
18327 : : }
18328 : : else
18329 : 0 : gcc_unreachable ();
18330 : : }
18331 : 13000630 : else if (IS_ORDINARY_LOC (loc))
18332 : : {
18333 : : /* If we ran out of locations for imported decls, this location could
18334 : : be a module unit's location. In that case, remap the location
18335 : : to be where we imported the module from. */
18336 : 13000630 : if (spans.locations_exhausted_p () || CHECKING_P)
18337 : : {
18338 : 13000630 : const line_map_ordinary *map
18339 : 13000630 : = linemap_check_ordinary (linemap_lookup (line_table, loc));
18340 : 13000630 : if (MAP_MODULE_P (map) && loc == MAP_START_LOCATION (map))
18341 : : {
18342 : 0 : gcc_checking_assert (spans.locations_exhausted_p ());
18343 : 0 : write_location (sec, linemap_included_from (map));
18344 : 0 : return;
18345 : : }
18346 : : }
18347 : :
18348 : 13000630 : const ord_loc_info *info = nullptr;
18349 : 13000630 : line_map_uint_t offset = 0;
18350 : 13000630 : if (line_map_uint_t hwm = ord_loc_remap->length ())
18351 : : {
18352 : 13000630 : info = ord_loc_remap->begin ();
18353 : 189493646 : while (hwm != 1)
18354 : : {
18355 : 163492386 : auto mid = hwm / 2;
18356 : 163492386 : if (MAP_START_LOCATION (info[mid].src) + info[mid].offset <= loc)
18357 : : {
18358 : 86462053 : info += mid;
18359 : 86462053 : hwm -= mid;
18360 : : }
18361 : : else
18362 : : hwm = mid;
18363 : : }
18364 : 13000630 : offset = loc - MAP_START_LOCATION (info->src) - info->offset;
18365 : 13000630 : if (offset > info->span)
18366 : 6734 : info = nullptr;
18367 : : }
18368 : :
18369 : 13000630 : gcc_checking_assert (bool (info) == bool (spans.ordinary (loc)));
18370 : :
18371 : 13000630 : if (info)
18372 : : {
18373 : 12993896 : offset += info->remap;
18374 : 12993896 : sec.u (LK_ORDINARY);
18375 : 12993896 : sec.loc (offset);
18376 : :
18377 : 12993896 : dump (dumper::LOCATION)
18378 : 78 : && dump ("Ordinary location %K output %K", loc, offset);
18379 : : }
18380 : 6734 : else if (const module_state *import = module_for_ordinary_loc (loc))
18381 : : {
18382 : 6734 : auto off = loc - import->ordinary_locs.first;
18383 : 6734 : sec.u (LK_IMPORT_ORDINARY);
18384 : 6734 : sec.u (import->remap);
18385 : 6734 : sec.loc (off);
18386 : 6734 : dump (dumper::LOCATION)
18387 : 0 : && dump ("Imported ordinary location %K output %u:%K",
18388 : 0 : loc, import->remap, off);
18389 : : }
18390 : : else
18391 : 0 : gcc_unreachable ();
18392 : : }
18393 : : else
18394 : 0 : gcc_unreachable ();
18395 : : }
18396 : :
18397 : : location_t
18398 : 18725958 : module_state::read_location (bytes_in &sec) const
18399 : : {
18400 : 18725958 : location_t locus = UNKNOWN_LOCATION;
18401 : 18725958 : unsigned kind = sec.u ();
18402 : 18725958 : switch (kind)
18403 : : {
18404 : 1728138 : default:
18405 : 1728138 : {
18406 : 1728138 : if (kind < LK_RESERVED + RESERVED_LOCATION_COUNT)
18407 : 1728138 : locus = location_t (kind - LK_RESERVED);
18408 : : else
18409 : 0 : sec.set_overrun ();
18410 : 1728138 : dump (dumper::LOCATION)
18411 : 0 : && dump ("Reserved location %K", locus);
18412 : : }
18413 : : break;
18414 : :
18415 : 1965277 : case LK_ADHOC:
18416 : 1965277 : {
18417 : 1965277 : dump (dumper::LOCATION) && dump ("Adhoc location");
18418 : 1965277 : locus = read_location (sec);
18419 : 1965277 : source_range range;
18420 : 1965277 : range.m_start = read_location (sec);
18421 : 1965277 : if (range.m_start == UNKNOWN_LOCATION)
18422 : 60024 : range.m_start = locus;
18423 : 1965277 : range.m_finish = read_location (sec);
18424 : 1965277 : unsigned discriminator = sec.u ();
18425 : 1965277 : if (locus != loc && range.m_start != loc && range.m_finish != loc)
18426 : 1965277 : locus = line_table->get_or_create_combined_loc (locus, range,
18427 : : nullptr, discriminator);
18428 : : }
18429 : : break;
18430 : :
18431 : 1462136 : case LK_MACRO:
18432 : 1462136 : {
18433 : 1462136 : auto off = sec.loc ();
18434 : :
18435 : 1462136 : if (macro_locs.second)
18436 : : {
18437 : 1462136 : if (off < macro_locs.second)
18438 : 1462136 : locus = off + macro_locs.first;
18439 : : else
18440 : 0 : sec.set_overrun ();
18441 : : }
18442 : : else
18443 : 0 : locus = loc;
18444 : 1462136 : dump (dumper::LOCATION)
18445 : 0 : && dump ("Macro %K becoming %K", off, locus);
18446 : : }
18447 : : break;
18448 : :
18449 : 13567399 : case LK_ORDINARY:
18450 : 13567399 : {
18451 : 13567399 : auto off = sec.loc ();
18452 : 13567399 : if (ordinary_locs.second)
18453 : : {
18454 : 13567399 : if (off < ordinary_locs.second)
18455 : 13567399 : locus = off + ordinary_locs.first;
18456 : : else
18457 : 0 : sec.set_overrun ();
18458 : : }
18459 : : else
18460 : 0 : locus = loc;
18461 : :
18462 : 13567399 : dump (dumper::LOCATION)
18463 : 0 : && dump ("Ordinary location %K becoming %K", off, locus);
18464 : : }
18465 : : break;
18466 : :
18467 : 3008 : case LK_IMPORT_MACRO:
18468 : 3008 : case LK_IMPORT_ORDINARY:
18469 : 3008 : {
18470 : 3008 : unsigned mod = sec.u ();
18471 : 3008 : location_t off = sec.loc ();
18472 : 3008 : const module_state *import = NULL;
18473 : :
18474 : 3008 : if (!mod && !slurp->remap)
18475 : : /* This is an early read of a partition location during the
18476 : : read of our ordinary location map. */
18477 : : import = this;
18478 : : else
18479 : : {
18480 : 3008 : mod = slurp->remap_module (mod);
18481 : 3008 : if (!mod)
18482 : 0 : sec.set_overrun ();
18483 : : else
18484 : 3008 : import = (*modules)[mod];
18485 : : }
18486 : :
18487 : 3008 : if (import)
18488 : : {
18489 : 3008 : if (kind == LK_IMPORT_MACRO)
18490 : : {
18491 : 24 : if (!import->macro_locs.second)
18492 : 0 : locus = import->loc;
18493 : 24 : else if (off < import->macro_locs.second)
18494 : 24 : locus = off + import->macro_locs.first;
18495 : : else
18496 : 0 : sec.set_overrun ();
18497 : : }
18498 : : else
18499 : : {
18500 : 2984 : if (!import->ordinary_locs.second)
18501 : 0 : locus = import->loc;
18502 : 2984 : else if (off < import->ordinary_locs.second)
18503 : 2984 : locus = import->ordinary_locs.first + off;
18504 : : else
18505 : 0 : sec.set_overrun ();
18506 : : }
18507 : : }
18508 : : }
18509 : : break;
18510 : : }
18511 : :
18512 : 18725958 : return locus;
18513 : : }
18514 : :
18515 : : /* Allocate hash tables to record needed locations. */
18516 : :
18517 : : void
18518 : 2609 : module_state::write_init_maps ()
18519 : : {
18520 : 2609 : macro_loc_table = new hash_table<macro_loc_traits> (EXPERIMENT (1, 400));
18521 : 2609 : ord_loc_table = new hash_table<ord_loc_traits> (EXPERIMENT (1, 400));
18522 : 2609 : }
18523 : :
18524 : : /* Prepare the span adjustments. We prune unneeded locations -- at
18525 : : this point every needed location must have been seen by
18526 : : note_location. */
18527 : :
18528 : : range_t
18529 : 2580 : module_state::write_prepare_maps (module_state_config *cfg, bool has_partitions)
18530 : : {
18531 : 2856 : dump () && dump ("Preparing locations");
18532 : 2580 : dump.indent ();
18533 : :
18534 : 2856 : dump () && dump ("Reserved locations [%K,%K) macro [%K,%K)",
18535 : 276 : spans[loc_spans::SPAN_RESERVED].ordinary.first,
18536 : 276 : spans[loc_spans::SPAN_RESERVED].ordinary.second,
18537 : 276 : spans[loc_spans::SPAN_RESERVED].macro.first,
18538 : 276 : spans[loc_spans::SPAN_RESERVED].macro.second);
18539 : :
18540 : 2580 : range_t info {0, 0};
18541 : :
18542 : : // Sort the noted lines.
18543 : 2580 : vec_alloc (ord_loc_remap, ord_loc_table->size ());
18544 : 2580 : for (auto iter = ord_loc_table->begin (), end = ord_loc_table->end ();
18545 : 3255522 : iter != end; ++iter)
18546 : 1626471 : ord_loc_remap->quick_push (*iter);
18547 : 2580 : ord_loc_remap->qsort (&ord_loc_info::compare);
18548 : :
18549 : : // Note included-from maps.
18550 : 2580 : bool added = false;
18551 : 2580 : const line_map_ordinary *current = nullptr;
18552 : 1634211 : for (auto iter = ord_loc_remap->begin (), end = ord_loc_remap->end ();
18553 : 1629051 : iter != end; ++iter)
18554 : 1626471 : if (iter->src != current)
18555 : : {
18556 : 27864 : current = iter->src;
18557 : 10810 : for (auto probe = current;
18558 : 27864 : auto from = linemap_included_from (probe);
18559 : 10810 : probe = linemap_check_ordinary (linemap_lookup (line_table, from)))
18560 : : {
18561 : 24939 : if (has_partitions)
18562 : : {
18563 : : // Partition locations need to elide their module map
18564 : : // entry.
18565 : 190 : probe
18566 : 190 : = linemap_check_ordinary (linemap_lookup (line_table, from));
18567 : 190 : if (MAP_MODULE_P (probe))
18568 : 160 : from = linemap_included_from (probe);
18569 : : }
18570 : :
18571 : 24939 : if (!note_location (from))
18572 : : break;
18573 : 10810 : added = true;
18574 : 10810 : }
18575 : : }
18576 : 2580 : if (added)
18577 : : {
18578 : : // Reconstruct the line array as we added items to the hash table.
18579 : 469 : vec_free (ord_loc_remap);
18580 : 469 : vec_alloc (ord_loc_remap, ord_loc_table->size ());
18581 : 469 : for (auto iter = ord_loc_table->begin (), end = ord_loc_table->end ();
18582 : 3253449 : iter != end; ++iter)
18583 : 1626490 : ord_loc_remap->quick_push (*iter);
18584 : 469 : ord_loc_remap->qsort (&ord_loc_info::compare);
18585 : : }
18586 : 2580 : delete ord_loc_table;
18587 : 2580 : ord_loc_table = nullptr;
18588 : :
18589 : : // Merge (sufficiently) adjacent spans, and calculate remapping.
18590 : 2580 : constexpr line_map_uint_t adjacency = 2; // Allow 2 missing lines.
18591 : 5160 : auto begin = ord_loc_remap->begin (), end = ord_loc_remap->end ();
18592 : 2580 : auto dst = begin;
18593 : 2580 : line_map_uint_t offset = 0;
18594 : 2580 : unsigned range_bits = 0;
18595 : 2580 : ord_loc_info *base = nullptr;
18596 : 1639861 : for (auto iter = begin; iter != end; ++iter)
18597 : : {
18598 : 1637281 : if (base && iter->src == base->src)
18599 : : {
18600 : 3049638 : if (base->offset + base->span +
18601 : 1613736 : ((adjacency << base->src->m_column_and_range_bits)
18602 : : // If there are few c&r bits, allow further separation.
18603 : 1613736 : | (adjacency << 4))
18604 : 1613736 : >= iter->offset)
18605 : : {
18606 : : // Merge.
18607 : 1435902 : offset -= base->span;
18608 : 1435902 : base->span = iter->offset + iter->span - base->offset;
18609 : 1435902 : offset += base->span;
18610 : 1435902 : continue;
18611 : : }
18612 : : }
18613 : 23545 : else if (range_bits < iter->src->m_range_bits)
18614 : 2484 : range_bits = iter->src->m_range_bits;
18615 : :
18616 : 201379 : offset += ((loc_one << iter->src->m_range_bits) - 1);
18617 : 201379 : offset &= ~((loc_one << iter->src->m_range_bits) - 1);
18618 : 201379 : iter->remap = offset;
18619 : 201379 : offset += iter->span;
18620 : 201379 : base = dst;
18621 : 201379 : *dst++ = *iter;
18622 : : }
18623 : 2580 : ord_loc_remap->truncate (dst - begin);
18624 : :
18625 : 2580 : info.first = ord_loc_remap->length ();
18626 : 2580 : cfg->ordinary_locs = offset;
18627 : 2580 : cfg->loc_range_bits = range_bits;
18628 : 2856 : dump () && dump ("Ordinary maps:%K locs:%K range_bits:%u",
18629 : : info.first,
18630 : : cfg->ordinary_locs,
18631 : : cfg->loc_range_bits);
18632 : :
18633 : : // Remap the macro locations.
18634 : 2580 : vec_alloc (macro_loc_remap, macro_loc_table->size ());
18635 : 2580 : for (auto iter = macro_loc_table->begin (), end = macro_loc_table->end ();
18636 : 272178 : iter != end; ++iter)
18637 : 134799 : macro_loc_remap->quick_push (*iter);
18638 : 2580 : delete macro_loc_table;
18639 : 2580 : macro_loc_table = nullptr;
18640 : :
18641 : 2580 : macro_loc_remap->qsort (¯o_loc_info::compare);
18642 : 2580 : offset = 0;
18643 : 7740 : for (auto iter = macro_loc_remap->begin (), end = macro_loc_remap->end ();
18644 : 137379 : iter != end; ++iter)
18645 : : {
18646 : 134799 : auto mac = iter->src;
18647 : 134799 : iter->remap = offset;
18648 : 134799 : offset += mac->n_tokens;
18649 : : }
18650 : 2580 : info.second = macro_loc_remap->length ();
18651 : 2580 : cfg->macro_locs = offset;
18652 : :
18653 : 2856 : dump () && dump ("Macro maps:%K locs:%K", info.second, cfg->macro_locs);
18654 : :
18655 : 2580 : dump.outdent ();
18656 : :
18657 : : // If we have no ordinary locs, we must also have no macro locs.
18658 : 2580 : gcc_checking_assert (cfg->ordinary_locs || !cfg->macro_locs);
18659 : :
18660 : 2580 : return info;
18661 : : }
18662 : :
18663 : : bool
18664 : 2811 : module_state::read_prepare_maps (const module_state_config *cfg)
18665 : : {
18666 : 2811 : location_t ordinary = line_table->highest_location + 1;
18667 : 2811 : ordinary += cfg->ordinary_locs;
18668 : :
18669 : 2811 : location_t macro = LINEMAPS_MACRO_LOWEST_LOCATION (line_table);
18670 : 2811 : macro -= cfg->macro_locs;
18671 : :
18672 : 2811 : if (ordinary < LINE_MAP_MAX_LOCATION_WITH_COLS
18673 : 2811 : && macro >= LINE_MAP_MAX_LOCATION)
18674 : : /* OK, we have enough locations. */
18675 : : return true;
18676 : :
18677 : 0 : ordinary_locs.first = ordinary_locs.second = 0;
18678 : 0 : macro_locs.first = macro_locs.second = 0;
18679 : :
18680 : 0 : spans.report_location_exhaustion (loc);
18681 : :
18682 : : return false;
18683 : : }
18684 : :
18685 : : /* Write & read the location maps. Not called if there are no
18686 : : locations. */
18687 : :
18688 : : void
18689 : 2484 : module_state::write_ordinary_maps (elf_out *to, range_t &info,
18690 : : bool has_partitions, unsigned *crc_p)
18691 : : {
18692 : 2738 : dump () && dump ("Writing ordinary location maps");
18693 : 2484 : dump.indent ();
18694 : :
18695 : 2484 : vec<const char *> filenames;
18696 : 2484 : filenames.create (20);
18697 : :
18698 : : /* Determine the unique filenames. */
18699 : 2484 : const line_map_ordinary *current = nullptr;
18700 : 208831 : for (auto iter = ord_loc_remap->begin (), end = ord_loc_remap->end ();
18701 : 203863 : iter != end; ++iter)
18702 : 201379 : if (iter->src != current)
18703 : : {
18704 : 23545 : current = iter->src;
18705 : 23545 : const char *fname = ORDINARY_MAP_FILE_NAME (iter->src);
18706 : :
18707 : : /* We should never find a module linemap in an interval. */
18708 : 23545 : gcc_checking_assert (!MAP_MODULE_P (iter->src));
18709 : :
18710 : : /* We expect very few filenames, so just an array.
18711 : : (Not true when headers are still in play :() */
18712 : 1460108 : for (unsigned jx = filenames.length (); jx--;)
18713 : : {
18714 : 1424020 : const char *name = filenames[jx];
18715 : 1424020 : if (0 == strcmp (name, fname))
18716 : : {
18717 : : /* Reset the linemap's name, because for things like
18718 : : preprocessed input we could have multiple instances
18719 : : of the same name, and we'd rather not percolate
18720 : : that. */
18721 : 11002 : const_cast<line_map_ordinary *> (iter->src)->to_file = name;
18722 : 11002 : fname = NULL;
18723 : 11002 : break;
18724 : : }
18725 : : }
18726 : 23545 : if (fname)
18727 : 12543 : filenames.safe_push (fname);
18728 : : }
18729 : :
18730 : 2484 : bytes_out sec (to);
18731 : 2484 : sec.begin ();
18732 : :
18733 : : /* Write the filenames. */
18734 : 2484 : unsigned len = filenames.length ();
18735 : 2484 : sec.u (len);
18736 : 2738 : dump () && dump ("%u source file names", len);
18737 : 15027 : for (unsigned ix = 0; ix != len; ix++)
18738 : : {
18739 : 12543 : const char *fname = filenames[ix];
18740 : 12558 : dump (dumper::LOCATION) && dump ("Source file[%u]=%s", ix, fname);
18741 : 12543 : sec.str (fname);
18742 : : }
18743 : :
18744 : 2484 : sec.loc (info.first); /* Num maps. */
18745 : 2484 : const ord_loc_info *base = nullptr;
18746 : 208831 : for (auto iter = ord_loc_remap->begin (), end = ord_loc_remap->end ();
18747 : 203863 : iter != end; ++iter)
18748 : : {
18749 : 201379 : dump (dumper::LOCATION)
18750 : 36 : && dump ("Span:%K ordinary [%K+%K,+%K)->[%K,+%K)",
18751 : 36 : (location_t) (iter - ord_loc_remap->begin ()),
18752 : 18 : MAP_START_LOCATION (iter->src),
18753 : : iter->offset, iter->span, iter->remap,
18754 : : iter->span);
18755 : :
18756 : 201379 : if (!base || iter->src != base->src)
18757 : 23545 : base = iter;
18758 : 201379 : sec.loc (iter->offset - base->offset);
18759 : 201379 : if (base == iter)
18760 : : {
18761 : 23545 : sec.u (iter->src->sysp);
18762 : 23545 : sec.u (iter->src->m_range_bits);
18763 : 23545 : sec.u (iter->src->m_column_and_range_bits - iter->src->m_range_bits);
18764 : :
18765 : 23545 : const char *fname = ORDINARY_MAP_FILE_NAME (iter->src);
18766 : 4553930 : for (unsigned ix = 0; ix != filenames.length (); ix++)
18767 : 2276965 : if (filenames[ix] == fname)
18768 : : {
18769 : 23545 : sec.u (ix);
18770 : 23545 : break;
18771 : : }
18772 : 23545 : unsigned line = ORDINARY_MAP_STARTING_LINE_NUMBER (iter->src);
18773 : 23545 : line += iter->offset >> iter->src->m_column_and_range_bits;
18774 : 23545 : sec.u (line);
18775 : : }
18776 : 201379 : sec.loc (iter->remap);
18777 : 201379 : if (base == iter)
18778 : : {
18779 : : /* Write the included from location, which means reading it
18780 : : while reading in the ordinary maps. So we'd better not
18781 : : be getting ahead of ourselves. */
18782 : 23545 : location_t from = linemap_included_from (iter->src);
18783 : 23545 : gcc_checking_assert (from < MAP_START_LOCATION (iter->src));
18784 : 23545 : if (from != UNKNOWN_LOCATION && has_partitions)
18785 : : {
18786 : : /* A partition's span will have a from pointing at a
18787 : : MODULE_INC. Find that map's from. */
18788 : 184 : line_map_ordinary const *fmap
18789 : 184 : = linemap_check_ordinary (linemap_lookup (line_table, from));
18790 : 184 : if (MAP_MODULE_P (fmap))
18791 : 154 : from = linemap_included_from (fmap);
18792 : : }
18793 : 23545 : write_location (sec, from);
18794 : : }
18795 : : }
18796 : :
18797 : 2484 : filenames.release ();
18798 : :
18799 : 2484 : sec.end (to, to->name (MOD_SNAME_PFX ".olm"), crc_p);
18800 : 2484 : dump.outdent ();
18801 : 2484 : }
18802 : :
18803 : : /* Return the prefix to use for dumping a #pragma diagnostic change to DK. */
18804 : :
18805 : : static const char *
18806 : 882 : dk_string (enum diagnostics::kind dk)
18807 : : {
18808 : 882 : gcc_assert (dk > diagnostics::kind::unspecified
18809 : : && dk < diagnostics::kind::last_diagnostic_kind);
18810 : 882 : if (dk == diagnostics::kind::ignored)
18811 : : /* diagnostics/kinds.def has an empty string for ignored. */
18812 : : return "ignored: ";
18813 : : else
18814 : 0 : return diagnostics::get_text_for_kind (dk);
18815 : : }
18816 : :
18817 : : /* Dump one #pragma GCC diagnostic entry. */
18818 : :
18819 : : static bool
18820 : 1796 : dump_dc_change (unsigned index, unsigned opt, enum diagnostics::kind dk)
18821 : : {
18822 : 1796 : if (dk == diagnostics::kind::pop)
18823 : 914 : return dump (" Index %u: pop from %d", index, opt);
18824 : : else
18825 : 882 : return dump (" Index %u: %s%s", index, dk_string (dk),
18826 : 1764 : cl_options[opt].opt_text);
18827 : : }
18828 : :
18829 : : /* Write out any #pragma GCC diagnostic info to the .dgc section. */
18830 : :
18831 : : void
18832 : 5064 : module_state::write_diagnostic_classification (elf_out *to,
18833 : : diagnostics::context *dc,
18834 : : unsigned *crc_p)
18835 : : {
18836 : 5064 : auto &changes = dc->get_classification_history ();
18837 : :
18838 : 5064 : bytes_out sec (to);
18839 : 5064 : if (sec.streaming_p ())
18840 : : {
18841 : 2484 : sec.begin ();
18842 : 2738 : dump () && dump ("Writing diagnostic change locations");
18843 : 2484 : dump.indent ();
18844 : : }
18845 : :
18846 : 5064 : unsigned len = changes.length ();
18847 : :
18848 : : /* We don't want to write out any entries that came from one of our imports.
18849 : : But then we need to adjust the total, and change diagnostics::kind::pop
18850 : : targets to match the index in our actual output. So remember how many
18851 : : lines we had skipped at each step, where -1 means this line itself
18852 : : is skipped. */
18853 : 5064 : int skips = 0;
18854 : 5064 : auto_vec<int> skips_at (len);
18855 : 5064 : skips_at.safe_grow (len);
18856 : :
18857 : 51228 : for (unsigned i = 0; i < len; ++i)
18858 : : {
18859 : 46164 : const auto &c = changes[i];
18860 : 46164 : skips_at[i] = skips;
18861 : 46164 : if (linemap_location_from_module_p (line_table, c.location))
18862 : : {
18863 : 9458 : ++skips;
18864 : 9458 : skips_at[i] = -1;
18865 : 9458 : continue;
18866 : : }
18867 : : }
18868 : :
18869 : 5064 : if (sec.streaming_p ())
18870 : : {
18871 : 2484 : sec.u (len - skips);
18872 : 2738 : dump () && dump ("Diagnostic changes: %u", len - skips);
18873 : : }
18874 : :
18875 : 51228 : for (unsigned i = 0; i < len; ++i)
18876 : : {
18877 : 46164 : if (skips_at[i] == -1)
18878 : 9458 : continue;
18879 : :
18880 : 36706 : const auto &c = changes[i];
18881 : 36706 : write_location (sec, c.location);
18882 : 36706 : if (sec.streaming_p ())
18883 : : {
18884 : 18353 : unsigned opt = c.option;
18885 : 18353 : if (c.kind == diagnostics::kind::pop)
18886 : 9365 : opt -= skips_at[opt];
18887 : 18353 : sec.u (opt);
18888 : 18353 : sec.u (static_cast<unsigned> (c.kind));
18889 : 47905 : dump () && dump_dc_change (i - skips_at[i], opt, c.kind);
18890 : : }
18891 : : }
18892 : :
18893 : 5064 : if (sec.streaming_p ())
18894 : : {
18895 : 2484 : sec.end (to, to->name (MOD_SNAME_PFX ".dgc"), crc_p);
18896 : 2484 : dump.outdent ();
18897 : : }
18898 : 5064 : }
18899 : :
18900 : : /* Read any #pragma GCC diagnostic info from the .dgc section. */
18901 : :
18902 : : bool
18903 : 2753 : module_state::read_diagnostic_classification (diagnostics::context *dc)
18904 : : {
18905 : 2753 : bytes_in sec;
18906 : :
18907 : 2753 : if (!sec.begin (loc, from (), MOD_SNAME_PFX ".dgc"))
18908 : : return false;
18909 : :
18910 : 3237 : dump () && dump ("Reading diagnostic change locations");
18911 : 2753 : dump.indent ();
18912 : :
18913 : 2753 : unsigned len = sec.u ();
18914 : 3237 : dump () && dump ("Diagnostic changes: %u", len);
18915 : :
18916 : 2753 : auto &changes = dc->get_classification_history ();
18917 : 2753 : int offset = changes.length ();
18918 : 2753 : changes.reserve (len + 1);
18919 : 25591 : for (unsigned i = 0; i < len; ++i)
18920 : : {
18921 : 22838 : location_t loc = read_location (sec);
18922 : 22838 : int opt = sec.u ();
18923 : 22838 : enum diagnostics::kind kind = (enum diagnostics::kind) sec.u ();
18924 : 22838 : if (kind == diagnostics::kind::pop)
18925 : : /* For a pop, opt is the 'changes' index to return to. */
18926 : 11680 : opt += offset;
18927 : 22838 : changes.quick_push ({ loc, opt, kind });
18928 : 22893 : dump () && dump_dc_change (changes.length () - 1, opt, kind);
18929 : : }
18930 : :
18931 : : /* Did the import pop all its diagnostic changes? */
18932 : 2753 : bool last_was_reset = (len == 0);
18933 : 2753 : if (len)
18934 : 226 : for (int i = changes.length () - 1; ; --i)
18935 : : {
18936 : 10126 : gcc_checking_assert (i >= offset);
18937 : :
18938 : 10126 : const auto &c = changes[i];
18939 : 10126 : if (c.kind != diagnostics::kind::pop)
18940 : : break;
18941 : 10117 : else if (c.option == offset)
18942 : : {
18943 : : last_was_reset = true;
18944 : : break;
18945 : : }
18946 : : else
18947 : : /* As in update_effective_level_from_pragmas, the loop will decrement
18948 : : i so we actually jump to c.option - 1. */
18949 : 10013 : i = c.option;
18950 : 10013 : }
18951 : 2753 : if (!last_was_reset)
18952 : : {
18953 : : /* It didn't, so add a pop at its last location to avoid affecting later
18954 : : imports. */
18955 : 9 : location_t last_loc = ordinary_locs.first + ordinary_locs.second - 1;
18956 : 9 : changes.quick_push ({ last_loc, offset, diagnostics::kind::pop });
18957 : 15 : dump () && dump (" Adding final pop from index %d", offset);
18958 : : }
18959 : :
18960 : 2753 : dump.outdent ();
18961 : 2753 : if (!sec.end (from ()))
18962 : : return false;
18963 : :
18964 : : return true;
18965 : 2753 : }
18966 : :
18967 : : void
18968 : 120 : module_state::write_macro_maps (elf_out *to, range_t &info, unsigned *crc_p)
18969 : : {
18970 : 132 : dump () && dump ("Writing macro location maps");
18971 : 120 : dump.indent ();
18972 : :
18973 : 120 : bytes_out sec (to);
18974 : 120 : sec.begin ();
18975 : :
18976 : 132 : dump () && dump ("Macro maps:%K", info.second);
18977 : 120 : sec.loc (info.second);
18978 : :
18979 : 120 : line_map_uint_t macro_num = 0;
18980 : 240 : for (auto iter = macro_loc_remap->end (), begin = macro_loc_remap->begin ();
18981 : 134919 : iter-- != begin;)
18982 : : {
18983 : 134799 : auto mac = iter->src;
18984 : 134799 : sec.loc (iter->remap);
18985 : 134799 : sec.u (mac->n_tokens);
18986 : 134799 : sec.cpp_node (mac->macro);
18987 : 134799 : write_location (sec, mac->m_expansion);
18988 : 134799 : const location_t *locs = mac->macro_locations;
18989 : : /* There are lots of identical runs. */
18990 : 134799 : location_t prev = UNKNOWN_LOCATION;
18991 : 134799 : unsigned count = 0;
18992 : 134799 : unsigned runs = 0;
18993 : 4833723 : for (unsigned jx = mac->n_tokens * 2; jx--;)
18994 : : {
18995 : 4698924 : location_t tok_loc = locs[jx];
18996 : 4698924 : if (tok_loc == prev)
18997 : : {
18998 : 2210447 : count++;
18999 : 2210447 : continue;
19000 : : }
19001 : 2488477 : runs++;
19002 : 2488477 : sec.u (count);
19003 : 2488477 : count = 1;
19004 : 2488477 : prev = tok_loc;
19005 : 2488477 : write_location (sec, tok_loc);
19006 : : }
19007 : 134799 : sec.u (count);
19008 : 134799 : dump (dumper::LOCATION)
19009 : 9 : && dump ("Macro:%K %I %u/%u*2 locations [%K,%K)->%K",
19010 : 9 : macro_num, identifier (mac->macro),
19011 : : runs, mac->n_tokens,
19012 : : MAP_START_LOCATION (mac),
19013 : 9 : MAP_START_LOCATION (mac) + mac->n_tokens,
19014 : : iter->remap);
19015 : 134799 : macro_num++;
19016 : : }
19017 : 120 : gcc_assert (macro_num == info.second);
19018 : :
19019 : 120 : sec.end (to, to->name (MOD_SNAME_PFX ".mlm"), crc_p);
19020 : 120 : dump.outdent ();
19021 : 120 : }
19022 : :
19023 : : bool
19024 : 2753 : module_state::read_ordinary_maps (line_map_uint_t num_ord_locs,
19025 : : unsigned range_bits)
19026 : : {
19027 : 2753 : bytes_in sec;
19028 : :
19029 : 2753 : if (!sec.begin (loc, from (), MOD_SNAME_PFX ".olm"))
19030 : : return false;
19031 : 3237 : dump () && dump ("Reading ordinary location maps");
19032 : 2753 : dump.indent ();
19033 : :
19034 : : /* Read the filename table. */
19035 : 2753 : unsigned len = sec.u ();
19036 : 3237 : dump () && dump ("%u source file names", len);
19037 : 2753 : vec<const char *> filenames;
19038 : 2753 : filenames.create (len);
19039 : 18270 : for (unsigned ix = 0; ix != len; ix++)
19040 : : {
19041 : 15517 : size_t l;
19042 : 15517 : const char *buf = sec.str (&l);
19043 : 15517 : char *fname = XNEWVEC (char, l + 1);
19044 : 15517 : memcpy (fname, buf, l + 1);
19045 : 15517 : dump (dumper::LOCATION) && dump ("Source file[%u]=%s", ix, fname);
19046 : : /* We leak these names into the line-map table. But it
19047 : : doesn't own them. */
19048 : 15517 : filenames.quick_push (fname);
19049 : : }
19050 : :
19051 : 2753 : line_map_uint_t num_ordinary = sec.loc ();
19052 : 3237 : dump () && dump ("Ordinary maps:%K, range_bits:%u",
19053 : : num_ordinary, range_bits);
19054 : :
19055 : 2753 : location_t offset = line_table->highest_location + 1;
19056 : 2753 : offset += ((loc_one << range_bits) - 1);
19057 : 2753 : offset &= ~((loc_one << range_bits) - 1);
19058 : 2753 : ordinary_locs.first = offset;
19059 : :
19060 : 2753 : bool propagated = spans.maybe_propagate (this, offset);
19061 : 2753 : line_map_ordinary *maps = static_cast<line_map_ordinary *>
19062 : 2753 : (line_map_new_raw (line_table, false, num_ordinary));
19063 : :
19064 : 2753 : const line_map_ordinary *base = nullptr;
19065 : 269992 : for (line_map_uint_t ix = 0; ix != num_ordinary && !sec.get_overrun (); ix++)
19066 : : {
19067 : 267239 : line_map_ordinary *map = &maps[ix];
19068 : :
19069 : 267239 : location_t offset = sec.loc ();
19070 : 267239 : if (!offset)
19071 : : {
19072 : 29898 : map->reason = LC_RENAME;
19073 : 29898 : map->sysp = sec.u ();
19074 : 29898 : map->m_range_bits = sec.u ();
19075 : 29898 : map->m_column_and_range_bits = sec.u () + map->m_range_bits;
19076 : 29898 : unsigned fnum = sec.u ();
19077 : 59796 : map->to_file = (fnum < filenames.length () ? filenames[fnum] : "");
19078 : 29898 : map->to_line = sec.u ();
19079 : 29898 : base = map;
19080 : : }
19081 : : else
19082 : : {
19083 : 237341 : *map = *base;
19084 : 237341 : map->to_line += offset >> map->m_column_and_range_bits;
19085 : : }
19086 : 267239 : location_t remap = sec.loc ();
19087 : 267239 : map->start_location = remap + ordinary_locs.first;
19088 : 267239 : if (base == map)
19089 : : {
19090 : : /* Root the outermost map at our location. */
19091 : 29898 : ordinary_locs.second = remap;
19092 : 29898 : location_t from = read_location (sec);
19093 : 29898 : map->included_from = from != UNKNOWN_LOCATION ? from : loc;
19094 : : }
19095 : : }
19096 : :
19097 : 2753 : ordinary_locs.second = num_ord_locs;
19098 : : /* highest_location is the one handed out, not the next one to
19099 : : hand out. */
19100 : 2753 : line_table->highest_location = ordinary_locs.first + ordinary_locs.second - 1;
19101 : :
19102 : 2753 : if (line_table->highest_location >= LINE_MAP_MAX_LOCATION_WITH_COLS)
19103 : : /* We shouldn't run out of locations, as we checked before
19104 : : starting. */
19105 : 0 : sec.set_overrun ();
19106 : 3237 : dump () && dump ("Ordinary location [%K,+%K)",
19107 : : ordinary_locs.first, ordinary_locs.second);
19108 : :
19109 : 2753 : if (propagated)
19110 : 154 : spans.close ();
19111 : :
19112 : 2753 : filenames.release ();
19113 : :
19114 : 2753 : dump.outdent ();
19115 : 2753 : if (!sec.end (from ()))
19116 : : return false;
19117 : :
19118 : : return true;
19119 : 2753 : }
19120 : :
19121 : : bool
19122 : 133 : module_state::read_macro_maps (line_map_uint_t num_macro_locs)
19123 : : {
19124 : 133 : bytes_in sec;
19125 : :
19126 : 133 : if (!sec.begin (loc, from (), MOD_SNAME_PFX ".mlm"))
19127 : : return false;
19128 : 139 : dump () && dump ("Reading macro location maps");
19129 : 133 : dump.indent ();
19130 : :
19131 : 133 : line_map_uint_t num_macros = sec.loc ();
19132 : 139 : dump () && dump ("Macro maps:%K locs:%K",
19133 : : num_macros, num_macro_locs);
19134 : :
19135 : 266 : bool propagated = spans.maybe_propagate (this,
19136 : 133 : line_table->highest_location + 1);
19137 : :
19138 : 133 : location_t offset = LINEMAPS_MACRO_LOWEST_LOCATION (line_table);
19139 : 133 : macro_locs.second = num_macro_locs;
19140 : 133 : macro_locs.first = offset - num_macro_locs;
19141 : :
19142 : 139 : dump () && dump ("Macro loc delta %K", offset);
19143 : 139 : dump () && dump ("Macro locations [%K,%K)",
19144 : : macro_locs.first, macro_locs.second);
19145 : :
19146 : 192934 : for (line_map_uint_t ix = 0; ix != num_macros && !sec.get_overrun (); ix++)
19147 : : {
19148 : 192801 : location_t offset = sec.loc ();
19149 : 192801 : unsigned n_tokens = sec.u ();
19150 : 192801 : cpp_hashnode *node = sec.cpp_node ();
19151 : 192801 : location_t exp_loc = read_location (sec);
19152 : :
19153 : 192801 : const line_map_macro *macro
19154 : 192801 : = linemap_enter_macro (line_table, node, exp_loc, n_tokens);
19155 : 192801 : if (!macro)
19156 : : /* We shouldn't run out of locations, as we checked that we
19157 : : had enough before starting. */
19158 : : break;
19159 : 192801 : gcc_checking_assert (MAP_START_LOCATION (macro)
19160 : : == offset + macro_locs.first);
19161 : :
19162 : 192801 : location_t *locs = macro->macro_locations;
19163 : 192801 : location_t tok_loc = UNKNOWN_LOCATION;
19164 : 192801 : unsigned count = sec.u ();
19165 : 192801 : unsigned runs = 0;
19166 : 6967209 : for (unsigned jx = macro->n_tokens * 2; jx-- && !sec.get_overrun ();)
19167 : : {
19168 : 10362186 : while (!count-- && !sec.get_overrun ())
19169 : : {
19170 : 3587778 : runs++;
19171 : 3587778 : tok_loc = read_location (sec);
19172 : 3587778 : count = sec.u ();
19173 : : }
19174 : 6774408 : locs[jx] = tok_loc;
19175 : : }
19176 : 192801 : if (count)
19177 : 0 : sec.set_overrun ();
19178 : 192828 : dump (dumper::LOCATION)
19179 : 0 : && dump ("Macro:%K %I %u/%u*2 locations [%K,%K)",
19180 : : ix, identifier (node), runs, n_tokens,
19181 : : MAP_START_LOCATION (macro),
19182 : 0 : MAP_START_LOCATION (macro) + n_tokens);
19183 : : }
19184 : :
19185 : 139 : dump () && dump ("Macro location lwm:%K", macro_locs.first);
19186 : 133 : if (propagated)
19187 : 3 : spans.close ();
19188 : :
19189 : 133 : dump.outdent ();
19190 : 133 : if (!sec.end (from ()))
19191 : : return false;
19192 : :
19193 : : return true;
19194 : 133 : }
19195 : :
19196 : : /* Serialize the definition of MACRO. */
19197 : :
19198 : : void
19199 : 71523 : module_state::write_define (bytes_out &sec, const cpp_macro *macro)
19200 : : {
19201 : 71523 : sec.u (macro->count);
19202 : :
19203 : 71523 : bytes_out::bits_out bits = sec.stream_bits ();
19204 : 71523 : bits.b (macro->fun_like);
19205 : 71523 : bits.b (macro->variadic);
19206 : 71523 : bits.b (macro->syshdr);
19207 : 71523 : bits.bflush ();
19208 : :
19209 : 71523 : write_location (sec, macro->line);
19210 : 71523 : if (macro->fun_like)
19211 : : {
19212 : 9330 : sec.u (macro->paramc);
19213 : 9330 : const cpp_hashnode *const *parms = macro->parm.params;
19214 : 23524 : for (unsigned ix = 0; ix != macro->paramc; ix++)
19215 : 14194 : sec.cpp_node (parms[ix]);
19216 : : }
19217 : :
19218 : : unsigned len = 0;
19219 : 233472 : for (unsigned ix = 0; ix != macro->count; ix++)
19220 : : {
19221 : 161949 : const cpp_token *token = ¯o->exp.tokens[ix];
19222 : 161949 : write_location (sec, token->src_loc);
19223 : 161949 : sec.u (token->type);
19224 : 161949 : sec.u (token->flags);
19225 : 161949 : switch (cpp_token_val_index (token))
19226 : : {
19227 : 0 : default:
19228 : 0 : gcc_unreachable ();
19229 : :
19230 : 12488 : case CPP_TOKEN_FLD_ARG_NO:
19231 : : /* An argument reference. */
19232 : 12488 : sec.u (token->val.macro_arg.arg_no);
19233 : 12488 : sec.cpp_node (token->val.macro_arg.spelling);
19234 : 12488 : break;
19235 : :
19236 : 32152 : case CPP_TOKEN_FLD_NODE:
19237 : : /* An identifier. */
19238 : 32152 : sec.cpp_node (token->val.node.node);
19239 : 32152 : if (token->val.node.spelling == token->val.node.node)
19240 : : /* The spelling will usually be the same. so optimize
19241 : : that. */
19242 : 32152 : sec.str (NULL, 0);
19243 : : else
19244 : 0 : sec.cpp_node (token->val.node.spelling);
19245 : : break;
19246 : :
19247 : : case CPP_TOKEN_FLD_NONE:
19248 : : break;
19249 : :
19250 : 50608 : case CPP_TOKEN_FLD_STR:
19251 : : /* A string, number or comment. Not always NUL terminated,
19252 : : we stream out in a single contatenation with embedded
19253 : : NULs as that's a safe default. */
19254 : 50608 : len += token->val.str.len + 1;
19255 : 50608 : sec.u (token->val.str.len);
19256 : 50608 : break;
19257 : :
19258 : 0 : case CPP_TOKEN_FLD_SOURCE:
19259 : 0 : case CPP_TOKEN_FLD_TOKEN_NO:
19260 : 0 : case CPP_TOKEN_FLD_PRAGMA:
19261 : : /* These do not occur inside a macro itself. */
19262 : 0 : gcc_unreachable ();
19263 : : }
19264 : : }
19265 : :
19266 : 71523 : if (len)
19267 : : {
19268 : 46948 : char *ptr = reinterpret_cast<char *> (sec.buf (len));
19269 : 46948 : len = 0;
19270 : 143474 : for (unsigned ix = 0; ix != macro->count; ix++)
19271 : : {
19272 : 96526 : const cpp_token *token = ¯o->exp.tokens[ix];
19273 : 96526 : if (cpp_token_val_index (token) == CPP_TOKEN_FLD_STR)
19274 : : {
19275 : 50608 : memcpy (ptr + len, token->val.str.text,
19276 : 50608 : token->val.str.len);
19277 : 50608 : len += token->val.str.len;
19278 : 50608 : ptr[len++] = 0;
19279 : : }
19280 : : }
19281 : : }
19282 : 71523 : }
19283 : :
19284 : : /* Read a macro definition. */
19285 : :
19286 : : cpp_macro *
19287 : 631 : module_state::read_define (bytes_in &sec, cpp_reader *reader) const
19288 : : {
19289 : 631 : unsigned count = sec.u ();
19290 : : /* We rely on knowing cpp_reader's hash table is ident_hash, and
19291 : : its subobject allocator is stringpool_ggc_alloc and that is just
19292 : : a wrapper for ggc_alloc_atomic. */
19293 : 631 : cpp_macro *macro
19294 : 1262 : = (cpp_macro *)ggc_alloc_atomic (sizeof (cpp_macro)
19295 : 631 : + sizeof (cpp_token) * (count - !!count));
19296 : 631 : memset (macro, 0, sizeof (cpp_macro) + sizeof (cpp_token) * (count - !!count));
19297 : :
19298 : 631 : macro->count = count;
19299 : 631 : macro->kind = cmk_macro;
19300 : 631 : macro->imported_p = true;
19301 : :
19302 : 631 : bytes_in::bits_in bits = sec.stream_bits ();
19303 : 631 : macro->fun_like = bits.b ();
19304 : 631 : macro->variadic = bits.b ();
19305 : 631 : macro->syshdr = bits.b ();
19306 : 631 : bits.bflush ();
19307 : :
19308 : 631 : macro->line = read_location (sec);
19309 : :
19310 : 631 : if (macro->fun_like)
19311 : : {
19312 : 79 : unsigned paramc = sec.u ();
19313 : 79 : cpp_hashnode **params
19314 : 79 : = (cpp_hashnode **)ggc_alloc_atomic (sizeof (cpp_hashnode *) * paramc);
19315 : 79 : macro->paramc = paramc;
19316 : 79 : macro->parm.params = params;
19317 : 169 : for (unsigned ix = 0; ix != paramc; ix++)
19318 : 90 : params[ix] = sec.cpp_node ();
19319 : : }
19320 : :
19321 : : unsigned len = 0;
19322 : 1852 : for (unsigned ix = 0; ix != count && !sec.get_overrun (); ix++)
19323 : : {
19324 : 1221 : cpp_token *token = ¯o->exp.tokens[ix];
19325 : 1221 : token->src_loc = read_location (sec);
19326 : 1221 : token->type = cpp_ttype (sec.u ());
19327 : 1221 : token->flags = sec.u ();
19328 : 1221 : switch (cpp_token_val_index (token))
19329 : : {
19330 : 0 : default:
19331 : 0 : sec.set_overrun ();
19332 : 0 : break;
19333 : :
19334 : 73 : case CPP_TOKEN_FLD_ARG_NO:
19335 : : /* An argument reference. */
19336 : 73 : {
19337 : 73 : unsigned arg_no = sec.u ();
19338 : 73 : if (arg_no - 1 >= macro->paramc)
19339 : 0 : sec.set_overrun ();
19340 : 73 : token->val.macro_arg.arg_no = arg_no;
19341 : 73 : token->val.macro_arg.spelling = sec.cpp_node ();
19342 : : }
19343 : 73 : break;
19344 : :
19345 : 262 : case CPP_TOKEN_FLD_NODE:
19346 : : /* An identifier. */
19347 : 262 : token->val.node.node = sec.cpp_node ();
19348 : 262 : token->val.node.spelling = sec.cpp_node ();
19349 : 262 : if (!token->val.node.spelling)
19350 : 262 : token->val.node.spelling = token->val.node.node;
19351 : : break;
19352 : :
19353 : : case CPP_TOKEN_FLD_NONE:
19354 : : break;
19355 : :
19356 : 461 : case CPP_TOKEN_FLD_STR:
19357 : : /* A string, number or comment. */
19358 : 461 : token->val.str.len = sec.u ();
19359 : 461 : len += token->val.str.len + 1;
19360 : 461 : break;
19361 : : }
19362 : : }
19363 : :
19364 : 631 : if (len)
19365 : 459 : if (const char *ptr = reinterpret_cast<const char *> (sec.buf (len)))
19366 : : {
19367 : : /* There should be a final NUL. */
19368 : 459 : if (ptr[len-1])
19369 : 0 : sec.set_overrun ();
19370 : : /* cpp_alloc_token_string will add a final NUL. */
19371 : 459 : const unsigned char *buf
19372 : 459 : = cpp_alloc_token_string (reader, (const unsigned char *)ptr, len - 1);
19373 : 459 : len = 0;
19374 : 1231 : for (unsigned ix = 0; ix != count && !sec.get_overrun (); ix++)
19375 : : {
19376 : 772 : cpp_token *token = ¯o->exp.tokens[ix];
19377 : 772 : if (cpp_token_val_index (token) == CPP_TOKEN_FLD_STR)
19378 : : {
19379 : 461 : token->val.str.text = buf + len;
19380 : 461 : len += token->val.str.len;
19381 : 461 : if (buf[len++])
19382 : 0 : sec.set_overrun ();
19383 : : }
19384 : : }
19385 : : }
19386 : :
19387 : 631 : if (sec.get_overrun ())
19388 : 0 : return NULL;
19389 : : return macro;
19390 : 631 : }
19391 : :
19392 : : /* Exported macro data. */
19393 : : struct GTY(()) macro_export {
19394 : : cpp_macro *def;
19395 : : location_t undef_loc;
19396 : :
19397 : 101464 : macro_export ()
19398 : 101464 : :def (NULL), undef_loc (UNKNOWN_LOCATION)
19399 : : {
19400 : : }
19401 : : };
19402 : :
19403 : : /* Imported macro data. */
19404 : : class macro_import {
19405 : : public:
19406 : : struct slot {
19407 : : #if defined (WORDS_BIGENDIAN) && SIZEOF_VOID_P == 8
19408 : : int offset;
19409 : : #endif
19410 : : /* We need to ensure we don't use the LSB for representation, as
19411 : : that's the union discriminator below. */
19412 : : unsigned bits;
19413 : :
19414 : : #if !(defined (WORDS_BIGENDIAN) && SIZEOF_VOID_P == 8)
19415 : : int offset;
19416 : : #endif
19417 : :
19418 : : public:
19419 : : enum Layout {
19420 : : L_DEF = 1,
19421 : : L_UNDEF = 2,
19422 : : L_BOTH = 3,
19423 : : L_MODULE_SHIFT = 2
19424 : : };
19425 : :
19426 : : public:
19427 : : /* Not a regular ctor, because we put it in a union, and that's
19428 : : not allowed in C++ 98. */
19429 : 180746 : static slot ctor (unsigned module, unsigned defness)
19430 : : {
19431 : 180746 : gcc_checking_assert (defness);
19432 : 180746 : slot s;
19433 : 180746 : s.bits = defness | (module << L_MODULE_SHIFT);
19434 : 180746 : s.offset = -1;
19435 : 180746 : return s;
19436 : : }
19437 : :
19438 : : public:
19439 : 145498 : unsigned get_defness () const
19440 : : {
19441 : 145498 : return bits & L_BOTH;
19442 : : }
19443 : 103518 : unsigned get_module () const
19444 : : {
19445 : 103518 : return bits >> L_MODULE_SHIFT;
19446 : : }
19447 : 12 : void become_undef ()
19448 : : {
19449 : 12 : bits &= ~unsigned (L_DEF);
19450 : 12 : bits |= unsigned (L_UNDEF);
19451 : : }
19452 : : };
19453 : :
19454 : : private:
19455 : : typedef vec<slot, va_heap, vl_embed> ary_t;
19456 : : union either {
19457 : : /* Discriminated by bits 0|1 != 0. The expected case is that
19458 : : there will be exactly one slot per macro, hence the effort of
19459 : : packing that. */
19460 : : ary_t *ary;
19461 : : slot single;
19462 : : } u;
19463 : :
19464 : : public:
19465 : 146675 : macro_import ()
19466 : 146675 : {
19467 : 146675 : u.ary = NULL;
19468 : : }
19469 : :
19470 : : private:
19471 : 7874143 : bool single_p () const
19472 : : {
19473 : 7874143 : return u.single.bits & slot::L_BOTH;
19474 : : }
19475 : 8020827 : bool occupied_p () const
19476 : : {
19477 : 8020827 : return u.ary != NULL;
19478 : : }
19479 : :
19480 : : public:
19481 : 1851 : unsigned length () const
19482 : : {
19483 : 1851 : gcc_checking_assert (occupied_p ());
19484 : 1851 : return single_p () ? 1 : u.ary->length ();
19485 : : }
19486 : 7737394 : slot &operator[] (unsigned ix)
19487 : : {
19488 : 7737394 : gcc_checking_assert (occupied_p ());
19489 : 7737394 : if (single_p ())
19490 : : {
19491 : 7651169 : gcc_checking_assert (!ix);
19492 : 7651169 : return u.single;
19493 : : }
19494 : : else
19495 : 86225 : return (*u.ary)[ix];
19496 : : }
19497 : :
19498 : : public:
19499 : : slot &exported ();
19500 : : slot &append (unsigned module, unsigned defness);
19501 : : };
19502 : :
19503 : : /* O is a new import to append to the list for. If we're an empty
19504 : : set, initialize us. */
19505 : :
19506 : : macro_import::slot &
19507 : 180746 : macro_import::append (unsigned module, unsigned defness)
19508 : : {
19509 : 180746 : if (!occupied_p ())
19510 : : {
19511 : 146675 : u.single = slot::ctor (module, defness);
19512 : 146675 : return u.single;
19513 : : }
19514 : : else
19515 : : {
19516 : 34071 : bool single = single_p ();
19517 : 34071 : ary_t *m = single ? NULL : u.ary;
19518 : 34071 : vec_safe_reserve (m, 1 + single);
19519 : 34071 : if (single)
19520 : 34068 : m->quick_push (u.single);
19521 : 34071 : u.ary = m;
19522 : 34071 : return *u.ary->quick_push (slot::ctor (module, defness));
19523 : : }
19524 : : }
19525 : :
19526 : : /* We're going to export something. Make sure the first import slot
19527 : : is us. */
19528 : :
19529 : : macro_import::slot &
19530 : 100836 : macro_import::exported ()
19531 : : {
19532 : 100836 : if (occupied_p () && !(*this)[0].get_module ())
19533 : : {
19534 : 9 : slot &res = (*this)[0];
19535 : 9 : res.bits |= slot::L_DEF;
19536 : 9 : return res;
19537 : : }
19538 : :
19539 : 100827 : slot *a = &append (0, slot::L_DEF);
19540 : 100827 : if (!single_p ())
19541 : : {
19542 : 29852 : slot &f = (*this)[0];
19543 : 29852 : std::swap (f, *a);
19544 : 29852 : a = &f;
19545 : : }
19546 : : return *a;
19547 : : }
19548 : :
19549 : : /* The import (&exported) macros. cpp_hasnode's deferred field
19550 : : indexes this array (offset by 1, so zero means 'not present'. */
19551 : :
19552 : : static vec<macro_import, va_heap, vl_embed> *macro_imports;
19553 : :
19554 : : /* The exported macros. A macro_import slot's zeroth element's offset
19555 : : indexes this array. If the zeroth slot is not for module zero,
19556 : : there is no export. */
19557 : :
19558 : : static GTY(()) vec<macro_export, va_gc> *macro_exports;
19559 : :
19560 : : /* The reachable set of header imports from this TU. */
19561 : :
19562 : : static GTY(()) bitmap headers;
19563 : :
19564 : : /* Get the (possibly empty) macro imports for NODE. */
19565 : :
19566 : : static macro_import &
19567 : 150903 : get_macro_imports (cpp_hashnode *node)
19568 : : {
19569 : 150903 : if (node->deferred)
19570 : 4228 : return (*macro_imports)[node->deferred - 1];
19571 : :
19572 : 146675 : vec_safe_reserve (macro_imports, 1);
19573 : 146675 : node->deferred = macro_imports->length () + 1;
19574 : 146675 : return *vec_safe_push (macro_imports, macro_import ());
19575 : : }
19576 : :
19577 : : /* Get the macro export for export EXP of NODE. */
19578 : :
19579 : : static macro_export &
19580 : 100836 : get_macro_export (macro_import::slot &slot)
19581 : : {
19582 : 100836 : if (slot.offset >= 0)
19583 : 9 : return (*macro_exports)[slot.offset];
19584 : :
19585 : 100827 : vec_safe_reserve (macro_exports, 1);
19586 : 100827 : slot.offset = macro_exports->length ();
19587 : 100827 : return *macro_exports->quick_push (macro_export ());
19588 : : }
19589 : :
19590 : : /* If NODE is an exportable macro, add it to the export set. */
19591 : :
19592 : : static int
19593 : 3816858 : maybe_add_macro (cpp_reader *, cpp_hashnode *node, void *data_)
19594 : : {
19595 : 3816858 : bool exporting = false;
19596 : :
19597 : 3816858 : if (cpp_user_macro_p (node))
19598 : 490800 : if (cpp_macro *macro = node->value.macro)
19599 : : /* Ignore imported, builtins, command line and forced header macros. */
19600 : 490364 : if (!macro->imported_p
19601 : 490364 : && !macro->lazy && macro->line >= spans.main_start ())
19602 : : {
19603 : 70984 : gcc_checking_assert (macro->kind == cmk_macro);
19604 : : /* I don't want to deal with this corner case, that I suspect is
19605 : : a devil's advocate reading of the standard. */
19606 : 70984 : gcc_checking_assert (!macro->extra_tokens);
19607 : :
19608 : 70984 : macro_import::slot &slot = get_macro_imports (node).exported ();
19609 : 70984 : macro_export &exp = get_macro_export (slot);
19610 : 70984 : exp.def = macro;
19611 : 70984 : exporting = true;
19612 : : }
19613 : :
19614 : 3745874 : if (!exporting && node->deferred)
19615 : : {
19616 : 577 : macro_import &imports = (*macro_imports)[node->deferred - 1];
19617 : 577 : macro_import::slot &slot = imports[0];
19618 : 577 : if (!slot.get_module ())
19619 : : {
19620 : 546 : gcc_checking_assert (slot.get_defness ());
19621 : : exporting = true;
19622 : : }
19623 : : }
19624 : :
19625 : 70984 : if (exporting)
19626 : 71530 : static_cast<vec<cpp_hashnode *> *> (data_)->safe_push (node);
19627 : :
19628 : 3816858 : return 1; /* Don't stop. */
19629 : : }
19630 : :
19631 : : /* Order cpp_hashnodes A_ and B_ by their exported macro locations. */
19632 : :
19633 : : static int
19634 : 3730179 : macro_loc_cmp (const void *a_, const void *b_)
19635 : : {
19636 : 3730179 : const cpp_hashnode *node_a = *(const cpp_hashnode *const *)a_;
19637 : 3730179 : macro_import &import_a = (*macro_imports)[node_a->deferred - 1];
19638 : 3730179 : const macro_export &export_a = (*macro_exports)[import_a[0].offset];
19639 : 3730179 : location_t loc_a = export_a.def ? export_a.def->line : export_a.undef_loc;
19640 : :
19641 : 3730179 : const cpp_hashnode *node_b = *(const cpp_hashnode *const *)b_;
19642 : 3730179 : macro_import &import_b = (*macro_imports)[node_b->deferred - 1];
19643 : 3730179 : const macro_export &export_b = (*macro_exports)[import_b[0].offset];
19644 : 3730179 : location_t loc_b = export_b.def ? export_b.def->line : export_b.undef_loc;
19645 : :
19646 : 3730179 : if (loc_a < loc_b)
19647 : : return +1;
19648 : 1916340 : else if (loc_a > loc_b)
19649 : : return -1;
19650 : : else
19651 : 0 : return 0;
19652 : : }
19653 : :
19654 : : /* Gather the macro definitions and undefinitions that we will need to
19655 : : write out. */
19656 : :
19657 : : vec<cpp_hashnode *> *
19658 : 877 : module_state::prepare_macros (cpp_reader *reader)
19659 : : {
19660 : 877 : vec<cpp_hashnode *> *macros;
19661 : 877 : vec_alloc (macros, 100);
19662 : :
19663 : 877 : cpp_forall_identifiers (reader, maybe_add_macro, macros);
19664 : :
19665 : 901 : dump (dumper::MACRO) && dump ("No more than %u macros", macros->length ());
19666 : :
19667 : 877 : macros->qsort (macro_loc_cmp);
19668 : :
19669 : : // Note the locations.
19670 : 73284 : for (unsigned ix = macros->length (); ix--;)
19671 : : {
19672 : 71530 : cpp_hashnode *node = (*macros)[ix];
19673 : 71530 : macro_import::slot &slot = (*macro_imports)[node->deferred - 1][0];
19674 : 71530 : macro_export &mac = (*macro_exports)[slot.offset];
19675 : :
19676 : 71530 : if (IDENTIFIER_KEYWORD_P (identifier (node)))
19677 : 1 : continue;
19678 : :
19679 : 71529 : if (mac.undef_loc != UNKNOWN_LOCATION)
19680 : 12 : note_location (mac.undef_loc);
19681 : 71529 : if (mac.def)
19682 : : {
19683 : 71523 : note_location (mac.def->line);
19684 : 233472 : for (unsigned ix = 0; ix != mac.def->count; ix++)
19685 : 161949 : note_location (mac.def->exp.tokens[ix].src_loc);
19686 : : }
19687 : : }
19688 : :
19689 : 877 : return macros;
19690 : : }
19691 : :
19692 : : /* Write out the exported defines. This is two sections, one
19693 : : containing the definitions, the other a table of node names. */
19694 : :
19695 : : unsigned
19696 : 877 : module_state::write_macros (elf_out *to, vec<cpp_hashnode *> *macros,
19697 : : unsigned *crc_p)
19698 : : {
19699 : 944 : dump () && dump ("Writing macros");
19700 : 877 : dump.indent ();
19701 : :
19702 : : /* Write the defs */
19703 : 877 : bytes_out sec (to);
19704 : 877 : sec.begin ();
19705 : :
19706 : 877 : unsigned count = 0;
19707 : 73284 : for (unsigned ix = macros->length (); ix--;)
19708 : : {
19709 : 71530 : cpp_hashnode *node = (*macros)[ix];
19710 : 71530 : macro_import::slot &slot = (*macro_imports)[node->deferred - 1][0];
19711 : 71530 : gcc_assert (!slot.get_module () && slot.get_defness ());
19712 : :
19713 : 71530 : macro_export &mac = (*macro_exports)[slot.offset];
19714 : 71530 : gcc_assert (!!(slot.get_defness () & macro_import::slot::L_UNDEF)
19715 : : == (mac.undef_loc != UNKNOWN_LOCATION)
19716 : : && !!(slot.get_defness () & macro_import::slot::L_DEF)
19717 : : == (mac.def != NULL));
19718 : :
19719 : 71530 : if (IDENTIFIER_KEYWORD_P (identifier (node)))
19720 : : {
19721 : 1 : warning_at (mac.def->line, 0,
19722 : : "not exporting %<#define %E%> as it is a keyword",
19723 : : identifier (node));
19724 : 1 : slot.offset = 0;
19725 : 1 : continue;
19726 : : }
19727 : :
19728 : 71529 : count++;
19729 : 71529 : slot.offset = sec.pos;
19730 : 71529 : dump (dumper::MACRO)
19731 : 24 : && dump ("Writing macro %s%s%s %I at %u",
19732 : 24 : slot.get_defness () & macro_import::slot::L_UNDEF
19733 : : ? "#undef" : "",
19734 : 24 : slot.get_defness () == macro_import::slot::L_BOTH
19735 : : ? " & " : "",
19736 : 24 : slot.get_defness () & macro_import::slot::L_DEF
19737 : : ? "#define" : "",
19738 : : identifier (node), slot.offset);
19739 : 71529 : if (mac.undef_loc != UNKNOWN_LOCATION)
19740 : 12 : write_location (sec, mac.undef_loc);
19741 : 71529 : if (mac.def)
19742 : 71523 : write_define (sec, mac.def);
19743 : : }
19744 : 877 : if (count)
19745 : : // We may have ended on a tokenless macro with a very short
19746 : : // location, that will cause problems reading its bit flags.
19747 : 143 : sec.u (0);
19748 : 877 : sec.end (to, to->name (MOD_SNAME_PFX ".def"), crc_p);
19749 : :
19750 : 877 : if (count)
19751 : : {
19752 : : /* Write the table. */
19753 : 143 : bytes_out sec (to);
19754 : 143 : sec.begin ();
19755 : 143 : sec.u (count);
19756 : :
19757 : 71815 : for (unsigned ix = macros->length (); ix--;)
19758 : : {
19759 : 71529 : const cpp_hashnode *node = (*macros)[ix];
19760 : 71529 : macro_import::slot &slot = (*macro_imports)[node->deferred - 1][0];
19761 : :
19762 : 71529 : if (slot.offset)
19763 : : {
19764 : 71529 : sec.cpp_node (node);
19765 : 71529 : sec.u (slot.get_defness ());
19766 : 71529 : sec.u (slot.offset);
19767 : : }
19768 : : }
19769 : 143 : sec.end (to, to->name (MOD_SNAME_PFX ".mac"), crc_p);
19770 : 143 : }
19771 : :
19772 : 877 : dump.outdent ();
19773 : 877 : return count;
19774 : 877 : }
19775 : :
19776 : : bool
19777 : 909 : module_state::read_macros ()
19778 : : {
19779 : : /* Get the def section. */
19780 : 909 : if (!slurp->macro_defs.begin (loc, from (), MOD_SNAME_PFX ".def"))
19781 : : return false;
19782 : :
19783 : : /* Get the tbl section, if there are defs. */
19784 : 909 : if (slurp->macro_defs.more_p ()
19785 : 909 : && !slurp->macro_tbl.begin (loc, from (), MOD_SNAME_PFX ".mac"))
19786 : : return false;
19787 : :
19788 : : return true;
19789 : : }
19790 : :
19791 : : /* Install the macro name table. */
19792 : :
19793 : : void
19794 : 915 : module_state::install_macros ()
19795 : : {
19796 : 915 : bytes_in &sec = slurp->macro_tbl;
19797 : 915 : if (!sec.size)
19798 : : return;
19799 : :
19800 : 202 : dump () && dump ("Reading macro table %M", this);
19801 : 180 : dump.indent ();
19802 : :
19803 : 180 : unsigned count = sec.u ();
19804 : 202 : dump () && dump ("%u macros", count);
19805 : 80099 : while (count--)
19806 : : {
19807 : 79919 : cpp_hashnode *node = sec.cpp_node ();
19808 : 79919 : macro_import &imp = get_macro_imports (node);
19809 : 79919 : unsigned flags = sec.u () & macro_import::slot::L_BOTH;
19810 : 79919 : if (!flags)
19811 : 0 : sec.set_overrun ();
19812 : :
19813 : 79919 : if (sec.get_overrun ())
19814 : : break;
19815 : :
19816 : 79919 : macro_import::slot &slot = imp.append (mod, flags);
19817 : 79919 : slot.offset = sec.u ();
19818 : :
19819 : 79919 : dump (dumper::MACRO)
19820 : 84 : && dump ("Read %s macro %s%s%s %I at %u",
19821 : 30 : imp.length () > 1 ? "add" : "new",
19822 : 27 : flags & macro_import::slot::L_UNDEF ? "#undef" : "",
19823 : : flags == macro_import::slot::L_BOTH ? " & " : "",
19824 : 30 : flags & macro_import::slot::L_DEF ? "#define" : "",
19825 : : identifier (node), slot.offset);
19826 : :
19827 : : /* We'll leak an imported definition's TOKEN_FLD_STR's data
19828 : : here. But that only happens when we've had to resolve the
19829 : : deferred macro before this import -- why are you doing
19830 : : that? */
19831 : 79919 : if (cpp_macro *cur = cpp_set_deferred_macro (node))
19832 : 29840 : if (!cur->imported_p)
19833 : : {
19834 : 29840 : macro_import::slot &slot = imp.exported ();
19835 : 29840 : macro_export &exp = get_macro_export (slot);
19836 : 29840 : exp.def = cur;
19837 : 109939 : dump (dumper::MACRO)
19838 : 0 : && dump ("Saving current #define %I", identifier (node));
19839 : : }
19840 : : }
19841 : :
19842 : : /* We're now done with the table. */
19843 : 180 : elf_in::release (slurp->from, sec);
19844 : :
19845 : 180 : dump.outdent ();
19846 : : }
19847 : :
19848 : : /* Import the transitive macros. */
19849 : :
19850 : : void
19851 : 873 : module_state::import_macros ()
19852 : : {
19853 : 873 : bitmap_ior_into (headers, slurp->headers);
19854 : :
19855 : 873 : bitmap_iterator bititer;
19856 : 873 : unsigned bitnum;
19857 : 1788 : EXECUTE_IF_SET_IN_BITMAP (slurp->headers, 0, bitnum, bititer)
19858 : 915 : (*modules)[bitnum]->install_macros ();
19859 : 873 : }
19860 : :
19861 : : /* NODE is being undefined at LOC. Record it in the export table, if
19862 : : necessary. */
19863 : :
19864 : : void
19865 : 228968 : module_state::undef_macro (cpp_reader *, location_t loc, cpp_hashnode *node)
19866 : : {
19867 : 228968 : if (!node->deferred)
19868 : : /* The macro is not imported, so our undef is irrelevant. */
19869 : : return;
19870 : :
19871 : 12 : unsigned n = dump.push (NULL);
19872 : :
19873 : 12 : macro_import::slot &slot = (*macro_imports)[node->deferred - 1].exported ();
19874 : 12 : macro_export &exp = get_macro_export (slot);
19875 : :
19876 : 12 : exp.undef_loc = loc;
19877 : 12 : slot.become_undef ();
19878 : 12 : exp.def = NULL;
19879 : :
19880 : 18 : dump (dumper::MACRO) && dump ("Recording macro #undef %I", identifier (node));
19881 : :
19882 : 12 : dump.pop (n);
19883 : : }
19884 : :
19885 : : /* NODE is a deferred macro node. Determine the definition and return
19886 : : it, with NULL if undefined. May issue diagnostics.
19887 : :
19888 : : This can leak memory, when merging declarations -- the string
19889 : : contents (TOKEN_FLD_STR) of each definition are allocated in
19890 : : unreclaimable cpp objstack. Only one will win. However, I do not
19891 : : expect this to be common -- mostly macros have a single point of
19892 : : definition. Perhaps we could restore the objstack to its position
19893 : : after the first imported definition (if that wins)? The macros
19894 : : themselves are GC'd. */
19895 : :
19896 : : cpp_macro *
19897 : 607 : module_state::deferred_macro (cpp_reader *reader, location_t loc,
19898 : : cpp_hashnode *node)
19899 : : {
19900 : 607 : macro_import &imports = (*macro_imports)[node->deferred - 1];
19901 : :
19902 : 607 : unsigned n = dump.push (NULL);
19903 : 613 : dump (dumper::MACRO) && dump ("Deferred macro %I", identifier (node));
19904 : :
19905 : 607 : bitmap visible (BITMAP_GGC_ALLOC ());
19906 : :
19907 : 607 : if (!((imports[0].get_defness () & macro_import::slot::L_UNDEF)
19908 : 0 : && !imports[0].get_module ()))
19909 : : {
19910 : : /* Calculate the set of visible header imports. */
19911 : 607 : bitmap_copy (visible, headers);
19912 : 1382 : for (unsigned ix = imports.length (); ix--;)
19913 : : {
19914 : 775 : const macro_import::slot &slot = imports[ix];
19915 : 775 : unsigned mod = slot.get_module ();
19916 : 775 : if ((slot.get_defness () & macro_import::slot::L_UNDEF)
19917 : 775 : && bitmap_bit_p (visible, mod))
19918 : : {
19919 : 12 : bitmap arg = mod ? (*modules)[mod]->slurp->headers : headers;
19920 : 12 : bitmap_and_compl_into (visible, arg);
19921 : 12 : bitmap_set_bit (visible, mod);
19922 : : }
19923 : : }
19924 : : }
19925 : 607 : bitmap_set_bit (visible, 0);
19926 : :
19927 : : /* Now find the macros that are still visible. */
19928 : 607 : bool failed = false;
19929 : 607 : cpp_macro *def = NULL;
19930 : 607 : vec<macro_export> defs;
19931 : 607 : defs.create (imports.length ());
19932 : 1382 : for (unsigned ix = imports.length (); ix--;)
19933 : : {
19934 : 775 : const macro_import::slot &slot = imports[ix];
19935 : 775 : unsigned mod = slot.get_module ();
19936 : 775 : if (bitmap_bit_p (visible, mod))
19937 : : {
19938 : 763 : macro_export *pushed = NULL;
19939 : 763 : if (mod)
19940 : : {
19941 : 637 : const module_state *imp = (*modules)[mod];
19942 : 637 : bytes_in &sec = imp->slurp->macro_defs;
19943 : 637 : if (!sec.get_overrun ())
19944 : : {
19945 : 637 : dump (dumper::MACRO)
19946 : 6 : && dump ("Reading macro %s%s%s %I module %M at %u",
19947 : 6 : slot.get_defness () & macro_import::slot::L_UNDEF
19948 : : ? "#undef" : "",
19949 : 6 : slot.get_defness () == macro_import::slot::L_BOTH
19950 : : ? " & " : "",
19951 : 6 : slot.get_defness () & macro_import::slot::L_DEF
19952 : : ? "#define" : "",
19953 : 6 : identifier (node), imp, slot.offset);
19954 : 637 : sec.random_access (slot.offset);
19955 : :
19956 : 637 : macro_export exp;
19957 : 637 : if (slot.get_defness () & macro_import::slot::L_UNDEF)
19958 : 12 : exp.undef_loc = imp->read_location (sec);
19959 : 637 : if (slot.get_defness () & macro_import::slot::L_DEF)
19960 : 631 : exp.def = imp->read_define (sec, reader);
19961 : 637 : if (sec.get_overrun ())
19962 : 0 : error_at (loc, "macro definitions of %qE corrupted",
19963 : 0 : imp->name);
19964 : : else
19965 : 637 : pushed = defs.quick_push (exp);
19966 : : }
19967 : : }
19968 : : else
19969 : 126 : pushed = defs.quick_push ((*macro_exports)[slot.offset]);
19970 : 763 : if (pushed && pushed->def)
19971 : : {
19972 : 757 : if (!def)
19973 : : def = pushed->def;
19974 : 153 : else if (cpp_compare_macros (def, pushed->def))
19975 : 775 : failed = true;
19976 : : }
19977 : : }
19978 : : }
19979 : :
19980 : 607 : if (failed)
19981 : : {
19982 : : /* If LOC is the first loc, this is the end of file check, which
19983 : : is a warning. */
19984 : 15 : auto_diagnostic_group d;
19985 : 15 : if (loc == MAP_START_LOCATION (LINEMAPS_ORDINARY_MAP_AT (line_table, 0)))
19986 : 9 : warning_at (loc, OPT_Winvalid_imported_macros,
19987 : : "inconsistent imported macro definition %qE",
19988 : : identifier (node));
19989 : : else
19990 : 6 : error_at (loc, "inconsistent imported macro definition %qE",
19991 : : identifier (node));
19992 : 60 : for (unsigned ix = defs.length (); ix--;)
19993 : : {
19994 : 30 : macro_export &exp = defs[ix];
19995 : 30 : if (exp.undef_loc)
19996 : 0 : inform (exp.undef_loc, "%<#undef %E%>", identifier (node));
19997 : 30 : if (exp.def)
19998 : 30 : inform (exp.def->line, "%<#define %s%>",
19999 : : cpp_macro_definition (reader, node, exp.def));
20000 : : }
20001 : 15 : def = NULL;
20002 : 15 : }
20003 : :
20004 : 607 : defs.release ();
20005 : :
20006 : 607 : dump.pop (n);
20007 : :
20008 : 607 : return def;
20009 : : }
20010 : :
20011 : : /* Stream the static aggregates. Sadly some headers (ahem:
20012 : : iostream) contain static vars, and rely on them to run global
20013 : : ctors. */
20014 : : unsigned
20015 : 877 : module_state::write_inits (elf_out *to, depset::hash &table, unsigned *crc_ptr)
20016 : : {
20017 : 877 : if (!static_aggregates && !tls_aggregates)
20018 : : return 0;
20019 : :
20020 : 45 : dump () && dump ("Writing initializers");
20021 : 45 : dump.indent ();
20022 : :
20023 : 45 : static_aggregates = nreverse (static_aggregates);
20024 : 45 : tls_aggregates = nreverse (tls_aggregates);
20025 : :
20026 : 45 : unsigned count = 0;
20027 : 45 : trees_out sec (to, this, table, ~0u);
20028 : 45 : sec.begin ();
20029 : :
20030 : 45 : tree list = static_aggregates;
20031 : 135 : for (int passes = 0; passes != 2; passes++)
20032 : : {
20033 : 258 : for (tree init = list; init; init = TREE_CHAIN (init))
20034 : 168 : if (TREE_LANG_FLAG_0 (init))
20035 : : {
20036 : 144 : if (STATIC_INIT_DECOMP_BASE_P (init))
20037 : : {
20038 : : /* Ensure that in the returned result chain if the
20039 : : STATIC_INIT_DECOMP_*BASE_P flags are set, there is
20040 : : always one or more STATIC_INIT_DECOMP_BASE_P TREE_LIST
20041 : : followed by one or more STATIC_INIT_DECOMP_NONBASE_P. */
20042 : 21 : int phase = 0;
20043 : 21 : tree last = NULL_TREE;
20044 : 21 : for (tree init2 = TREE_CHAIN (init);
20045 : 126 : init2; init2 = TREE_CHAIN (init2))
20046 : : {
20047 : 147 : if (phase == 0 && STATIC_INIT_DECOMP_BASE_P (init2))
20048 : : ;
20049 : 126 : else if (phase == 0
20050 : 147 : && STATIC_INIT_DECOMP_NONBASE_P (init2))
20051 : : {
20052 : 120 : phase = TREE_LANG_FLAG_0 (init2) ? 2 : 1;
20053 : : last = init2;
20054 : : }
20055 : 105 : else if (IN_RANGE (phase, 1, 2)
20056 : 210 : && STATIC_INIT_DECOMP_NONBASE_P (init2))
20057 : : {
20058 : 84 : if (TREE_LANG_FLAG_0 (init2))
20059 : 81 : phase = 2;
20060 : : last = init2;
20061 : : }
20062 : : else
20063 : : break;
20064 : : }
20065 : 21 : if (phase == 2)
20066 : : {
20067 : : /* In that case, add markers about it so that the
20068 : : STATIC_INIT_DECOMP_BASE_P and
20069 : : STATIC_INIT_DECOMP_NONBASE_P flags can be restored. */
20070 : 21 : sec.tree_node (build_int_cst (integer_type_node,
20071 : 21 : 2 * passes + 1));
20072 : 21 : phase = 1;
20073 : 147 : for (tree init2 = init; init2 != TREE_CHAIN (last);
20074 : 126 : init2 = TREE_CHAIN (init2))
20075 : 126 : if (TREE_LANG_FLAG_0 (init2))
20076 : : {
20077 : 102 : tree decl = TREE_VALUE (init2);
20078 : 102 : if (phase == 1
20079 : 102 : && STATIC_INIT_DECOMP_NONBASE_P (init2))
20080 : : {
20081 : 21 : sec.tree_node (build_int_cst (integer_type_node,
20082 : 21 : 2 * passes + 2));
20083 : 21 : phase = 2;
20084 : : }
20085 : 102 : dump ("Initializer:%u for %N", count, decl);
20086 : 102 : sec.tree_node (decl);
20087 : 102 : ++count;
20088 : : }
20089 : 21 : sec.tree_node (integer_zero_node);
20090 : 21 : init = last;
20091 : 21 : continue;
20092 : 21 : }
20093 : : }
20094 : :
20095 : 123 : tree decl = TREE_VALUE (init);
20096 : :
20097 : 123 : dump ("Initializer:%u for %N", count, decl);
20098 : 123 : sec.tree_node (decl);
20099 : 123 : ++count;
20100 : : }
20101 : :
20102 : 90 : list = tls_aggregates;
20103 : : }
20104 : :
20105 : 45 : sec.end (to, to->name (MOD_SNAME_PFX ".ini"), crc_ptr);
20106 : 45 : dump.outdent ();
20107 : :
20108 : 45 : return count;
20109 : 45 : }
20110 : :
20111 : : /* We have to defer some post-load processing until we've completed
20112 : : reading, because they can cause more reading. */
20113 : :
20114 : : static void
20115 : 11082 : post_load_processing ()
20116 : : {
20117 : : /* We mustn't cause a GC, our caller should have arranged for that
20118 : : not to happen. */
20119 : 11082 : gcc_checking_assert (function_depth);
20120 : :
20121 : 11082 : if (!post_load_decls)
20122 : : return;
20123 : :
20124 : 6727 : tree old_cfd = current_function_decl;
20125 : 6727 : struct function *old_cfun = cfun;
20126 : 14136 : while (post_load_decls->length ())
20127 : : {
20128 : 7409 : tree decl = post_load_decls->pop ();
20129 : :
20130 : 7464 : dump () && dump ("Post-load processing of %N", decl);
20131 : :
20132 : 7409 : gcc_checking_assert (DECL_MAYBE_IN_CHARGE_CDTOR_P (decl));
20133 : 7409 : expand_or_defer_fn (decl);
20134 : : /* As in module_state::read_cluster. */
20135 : 726 : if (at_eof && DECL_COMDAT (decl) && DECL_EXTERNAL (decl)
20136 : 7505 : && DECL_NOT_REALLY_EXTERN (decl))
20137 : 66 : DECL_EXTERNAL (decl) = false;
20138 : : }
20139 : :
20140 : 6727 : set_cfun (old_cfun);
20141 : 6727 : current_function_decl = old_cfd;
20142 : : }
20143 : :
20144 : : bool
20145 : 45 : module_state::read_inits (unsigned count)
20146 : : {
20147 : 45 : trees_in sec (this);
20148 : 45 : if (!sec.begin (loc, from (), from ()->find (MOD_SNAME_PFX ".ini")))
20149 : : return false;
20150 : 57 : dump () && dump ("Reading %u initializers", count);
20151 : 45 : dump.indent ();
20152 : :
20153 : 45 : lazy_snum = ~0u;
20154 : 45 : int decomp_phase = 0;
20155 : 45 : tree *aggrp = NULL;
20156 : 270 : for (unsigned ix = 0; ix != count; ix++)
20157 : : {
20158 : 225 : tree last = NULL_TREE;
20159 : 225 : if (decomp_phase)
20160 : 102 : last = *aggrp;
20161 : : /* Merely referencing the decl causes its initializer to be read
20162 : : and added to the correct list. */
20163 : 225 : tree decl = sec.tree_node ();
20164 : : /* module_state::write_inits can add special INTEGER_CST markers in
20165 : : between the decls. 1 means STATIC_INIT_DECOMP_BASE_P entries
20166 : : follow in static_aggregates, 2 means STATIC_INIT_DECOMP_NONBASE_P
20167 : : entries follow in static_aggregates, 3 means
20168 : : STATIC_INIT_DECOMP_BASE_P entries follow in tls_aggregates,
20169 : : 4 means STATIC_INIT_DECOMP_NONBASE_P follow in tls_aggregates,
20170 : : 0 means end of STATIC_INIT_DECOMP_{,NON}BASE_P sequence. */
20171 : 225 : if (tree_fits_shwi_p (decl))
20172 : : {
20173 : 63 : if (sec.get_overrun ())
20174 : : break;
20175 : 63 : decomp_phase = tree_to_shwi (decl);
20176 : 63 : if (decomp_phase)
20177 : : {
20178 : 42 : aggrp = decomp_phase > 2 ? &tls_aggregates : &static_aggregates;
20179 : : last = *aggrp;
20180 : : }
20181 : 63 : decl = sec.tree_node ();
20182 : : }
20183 : :
20184 : 225 : if (sec.get_overrun ())
20185 : : break;
20186 : 225 : if (decl)
20187 : 225 : dump ("Initializer:%u for %N", ix, decl);
20188 : 225 : if (decomp_phase)
20189 : : {
20190 : 102 : tree init = *aggrp;
20191 : 102 : gcc_assert (TREE_VALUE (init) == decl && TREE_CHAIN (init) == last);
20192 : 102 : if ((decomp_phase & 1) != 0)
20193 : 21 : STATIC_INIT_DECOMP_BASE_P (init) = 1;
20194 : : else
20195 : 81 : STATIC_INIT_DECOMP_NONBASE_P (init) = 1;
20196 : : }
20197 : : }
20198 : 45 : if (decomp_phase && !sec.get_overrun ())
20199 : : {
20200 : 0 : tree decl = sec.tree_node ();
20201 : 0 : gcc_assert (integer_zerop (decl));
20202 : : }
20203 : 45 : lazy_snum = 0;
20204 : 45 : post_load_processing ();
20205 : 45 : dump.outdent ();
20206 : 45 : if (!sec.end (from ()))
20207 : : return false;
20208 : : return true;
20209 : 45 : }
20210 : :
20211 : : void
20212 : 2580 : module_state::write_counts (elf_out *to, unsigned counts[MSC_HWM],
20213 : : unsigned *crc_ptr)
20214 : : {
20215 : 2580 : bytes_out cfg (to);
20216 : :
20217 : 2580 : cfg.begin ();
20218 : :
20219 : 25800 : for (unsigned ix = MSC_HWM; ix--;)
20220 : 23220 : cfg.u (counts[ix]);
20221 : :
20222 : 2580 : if (dump ())
20223 : : {
20224 : 276 : dump ("Cluster sections are [%u,%u)",
20225 : : counts[MSC_sec_lwm], counts[MSC_sec_hwm]);
20226 : 276 : dump ("Bindings %u", counts[MSC_bindings]);
20227 : 276 : dump ("Pendings %u", counts[MSC_pendings]);
20228 : 276 : dump ("Entities %u", counts[MSC_entities]);
20229 : 276 : dump ("Namespaces %u", counts[MSC_namespaces]);
20230 : 276 : dump ("Using-directives %u", counts[MSC_using_directives]);
20231 : 276 : dump ("Macros %u", counts[MSC_macros]);
20232 : 276 : dump ("Initializers %u", counts[MSC_inits]);
20233 : : }
20234 : :
20235 : 2580 : cfg.end (to, to->name (MOD_SNAME_PFX ".cnt"), crc_ptr);
20236 : 2580 : }
20237 : :
20238 : : bool
20239 : 2760 : module_state::read_counts (unsigned counts[MSC_HWM])
20240 : : {
20241 : 2760 : bytes_in cfg;
20242 : :
20243 : 2760 : if (!cfg.begin (loc, from (), MOD_SNAME_PFX ".cnt"))
20244 : : return false;
20245 : :
20246 : 27600 : for (unsigned ix = MSC_HWM; ix--;)
20247 : 24840 : counts[ix] = cfg.u ();
20248 : :
20249 : 2760 : if (dump ())
20250 : : {
20251 : 496 : dump ("Declaration sections are [%u,%u)",
20252 : : counts[MSC_sec_lwm], counts[MSC_sec_hwm]);
20253 : 496 : dump ("Bindings %u", counts[MSC_bindings]);
20254 : 496 : dump ("Pendings %u", counts[MSC_pendings]);
20255 : 496 : dump ("Entities %u", counts[MSC_entities]);
20256 : 496 : dump ("Namespaces %u", counts[MSC_namespaces]);
20257 : 496 : dump ("Using-directives %u", counts[MSC_using_directives]);
20258 : 496 : dump ("Macros %u", counts[MSC_macros]);
20259 : 496 : dump ("Initializers %u", counts[MSC_inits]);
20260 : : }
20261 : :
20262 : 2760 : return cfg.end (from ());
20263 : 2760 : }
20264 : :
20265 : : /* Tool configuration: MOD_SNAME_PFX .config
20266 : :
20267 : : This is data that confirms current state (or fails). */
20268 : :
20269 : : void
20270 : 2580 : module_state::write_config (elf_out *to, module_state_config &config,
20271 : : unsigned inner_crc)
20272 : : {
20273 : 2580 : bytes_out cfg (to);
20274 : :
20275 : 2580 : cfg.begin ();
20276 : :
20277 : : /* Write version and inner crc as u32 values, for easier
20278 : : debug inspection. */
20279 : 2856 : dump () && dump ("Writing version=%V, inner_crc=%x",
20280 : : MODULE_VERSION, inner_crc);
20281 : 2580 : cfg.u32 (unsigned (MODULE_VERSION));
20282 : 2580 : cfg.u32 (inner_crc);
20283 : :
20284 : 2580 : cfg.u (to->name (is_header () ? "" : get_flatname ()));
20285 : :
20286 : : /* Configuration. */
20287 : 2856 : dump () && dump ("Writing target='%s', host='%s'",
20288 : : TARGET_MACHINE, HOST_MACHINE);
20289 : 2580 : unsigned target = to->name (TARGET_MACHINE);
20290 : 2580 : unsigned host = (!strcmp (TARGET_MACHINE, HOST_MACHINE)
20291 : : ? target : to->name (HOST_MACHINE));
20292 : 2580 : cfg.u (target);
20293 : 2580 : cfg.u (host);
20294 : :
20295 : 2580 : cfg.str (config.dialect_str);
20296 : 2580 : cfg.u (extensions);
20297 : :
20298 : : /* Global tree information. We write the globals crc separately,
20299 : : rather than mix it directly into the overall crc, as it is used
20300 : : to ensure data match between instances of the compiler, not
20301 : : integrity of the file. */
20302 : 2856 : dump () && dump ("Writing globals=%u, crc=%x",
20303 : : fixed_trees->length (), global_crc);
20304 : 2580 : cfg.u (fixed_trees->length ());
20305 : 2580 : cfg.u32 (global_crc);
20306 : :
20307 : 2580 : if (is_partition ())
20308 : 178 : cfg.u (is_interface ());
20309 : :
20310 : 2580 : cfg.u (config.num_imports);
20311 : 2580 : cfg.u (config.num_partitions);
20312 : 2580 : cfg.u (config.num_entities);
20313 : :
20314 : 2580 : cfg.loc (config.ordinary_locs);
20315 : 2580 : cfg.loc (config.macro_locs);
20316 : 2580 : cfg.u (config.loc_range_bits);
20317 : :
20318 : 2580 : cfg.u (config.active_init);
20319 : :
20320 : : /* Now generate CRC, we'll have incorporated the inner CRC because
20321 : : of its serialization above. */
20322 : 2580 : cfg.end (to, to->name (MOD_SNAME_PFX ".cfg"), &crc);
20323 : 2856 : dump () && dump ("Writing CRC=%x", crc);
20324 : 2580 : }
20325 : :
20326 : : void
20327 : 40 : module_state::note_cmi_name ()
20328 : : {
20329 : 40 : if (!cmi_noted_p && filename)
20330 : : {
20331 : 40 : cmi_noted_p = true;
20332 : 40 : inform (loc, "compiled module file is %qs",
20333 : : maybe_add_cmi_prefix (filename));
20334 : : }
20335 : 40 : }
20336 : :
20337 : : bool
20338 : 2869 : module_state::read_config (module_state_config &config, bool complain)
20339 : : {
20340 : 2869 : bytes_in cfg;
20341 : :
20342 : 2869 : if (!cfg.begin (loc, from (), MOD_SNAME_PFX ".cfg"))
20343 : : return false;
20344 : :
20345 : : /* Check version. */
20346 : 2869 : unsigned my_ver = MODULE_VERSION;
20347 : 2869 : unsigned their_ver = cfg.u32 ();
20348 : 3368 : dump () && dump (my_ver == their_ver ? "Version %V"
20349 : : : "Expecting %V found %V", my_ver, their_ver);
20350 : 2869 : if (their_ver != my_ver)
20351 : : {
20352 : : /* The compiler versions differ. Close enough? */
20353 : 0 : verstr_t my_string, their_string;
20354 : :
20355 : 0 : version2string (my_ver, my_string);
20356 : 0 : version2string (their_ver, their_string);
20357 : :
20358 : : /* Reject when either is non-experimental or when experimental
20359 : : major versions differ. */
20360 : 0 : auto_diagnostic_group d;
20361 : 0 : bool reject_p = ((!IS_EXPERIMENTAL (my_ver)
20362 : : || !IS_EXPERIMENTAL (their_ver)
20363 : 0 : || MODULE_MAJOR (my_ver) != MODULE_MAJOR (their_ver))
20364 : : /* The 'I know what I'm doing' switch. */
20365 : 0 : && !flag_module_version_ignore);
20366 : 0 : bool inform_p = true;
20367 : 0 : if (!complain)
20368 : : inform_p = false;
20369 : 0 : else if (reject_p)
20370 : : {
20371 : 0 : cfg.set_overrun ();
20372 : 0 : error_at (loc, "compiled module is %sversion %s",
20373 : : IS_EXPERIMENTAL (their_ver) ? "experimental " : "",
20374 : : their_string);
20375 : : }
20376 : : else
20377 : 0 : inform_p = warning_at (loc, 0, "compiled module is %sversion %s",
20378 : : IS_EXPERIMENTAL (their_ver) ? "experimental " : "",
20379 : : their_string);
20380 : :
20381 : 0 : if (inform_p)
20382 : : {
20383 : 0 : inform (loc, "compiler is %sversion %s%s%s",
20384 : : IS_EXPERIMENTAL (my_ver) ? "experimental " : "",
20385 : : my_string,
20386 : 0 : reject_p ? "" : flag_module_version_ignore
20387 : 0 : ? ", be it on your own head!" : ", close enough?",
20388 : : reject_p ? "" : " \xc2\xaf\\_(\xe3\x83\x84)_/\xc2\xaf");
20389 : 0 : note_cmi_name ();
20390 : : }
20391 : :
20392 : 0 : if (reject_p)
20393 : 0 : goto done;
20394 : 0 : }
20395 : :
20396 : : /* We wrote the inner crc merely to merge it, so simply read it
20397 : : back and forget it. */
20398 : 2869 : cfg.u32 ();
20399 : :
20400 : : /* Check module name. */
20401 : 2869 : {
20402 : 2869 : const char *their_name = from ()->name (cfg.u ());
20403 : 2869 : const char *our_name = "";
20404 : :
20405 : 2869 : if (!is_header ())
20406 : 1860 : our_name = get_flatname ();
20407 : :
20408 : : /* Header units can be aliased, so name checking is
20409 : : inappropriate. */
20410 : 2869 : if (0 != strcmp (their_name, our_name))
20411 : : {
20412 : 0 : error_at (loc,
20413 : 0 : their_name[0] && our_name[0] ? G_("module %qs found")
20414 : : : their_name[0]
20415 : : ? G_("header module expected, module %qs found")
20416 : : : G_("module %qs expected, header module found"),
20417 : 0 : their_name[0] ? their_name : our_name);
20418 : 0 : cfg.set_overrun ();
20419 : 0 : goto done;
20420 : : }
20421 : : }
20422 : :
20423 : : /* Check the CRC after the above sanity checks, so that the user is
20424 : : clued in. */
20425 : 2869 : {
20426 : 2869 : unsigned e_crc = crc;
20427 : 2869 : crc = cfg.get_crc ();
20428 : 3368 : dump () && dump ("Reading CRC=%x", crc);
20429 : : /* When not complaining we haven't set directness yet, so ignore the
20430 : : mismatch. */
20431 : 2869 : if (complain && !is_direct () && crc != e_crc)
20432 : : {
20433 : 3 : error_at (loc, "module %qs CRC mismatch", get_flatname ());
20434 : 3 : cfg.set_overrun ();
20435 : 3 : goto done;
20436 : : }
20437 : : }
20438 : :
20439 : : /* Check target & host. */
20440 : 2866 : {
20441 : 2866 : const char *their_target = from ()->name (cfg.u ());
20442 : 2866 : const char *their_host = from ()->name (cfg.u ());
20443 : 3365 : dump () && dump ("Read target='%s', host='%s'", their_target, their_host);
20444 : 2866 : if (strcmp (their_target, TARGET_MACHINE)
20445 : 2866 : || strcmp (their_host, HOST_MACHINE))
20446 : : {
20447 : 0 : error_at (loc, "target & host is %qs:%qs, expected %qs:%qs",
20448 : : their_target, TARGET_MACHINE, their_host, HOST_MACHINE);
20449 : 0 : cfg.set_overrun ();
20450 : 0 : goto done;
20451 : : }
20452 : : }
20453 : :
20454 : : /* Check compilation dialect. This must match. */
20455 : 2866 : {
20456 : 2866 : const char *their_dialect = cfg.str ();
20457 : 2866 : if (strcmp (their_dialect, config.dialect_str))
20458 : : {
20459 : 1 : if (complain)
20460 : 1 : error_at (loc, "language dialect differs %qs, expected %qs",
20461 : : their_dialect, config.dialect_str);
20462 : 1 : cfg.set_overrun ();
20463 : 1 : goto done;
20464 : : }
20465 : : }
20466 : :
20467 : : /* Check for extensions. If they set any, we must have them set
20468 : : too. */
20469 : 2865 : {
20470 : 2865 : unsigned ext = cfg.u ();
20471 : 2865 : unsigned allowed = (flag_openmp ? SE_OPENMP | SE_OPENMP_SIMD : 0);
20472 : 2865 : if (flag_openmp_simd)
20473 : 3 : allowed |= SE_OPENMP_SIMD;
20474 : 2865 : if (flag_openacc)
20475 : 3 : allowed |= SE_OPENACC;
20476 : :
20477 : 2865 : if (unsigned bad = ext & ~allowed)
20478 : : {
20479 : 9 : if (bad & SE_OPENMP)
20480 : 3 : error_at (loc, "module contains OpenMP, use %<-fopenmp%> to enable");
20481 : 6 : else if (bad & SE_OPENMP_SIMD)
20482 : 3 : error_at (loc, "module contains OpenMP, use %<-fopenmp%> or "
20483 : : "%<-fopenmp-simd%> to enable");
20484 : 9 : if (bad & SE_OPENACC)
20485 : 3 : error_at (loc, "module contains OpenACC, use %<-fopenacc%> to "
20486 : : "enable");
20487 : 9 : cfg.set_overrun ();
20488 : 9 : goto done;
20489 : : }
20490 : 2856 : extensions = ext;
20491 : : }
20492 : :
20493 : : /* Check global trees. */
20494 : 2856 : {
20495 : 2856 : unsigned their_fixed_length = cfg.u ();
20496 : 2856 : unsigned their_fixed_crc = cfg.u32 ();
20497 : 3355 : dump () && dump ("Read globals=%u, crc=%x",
20498 : : their_fixed_length, their_fixed_crc);
20499 : 2856 : if (!flag_preprocess_only
20500 : 2856 : && (their_fixed_length != fixed_trees->length ()
20501 : 2802 : || their_fixed_crc != global_crc))
20502 : : {
20503 : 0 : error_at (loc, "fixed tree mismatch");
20504 : 0 : cfg.set_overrun ();
20505 : 0 : goto done;
20506 : : }
20507 : : }
20508 : :
20509 : : /* All non-partitions are interfaces. */
20510 : 2856 : interface_p = !is_partition () || cfg.u ();
20511 : :
20512 : 2856 : config.num_imports = cfg.u ();
20513 : 2856 : config.num_partitions = cfg.u ();
20514 : 2856 : config.num_entities = cfg.u ();
20515 : :
20516 : 2856 : config.ordinary_locs = cfg.loc ();
20517 : 2856 : config.macro_locs = cfg.loc ();
20518 : 2856 : config.loc_range_bits = cfg.u ();
20519 : :
20520 : 2856 : config.active_init = cfg.u ();
20521 : :
20522 : 2869 : done:
20523 : 2869 : return cfg.end (from ());
20524 : 2869 : }
20525 : :
20526 : : /* Comparator for ordering the Ordered Ordinary Location array. */
20527 : :
20528 : : static int
20529 : 112 : ool_cmp (const void *a_, const void *b_)
20530 : : {
20531 : 112 : auto *a = *static_cast<const module_state *const *> (a_);
20532 : 112 : auto *b = *static_cast<const module_state *const *> (b_);
20533 : 112 : if (a == b)
20534 : : return 0;
20535 : 112 : else if (a->ordinary_locs.first < b->ordinary_locs.first)
20536 : : return -1;
20537 : : else
20538 : 46 : return +1;
20539 : : }
20540 : :
20541 : : /* Use ELROND format to record the following sections:
20542 : : qualified-names : binding value(s)
20543 : : MOD_SNAME_PFX.README : human readable, strings
20544 : : MOD_SNAME_PFX.ENV : environment strings, strings
20545 : : MOD_SNAME_PFX.nms : namespace hierarchy
20546 : : MOD_SNAME_PFX.udi : namespace using-directives
20547 : : MOD_SNAME_PFX.bnd : binding table
20548 : : MOD_SNAME_PFX.spc : specialization table
20549 : : MOD_SNAME_PFX.imp : import table
20550 : : MOD_SNAME_PFX.ent : entity table
20551 : : MOD_SNAME_PFX.prt : partitions table
20552 : : MOD_SNAME_PFX.olm : ordinary line maps
20553 : : MOD_SNAME_PFX.mlm : macro line maps
20554 : : MOD_SNAME_PFX.def : macro definitions
20555 : : MOD_SNAME_PFX.mac : macro index
20556 : : MOD_SNAME_PFX.ini : inits
20557 : : MOD_SNAME_PFX.cnt : counts
20558 : : MOD_SNAME_PFX.cfg : config data
20559 : : */
20560 : :
20561 : : bool
20562 : 2609 : module_state::write_begin (elf_out *to, cpp_reader *reader,
20563 : : module_state_config &config, unsigned &crc)
20564 : : {
20565 : : /* Figure out remapped module numbers, which might elide
20566 : : partitions. */
20567 : 2609 : bitmap partitions = NULL;
20568 : 2609 : if (!is_header () && !is_partition ())
20569 : 1554 : partitions = BITMAP_GGC_ALLOC ();
20570 : 2609 : write_init_maps ();
20571 : :
20572 : 2609 : unsigned mod_hwm = 1;
20573 : 3226 : for (unsigned ix = 1; ix != modules->length (); ix++)
20574 : : {
20575 : 617 : module_state *imp = (*modules)[ix];
20576 : :
20577 : : /* Promote any non-partition direct import from a partition, unless
20578 : : we're a partition. */
20579 : 563 : if (!is_partition () && !imp->is_partition ()
20580 : 1011 : && imp->is_partition_direct ())
20581 : 9 : imp->directness = MD_PURVIEW_DIRECT;
20582 : :
20583 : : /* Write any import that is not a partition, unless we're a
20584 : : partition. */
20585 : 617 : if (!partitions || !imp->is_partition ())
20586 : 448 : imp->remap = mod_hwm++;
20587 : : else
20588 : : {
20589 : 202 : dump () && dump ("Partition %M %u", imp, ix);
20590 : 169 : bitmap_set_bit (partitions, ix);
20591 : 169 : imp->remap = 0;
20592 : : /* All interface partitions must be exported. */
20593 : 169 : if (imp->is_interface () && !bitmap_bit_p (exports, imp->mod))
20594 : : {
20595 : 3 : error_at (imp->loc, "interface partition is not exported");
20596 : 3 : bitmap_set_bit (exports, imp->mod);
20597 : : }
20598 : :
20599 : : /* All the partition entities should have been loaded when
20600 : : loading the partition. */
20601 : : if (CHECKING_P)
20602 : 927 : for (unsigned jx = 0; jx != imp->entity_num; jx++)
20603 : : {
20604 : 758 : binding_slot *slot = &(*entity_ary)[imp->entity_lwm + jx];
20605 : 758 : gcc_checking_assert (!slot->is_lazy ());
20606 : : }
20607 : : }
20608 : :
20609 : 617 : if (imp->is_direct () && (imp->remap || imp->is_partition ()))
20610 : 599 : note_location (imp->imported_from ());
20611 : : }
20612 : :
20613 : 2609 : if (partitions && bitmap_empty_p (partitions))
20614 : : /* No partitions present. */
20615 : : partitions = nullptr;
20616 : :
20617 : : /* Find the set of decls we must write out. */
20618 : 2609 : depset::hash table (DECL_NAMESPACE_BINDINGS (global_namespace)->size () * 8);
20619 : : /* Add the specializations before the writables, so that we can
20620 : : detect injected friend specializations. */
20621 : 2609 : table.add_specializations (true);
20622 : 2609 : table.add_specializations (false);
20623 : 2609 : if (partial_specializations)
20624 : : {
20625 : 203 : table.add_partial_entities (partial_specializations);
20626 : 203 : partial_specializations = NULL;
20627 : : }
20628 : 2609 : table.add_namespace_entities (global_namespace, partitions);
20629 : 2609 : if (class_members)
20630 : : {
20631 : 12 : table.add_class_entities (class_members);
20632 : 12 : class_members = NULL;
20633 : : }
20634 : :
20635 : : /* Now join everything up. */
20636 : 2609 : table.find_dependencies (this);
20637 : :
20638 : 2609 : if (!table.finalize_dependencies ())
20639 : : return false;
20640 : :
20641 : : #if CHECKING_P
20642 : : /* We're done verifying at-most once reading, reset to verify
20643 : : at-most once writing. */
20644 : 2580 : note_defs = note_defs_table_t::create_ggc (1000);
20645 : : #endif
20646 : :
20647 : : /* Determine Strongly Connected Components. This will also strip any
20648 : : unnecessary dependencies on imported or TU-local entities. */
20649 : 2580 : vec<depset *> sccs = table.connect ();
20650 : :
20651 : 2580 : vec_alloc (ool, modules->length ());
20652 : 3190 : for (unsigned ix = modules->length (); --ix;)
20653 : : {
20654 : 610 : auto *import = (*modules)[ix];
20655 : 610 : if (import->loadedness > ML_NONE
20656 : 610 : && !(partitions && bitmap_bit_p (partitions, import->mod)))
20657 : 441 : ool->quick_push (import);
20658 : : }
20659 : 2580 : ool->qsort (ool_cmp);
20660 : :
20661 : 2580 : write_diagnostic_classification (nullptr, global_dc, nullptr);
20662 : :
20663 : 2580 : vec<cpp_hashnode *> *macros = nullptr;
20664 : 2580 : if (is_header ())
20665 : 877 : macros = prepare_macros (reader);
20666 : :
20667 : 2580 : config.num_imports = mod_hwm;
20668 : 2580 : config.num_partitions = modules->length () - mod_hwm;
20669 : 2580 : auto map_info = write_prepare_maps (&config, bool (config.num_partitions));
20670 : 2580 : unsigned counts[MSC_HWM];
20671 : 2580 : memset (counts, 0, sizeof (counts));
20672 : :
20673 : : /* depset::cluster is the cluster number,
20674 : : depset::section is unspecified scratch value.
20675 : :
20676 : : The following loops make use of the tarjan property that
20677 : : dependencies will be earlier in the SCCS array. */
20678 : :
20679 : : /* This first loop determines the number of depsets in each SCC, and
20680 : : also the number of namespaces we're dealing with. During the
20681 : : loop, the meaning of a couple of depset fields now change:
20682 : :
20683 : : depset::cluster -> size_of cluster, if first of cluster & !namespace
20684 : : depset::section -> section number of cluster (if !namespace). */
20685 : :
20686 : 2580 : unsigned n_spaces = 0;
20687 : 2580 : counts[MSC_sec_lwm] = counts[MSC_sec_hwm] = to->get_section_limit ();
20688 : 248076 : for (unsigned size, ix = 0; ix < sccs.length (); ix += size)
20689 : : {
20690 : 245496 : depset **base = &sccs[ix];
20691 : :
20692 : 463711 : if (base[0]->get_entity_kind () == depset::EK_NAMESPACE)
20693 : : {
20694 : 4542 : n_spaces++;
20695 : 4542 : size = 1;
20696 : : }
20697 : : else
20698 : : {
20699 : : /* Count the members in this cluster. */
20700 : 1009587 : for (size = 1; ix + size < sccs.length (); size++)
20701 : 1007317 : if (base[size]->cluster != base[0]->cluster)
20702 : : break;
20703 : :
20704 : 1250541 : for (unsigned jx = 0; jx != size; jx++)
20705 : : {
20706 : : /* Set the section number. */
20707 : 1009587 : base[jx]->cluster = ~(~0u >> 1); /* A bad value. */
20708 : 1009587 : base[jx]->section = counts[MSC_sec_hwm];
20709 : : }
20710 : :
20711 : : /* Save the size in the first member's cluster slot. */
20712 : 240954 : base[0]->cluster = size;
20713 : :
20714 : 240954 : counts[MSC_sec_hwm]++;
20715 : : }
20716 : : }
20717 : :
20718 : : /* Write the clusters. Namespace decls are put in the spaces array.
20719 : : The meaning of depset::cluster changes to provide the
20720 : : unnamed-decl count of the depset's decl (and remains zero for
20721 : : non-decls and non-unnamed). */
20722 : 2580 : unsigned bytes = 0;
20723 : 2580 : vec<depset *> spaces;
20724 : 2580 : spaces.create (n_spaces);
20725 : :
20726 : 248076 : for (unsigned size, ix = 0; ix < sccs.length (); ix += size)
20727 : : {
20728 : 245496 : depset **base = &sccs[ix];
20729 : :
20730 : 245496 : if (base[0]->get_entity_kind () == depset::EK_NAMESPACE)
20731 : : {
20732 : 4542 : tree decl = base[0]->get_entity ();
20733 : 4542 : if (decl == global_namespace)
20734 : 2341 : base[0]->cluster = 0;
20735 : 2201 : else if (!base[0]->is_import ())
20736 : : {
20737 : 2201 : base[0]->cluster = counts[MSC_entities]++;
20738 : 2201 : spaces.quick_push (base[0]);
20739 : 2201 : counts[MSC_namespaces]++;
20740 : 2201 : if (CHECKING_P)
20741 : : {
20742 : : /* Add it to the entity map, such that we can tell it is
20743 : : part of us. */
20744 : 2201 : bool existed;
20745 : 2201 : unsigned *slot = &entity_map->get_or_insert
20746 : 2201 : (DECL_UID (decl), &existed);
20747 : 2201 : if (existed)
20748 : : /* It must have come from a partition. */
20749 : 0 : gcc_checking_assert
20750 : : (import_entity_module (*slot)->is_partition ());
20751 : 2201 : *slot = ~base[0]->cluster;
20752 : : }
20753 : 247727 : dump (dumper::CLUSTER) && dump ("Cluster namespace %N", decl);
20754 : : }
20755 : : size = 1;
20756 : : }
20757 : : else
20758 : : {
20759 : 240954 : size = base[0]->cluster;
20760 : :
20761 : : /* Cluster is now used to number entities. */
20762 : 240954 : base[0]->cluster = ~(~0u >> 1); /* A bad value. */
20763 : :
20764 : 240954 : sort_cluster (&table, base, size);
20765 : :
20766 : : /* Record the section for consistency checking during stream
20767 : : out -- we don't want to start writing decls in different
20768 : : sections. */
20769 : 240954 : table.section = base[0]->section;
20770 : 240954 : bytes += write_cluster (to, base, size, table, counts, &crc);
20771 : 240954 : table.section = 0;
20772 : : }
20773 : : }
20774 : :
20775 : : /* depset::cluster - entity number (on entities)
20776 : : depset::section - cluster number */
20777 : : /* We'd better have written as many sections and found as many
20778 : : namespaces as we predicted. */
20779 : 5160 : gcc_assert (counts[MSC_sec_hwm] == to->get_section_limit ()
20780 : : && spaces.length () == counts[MSC_namespaces]);
20781 : :
20782 : : /* Write the entitites. None happens if we contain namespaces or
20783 : : nothing. */
20784 : 2580 : config.num_entities = counts[MSC_entities];
20785 : 2580 : if (counts[MSC_entities])
20786 : 2335 : write_entities (to, sccs, counts[MSC_entities], &crc);
20787 : :
20788 : : /* Write the namespaces. */
20789 : 2580 : if (counts[MSC_namespaces])
20790 : 545 : write_namespaces (to, spaces, counts[MSC_namespaces], &crc);
20791 : :
20792 : : /* Write any using-directives. */
20793 : 2580 : if (counts[MSC_namespaces])
20794 : 545 : counts[MSC_using_directives]
20795 : 545 : = write_using_directives (to, table, spaces, &crc);
20796 : :
20797 : : /* Write the bindings themselves. */
20798 : 2580 : counts[MSC_bindings] = write_bindings (to, sccs, &crc);
20799 : :
20800 : : /* Write the unnamed. */
20801 : 2580 : counts[MSC_pendings] = write_pendings (to, sccs, table, &crc);
20802 : :
20803 : : /* Write the import table. */
20804 : 2580 : if (config.num_imports > 1)
20805 : 413 : write_imports (to, &crc);
20806 : :
20807 : : /* Write elided partition table. */
20808 : 2580 : if (config.num_partitions)
20809 : 121 : write_partitions (to, config.num_partitions, &crc);
20810 : :
20811 : : /* Write the line maps. */
20812 : 2580 : if (config.ordinary_locs)
20813 : : {
20814 : 2484 : write_ordinary_maps (to, map_info, bool (config.num_partitions), &crc);
20815 : 2484 : write_diagnostic_classification (to, global_dc, &crc);
20816 : : }
20817 : 2580 : if (config.macro_locs)
20818 : 120 : write_macro_maps (to, map_info, &crc);
20819 : :
20820 : 2580 : if (is_header ())
20821 : : {
20822 : 877 : counts[MSC_macros] = write_macros (to, macros, &crc);
20823 : 877 : counts[MSC_inits] = write_inits (to, table, &crc);
20824 : 877 : vec_free (macros);
20825 : : }
20826 : :
20827 : 2580 : unsigned clusters = counts[MSC_sec_hwm] - counts[MSC_sec_lwm];
20828 : 2580 : dump () && dump ("Wrote %u clusters, average %u bytes/cluster",
20829 : 276 : clusters, (bytes + clusters / 2) / (clusters + !clusters));
20830 : 2580 : trees_out::instrument ();
20831 : :
20832 : 2580 : write_counts (to, counts, &crc);
20833 : :
20834 : 2580 : spaces.release ();
20835 : 2580 : sccs.release ();
20836 : :
20837 : 2580 : vec_free (macro_loc_remap);
20838 : 2580 : vec_free (ord_loc_remap);
20839 : 2580 : vec_free (ool);
20840 : :
20841 : : // FIXME:QOI: Have a command line switch to control more detailed
20842 : : // information (which might leak data you do not want to leak).
20843 : : // Perhaps (some of) the write_readme contents should also be
20844 : : // so-controlled.
20845 : 2580 : if (false)
20846 : : write_env (to);
20847 : :
20848 : 2580 : return true;
20849 : 2609 : }
20850 : :
20851 : : // Finish module writing after we've emitted all dynamic initializers.
20852 : :
20853 : : void
20854 : 2580 : module_state::write_end (elf_out *to, cpp_reader *reader,
20855 : : module_state_config &config, unsigned &crc)
20856 : : {
20857 : : /* And finish up. */
20858 : 2580 : write_config (to, config, crc);
20859 : :
20860 : : /* Human-readable info. */
20861 : 2580 : write_readme (to, reader, config.dialect_str);
20862 : :
20863 : 2856 : dump () && dump ("Wrote %u sections", to->get_section_limit ());
20864 : 2580 : }
20865 : :
20866 : : /* Initial read of a CMI. Checks config, loads up imports and line
20867 : : maps. */
20868 : :
20869 : : bool
20870 : 2824 : module_state::read_initial (cpp_reader *reader)
20871 : : {
20872 : 2824 : module_state_config config;
20873 : 2824 : bool ok = true;
20874 : :
20875 : 2824 : if (ok && !read_config (config))
20876 : : ok = false;
20877 : :
20878 : 2811 : bool have_locs = ok && read_prepare_maps (&config);
20879 : :
20880 : : /* Ordinary maps before the imports. */
20881 : 2811 : if (!(have_locs && config.ordinary_locs))
20882 : 71 : ordinary_locs.first = line_table->highest_location + 1;
20883 : 2753 : else if (!read_ordinary_maps (config.ordinary_locs, config.loc_range_bits))
20884 : : ok = false;
20885 : :
20886 : 2811 : if (ok && have_locs && config.ordinary_locs
20887 : 5577 : && !read_diagnostic_classification (global_dc))
20888 : : ok = false;
20889 : :
20890 : : /* Allocate the REMAP vector. */
20891 : 2824 : slurp->alloc_remap (config.num_imports);
20892 : :
20893 : 2824 : if (ok)
20894 : : {
20895 : : /* Read the import table. Decrement current to stop this CMI
20896 : : from being evicted during the import. */
20897 : 2811 : slurp->current--;
20898 : 2811 : if (config.num_imports > 1 && !read_imports (reader, line_table))
20899 : : ok = false;
20900 : 2811 : slurp->current++;
20901 : : }
20902 : :
20903 : : /* Read the elided partition table, if we're the primary partition. */
20904 : 2811 : if (ok && config.num_partitions && is_module ()
20905 : 2832 : && !read_partitions (config.num_partitions))
20906 : : ok = false;
20907 : :
20908 : : /* Determine the module's number. */
20909 : 2824 : gcc_checking_assert (mod == MODULE_UNKNOWN);
20910 : 2824 : gcc_checking_assert (this != this_module ());
20911 : :
20912 : 2824 : {
20913 : : /* Allocate space in the entities array now -- that array must be
20914 : : monotonically in step with the modules array. */
20915 : 2824 : entity_lwm = vec_safe_length (entity_ary);
20916 : 2824 : entity_num = config.num_entities;
20917 : 2824 : gcc_checking_assert (modules->length () == 1
20918 : : || modules->last ()->entity_lwm <= entity_lwm);
20919 : 2824 : vec_safe_reserve (entity_ary, config.num_entities);
20920 : :
20921 : 2824 : binding_slot slot;
20922 : 2824 : slot.u.binding = NULL_TREE;
20923 : 1155845 : for (unsigned count = config.num_entities; count--;)
20924 : 1153021 : entity_ary->quick_push (slot);
20925 : : }
20926 : :
20927 : : /* We'll run out of other resources before we run out of module
20928 : : indices. */
20929 : 2824 : mod = modules->length ();
20930 : 2824 : vec_safe_push (modules, this);
20931 : :
20932 : : /* We always import and export ourselves. */
20933 : 2824 : bitmap_set_bit (imports, mod);
20934 : 2824 : bitmap_set_bit (exports, mod);
20935 : :
20936 : 2824 : if (ok)
20937 : 2811 : (*slurp->remap)[0] = mod << 1;
20938 : 3320 : dump () && dump ("Assigning %M module number %u", this, mod);
20939 : :
20940 : : /* We should not have been frozen during the importing done by
20941 : : read_config. */
20942 : 2824 : gcc_assert (!from ()->is_frozen ());
20943 : :
20944 : : /* Macro maps after the imports. */
20945 : 2824 : if (!(ok && have_locs && config.macro_locs))
20946 : 2691 : macro_locs.first = LINEMAPS_MACRO_LOWEST_LOCATION (line_table);
20947 : 133 : else if (!read_macro_maps (config.macro_locs))
20948 : : ok = false;
20949 : :
20950 : : /* Note whether there's an active initializer. */
20951 : 2824 : active_init_p = !is_header () && bool (config.active_init);
20952 : :
20953 : 2824 : gcc_assert (slurp->current == ~0u);
20954 : 2824 : return ok;
20955 : : }
20956 : :
20957 : : /* Read a preprocessor state. */
20958 : :
20959 : : bool
20960 : 915 : module_state::read_preprocessor (bool outermost)
20961 : : {
20962 : 915 : gcc_checking_assert (is_header () && slurp
20963 : : && slurp->remap_module (0) == mod);
20964 : :
20965 : 915 : if (loadedness == ML_PREPROCESSOR)
20966 : 6 : return !(from () && from ()->get_error ());
20967 : :
20968 : 909 : bool ok = true;
20969 : :
20970 : : /* Read direct header imports. */
20971 : 909 : unsigned len = slurp->remap->length ();
20972 : 951 : for (unsigned ix = 1; ok && ix != len; ix++)
20973 : : {
20974 : 42 : unsigned map = (*slurp->remap)[ix];
20975 : 42 : if (map & 1)
20976 : : {
20977 : 42 : module_state *import = (*modules)[map >> 1];
20978 : 42 : if (import->is_header ())
20979 : : {
20980 : 42 : ok = import->read_preprocessor (false);
20981 : 42 : bitmap_ior_into (slurp->headers, import->slurp->headers);
20982 : : }
20983 : : }
20984 : : }
20985 : :
20986 : : /* Record as a direct header. */
20987 : 909 : if (ok)
20988 : 909 : bitmap_set_bit (slurp->headers, mod);
20989 : :
20990 : 909 : if (ok && !read_macros ())
20991 : : ok = false;
20992 : :
20993 : 909 : loadedness = ML_PREPROCESSOR;
20994 : 909 : announce ("macros");
20995 : :
20996 : 909 : if (flag_preprocess_only)
20997 : : /* We're done with the string table. */
20998 : 39 : from ()->release ();
20999 : :
21000 : 909 : return check_read (outermost, ok);
21001 : : }
21002 : :
21003 : : /* Read language state. */
21004 : :
21005 : : bool
21006 : 2836 : module_state::read_language (bool outermost)
21007 : : {
21008 : 2836 : gcc_checking_assert (!lazy_snum);
21009 : :
21010 : 2836 : if (loadedness == ML_LANGUAGE)
21011 : 76 : return !(slurp && from () && from ()->get_error ());
21012 : :
21013 : 2760 : gcc_checking_assert (slurp && slurp->current == ~0u
21014 : : && slurp->remap_module (0) == mod);
21015 : :
21016 : 2760 : bool ok = true;
21017 : :
21018 : : /* Read direct imports. */
21019 : 2760 : unsigned len = slurp->remap->length ();
21020 : 3125 : for (unsigned ix = 1; ok && ix != len; ix++)
21021 : : {
21022 : 365 : unsigned map = (*slurp->remap)[ix];
21023 : 365 : if (map & 1)
21024 : : {
21025 : 361 : module_state *import = (*modules)[map >> 1];
21026 : 361 : if (!import->read_language (false))
21027 : 365 : ok = false;
21028 : : }
21029 : : }
21030 : :
21031 : 2760 : unsigned counts[MSC_HWM];
21032 : :
21033 : 2760 : if (ok && !read_counts (counts))
21034 : : ok = false;
21035 : :
21036 : 2760 : function_depth++; /* Prevent unexpected GCs. */
21037 : :
21038 : 2760 : if (ok && counts[MSC_entities] != entity_num)
21039 : : ok = false;
21040 : 2760 : if (ok && counts[MSC_entities]
21041 : 2555 : && !read_entities (counts[MSC_entities],
21042 : : counts[MSC_sec_lwm], counts[MSC_sec_hwm]))
21043 : : ok = false;
21044 : :
21045 : : /* Read the namespace hierarchy. */
21046 : 2760 : if (ok && counts[MSC_namespaces]
21047 : 3336 : && !read_namespaces (counts[MSC_namespaces]))
21048 : : ok = false;
21049 : :
21050 : : /* Read any using-directives. */
21051 : 2760 : if (ok && counts[MSC_using_directives]
21052 : 2907 : && !read_using_directives (counts[MSC_using_directives]))
21053 : : ok = false;
21054 : :
21055 : 2760 : if (ok && !read_bindings (counts[MSC_bindings],
21056 : : counts[MSC_sec_lwm], counts[MSC_sec_hwm]))
21057 : : ok = false;
21058 : :
21059 : : /* And unnamed. */
21060 : 2760 : if (ok && counts[MSC_pendings] && !read_pendings (counts[MSC_pendings]))
21061 : : ok = false;
21062 : :
21063 : 2760 : if (ok)
21064 : : {
21065 : 2760 : slurp->remaining = counts[MSC_sec_hwm] - counts[MSC_sec_lwm];
21066 : 2760 : available_clusters += counts[MSC_sec_hwm] - counts[MSC_sec_lwm];
21067 : : }
21068 : :
21069 : 2760 : if (!flag_module_lazy
21070 : 2760 : || (is_partition ()
21071 : 220 : && module_interface_p ()
21072 : 193 : && !module_partition_p ()))
21073 : : {
21074 : : /* Read the sections in forward order, so that dependencies are read
21075 : : first. See note about tarjan_connect. */
21076 : 501 : ggc_collect ();
21077 : :
21078 : 501 : lazy_snum = ~0u;
21079 : :
21080 : 501 : unsigned hwm = counts[MSC_sec_hwm];
21081 : 137522 : for (unsigned ix = counts[MSC_sec_lwm]; ok && ix != hwm; ix++)
21082 : 137021 : if (!load_section (ix, NULL))
21083 : : {
21084 : : ok = false;
21085 : : break;
21086 : : }
21087 : 501 : lazy_snum = 0;
21088 : 501 : post_load_processing ();
21089 : :
21090 : 501 : ggc_collect ();
21091 : :
21092 : 501 : if (ok && CHECKING_P)
21093 : 497367 : for (unsigned ix = 0; ix != entity_num; ix++)
21094 : 496866 : gcc_assert (!(*entity_ary)[ix + entity_lwm].is_lazy ());
21095 : : }
21096 : :
21097 : : // If the import is a header-unit, we need to register initializers
21098 : : // of any static objects it contains (looking at you _Ioinit).
21099 : : // Notice, the ordering of these initializers will be that of a
21100 : : // dynamic initializer at this point in the current TU. (Other
21101 : : // instances of these objects in other TUs will be initialized as
21102 : : // part of that TU's global initializers.)
21103 : 2760 : if (ok && counts[MSC_inits] && !read_inits (counts[MSC_inits]))
21104 : : ok = false;
21105 : :
21106 : 2760 : function_depth--;
21107 : :
21108 : 3092 : announce (flag_module_lazy ? "lazy" : "imported");
21109 : 2760 : loadedness = ML_LANGUAGE;
21110 : :
21111 : 2760 : gcc_assert (slurp->current == ~0u);
21112 : :
21113 : : /* We're done with the string table. */
21114 : 2760 : from ()->release ();
21115 : :
21116 : 2760 : return check_read (outermost, ok);
21117 : : }
21118 : :
21119 : : bool
21120 : 189914 : module_state::maybe_defrost ()
21121 : : {
21122 : 189914 : bool ok = true;
21123 : 189914 : if (from ()->is_frozen ())
21124 : : {
21125 : 9 : if (lazy_open >= lazy_limit)
21126 : 3 : freeze_an_elf ();
21127 : 18 : dump () && dump ("Defrosting '%s'", filename);
21128 : 9 : ok = from ()->defrost (maybe_add_cmi_prefix (filename));
21129 : 9 : lazy_open++;
21130 : : }
21131 : :
21132 : 189914 : return ok;
21133 : : }
21134 : :
21135 : : /* Load section SNUM, dealing with laziness. It doesn't matter if we
21136 : : have multiple concurrent loads, because we do not use TREE_VISITED
21137 : : when reading back in. */
21138 : :
21139 : : bool
21140 : 189914 : module_state::load_section (unsigned snum, binding_slot *mslot)
21141 : : {
21142 : 189914 : if (from ()->get_error ())
21143 : : return false;
21144 : :
21145 : 189914 : if (snum >= slurp->current)
21146 : 0 : from ()->set_error (elf::E_BAD_LAZY);
21147 : 189914 : else if (maybe_defrost ())
21148 : : {
21149 : 189914 : unsigned old_current = slurp->current;
21150 : 189914 : slurp->current = snum;
21151 : 189914 : slurp->lru = 0; /* Do not swap out. */
21152 : 189914 : slurp->remaining--;
21153 : 189914 : read_cluster (snum);
21154 : 189914 : slurp->lru = ++lazy_lru;
21155 : 189914 : slurp->current = old_current;
21156 : : }
21157 : :
21158 : 189914 : if (mslot && mslot->is_lazy ())
21159 : : {
21160 : : /* Oops, the section didn't set this slot. */
21161 : 0 : from ()->set_error (elf::E_BAD_DATA);
21162 : 0 : *mslot = NULL_TREE;
21163 : : }
21164 : :
21165 : 189914 : bool ok = !from ()->get_error ();
21166 : 189914 : if (!ok)
21167 : : {
21168 : 0 : error_at (loc, "failed to read compiled module cluster %u: %s",
21169 : 0 : snum, from ()->get_error (filename));
21170 : 0 : note_cmi_name ();
21171 : : }
21172 : :
21173 : 189914 : maybe_completed_reading ();
21174 : :
21175 : 189914 : return ok;
21176 : : }
21177 : :
21178 : : void
21179 : 196391 : module_state::maybe_completed_reading ()
21180 : : {
21181 : 196391 : if (loadedness == ML_LANGUAGE && slurp->current == ~0u && !slurp->remaining)
21182 : : {
21183 : 2279 : lazy_open--;
21184 : : /* We no longer need the macros, all tokenizing has been done. */
21185 : 2279 : slurp->release_macros ();
21186 : :
21187 : 2279 : from ()->end ();
21188 : 2279 : slurp->close ();
21189 : 2279 : slurped ();
21190 : : }
21191 : 196391 : }
21192 : :
21193 : : /* After a reading operation, make sure things are still ok. If not,
21194 : : emit an error and clean up. */
21195 : :
21196 : : bool
21197 : 6514 : module_state::check_read (bool outermost, bool ok)
21198 : : {
21199 : 6514 : gcc_checking_assert (!outermost || slurp->current == ~0u);
21200 : :
21201 : 6514 : if (!ok)
21202 : 34 : from ()->set_error ();
21203 : :
21204 : 6514 : if (int e = from ()->get_error ())
21205 : : {
21206 : 40 : auto_diagnostic_group d;
21207 : 40 : error_at (loc, "failed to read compiled module: %s",
21208 : 40 : from ()->get_error (filename));
21209 : 40 : note_cmi_name ();
21210 : :
21211 : 40 : if (e == EMFILE
21212 : 40 : || e == ENFILE
21213 : : #if MAPPED_READING
21214 : 40 : || e == ENOMEM
21215 : : #endif
21216 : : || false)
21217 : 0 : inform (loc, "consider using %<-fno-module-lazy%>,"
21218 : : " increasing %<-param-lazy-modules=%u%> value,"
21219 : : " or increasing the per-process file descriptor limit",
21220 : : param_lazy_modules);
21221 : 40 : else if (e == ENOENT)
21222 : 21 : inform (loc, "imports must be built before being imported");
21223 : :
21224 : 40 : if (outermost)
21225 : 37 : fatal_error (loc, "returning to the gate for a mechanical issue");
21226 : :
21227 : 3 : ok = false;
21228 : 3 : }
21229 : :
21230 : 6477 : maybe_completed_reading ();
21231 : :
21232 : 6477 : return ok;
21233 : : }
21234 : :
21235 : : /* Return the IDENTIFIER_NODE naming module IX. This is the name
21236 : : including dots. */
21237 : :
21238 : : char const *
21239 : 331 : module_name (unsigned ix, bool header_ok)
21240 : : {
21241 : 331 : if (modules)
21242 : : {
21243 : 331 : module_state *imp = (*modules)[ix];
21244 : :
21245 : 331 : if (ix && !imp->name)
21246 : 0 : imp = imp->parent;
21247 : :
21248 : 331 : if (header_ok || !imp->is_header ())
21249 : 331 : return imp->get_flatname ();
21250 : : }
21251 : :
21252 : : return NULL;
21253 : : }
21254 : :
21255 : : /* Return the bitmap describing what modules are imported. Remember,
21256 : : we always import ourselves. */
21257 : :
21258 : : bitmap
21259 : 102562 : get_import_bitmap ()
21260 : : {
21261 : 102562 : return this_module ()->imports;
21262 : : }
21263 : :
21264 : : /* Return the visible imports and path of instantiation for an
21265 : : instantiation at TINST. If TINST is nullptr, we're not in an
21266 : : instantiation, and thus will return the visible imports of the
21267 : : current TU (and NULL *PATH_MAP_P). We cache the information on
21268 : : the tinst level itself. */
21269 : :
21270 : : static bitmap
21271 : 106826 : path_of_instantiation (tinst_level *tinst, bitmap *path_map_p)
21272 : : {
21273 : 106826 : gcc_checking_assert (modules_p ());
21274 : :
21275 : 106826 : if (!tinst || TREE_CODE (tinst->tldcl) == TEMPLATE_FOR_STMT)
21276 : : {
21277 : 1 : gcc_assert (!tinst || !tinst->next);
21278 : : /* Not inside an instantiation, just the regular case. */
21279 : 46042 : *path_map_p = nullptr;
21280 : 46042 : return get_import_bitmap ();
21281 : : }
21282 : :
21283 : 60784 : if (!tinst->path)
21284 : : {
21285 : : /* Calculate. */
21286 : 17576 : bitmap visible = path_of_instantiation (tinst->next, path_map_p);
21287 : 17576 : bitmap path_map = *path_map_p;
21288 : :
21289 : 17576 : if (!path_map)
21290 : : {
21291 : 2182 : path_map = BITMAP_GGC_ALLOC ();
21292 : 2182 : bitmap_set_bit (path_map, 0);
21293 : : }
21294 : :
21295 : 17576 : tree decl = tinst->tldcl;
21296 : 17576 : if (TREE_CODE (decl) == TREE_LIST)
21297 : 0 : decl = TREE_PURPOSE (decl);
21298 : 17576 : if (TYPE_P (decl))
21299 : 1942 : decl = TYPE_NAME (decl);
21300 : :
21301 : 17576 : if (unsigned mod = get_originating_module (decl))
21302 : 3491 : if (!bitmap_bit_p (path_map, mod))
21303 : : {
21304 : : /* This is brand new information! */
21305 : 148 : bitmap new_path = BITMAP_GGC_ALLOC ();
21306 : 148 : bitmap_copy (new_path, path_map);
21307 : 148 : bitmap_set_bit (new_path, mod);
21308 : 148 : path_map = new_path;
21309 : :
21310 : 148 : bitmap imports = (*modules)[mod]->imports;
21311 : 148 : if (bitmap_intersect_compl_p (imports, visible))
21312 : : {
21313 : : /* IMPORTS contains additional modules to VISIBLE. */
21314 : 24 : bitmap new_visible = BITMAP_GGC_ALLOC ();
21315 : :
21316 : 24 : bitmap_ior (new_visible, visible, imports);
21317 : 24 : visible = new_visible;
21318 : : }
21319 : : }
21320 : :
21321 : 17576 : tinst->path = path_map;
21322 : 17576 : tinst->visible = visible;
21323 : : }
21324 : :
21325 : 60784 : *path_map_p = tinst->path;
21326 : 60784 : return tinst->visible;
21327 : : }
21328 : :
21329 : : /* Return the bitmap describing what modules are visible along the
21330 : : path of instantiation. If we're not an instantiation, this will be
21331 : : the visible imports of the TU. *PATH_MAP_P is filled in with the
21332 : : modules owning the instantiation path -- we see the module-linkage
21333 : : entities of those modules. */
21334 : :
21335 : : bitmap
21336 : 22777467 : visible_instantiation_path (bitmap *path_map_p)
21337 : : {
21338 : 22777467 : if (!modules_p ())
21339 : : return NULL;
21340 : :
21341 : 89250 : return path_of_instantiation (current_instantiation (), path_map_p);
21342 : : }
21343 : :
21344 : : /* We've just directly imported IMPORT. Update our import/export
21345 : : bitmaps. IS_EXPORT is true if we're reexporting the OTHER. */
21346 : :
21347 : : void
21348 : 2901 : module_state::set_import (module_state const *import, bool is_export)
21349 : : {
21350 : 2901 : gcc_checking_assert (this != import);
21351 : :
21352 : : /* We see IMPORT's exports (which includes IMPORT). If IMPORT is
21353 : : the primary interface or a partition we'll see its imports. */
21354 : 2901 : bitmap_ior_into (imports, import->is_module () || import->is_partition ()
21355 : : ? import->imports : import->exports);
21356 : :
21357 : 2901 : if (is_export)
21358 : : /* We'll export OTHER's exports. */
21359 : 415 : bitmap_ior_into (exports, import->exports);
21360 : 2901 : }
21361 : :
21362 : : /* Return the declaring entity of DECL. That is the decl determining
21363 : : how to decorate DECL with module information. Returns NULL_TREE if
21364 : : it's the global module. */
21365 : :
21366 : : tree
21367 : 167131809 : get_originating_module_decl (tree decl)
21368 : : {
21369 : : /* An enumeration constant. */
21370 : 167131809 : if (TREE_CODE (decl) == CONST_DECL
21371 : 4884 : && DECL_CONTEXT (decl)
21372 : 167136693 : && (TREE_CODE (DECL_CONTEXT (decl)) == ENUMERAL_TYPE))
21373 : 4365 : decl = TYPE_NAME (DECL_CONTEXT (decl));
21374 : 167127444 : else if (TREE_CODE (decl) == FIELD_DECL
21375 : 167118285 : || TREE_CODE (decl) == USING_DECL
21376 : 334237907 : || CONST_DECL_USING_P (decl))
21377 : : {
21378 : 17500 : decl = DECL_CONTEXT (decl);
21379 : 17500 : if (TREE_CODE (decl) != FUNCTION_DECL)
21380 : 17500 : decl = TYPE_NAME (decl);
21381 : : }
21382 : :
21383 : 167131809 : gcc_checking_assert (TREE_CODE (decl) == TEMPLATE_DECL
21384 : : || TREE_CODE (decl) == FUNCTION_DECL
21385 : : || TREE_CODE (decl) == TYPE_DECL
21386 : : || TREE_CODE (decl) == VAR_DECL
21387 : : || TREE_CODE (decl) == CONCEPT_DECL
21388 : : || TREE_CODE (decl) == NAMESPACE_DECL);
21389 : :
21390 : 168499007 : for (;;)
21391 : : {
21392 : : /* Uninstantiated template friends are owned by the befriending
21393 : : class -- not their context. */
21394 : 167815408 : if (TREE_CODE (decl) == TEMPLATE_DECL
21395 : 167815408 : && DECL_UNINSTANTIATED_TEMPLATE_FRIEND_P (decl))
21396 : 6527 : decl = TYPE_NAME (DECL_CHAIN (decl));
21397 : :
21398 : : /* An imported temploid friend is attached to the same module the
21399 : : befriending class was. */
21400 : 167815408 : if (imported_temploid_friends)
21401 : 2482874 : if (tree *slot = imported_temploid_friends->get (decl))
21402 : 856 : decl = *slot;
21403 : :
21404 : 167815408 : int use;
21405 : 167815408 : if (tree ti = node_template_info (decl, use))
21406 : : {
21407 : 562282 : decl = TI_TEMPLATE (ti);
21408 : 562282 : if (TREE_CODE (decl) != TEMPLATE_DECL)
21409 : : {
21410 : : /* A friend template specialization. */
21411 : 28 : gcc_checking_assert (OVL_P (decl));
21412 : 28 : return global_namespace;
21413 : : }
21414 : : }
21415 : : else
21416 : : {
21417 : 167253126 : tree ctx = CP_DECL_CONTEXT (decl);
21418 : 167253126 : if (TREE_CODE (ctx) == NAMESPACE_DECL)
21419 : : break;
21420 : :
21421 : 121345 : if (TYPE_P (ctx))
21422 : : {
21423 : 112490 : ctx = TYPE_NAME (ctx);
21424 : 112490 : if (!ctx)
21425 : : {
21426 : : /* Some kind of internal type. */
21427 : 0 : gcc_checking_assert (DECL_ARTIFICIAL (decl));
21428 : 0 : return global_namespace;
21429 : : }
21430 : : }
21431 : 121345 : decl = ctx;
21432 : : }
21433 : 683599 : }
21434 : :
21435 : 167131781 : return decl;
21436 : : }
21437 : :
21438 : : /* If DECL is imported, return which module imported it, or 0 for the current
21439 : : module. Except that if GLOBAL_M1, return -1 for decls attached to the
21440 : : global module. */
21441 : :
21442 : : int
21443 : 1078968 : get_originating_module (tree decl, bool global_m1)
21444 : : {
21445 : 1078968 : tree owner = get_originating_module_decl (decl);
21446 : 1078968 : tree not_tmpl = STRIP_TEMPLATE (owner);
21447 : :
21448 : 1078968 : if (!DECL_LANG_SPECIFIC (not_tmpl))
21449 : 228050 : return global_m1 ? -1 : 0;
21450 : :
21451 : 1663236 : if (global_m1 && !DECL_MODULE_ATTACH_P (not_tmpl))
21452 : : return -1;
21453 : :
21454 : 94660 : int mod = !DECL_MODULE_IMPORT_P (not_tmpl) ? 0 : get_importing_module (owner);
21455 : 94660 : gcc_checking_assert (!global_m1 || !(*modules)[mod]->is_header ());
21456 : : return mod;
21457 : : }
21458 : :
21459 : : /* DECL is imported, return which module imported it.
21460 : : If FLEXIBLE, return -1 if not found, otherwise checking ICE. */
21461 : :
21462 : : unsigned
21463 : 6900 : get_importing_module (tree decl, bool flexible)
21464 : : {
21465 : 6900 : unsigned index = import_entity_index (decl, flexible);
21466 : 6900 : if (index == ~(~0u >> 1))
21467 : : return -1;
21468 : 6900 : module_state *module = import_entity_module (index);
21469 : :
21470 : 6900 : return module->mod;
21471 : : }
21472 : :
21473 : : /* Is it permissible to redeclare OLDDECL with NEWDECL.
21474 : :
21475 : : If NEWDECL is NULL, assumes that OLDDECL will be redeclared using
21476 : : the current scope's module and attachment. */
21477 : :
21478 : : bool
21479 : 145856 : module_may_redeclare (tree olddecl, tree newdecl)
21480 : : {
21481 : 145856 : tree decl = olddecl;
21482 : 163406 : for (;;)
21483 : : {
21484 : 154631 : tree ctx = CP_DECL_CONTEXT (decl);
21485 : 154631 : if (TREE_CODE (ctx) == NAMESPACE_DECL)
21486 : : // Found the namespace-scope decl.
21487 : : break;
21488 : 17109 : if (!CLASS_TYPE_P (ctx))
21489 : : // We've met a non-class scope. Such a thing is not
21490 : : // reopenable, so we must be ok.
21491 : : return true;
21492 : 8775 : decl = TYPE_NAME (ctx);
21493 : 8775 : }
21494 : :
21495 : 137522 : int use_tpl = 0;
21496 : 137522 : if (node_template_info (STRIP_TEMPLATE (decl), use_tpl) && use_tpl)
21497 : : // Specializations of any kind can be redeclared anywhere.
21498 : : // FIXME: Should we be checking this in more places on the scope chain?
21499 : : return true;
21500 : :
21501 : 92443 : module_state *old_mod = get_primary (this_module ());
21502 : 92443 : module_state *new_mod = old_mod;
21503 : :
21504 : 92443 : tree old_origin = get_originating_module_decl (decl);
21505 : 92443 : tree old_inner = STRIP_TEMPLATE (old_origin);
21506 : 92443 : bool olddecl_attached_p = (DECL_LANG_SPECIFIC (old_inner)
21507 : 149149 : && DECL_MODULE_ATTACH_P (old_inner));
21508 : 149149 : if (DECL_LANG_SPECIFIC (old_inner) && DECL_MODULE_IMPORT_P (old_inner))
21509 : : {
21510 : 607 : unsigned index = import_entity_index (old_origin);
21511 : 607 : old_mod = get_primary (import_entity_module (index));
21512 : : }
21513 : :
21514 : 92443 : bool newdecl_attached_p = module_attach_p ();
21515 : 92443 : if (newdecl)
21516 : : {
21517 : 26312 : tree new_origin = get_originating_module_decl (newdecl);
21518 : 26312 : tree new_inner = STRIP_TEMPLATE (new_origin);
21519 : 26312 : newdecl_attached_p = (DECL_LANG_SPECIFIC (new_inner)
21520 : 51411 : && DECL_MODULE_ATTACH_P (new_inner));
21521 : 51411 : if (DECL_LANG_SPECIFIC (new_inner) && DECL_MODULE_IMPORT_P (new_inner))
21522 : : {
21523 : 175 : unsigned index = import_entity_index (new_origin);
21524 : 175 : new_mod = get_primary (import_entity_module (index));
21525 : : }
21526 : : }
21527 : :
21528 : : /* Module attachment needs to match. */
21529 : 92443 : if (olddecl_attached_p == newdecl_attached_p)
21530 : : {
21531 : 92401 : if (!olddecl_attached_p)
21532 : : /* Both are GM entities, OK. */
21533 : : return true;
21534 : :
21535 : 1238 : if (new_mod == old_mod)
21536 : : /* Both attached to same named module, OK. */
21537 : : return true;
21538 : : }
21539 : :
21540 : : /* Attached to different modules, error. */
21541 : 45 : decl = newdecl ? newdecl : olddecl;
21542 : 45 : location_t loc = newdecl ? DECL_SOURCE_LOCATION (newdecl) : input_location;
21543 : 45 : if (DECL_IS_UNDECLARED_BUILTIN (olddecl))
21544 : : {
21545 : 3 : if (newdecl_attached_p)
21546 : 3 : error_at (loc, "declaring %qD in module %qs conflicts with builtin "
21547 : : "in global module", decl, new_mod->get_flatname ());
21548 : : else
21549 : 0 : error_at (loc, "declaration %qD conflicts with builtin", decl);
21550 : : }
21551 : 81 : else if (DECL_LANG_SPECIFIC (old_inner) && DECL_MODULE_IMPORT_P (old_inner))
21552 : : {
21553 : 27 : auto_diagnostic_group d;
21554 : 27 : if (newdecl_attached_p)
21555 : 3 : error_at (loc, "redeclaring %qD in module %qs conflicts with import",
21556 : : decl, new_mod->get_flatname ());
21557 : : else
21558 : 24 : error_at (loc, "redeclaring %qD in global module conflicts with import",
21559 : : decl);
21560 : :
21561 : 27 : if (olddecl_attached_p)
21562 : 27 : inform (DECL_SOURCE_LOCATION (olddecl),
21563 : : "import declared attached to module %qs",
21564 : : old_mod->get_flatname ());
21565 : : else
21566 : 0 : inform (DECL_SOURCE_LOCATION (olddecl),
21567 : : "import declared in global module");
21568 : 27 : }
21569 : : else
21570 : : {
21571 : 15 : auto_diagnostic_group d;
21572 : 15 : if (newdecl_attached_p)
21573 : 15 : error_at (loc, "conflicting declaration of %qD in module %qs",
21574 : : decl, new_mod->get_flatname ());
21575 : : else
21576 : 0 : error_at (loc, "conflicting declaration of %qD in global module",
21577 : : decl);
21578 : :
21579 : 15 : if (olddecl_attached_p)
21580 : 0 : inform (DECL_SOURCE_LOCATION (olddecl),
21581 : : "previously declared in module %qs",
21582 : : old_mod->get_flatname ());
21583 : : else
21584 : 15 : inform (DECL_SOURCE_LOCATION (olddecl),
21585 : : "previously declared in global module");
21586 : 15 : }
21587 : : return false;
21588 : : }
21589 : :
21590 : : /* DECL is being created by this TU. Record it came from here. We
21591 : : record module purview, so we can see if partial or explicit
21592 : : specialization needs to be written out, even though its purviewness
21593 : : comes from the most general template. */
21594 : :
21595 : : void
21596 : 813599365 : set_instantiating_module (tree decl)
21597 : : {
21598 : 813599365 : gcc_assert (TREE_CODE (decl) == FUNCTION_DECL
21599 : : || VAR_P (decl)
21600 : : || TREE_CODE (decl) == TYPE_DECL
21601 : : || TREE_CODE (decl) == CONCEPT_DECL
21602 : : || TREE_CODE (decl) == TEMPLATE_DECL
21603 : : || TREE_CODE (decl) == CONST_DECL
21604 : : || (TREE_CODE (decl) == NAMESPACE_DECL
21605 : : && DECL_NAMESPACE_ALIAS (decl)));
21606 : :
21607 : 813599365 : if (!modules_p ())
21608 : : return;
21609 : :
21610 : 2720486 : decl = STRIP_TEMPLATE (decl);
21611 : :
21612 : 2720486 : if (!DECL_LANG_SPECIFIC (decl) && module_purview_p ())
21613 : 350737 : retrofit_lang_decl (decl);
21614 : :
21615 : 2720486 : if (DECL_LANG_SPECIFIC (decl))
21616 : : {
21617 : 2175386 : DECL_MODULE_PURVIEW_P (decl) = module_purview_p ();
21618 : : /* If this was imported, we'll still be in the entity_hash. */
21619 : 2175386 : DECL_MODULE_IMPORT_P (decl) = false;
21620 : : }
21621 : : }
21622 : :
21623 : : /* If DECL is a class member, whose class is not defined in this TU
21624 : : (it was imported), remember this decl. */
21625 : :
21626 : : void
21627 : 123559 : set_defining_module (tree decl)
21628 : : {
21629 : 123559 : gcc_checking_assert (!DECL_LANG_SPECIFIC (decl)
21630 : : || !DECL_MODULE_IMPORT_P (decl));
21631 : :
21632 : 123559 : if (module_maybe_has_cmi_p ())
21633 : : {
21634 : : /* We need to track all declarations within a module, not just those
21635 : : in the module purview, because we don't necessarily know yet if
21636 : : this module will require a CMI while in the global fragment. */
21637 : 84384 : tree ctx = DECL_CONTEXT (decl);
21638 : 84384 : if (ctx
21639 : 84384 : && (TREE_CODE (ctx) == RECORD_TYPE || TREE_CODE (ctx) == UNION_TYPE)
21640 : 15052 : && DECL_LANG_SPECIFIC (TYPE_NAME (ctx))
21641 : 95776 : && DECL_MODULE_IMPORT_P (TYPE_NAME (ctx)))
21642 : : {
21643 : : /* This entity's context is from an import. We may need to
21644 : : record this entity to make sure we emit it in the CMI.
21645 : : Template specializations are in the template hash tables,
21646 : : so we don't need to record them here as well. */
21647 : 12 : int use_tpl = -1;
21648 : 12 : tree ti = node_template_info (decl, use_tpl);
21649 : 12 : if (use_tpl <= 0)
21650 : : {
21651 : 12 : if (ti)
21652 : : {
21653 : 3 : gcc_checking_assert (!use_tpl);
21654 : : /* Get to the TEMPLATE_DECL. */
21655 : 3 : decl = TI_TEMPLATE (ti);
21656 : : }
21657 : :
21658 : : /* Record it on the class_members list. */
21659 : 12 : vec_safe_push (class_members, decl);
21660 : : }
21661 : : }
21662 : : }
21663 : 123559 : }
21664 : :
21665 : : /* Also remember DECL if it's a newly declared class template partial
21666 : : specialization, because these are not necessarily added to the
21667 : : instantiation tables. */
21668 : :
21669 : : void
21670 : 7369171 : set_defining_module_for_partial_spec (tree decl)
21671 : : {
21672 : 7369171 : if (module_maybe_has_cmi_p ()
21673 : 19755 : && DECL_IMPLICIT_TYPEDEF_P (decl)
21674 : 7386040 : && CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (decl)))
21675 : 16869 : vec_safe_push (partial_specializations, decl);
21676 : 7369171 : }
21677 : :
21678 : : void
21679 : 278570527 : set_originating_module (tree decl, bool friend_p ATTRIBUTE_UNUSED)
21680 : : {
21681 : 278570527 : set_instantiating_module (decl);
21682 : :
21683 : 278570527 : if (!DECL_NAMESPACE_SCOPE_P (decl))
21684 : : return;
21685 : :
21686 : 170361577 : gcc_checking_assert (friend_p || decl == get_originating_module_decl (decl));
21687 : :
21688 : 170361577 : if (module_attach_p ())
21689 : : {
21690 : 4270 : retrofit_lang_decl (decl);
21691 : 4270 : DECL_MODULE_ATTACH_P (decl) = true;
21692 : : }
21693 : :
21694 : : /* It is ill-formed to export a declaration with internal linkage. However,
21695 : : at the point this function is called we don't yet always know whether this
21696 : : declaration has internal linkage; instead we defer this check for callers
21697 : : to do once visibility has been determined. */
21698 : 170361577 : if (module_exporting_p ())
21699 : 218186 : DECL_MODULE_EXPORT_P (decl) = true;
21700 : : }
21701 : :
21702 : : /* Checks whether DECL within a module unit has valid linkage for its kind.
21703 : : Must be called after visibility for DECL has been finalised. */
21704 : :
21705 : : void
21706 : 344287482 : check_module_decl_linkage (tree decl)
21707 : : {
21708 : 344287482 : if (!module_has_cmi_p ())
21709 : : return;
21710 : :
21711 : : /* A header unit shall not contain a definition of a non-inline function
21712 : : or variable (not template) whose name has external linkage. */
21713 : 501886 : if (header_module_p ()
21714 : 452861 : && !processing_template_decl
21715 : 147601 : && ((TREE_CODE (decl) == FUNCTION_DECL
21716 : 87533 : && !DECL_DECLARED_INLINE_P (decl))
21717 : 113054 : || (TREE_CODE (decl) == VAR_DECL
21718 : 33688 : && !DECL_INLINE_VAR_P (decl)))
21719 : 43346 : && decl_defined_p (decl)
21720 : 4168 : && !(DECL_LANG_SPECIFIC (decl)
21721 : 4168 : && DECL_TEMPLATE_INSTANTIATION (decl))
21722 : 506054 : && decl_linkage (decl) == lk_external)
21723 : 60 : error_at (DECL_SOURCE_LOCATION (decl),
21724 : : "external linkage definition of %qD in header module must "
21725 : : "be declared %<inline%>", decl);
21726 : :
21727 : : /* An internal-linkage declaration cannot be generally be exported.
21728 : : But it's OK to export any declaration from a header unit, including
21729 : : internal linkage declarations. */
21730 : 501886 : if (!header_module_p () && DECL_MODULE_EXPORT_P (decl))
21731 : : {
21732 : : /* Let's additionally treat any exported declaration within an
21733 : : internal namespace as exporting a declaration with internal
21734 : : linkage, as this would also implicitly export the internal
21735 : : linkage namespace. */
21736 : 2035 : if (decl_internal_context_p (decl))
21737 : : {
21738 : 50 : error_at (DECL_SOURCE_LOCATION (decl),
21739 : : "exporting declaration %qD declared in unnamed namespace",
21740 : : decl);
21741 : 50 : DECL_MODULE_EXPORT_P (decl) = false;
21742 : : }
21743 : 1985 : else if (decl_linkage (decl) == lk_internal)
21744 : : {
21745 : 17 : error_at (DECL_SOURCE_LOCATION (decl),
21746 : : "exporting declaration %qD with internal linkage", decl);
21747 : 17 : DECL_MODULE_EXPORT_P (decl) = false;
21748 : : }
21749 : : }
21750 : : }
21751 : :
21752 : : /* DECL is keyed to CTX for odr purposes. */
21753 : :
21754 : : void
21755 : 1044156 : maybe_key_decl (tree ctx, tree decl)
21756 : : {
21757 : 1044156 : if (!modules_p ())
21758 : : return;
21759 : :
21760 : : /* We only need to deal with lambdas attached to var, field,
21761 : : parm, type, or concept decls. */
21762 : 7688 : if (TREE_CODE (ctx) != VAR_DECL
21763 : 7688 : && TREE_CODE (ctx) != FIELD_DECL
21764 : : && TREE_CODE (ctx) != PARM_DECL
21765 : : && TREE_CODE (ctx) != TYPE_DECL
21766 : : && TREE_CODE (ctx) != CONCEPT_DECL)
21767 : : return;
21768 : :
21769 : : /* For members, key it to the containing type to handle deduplication
21770 : : correctly. For fields, this is necessary as FIELD_DECLs have no
21771 : : dep and so would only be streamed after the lambda type, defeating
21772 : : our ability to merge them.
21773 : :
21774 : : Other class-scope key decls might depend on the type of the lambda
21775 : : but be within the same cluster; we need to ensure that we never
21776 : : first see the key decl while streaming the lambda type as merging
21777 : : would then fail when comparing the partially-streamed lambda type
21778 : : of the key decl with the existing (PR c++/122310).
21779 : :
21780 : : Perhaps sort_cluster can be adjusted to handle this better, but
21781 : : this is a simple workaround (and might down on the number of
21782 : : entries in keyed_table as a bonus). */
21783 : 612 : while (DECL_CLASS_SCOPE_P (ctx))
21784 : 236 : ctx = TYPE_NAME (DECL_CONTEXT (ctx));
21785 : :
21786 : 376 : if (!keyed_table)
21787 : 89 : keyed_table = new keyed_map_t (EXPERIMENT (1, 400));
21788 : :
21789 : 376 : auto &vec = keyed_table->get_or_insert (ctx);
21790 : 376 : if (!vec.length ())
21791 : : {
21792 : 352 : retrofit_lang_decl (ctx);
21793 : 352 : DECL_MODULE_KEYED_DECLS_P (ctx) = true;
21794 : : }
21795 : 376 : vec.safe_push (decl);
21796 : : }
21797 : :
21798 : : /* Find the scope that the lambda DECL is keyed to, if any. */
21799 : :
21800 : : static tree
21801 : 1178 : get_keyed_decl_scope (tree decl)
21802 : : {
21803 : 2356 : gcc_checking_assert (LAMBDA_TYPE_P (TREE_TYPE (decl)));
21804 : 1178 : tree scope = LAMBDA_TYPE_EXTRA_SCOPE (TREE_TYPE (decl));
21805 : 1178 : if (!scope)
21806 : : return NULL_TREE;
21807 : :
21808 : 1160 : gcc_checking_assert (TREE_CODE (scope) == VAR_DECL
21809 : : || TREE_CODE (scope) == FIELD_DECL
21810 : : || TREE_CODE (scope) == PARM_DECL
21811 : : || TREE_CODE (scope) == TYPE_DECL
21812 : : || TREE_CODE (scope) == CONCEPT_DECL);
21813 : :
21814 : 1926 : while (DECL_CLASS_SCOPE_P (scope))
21815 : 766 : scope = TYPE_NAME (DECL_CONTEXT (scope));
21816 : :
21817 : 2320 : gcc_checking_assert (DECL_LANG_SPECIFIC (scope)
21818 : : && DECL_MODULE_KEYED_DECLS_P (scope));
21819 : : return scope;
21820 : : }
21821 : :
21822 : : /* DECL is an instantiated friend that should be attached to the same
21823 : : module that ORIG is. */
21824 : :
21825 : : void
21826 : 2795012 : propagate_defining_module (tree decl, tree orig)
21827 : : {
21828 : 2795012 : if (!modules_p ())
21829 : : return;
21830 : :
21831 : 5218 : tree not_tmpl = STRIP_TEMPLATE (orig);
21832 : 10415 : if (DECL_LANG_SPECIFIC (not_tmpl) && DECL_MODULE_ATTACH_P (not_tmpl))
21833 : : {
21834 : 120 : tree inner = STRIP_TEMPLATE (decl);
21835 : 120 : retrofit_lang_decl (inner);
21836 : 120 : DECL_MODULE_ATTACH_P (inner) = true;
21837 : : }
21838 : :
21839 : 10415 : if (DECL_LANG_SPECIFIC (not_tmpl) && DECL_MODULE_IMPORT_P (not_tmpl))
21840 : : {
21841 : 386 : bool exists = imported_temploid_friends->put (decl, orig);
21842 : :
21843 : : /* We should only be called if lookup for an existing decl
21844 : : failed, in which case there shouldn't already be an entry
21845 : : in the map. */
21846 : 386 : gcc_assert (!exists);
21847 : : }
21848 : : }
21849 : :
21850 : : /* NEWDECL matched with OLDDECL, transfer defining module information
21851 : : onto OLDDECL. We've already validated attachment matches. */
21852 : :
21853 : : void
21854 : 19279797 : transfer_defining_module (tree olddecl, tree newdecl)
21855 : : {
21856 : 19279797 : if (!modules_p ())
21857 : : return;
21858 : :
21859 : 43630 : tree old_inner = STRIP_TEMPLATE (olddecl);
21860 : 43630 : tree new_inner = STRIP_TEMPLATE (newdecl);
21861 : :
21862 : 43630 : if (DECL_LANG_SPECIFIC (new_inner))
21863 : : {
21864 : 43542 : gcc_checking_assert (DECL_LANG_SPECIFIC (old_inner));
21865 : 43542 : if (DECL_MODULE_PURVIEW_P (new_inner))
21866 : 21639 : DECL_MODULE_PURVIEW_P (old_inner) = true;
21867 : 43542 : if (!DECL_MODULE_IMPORT_P (new_inner))
21868 : 43542 : DECL_MODULE_IMPORT_P (old_inner) = false;
21869 : : }
21870 : :
21871 : 43630 : if (tree *p = imported_temploid_friends->get (newdecl))
21872 : : {
21873 : 73 : tree orig = *p;
21874 : 73 : tree &slot = imported_temploid_friends->get_or_insert (olddecl);
21875 : 73 : if (!slot)
21876 : 50 : slot = orig;
21877 : 23 : else if (slot != orig)
21878 : : /* This can happen when multiple classes declare the same
21879 : : friend function (e.g. g++.dg/modules/tpl-friend-4);
21880 : : make sure we at least attach to the same module. */
21881 : 3 : gcc_checking_assert (get_originating_module (slot)
21882 : : == get_originating_module (orig));
21883 : : }
21884 : : }
21885 : :
21886 : : /* DECL is being freed, clear data we don't need anymore. */
21887 : :
21888 : : void
21889 : 31272 : remove_defining_module (tree decl)
21890 : : {
21891 : 31272 : if (!modules_p ())
21892 : : return;
21893 : :
21894 : 31272 : if (imported_temploid_friends)
21895 : 31272 : imported_temploid_friends->remove (decl);
21896 : : }
21897 : :
21898 : : /* Create the flat name string. It is simplest to have it handy. */
21899 : :
21900 : : void
21901 : 5972 : module_state::set_flatname ()
21902 : : {
21903 : 5972 : gcc_checking_assert (!flatname);
21904 : 5972 : if (parent)
21905 : : {
21906 : 627 : auto_vec<tree,5> ids;
21907 : 627 : size_t len = 0;
21908 : 627 : char const *primary = NULL;
21909 : 627 : size_t pfx_len = 0;
21910 : :
21911 : 627 : for (module_state *probe = this;
21912 : 1549 : probe;
21913 : 922 : probe = probe->parent)
21914 : 1386 : if (is_partition () && !probe->is_partition ())
21915 : : {
21916 : 464 : primary = probe->get_flatname ();
21917 : 464 : pfx_len = strlen (primary);
21918 : 464 : break;
21919 : : }
21920 : : else
21921 : : {
21922 : 922 : ids.safe_push (probe->name);
21923 : 922 : len += IDENTIFIER_LENGTH (probe->name) + 1;
21924 : : }
21925 : :
21926 : 627 : char *flat = XNEWVEC (char, pfx_len + len + is_partition ());
21927 : 627 : flatname = flat;
21928 : :
21929 : 627 : if (primary)
21930 : : {
21931 : 464 : memcpy (flat, primary, pfx_len);
21932 : 464 : flat += pfx_len;
21933 : 464 : *flat++ = ':';
21934 : : }
21935 : :
21936 : 1549 : for (unsigned len = 0; ids.length ();)
21937 : : {
21938 : 922 : if (len)
21939 : 295 : flat[len++] = '.';
21940 : 922 : tree elt = ids.pop ();
21941 : 922 : unsigned l = IDENTIFIER_LENGTH (elt);
21942 : 922 : memcpy (flat + len, IDENTIFIER_POINTER (elt), l + 1);
21943 : 922 : len += l;
21944 : : }
21945 : 627 : }
21946 : 5345 : else if (is_header ())
21947 : 1865 : flatname = TREE_STRING_POINTER (name);
21948 : : else
21949 : 3480 : flatname = IDENTIFIER_POINTER (name);
21950 : 5972 : }
21951 : :
21952 : : /* Open the GCM file and prepare to read. Return whether that was
21953 : : successful. */
21954 : :
21955 : : bool
21956 : 2890 : module_state::open_slurp (cpp_reader *reader)
21957 : : {
21958 : 2890 : if (slurp)
21959 : : return true;
21960 : :
21961 : 2845 : if (lazy_open >= lazy_limit)
21962 : 9 : freeze_an_elf ();
21963 : :
21964 : 2845 : int fd = -1;
21965 : 2845 : int e = ENOENT;
21966 : 2845 : if (filename)
21967 : : {
21968 : 2845 : const char *file = maybe_add_cmi_prefix (filename);
21969 : 3341 : dump () && dump ("CMI is %s", file);
21970 : 2845 : if (note_module_cmi_yes || inform_cmi_p)
21971 : 12 : inform (loc, "reading CMI %qs", file);
21972 : : /* Add the CMI file to the dependency tracking. */
21973 : 2845 : if (cpp_get_deps (reader))
21974 : 12 : deps_add_dep (cpp_get_deps (reader), file);
21975 : 2845 : fd = open (file, O_RDONLY | O_CLOEXEC | O_BINARY);
21976 : 2845 : e = errno;
21977 : : }
21978 : :
21979 : 2845 : gcc_checking_assert (!slurp);
21980 : 5669 : slurp = new slurping (new elf_in (fd, e));
21981 : :
21982 : 2845 : bool ok = from ()->begin (loc);
21983 : 2845 : if (ok)
21984 : : {
21985 : 2824 : lazy_open++;
21986 : 2824 : slurp->lru = ++lazy_lru;
21987 : : }
21988 : : return ok;
21989 : : }
21990 : :
21991 : : /* Return whether importing this GCM would work without an error in
21992 : : read_config. */
21993 : :
21994 : : bool
21995 : 48 : module_state::check_importable (cpp_reader *reader)
21996 : : {
21997 : 48 : if (loadedness > ML_CONFIG)
21998 : : return true;
21999 : 45 : if (!open_slurp (reader))
22000 : : return false;
22001 : 45 : module_state_config config;
22002 : 45 : return read_config (config, /*complain*/false);
22003 : : }
22004 : :
22005 : : /* Read the CMI file for a module. */
22006 : :
22007 : : bool
22008 : 2845 : module_state::do_import (cpp_reader *reader, bool outermost)
22009 : : {
22010 : 2845 : gcc_assert (global_namespace == current_scope () && loadedness == ML_NONE);
22011 : :
22012 : : /* If this TU is a partition of the module we're importing,
22013 : : that module is the primary module interface. */
22014 : 2845 : if (this_module ()->is_partition ()
22015 : 2890 : && this == get_primary (this_module ()))
22016 : 6 : module_p = true;
22017 : :
22018 : 2845 : loc = linemap_module_loc (line_table, loc, get_flatname ());
22019 : :
22020 : 2845 : bool ok = open_slurp (reader);
22021 : 2845 : if (!from ()->get_error ())
22022 : : {
22023 : 2824 : announce ("importing");
22024 : 2824 : loadedness = ML_CONFIG;
22025 : 2824 : ok = read_initial (reader);
22026 : : }
22027 : :
22028 : 2845 : gcc_assert (slurp->current == ~0u);
22029 : :
22030 : 2845 : return check_read (outermost, ok);
22031 : : }
22032 : :
22033 : : /* Attempt to increase the file descriptor limit. */
22034 : :
22035 : : static bool
22036 : 4656 : try_increase_lazy (unsigned want)
22037 : : {
22038 : 4656 : gcc_checking_assert (lazy_open >= lazy_limit);
22039 : :
22040 : : /* If we're increasing, saturate at hard limit. */
22041 : 4656 : if (want > lazy_hard_limit && lazy_limit < lazy_hard_limit)
22042 : 4656 : want = lazy_hard_limit;
22043 : :
22044 : : #if HAVE_SETRLIMIT
22045 : 4656 : if ((!lazy_limit || !param_lazy_modules)
22046 : 4644 : && lazy_hard_limit
22047 : 4644 : && want <= lazy_hard_limit)
22048 : : {
22049 : 4644 : struct rlimit rlimit;
22050 : 4644 : rlimit.rlim_cur = want + LAZY_HEADROOM;
22051 : 4644 : rlimit.rlim_max = lazy_hard_limit + LAZY_HEADROOM;
22052 : 4644 : if (!setrlimit (RLIMIT_NOFILE, &rlimit))
22053 : 4644 : lazy_limit = want;
22054 : : }
22055 : : #endif
22056 : :
22057 : 4656 : return lazy_open < lazy_limit;
22058 : : }
22059 : :
22060 : : /* Pick a victim module to freeze its reader. */
22061 : :
22062 : : void
22063 : 12 : module_state::freeze_an_elf ()
22064 : : {
22065 : 12 : if (try_increase_lazy (lazy_open * 2))
22066 : : return;
22067 : :
22068 : 12 : module_state *victim = NULL;
22069 : 12 : for (unsigned ix = modules->length (); ix--;)
22070 : : {
22071 : 30 : module_state *candidate = (*modules)[ix];
22072 : 30 : if (candidate && candidate->slurp && candidate->slurp->lru
22073 : 60 : && candidate->from ()->is_freezable ()
22074 : 39 : && (!victim || victim->slurp->lru > candidate->slurp->lru))
22075 : : victim = candidate;
22076 : : }
22077 : :
22078 : 12 : if (victim)
22079 : : {
22080 : 18 : dump () && dump ("Freezing '%s'", victim->filename);
22081 : 9 : if (victim->slurp->macro_defs.size)
22082 : : /* Save the macro definitions to a buffer. */
22083 : 0 : victim->from ()->preserve (victim->slurp->macro_defs);
22084 : 9 : if (victim->slurp->macro_tbl.size)
22085 : : /* Save the macro definitions to a buffer. */
22086 : 0 : victim->from ()->preserve (victim->slurp->macro_tbl);
22087 : 9 : victim->from ()->freeze ();
22088 : 9 : lazy_open--;
22089 : : }
22090 : : else
22091 : 3 : dump () && dump ("No module available for freezing");
22092 : : }
22093 : :
22094 : : /* Load the lazy slot *MSLOT, INDEX'th slot of the module. */
22095 : :
22096 : : bool
22097 : 48043 : module_state::lazy_load (unsigned index, binding_slot *mslot)
22098 : : {
22099 : 48043 : unsigned n = dump.push (this);
22100 : :
22101 : 48043 : gcc_checking_assert (function_depth);
22102 : :
22103 : 48043 : unsigned cookie = mslot->get_lazy ();
22104 : 48043 : unsigned snum = cookie >> 2;
22105 : 48378 : dump () && dump ("Loading entity %M[%u] section:%u", this, index, snum);
22106 : :
22107 : 48043 : bool ok = load_section (snum, mslot);
22108 : :
22109 : 48043 : dump.pop (n);
22110 : :
22111 : 48043 : return ok;
22112 : : }
22113 : :
22114 : : /* Load MOD's binding for NS::ID into *MSLOT. *MSLOT contains the
22115 : : lazy cookie. OUTER is true if this is the outermost lazy, (used
22116 : : for diagnostics). */
22117 : :
22118 : : void
22119 : 4850 : lazy_load_binding (unsigned mod, tree ns, tree id, binding_slot *mslot)
22120 : : {
22121 : 4850 : int count = errorcount + warningcount;
22122 : :
22123 : 4850 : bool timer_running = timevar_cond_start (TV_MODULE_IMPORT);
22124 : :
22125 : : /* Make sure lazy loading from a template context behaves as if
22126 : : from a non-template context. */
22127 : 4850 : processing_template_decl_sentinel ptds;
22128 : :
22129 : : /* Stop GC happening, even in outermost loads (because our caller
22130 : : could well be building up a lookup set). */
22131 : 4850 : function_depth++;
22132 : :
22133 : 4850 : gcc_checking_assert (mod);
22134 : 4850 : module_state *module = (*modules)[mod];
22135 : 4850 : unsigned n = dump.push (module);
22136 : :
22137 : 4850 : unsigned snum = mslot->get_lazy ();
22138 : 5157 : dump () && dump ("Lazily binding %P@%N section:%u", ns, id,
22139 : : module->name, snum);
22140 : :
22141 : 4850 : bool ok = !recursive_lazy (snum);
22142 : 4850 : if (ok)
22143 : : {
22144 : 4850 : ok = module->load_section (snum, mslot);
22145 : 4850 : lazy_snum = 0;
22146 : 4850 : post_load_processing ();
22147 : : }
22148 : :
22149 : 4850 : dump.pop (n);
22150 : :
22151 : 4850 : function_depth--;
22152 : :
22153 : 4850 : timevar_cond_stop (TV_MODULE_IMPORT, timer_running);
22154 : :
22155 : 4850 : if (!ok)
22156 : 0 : fatal_error (input_location,
22157 : 0 : module->is_header ()
22158 : : ? G_("failed to load binding %<%E%s%E%>")
22159 : : : G_("failed to load binding %<%E%s%E@%s%>"),
22160 : 0 : ns, &"::"[ns == global_namespace ? 2 : 0], id,
22161 : : module->get_flatname ());
22162 : :
22163 : 4850 : if (count != errorcount + warningcount)
22164 : 27 : inform (input_location,
22165 : 27 : module->is_header ()
22166 : : ? G_("during load of binding %<%E%s%E%>")
22167 : : : G_("during load of binding %<%E%s%E@%s%>"),
22168 : 27 : ns, &"::"[ns == global_namespace ? 2 : 0], id,
22169 : : module->get_flatname ());
22170 : 4850 : }
22171 : :
22172 : : /* Load any pending entities keyed to the top-key of DECL. */
22173 : :
22174 : : void
22175 : 26987660 : lazy_load_pendings (tree decl)
22176 : : {
22177 : : /* Make sure lazy loading from a template context behaves as if
22178 : : from a non-template context. */
22179 : 26987660 : processing_template_decl_sentinel ptds;
22180 : :
22181 : 26987660 : tree key_decl;
22182 : 26987660 : pending_key key;
22183 : 26987660 : key.ns = find_pending_key (decl, &key_decl);
22184 : 26987660 : key.id = DECL_NAME (key_decl);
22185 : :
22186 : 26987660 : auto *pending_vec = pending_table ? pending_table->get (key) : nullptr;
22187 : 26983012 : if (!pending_vec)
22188 : 26981974 : return;
22189 : :
22190 : 5686 : int count = errorcount + warningcount;
22191 : :
22192 : 5686 : bool timer_running = timevar_cond_start (TV_MODULE_IMPORT);
22193 : 5686 : bool ok = !recursive_lazy ();
22194 : 5686 : if (ok)
22195 : : {
22196 : 5686 : function_depth++; /* Prevent GC */
22197 : 5686 : unsigned n = dump.push (NULL);
22198 : 6128 : dump () && dump ("Reading %u pending entities keyed to %P",
22199 : : pending_vec->length (), key.ns, key.id);
22200 : 5686 : for (unsigned ix = pending_vec->length (); ix--;)
22201 : : {
22202 : 67061 : unsigned index = (*pending_vec)[ix];
22203 : 67061 : binding_slot *slot = &(*entity_ary)[index];
22204 : :
22205 : 67061 : if (slot->is_lazy ())
22206 : : {
22207 : 5882 : module_state *import = import_entity_module (index);
22208 : 5882 : if (!import->lazy_load (index - import->entity_lwm, slot))
22209 : 67061 : ok = false;
22210 : : }
22211 : 133926 : else if (dump ())
22212 : : {
22213 : 323 : module_state *import = import_entity_module (index);
22214 : 323 : dump () && dump ("Entity %M[%u] already loaded",
22215 : 323 : import, index - import->entity_lwm);
22216 : : }
22217 : : }
22218 : :
22219 : 5686 : pending_table->remove (key);
22220 : 5686 : dump.pop (n);
22221 : 5686 : lazy_snum = 0;
22222 : 5686 : post_load_processing ();
22223 : 5686 : function_depth--;
22224 : : }
22225 : :
22226 : 5686 : timevar_cond_stop (TV_MODULE_IMPORT, timer_running);
22227 : :
22228 : 5686 : if (!ok)
22229 : 0 : fatal_error (input_location, "failed to load pendings for %<%E%s%E%>",
22230 : 0 : key.ns, &"::"[key.ns == global_namespace ? 2 : 0], key.id);
22231 : :
22232 : 5686 : if (count != errorcount + warningcount)
22233 : 0 : inform (input_location, "during load of pendings for %<%E%s%E%>",
22234 : 0 : key.ns, &"::"[key.ns == global_namespace ? 2 : 0], key.id);
22235 : 26987660 : }
22236 : :
22237 : : static void
22238 : 2591 : direct_import (module_state *import, cpp_reader *reader)
22239 : : {
22240 : 2591 : timevar_start (TV_MODULE_IMPORT);
22241 : 2591 : unsigned n = dump.push (import);
22242 : :
22243 : 2591 : gcc_checking_assert (import->is_direct () && import->has_location ());
22244 : 2591 : if (import->loadedness == ML_NONE)
22245 : 1675 : if (!import->do_import (reader, true))
22246 : 0 : gcc_unreachable ();
22247 : :
22248 : 2554 : this_module ()->set_import (import, import->exported_p);
22249 : :
22250 : 2554 : if (import->loadedness < ML_LANGUAGE)
22251 : : {
22252 : 2475 : if (!keyed_table)
22253 : 2176 : keyed_table = new keyed_map_t (EXPERIMENT (1, 400));
22254 : 2475 : import->read_language (true);
22255 : : }
22256 : :
22257 : 2554 : dump.pop (n);
22258 : 2554 : timevar_stop (TV_MODULE_IMPORT);
22259 : 2554 : }
22260 : :
22261 : : /* Import module IMPORT. */
22262 : :
22263 : : void
22264 : 2361 : import_module (module_state *import, location_t from_loc, bool exporting_p,
22265 : : tree, cpp_reader *reader)
22266 : : {
22267 : : /* A non-partition implementation unit has no name. */
22268 : 2361 : if (!this_module ()->name && this_module ()->parent == import)
22269 : : {
22270 : 3 : auto_diagnostic_group d;
22271 : 3 : error_at (from_loc, "import of %qs within its own implementation unit",
22272 : : import->get_flatname());
22273 : 3 : inform (import->loc, "module declared here");
22274 : 3 : return;
22275 : 3 : }
22276 : :
22277 : 2358 : if (!import->check_circular_import (from_loc))
22278 : : return;
22279 : :
22280 : 2352 : if (!import->is_header () && current_lang_depth ())
22281 : : /* Only header units should appear inside language
22282 : : specifications. The std doesn't specify this, but I think
22283 : : that's an error in resolving US 033, because language linkage
22284 : : is also our escape clause to getting things into the global
22285 : : module, so we don't want to confuse things by having to think
22286 : : about whether 'extern "C++" { import foo; }' puts foo's
22287 : : contents into the global module all of a sudden. */
22288 : 6 : warning (0, "import of named module %qs inside language-linkage block",
22289 : : import->get_flatname ());
22290 : :
22291 : 2352 : if (exporting_p || module_exporting_p ())
22292 : 287 : import->exported_p = true;
22293 : :
22294 : 2352 : if (import->loadedness != ML_NONE)
22295 : : {
22296 : 913 : from_loc = ordinary_loc_of (line_table, from_loc);
22297 : 913 : linemap_module_reparent (line_table, import->loc, from_loc);
22298 : : }
22299 : :
22300 : 2352 : gcc_checking_assert (import->is_direct () && import->has_location ());
22301 : :
22302 : 2352 : direct_import (import, reader);
22303 : : }
22304 : :
22305 : : /* Declare the name of the current module to be NAME. EXPORTING_p is
22306 : : true if this TU is the exporting module unit. */
22307 : :
22308 : : void
22309 : 2969 : declare_module (module_state *module, location_t from_loc, bool exporting_p,
22310 : : tree, cpp_reader *reader)
22311 : : {
22312 : 2969 : gcc_assert (global_namespace == current_scope ());
22313 : :
22314 : 2969 : module_state *current = this_module ();
22315 : 2969 : if (module_purview_p () || module->loadedness > ML_CONFIG)
22316 : : {
22317 : 6 : auto_diagnostic_group d;
22318 : 12 : error_at (from_loc, module_purview_p ()
22319 : : ? G_("module already declared")
22320 : : : G_("module already imported"));
22321 : 6 : if (module_purview_p ())
22322 : 0 : module = current;
22323 : 12 : inform (module->loc, module_purview_p ()
22324 : : ? G_("module %qs declared here")
22325 : : : G_("module %qs imported here"),
22326 : : module->get_flatname ());
22327 : 6 : return;
22328 : 6 : }
22329 : :
22330 : 2963 : gcc_checking_assert (module->is_module ());
22331 : 2963 : gcc_checking_assert (module->is_direct () && module->has_location ());
22332 : :
22333 : : /* Yer a module, 'arry. */
22334 : 2963 : module_kind = module->is_header () ? MK_HEADER : MK_NAMED | MK_ATTACH;
22335 : :
22336 : : // Even in header units, we consider the decls to be purview
22337 : 2963 : module_kind |= MK_PURVIEW;
22338 : :
22339 : 2963 : if (module->is_partition ())
22340 : 184 : module_kind |= MK_PARTITION;
22341 : 2963 : if (exporting_p)
22342 : : {
22343 : 2665 : module->interface_p = true;
22344 : 2665 : module_kind |= MK_INTERFACE;
22345 : : }
22346 : :
22347 : 2963 : if (module_has_cmi_p ())
22348 : : {
22349 : : /* Copy the importing information we may have already done. We
22350 : : do not need to separate out the imports that only happen in
22351 : : the GMF, inspite of what the literal wording of the std
22352 : : might imply. See p2191, the core list had a discussion
22353 : : where the module implementors agreed that the GMF of a named
22354 : : module is invisible to importers. */
22355 : 2724 : module->imports = current->imports;
22356 : :
22357 : 2724 : module->mod = 0;
22358 : 2724 : (*modules)[0] = module;
22359 : : }
22360 : : else
22361 : : {
22362 : 239 : module->interface_p = true;
22363 : 239 : current->parent = module; /* So mangler knows module identity. */
22364 : 239 : direct_import (module, reader);
22365 : : }
22366 : : }
22367 : :
22368 : : /* Return true IFF we must emit a module global initializer function
22369 : : (which will be called by importers' init code). */
22370 : :
22371 : : bool
22372 : 102266 : module_global_init_needed ()
22373 : : {
22374 : 102266 : return module_has_cmi_p () && !header_module_p ();
22375 : : }
22376 : :
22377 : : /* Calculate which, if any, import initializers need calling. */
22378 : :
22379 : : bool
22380 : 95173 : module_determine_import_inits ()
22381 : : {
22382 : 95173 : if (!modules || header_module_p ())
22383 : : return false;
22384 : :
22385 : : /* Prune active_init_p. We need the same bitmap allocation
22386 : : scheme as for the imports member. */
22387 : 3586 : function_depth++; /* Disable GC. */
22388 : 3586 : bitmap covered_imports (BITMAP_GGC_ALLOC ());
22389 : :
22390 : 3586 : bool any = false;
22391 : :
22392 : : /* Because indirect imports are before their direct import, and
22393 : : we're scanning the array backwards, we only need one pass! */
22394 : 6247 : for (unsigned ix = modules->length (); --ix;)
22395 : : {
22396 : 2661 : module_state *import = (*modules)[ix];
22397 : :
22398 : 2661 : if (!import->active_init_p)
22399 : : ;
22400 : 43 : else if (bitmap_bit_p (covered_imports, ix))
22401 : 6 : import->active_init_p = false;
22402 : : else
22403 : : {
22404 : : /* Everything this imports is therefore handled by its
22405 : : initializer, so doesn't need initializing by us. */
22406 : 37 : bitmap_ior_into (covered_imports, import->imports);
22407 : 37 : any = true;
22408 : : }
22409 : : }
22410 : 3586 : function_depth--;
22411 : :
22412 : 3586 : return any;
22413 : : }
22414 : :
22415 : : /* Emit calls to each direct import's global initializer. Including
22416 : : direct imports of directly imported header units. The initializers
22417 : : of (static) entities in header units will be called by their
22418 : : importing modules (for the instance contained within that), or by
22419 : : the current TU (for the instances we've brought in). Of course
22420 : : such header unit behaviour is evil, but iostream went through that
22421 : : door some time ago. */
22422 : :
22423 : : void
22424 : 37 : module_add_import_initializers ()
22425 : : {
22426 : 37 : if (!modules || header_module_p ())
22427 : 0 : return;
22428 : :
22429 : 37 : tree fntype = build_function_type (void_type_node, void_list_node);
22430 : 37 : releasing_vec args; // There are no args
22431 : :
22432 : 83 : for (unsigned ix = modules->length (); --ix;)
22433 : : {
22434 : 46 : module_state *import = (*modules)[ix];
22435 : 46 : if (import->active_init_p)
22436 : : {
22437 : 37 : tree name = mangle_module_global_init (ix);
22438 : 37 : tree fndecl = build_lang_decl (FUNCTION_DECL, name, fntype);
22439 : :
22440 : 37 : DECL_CONTEXT (fndecl) = FROB_CONTEXT (global_namespace);
22441 : 37 : SET_DECL_ASSEMBLER_NAME (fndecl, name);
22442 : 37 : TREE_PUBLIC (fndecl) = true;
22443 : 37 : determine_visibility (fndecl);
22444 : :
22445 : 37 : tree call = cp_build_function_call_vec (fndecl, &args,
22446 : : tf_warning_or_error);
22447 : 37 : finish_expr_stmt (call);
22448 : : }
22449 : : }
22450 : 37 : }
22451 : :
22452 : : /* NAME & LEN are a preprocessed header name, possibly including the
22453 : : surrounding "" or <> characters. Return the raw string name of the
22454 : : module to which it refers. This will be an absolute path, or begin
22455 : : with ./, so it is immediately distinguishable from a (non-header
22456 : : unit) module name. If READER is non-null, ask the preprocessor to
22457 : : locate the header to which it refers using the appropriate include
22458 : : path. Note that we do never do \ processing of the string, as that
22459 : : matches the preprocessor's behaviour. */
22460 : :
22461 : : static const char *
22462 : 24086 : canonicalize_header_name (cpp_reader *reader, location_t loc, bool unquoted,
22463 : : const char *str, size_t &len_r)
22464 : : {
22465 : 24086 : size_t len = len_r;
22466 : 24086 : static char *buf = 0;
22467 : 24086 : static size_t alloc = 0;
22468 : :
22469 : 24086 : if (!unquoted)
22470 : : {
22471 : 4 : gcc_checking_assert (len >= 2
22472 : : && ((reader && str[0] == '<' && str[len-1] == '>')
22473 : : || (str[0] == '"' && str[len-1] == '"')));
22474 : 4 : str += 1;
22475 : 4 : len -= 2;
22476 : : }
22477 : :
22478 : 24086 : if (reader)
22479 : : {
22480 : 4 : gcc_assert (!unquoted);
22481 : :
22482 : 4 : if (len >= alloc)
22483 : : {
22484 : 4 : alloc = len + 1;
22485 : 4 : buf = XRESIZEVEC (char, buf, alloc);
22486 : : }
22487 : 4 : memcpy (buf, str, len);
22488 : 4 : buf[len] = 0;
22489 : :
22490 : 8 : if (const char *hdr
22491 : 4 : = cpp_probe_header_unit (reader, buf, str[-1] == '<', loc))
22492 : : {
22493 : 4 : len = strlen (hdr);
22494 : 4 : str = hdr;
22495 : : }
22496 : : else
22497 : 0 : str = buf;
22498 : : }
22499 : :
22500 : 24086 : if (!(str[0] == '.' ? IS_DIR_SEPARATOR (str[1]) : IS_ABSOLUTE_PATH (str)))
22501 : : {
22502 : : /* Prepend './' */
22503 : 9 : if (len + 3 > alloc)
22504 : : {
22505 : 9 : alloc = len + 3;
22506 : 9 : buf = XRESIZEVEC (char, buf, alloc);
22507 : : }
22508 : :
22509 : 9 : buf[0] = '.';
22510 : 9 : buf[1] = DIR_SEPARATOR;
22511 : 9 : memmove (buf + 2, str, len);
22512 : 9 : len += 2;
22513 : 9 : buf[len] = 0;
22514 : 9 : str = buf;
22515 : : }
22516 : :
22517 : 24086 : len_r = len;
22518 : 24086 : return str;
22519 : : }
22520 : :
22521 : : /* Set the CMI name from a cody packet. Issue an error if
22522 : : ill-formed. */
22523 : :
22524 : 5440 : void module_state::set_filename (const Cody::Packet &packet)
22525 : : {
22526 : 5440 : if (packet.GetCode () == Cody::Client::PC_PATHNAME)
22527 : : {
22528 : : /* If we've seen this import before we better have the same CMI. */
22529 : 5437 : const std::string &path = packet.GetString ();
22530 : 5437 : if (!filename)
22531 : 5434 : filename = xstrdup (packet.GetString ().c_str ());
22532 : 3 : else if (filename != path)
22533 : 0 : error_at (loc, "mismatching compiled module interface: "
22534 : : "had %qs, got %qs", filename, path.c_str ());
22535 : : }
22536 : : else
22537 : : {
22538 : 3 : gcc_checking_assert (packet.GetCode () == Cody::Client::PC_ERROR);
22539 : 3 : fatal_error (loc, "unknown compiled module interface: %s",
22540 : 3 : packet.GetString ().c_str ());
22541 : : }
22542 : 5437 : }
22543 : :
22544 : : /* Figure out whether to treat HEADER as an include or an import. */
22545 : :
22546 : : static char *
22547 : 23181 : maybe_translate_include (cpp_reader *reader, line_maps *lmaps, location_t loc,
22548 : : const char *path)
22549 : : {
22550 : 23181 : if (!modules_p ())
22551 : : {
22552 : : /* Turn off. */
22553 : 0 : cpp_get_callbacks (reader)->translate_include = NULL;
22554 : 0 : return nullptr;
22555 : : }
22556 : :
22557 : 23181 : dump.push (NULL);
22558 : :
22559 : 25096 : dump () && dump ("Checking include translation '%s'", path);
22560 : 23181 : auto *mapper = get_mapper (cpp_main_loc (reader), cpp_get_deps (reader));
22561 : :
22562 : 23181 : size_t len = strlen (path);
22563 : 23181 : path = canonicalize_header_name (NULL, loc, true, path, len);
22564 : 23181 : auto packet = mapper->IncludeTranslate (path, Cody::Flags::None, len);
22565 : :
22566 : 23181 : enum class xlate_kind {
22567 : : unknown, text, import, invalid
22568 : 23181 : } translate = xlate_kind::unknown;
22569 : :
22570 : 23181 : if (packet.GetCode () == Cody::Client::PC_BOOL)
22571 : 23133 : translate = packet.GetInteger () ? xlate_kind::text : xlate_kind::unknown;
22572 : 48 : else if (packet.GetCode () == Cody::Client::PC_PATHNAME)
22573 : : {
22574 : : /* Record the CMI name for when we do the import.
22575 : : We may already know about this import, but libcpp doesn't yet. */
22576 : 48 : module_state *import = get_module (build_string (len, path));
22577 : 48 : import->set_filename (packet);
22578 : 48 : if (import->check_importable (reader))
22579 : : translate = xlate_kind::import;
22580 : : else
22581 : 0 : translate = xlate_kind::invalid;
22582 : : }
22583 : : else
22584 : : {
22585 : 0 : gcc_checking_assert (packet.GetCode () == Cody::Client::PC_ERROR);
22586 : 0 : error_at (loc, "cannot determine %<#include%> translation of %s: %s",
22587 : 0 : path, packet.GetString ().c_str ());
22588 : : }
22589 : :
22590 : 23181 : bool note = (translate == xlate_kind::invalid);
22591 : 23181 : if (note_include_translate_yes && translate == xlate_kind::import)
22592 : : note = true;
22593 : 23178 : else if (note_include_translate_no && translate == xlate_kind::unknown)
22594 : : note = true;
22595 : 23172 : else if (note_includes)
22596 : : /* We do not expect the note_includes vector to be large, so O(N)
22597 : : iteration. */
22598 : 410 : for (unsigned ix = note_includes->length (); !note && ix--;)
22599 : 205 : if (!strcmp ((*note_includes)[ix], path))
22600 : 1 : note = true;
22601 : :
22602 : 23172 : if (note)
22603 : 16 : inform (loc, translate == xlate_kind::import
22604 : : ? G_("include %qs translated to import")
22605 : : : translate == xlate_kind::invalid
22606 : 6 : ? G_("import of %qs failed, falling back to include")
22607 : : : G_("include %qs processed textually"), path);
22608 : :
22609 : 27008 : dump () && dump (translate == xlate_kind::import
22610 : : ? "Translating include to import"
22611 : : : "Keeping include as include");
22612 : 23181 : dump.pop (0);
22613 : :
22614 : 23181 : if (translate != xlate_kind::import)
22615 : : return nullptr;
22616 : :
22617 : : /* Create the translation text. */
22618 : 48 : loc = ordinary_loc_of (lmaps, loc);
22619 : 48 : const line_map_ordinary *map
22620 : 48 : = linemap_check_ordinary (linemap_lookup (lmaps, loc));
22621 : 48 : unsigned col = SOURCE_COLUMN (map, loc);
22622 : 48 : col -= (col != 0); /* Columns are 1-based. */
22623 : :
22624 : 48 : unsigned alloc = len + col + 60;
22625 : 48 : char *res = XNEWVEC (char, alloc);
22626 : :
22627 : 48 : strcpy (res, "__import");
22628 : 48 : unsigned actual = 8;
22629 : 48 : if (col > actual)
22630 : : {
22631 : : /* Pad out so the filename appears at the same position. */
22632 : 45 : memset (res + actual, ' ', col - actual);
22633 : 45 : actual = col;
22634 : : }
22635 : : /* No need to encode characters, that's not how header names are
22636 : : handled. */
22637 : 48 : actual += snprintf (res + actual, alloc - actual,
22638 : : "\"%s\" [[__translated]];\n", path);
22639 : 48 : gcc_checking_assert (actual < alloc);
22640 : :
22641 : : /* cpplib will delete the buffer. */
22642 : : return res;
22643 : 23181 : }
22644 : :
22645 : : static void
22646 : 901 : begin_header_unit (cpp_reader *reader)
22647 : : {
22648 : : /* Set the module header name from the main_input_filename. */
22649 : 901 : const char *main = main_input_filename;
22650 : 901 : size_t len = strlen (main);
22651 : 901 : main = canonicalize_header_name (NULL, 0, true, main, len);
22652 : 901 : module_state *module = get_module (build_string (len, main));
22653 : :
22654 : 901 : preprocess_module (module, cpp_main_loc (reader), false, false, true, reader);
22655 : 901 : }
22656 : :
22657 : : /* We've just properly entered the main source file. I.e. after the
22658 : : command line, builtins and forced headers. Record the line map and
22659 : : location of this map. Note we may be called more than once. The
22660 : : first call sticks. */
22661 : :
22662 : : void
22663 : 97114 : module_begin_main_file (cpp_reader *reader, line_maps *lmaps,
22664 : : const line_map_ordinary *map)
22665 : : {
22666 : 97114 : gcc_checking_assert (lmaps == line_table);
22667 : 97114 : if (modules_p () && !spans.init_p ())
22668 : : {
22669 : 4644 : unsigned n = dump.push (NULL);
22670 : 4644 : spans.init (lmaps, map);
22671 : 4644 : dump.pop (n);
22672 : 4644 : if (flag_header_unit && !cpp_get_options (reader)->preprocessed)
22673 : : {
22674 : : /* Tell the preprocessor this is an include file. */
22675 : 892 : cpp_retrofit_as_include (reader);
22676 : 892 : begin_header_unit (reader);
22677 : : }
22678 : : }
22679 : 97114 : }
22680 : :
22681 : : /* Process the pending_import queue, making sure we know the
22682 : : filenames. */
22683 : :
22684 : : static void
22685 : 5526 : name_pending_imports (cpp_reader *reader)
22686 : : {
22687 : 5526 : auto *mapper = get_mapper (cpp_main_loc (reader), cpp_get_deps (reader));
22688 : :
22689 : 5526 : if (!vec_safe_length (pending_imports))
22690 : : /* Not doing anything. */
22691 : : return;
22692 : :
22693 : 4712 : timevar_start (TV_MODULE_MAPPER);
22694 : :
22695 : 4712 : auto n = dump.push (NULL);
22696 : 5295 : dump () && dump ("Resolving direct import names");
22697 : 4712 : bool want_deps = (bool (mapper->get_flags () & Cody::Flags::NameOnly)
22698 : 4712 : || cpp_get_deps (reader));
22699 : 4712 : bool any = false;
22700 : :
22701 : 10278 : for (unsigned ix = 0; ix != pending_imports->length (); ix++)
22702 : : {
22703 : 5566 : module_state *module = (*pending_imports)[ix];
22704 : 5566 : gcc_checking_assert (module->is_direct ());
22705 : 5566 : if (!module->filename && !module->visited_p)
22706 : : {
22707 : 5458 : bool export_p = (module->is_module ()
22708 : 5458 : && (module->is_partition ()
22709 : 2869 : || module->is_exported ()));
22710 : :
22711 : 5458 : Cody::Flags flags = Cody::Flags::None;
22712 : 5458 : if (flag_preprocess_only
22713 : 5458 : && !(module->is_header () && !export_p))
22714 : : {
22715 : 129 : if (!want_deps)
22716 : 66 : continue;
22717 : : flags = Cody::Flags::NameOnly;
22718 : : }
22719 : :
22720 : 5392 : if (!any)
22721 : : {
22722 : 4641 : any = true;
22723 : 4641 : mapper->Cork ();
22724 : : }
22725 : 5392 : if (export_p)
22726 : 2775 : mapper->ModuleExport (module->get_flatname (), flags);
22727 : : else
22728 : 2617 : mapper->ModuleImport (module->get_flatname (), flags);
22729 : 5392 : module->visited_p = true;
22730 : : }
22731 : : }
22732 : :
22733 : 4712 : if (any)
22734 : : {
22735 : 4641 : auto response = mapper->Uncork ();
22736 : 4641 : auto r_iter = response.begin ();
22737 : 10121 : for (unsigned ix = 0; ix != pending_imports->length (); ix++)
22738 : : {
22739 : 5483 : module_state *module = (*pending_imports)[ix];
22740 : 5483 : if (module->visited_p)
22741 : : {
22742 : 5392 : module->visited_p = false;
22743 : 5392 : gcc_checking_assert (!module->filename);
22744 : :
22745 : 5392 : module->set_filename (*r_iter);
22746 : 5389 : ++r_iter;
22747 : : }
22748 : : }
22749 : 4638 : }
22750 : :
22751 : 4709 : dump.pop (n);
22752 : :
22753 : 4709 : timevar_stop (TV_MODULE_MAPPER);
22754 : : }
22755 : :
22756 : : /* We've just lexed a module-specific control line for MODULE. Mark
22757 : : the module as a direct import, and possibly load up its macro
22758 : : state. Returns the primary module, if this is a module
22759 : : declaration. */
22760 : : /* Perhaps we should offer a preprocessing mode where we read the
22761 : : directives from the header unit, rather than require the header's
22762 : : CMI. */
22763 : :
22764 : : module_state *
22765 : 5585 : preprocess_module (module_state *module, location_t from_loc,
22766 : : bool in_purview, bool is_import, bool is_export,
22767 : : cpp_reader *reader)
22768 : : {
22769 : 5585 : if (!is_import)
22770 : : {
22771 : 3113 : if (module->loc)
22772 : : /* It's already been mentioned, so ignore its module-ness. */
22773 : : is_import = true;
22774 : : else
22775 : : {
22776 : : /* Record it is the module. */
22777 : 3092 : module->module_p = true;
22778 : 3092 : if (is_export)
22779 : : {
22780 : 2761 : module->exported_p = true;
22781 : 2761 : module->interface_p = true;
22782 : : }
22783 : : }
22784 : : }
22785 : :
22786 : 5585 : if (module->directness < MD_DIRECT + in_purview)
22787 : : {
22788 : : /* Mark as a direct import. */
22789 : 5539 : module->directness = module_directness (MD_DIRECT + in_purview);
22790 : :
22791 : : /* Set the location to be most informative for users. */
22792 : 5539 : from_loc = ordinary_loc_of (line_table, from_loc);
22793 : 5539 : if (module->loadedness != ML_NONE)
22794 : 6 : linemap_module_reparent (line_table, module->loc, from_loc);
22795 : : else
22796 : : {
22797 : : /* Don't overwrite the location if we're importing ourselves
22798 : : after already having seen a module-declaration. */
22799 : 5533 : if (!(is_import && module->is_module ()))
22800 : 5506 : module->loc = from_loc;
22801 : 5533 : if (!module->flatname)
22802 : 5500 : module->set_flatname ();
22803 : : }
22804 : : }
22805 : :
22806 : 5585 : auto desired = ML_CONFIG;
22807 : 5585 : if (is_import
22808 : 2493 : && module->is_header ()
22809 : 6480 : && (!cpp_get_options (reader)->preprocessed
22810 : 3 : || cpp_get_options (reader)->directives_only))
22811 : : /* We need preprocessor state now. */
22812 : : desired = ML_PREPROCESSOR;
22813 : :
22814 : 5585 : if (!is_import || module->loadedness < desired)
22815 : : {
22816 : 5566 : vec_safe_push (pending_imports, module);
22817 : :
22818 : 5566 : if (desired == ML_PREPROCESSOR)
22819 : : {
22820 : 876 : unsigned n = dump.push (NULL);
22821 : :
22822 : 1075 : dump () && dump ("Reading %M preprocessor state", module);
22823 : 876 : name_pending_imports (reader);
22824 : :
22825 : : /* Preserve the state of the line-map. */
22826 : 876 : auto pre_hwm = LINEMAPS_ORDINARY_USED (line_table);
22827 : :
22828 : : /* We only need to close the span, if we're going to emit a
22829 : : CMI. But that's a little tricky -- our token scanner
22830 : : needs to be smarter -- and this isn't much state.
22831 : : Remember, we've not parsed anything at this point, so
22832 : : our module state flags are inadequate. */
22833 : 876 : spans.maybe_init ();
22834 : 876 : spans.close ();
22835 : :
22836 : 876 : timevar_start (TV_MODULE_IMPORT);
22837 : :
22838 : : /* Load the config of each pending import -- we must assign
22839 : : module numbers monotonically. */
22840 : 1937 : for (unsigned ix = 0; ix != pending_imports->length (); ix++)
22841 : : {
22842 : 1061 : auto *import = (*pending_imports)[ix];
22843 : 1237 : if (!(import->is_module ()
22844 : 176 : && (import->is_partition () || import->is_exported ()))
22845 : 891 : && import->loadedness == ML_NONE
22846 : 1952 : && (import->is_header () || !flag_preprocess_only))
22847 : : {
22848 : 882 : unsigned n = dump.push (import);
22849 : 882 : import->do_import (reader, true);
22850 : 882 : dump.pop (n);
22851 : : }
22852 : : }
22853 : 876 : vec_free (pending_imports);
22854 : :
22855 : : /* Restore the line-map state. */
22856 : 876 : spans.open (linemap_module_restore (line_table, pre_hwm));
22857 : :
22858 : : /* Now read the preprocessor state of this particular
22859 : : import. */
22860 : 876 : if (module->loadedness == ML_CONFIG
22861 : 876 : && module->read_preprocessor (true))
22862 : 873 : module->import_macros ();
22863 : :
22864 : 876 : timevar_stop (TV_MODULE_IMPORT);
22865 : :
22866 : 876 : dump.pop (n);
22867 : : }
22868 : : }
22869 : :
22870 : 5585 : return is_import ? NULL : get_primary (module);
22871 : : }
22872 : :
22873 : : /* We've completed phase-4 translation. Emit any dependency
22874 : : information for the not-yet-loaded direct imports, and fill in
22875 : : their file names. We'll have already loaded up the direct header
22876 : : unit wavefront. */
22877 : :
22878 : : void
22879 : 4650 : preprocessed_module (cpp_reader *reader)
22880 : : {
22881 : 4650 : unsigned n = dump.push (NULL);
22882 : :
22883 : 5197 : dump () && dump ("Completed phase-4 (tokenization) processing");
22884 : :
22885 : 4650 : name_pending_imports (reader);
22886 : 4647 : vec_free (pending_imports);
22887 : :
22888 : 4647 : spans.maybe_init ();
22889 : 4647 : spans.close ();
22890 : :
22891 : 4647 : using iterator = hash_table<module_state_hash>::iterator;
22892 : 4647 : if (mkdeps *deps = cpp_get_deps (reader))
22893 : : {
22894 : : /* Walk the module hash, informing the dependency machinery. */
22895 : 51 : iterator end = modules_hash->end ();
22896 : 294 : for (iterator iter = modules_hash->begin (); iter != end; ++iter)
22897 : : {
22898 : 96 : module_state *module = *iter;
22899 : :
22900 : 96 : if (module->is_direct ())
22901 : : {
22902 : 81 : if (module->is_module ()
22903 : 81 : && (module->is_interface () || module->is_partition ()))
22904 : 33 : deps_add_module_target (deps, module->get_flatname (),
22905 : 33 : maybe_add_cmi_prefix (module->filename),
22906 : 33 : module->is_header (),
22907 : 33 : module->is_exported ());
22908 : : else
22909 : 48 : deps_add_module_dep (deps, module->get_flatname ());
22910 : : }
22911 : : }
22912 : : }
22913 : :
22914 : 4647 : if (flag_header_unit && !flag_preprocess_only)
22915 : : {
22916 : : /* Find the main module -- remember, it's not yet in the module
22917 : : array. */
22918 : 889 : iterator end = modules_hash->end ();
22919 : 1886 : for (iterator iter = modules_hash->begin (); iter != end; ++iter)
22920 : : {
22921 : 943 : module_state *module = *iter;
22922 : 943 : if (module->is_module ())
22923 : : {
22924 : 889 : declare_module (module, cpp_main_loc (reader), true, NULL, reader);
22925 : 889 : module_kind |= MK_EXPORTING;
22926 : 889 : break;
22927 : : }
22928 : : }
22929 : : }
22930 : :
22931 : 4647 : dump.pop (n);
22932 : 4647 : }
22933 : :
22934 : : /* VAL is a global tree, add it to the global vec if it is
22935 : : interesting. Add some of its targets, if they too are
22936 : : interesting. We do not add identifiers, as they can be re-found
22937 : : via the identifier hash table. There is a cost to the number of
22938 : : global trees. */
22939 : :
22940 : : static int
22941 : 2876874 : maybe_add_global (tree val, unsigned &crc)
22942 : : {
22943 : 2876874 : int v = 0;
22944 : :
22945 : 2876874 : if (val && !(identifier_p (val) || TREE_VISITED (val)))
22946 : : {
22947 : 886674 : TREE_VISITED (val) = true;
22948 : 886674 : crc = crc32_unsigned (crc, fixed_trees->length ());
22949 : 886674 : vec_safe_push (fixed_trees, val);
22950 : 886674 : v++;
22951 : :
22952 : 886674 : if (CODE_CONTAINS_STRUCT (TREE_CODE (val), TS_TYPED))
22953 : 886674 : v += maybe_add_global (TREE_TYPE (val), crc);
22954 : 886674 : if (CODE_CONTAINS_STRUCT (TREE_CODE (val), TS_TYPE_COMMON))
22955 : 581250 : v += maybe_add_global (TYPE_NAME (val), crc);
22956 : : }
22957 : :
22958 : 2876874 : return v;
22959 : : }
22960 : :
22961 : : /* Initialize module state. Create the hash table, determine the
22962 : : global trees. Create the module for current TU. */
22963 : :
22964 : : void
22965 : 4650 : init_modules (cpp_reader *reader)
22966 : : {
22967 : : /* PCH should not be reachable because of lang-specs, but the
22968 : : user could have overriden that. */
22969 : 4650 : if (pch_file)
22970 : 0 : fatal_error (input_location,
22971 : : "C++ modules are incompatible with precompiled headers");
22972 : :
22973 : 4650 : if (cpp_get_options (reader)->traditional)
22974 : 0 : fatal_error (input_location,
22975 : : "C++ modules are incompatible with traditional preprocessing");
22976 : :
22977 : : /* :: is always exported. */
22978 : 4650 : DECL_MODULE_EXPORT_P (global_namespace) = true;
22979 : :
22980 : 4650 : modules_hash = hash_table<module_state_hash>::create_ggc (31);
22981 : 4650 : vec_safe_reserve (modules, 20);
22982 : :
22983 : : /* Create module for current TU. */
22984 : 4650 : module_state *current
22985 : 4650 : = new (ggc_alloc<module_state> ()) module_state (NULL_TREE, NULL, false);
22986 : 4650 : current->mod = 0;
22987 : 4650 : bitmap_set_bit (current->imports, 0);
22988 : 4650 : modules->quick_push (current);
22989 : :
22990 : 4650 : gcc_checking_assert (!fixed_trees);
22991 : :
22992 : 4650 : headers = BITMAP_GGC_ALLOC ();
22993 : :
22994 : 4650 : if (note_includes)
22995 : : /* Canonicalize header names. */
22996 : 2 : for (unsigned ix = 0; ix != note_includes->length (); ix++)
22997 : : {
22998 : 1 : const char *hdr = (*note_includes)[ix];
22999 : 1 : size_t len = strlen (hdr);
23000 : :
23001 : 1 : bool system = hdr[0] == '<';
23002 : 1 : bool user = hdr[0] == '"';
23003 : 1 : bool delimed = system || user;
23004 : :
23005 : 1 : if (len <= (delimed ? 2 : 0)
23006 : 1 : || (delimed && hdr[len-1] != (system ? '>' : '"')))
23007 : 0 : error ("invalid header name %qs", hdr);
23008 : :
23009 : 1 : hdr = canonicalize_header_name (delimed ? reader : NULL,
23010 : : 0, !delimed, hdr, len);
23011 : 1 : char *path = XNEWVEC (char, len + 1);
23012 : 1 : memcpy (path, hdr, len);
23013 : 1 : path[len] = 0;
23014 : :
23015 : 1 : (*note_includes)[ix] = path;
23016 : : }
23017 : :
23018 : 4650 : if (note_cmis)
23019 : : /* Canonicalize & mark module names. */
23020 : 12 : for (unsigned ix = 0; ix != note_cmis->length (); ix++)
23021 : : {
23022 : 6 : const char *name = (*note_cmis)[ix];
23023 : 6 : size_t len = strlen (name);
23024 : :
23025 : 6 : bool is_system = name[0] == '<';
23026 : 6 : bool is_user = name[0] == '"';
23027 : 6 : bool is_pathname = false;
23028 : 6 : if (!(is_system || is_user))
23029 : 12 : for (unsigned ix = len; !is_pathname && ix--;)
23030 : 9 : is_pathname = IS_DIR_SEPARATOR (name[ix]);
23031 : 6 : if (is_system || is_user || is_pathname)
23032 : : {
23033 : 3 : if (len <= (is_pathname ? 0 : 2)
23034 : 3 : || (!is_pathname && name[len-1] != (is_system ? '>' : '"')))
23035 : : {
23036 : 0 : error ("invalid header name %qs", name);
23037 : 0 : continue;
23038 : : }
23039 : : else
23040 : 3 : name = canonicalize_header_name (is_pathname ? nullptr : reader,
23041 : : 0, is_pathname, name, len);
23042 : : }
23043 : 6 : if (auto module = get_module (name))
23044 : 6 : module->inform_cmi_p = 1;
23045 : : else
23046 : 0 : error ("invalid module name %qs", name);
23047 : : }
23048 : :
23049 : 4650 : dump.push (NULL);
23050 : :
23051 : : /* Determine lazy handle bound. */
23052 : 4650 : {
23053 : 4650 : unsigned limit = 1000;
23054 : : #if HAVE_GETRLIMIT
23055 : 4650 : struct rlimit rlimit;
23056 : 4650 : if (!getrlimit (RLIMIT_NOFILE, &rlimit))
23057 : : {
23058 : 4650 : lazy_hard_limit = (rlimit.rlim_max < 1000000
23059 : 4650 : ? unsigned (rlimit.rlim_max) : 1000000);
23060 : 4650 : lazy_hard_limit = (lazy_hard_limit > LAZY_HEADROOM
23061 : 4650 : ? lazy_hard_limit - LAZY_HEADROOM : 0);
23062 : 4650 : if (rlimit.rlim_cur < limit)
23063 : 0 : limit = unsigned (rlimit.rlim_cur);
23064 : : }
23065 : : #endif
23066 : 4650 : limit = limit > LAZY_HEADROOM ? limit - LAZY_HEADROOM : 1;
23067 : :
23068 : 4650 : if (unsigned parm = param_lazy_modules)
23069 : : {
23070 : 4650 : if (parm <= limit || !lazy_hard_limit || !try_increase_lazy (parm))
23071 : 6 : lazy_limit = parm;
23072 : : }
23073 : : else
23074 : 0 : lazy_limit = limit;
23075 : : }
23076 : :
23077 : 4650 : if (dump ())
23078 : : {
23079 : 547 : verstr_t ver;
23080 : 547 : version2string (MODULE_VERSION, ver);
23081 : 547 : dump ("Source: %s", main_input_filename);
23082 : 547 : dump ("Compiler: %s", version_string);
23083 : 547 : dump ("Modules: %s", ver);
23084 : 547 : dump ("Checking: %s",
23085 : : #if CHECKING_P
23086 : : "checking"
23087 : : #elif ENABLE_ASSERT_CHECKING
23088 : : "asserting"
23089 : : #else
23090 : : "release"
23091 : : #endif
23092 : : );
23093 : 547 : dump ("Compiled by: "
23094 : : #ifdef __GNUC__
23095 : : "GCC %d.%d, %s", __GNUC__, __GNUC_MINOR__,
23096 : : #ifdef __OPTIMIZE__
23097 : : "optimizing"
23098 : : #else
23099 : : "not optimizing"
23100 : : #endif
23101 : : #else
23102 : : "not GCC"
23103 : : #endif
23104 : : );
23105 : 547 : dump ("Reading: %s", MAPPED_READING ? "mmap" : "fileio");
23106 : 547 : dump ("Writing: %s", MAPPED_WRITING ? "mmap" : "fileio");
23107 : 547 : dump ("Lazy limit: %u", lazy_limit);
23108 : 547 : dump ("Lazy hard limit: %u", lazy_hard_limit);
23109 : 547 : dump ("");
23110 : : }
23111 : :
23112 : : /* Construct the global tree array. This is an array of unique
23113 : : global trees (& types). Do this now, rather than lazily, as
23114 : : some global trees are lazily created and we don't want that to
23115 : : mess with our syndrome of fixed trees. */
23116 : 4650 : unsigned crc = 0;
23117 : 4650 : vec_alloc (fixed_trees, 250);
23118 : :
23119 : 5197 : dump () && dump ("+Creating globals");
23120 : : /* Insert the TRANSLATION_UNIT_DECL. */
23121 : 4650 : TREE_VISITED (DECL_CONTEXT (global_namespace)) = true;
23122 : 4650 : fixed_trees->quick_push (DECL_CONTEXT (global_namespace));
23123 : 27900 : for (unsigned jx = 0; global_tree_arys[jx].first; jx++)
23124 : : {
23125 : 23250 : const tree *ptr = global_tree_arys[jx].first;
23126 : 23250 : unsigned limit = global_tree_arys[jx].second;
23127 : :
23128 : 1418250 : for (unsigned ix = 0; ix != limit; ix++, ptr++)
23129 : : {
23130 : 1395000 : !(ix & 31) && dump ("") && dump ("+\t%u:%u:", jx, ix);
23131 : 1395000 : unsigned v = maybe_add_global (*ptr, crc);
23132 : 1559100 : dump () && dump ("+%u", v);
23133 : : }
23134 : : }
23135 : : /* OS- and machine-specific types are dynamically registered at
23136 : : runtime, so cannot be part of global_tree_arys. */
23137 : 4650 : registered_builtin_types && dump ("") && dump ("+\tB:");
23138 : 18600 : for (tree t = registered_builtin_types; t; t = TREE_CHAIN (t))
23139 : : {
23140 : 13950 : unsigned v = maybe_add_global (TREE_VALUE (t), crc);
23141 : 15591 : dump () && dump ("+%u", v);
23142 : : }
23143 : 4650 : global_crc = crc32_unsigned (crc, fixed_trees->length ());
23144 : 4650 : dump ("") && dump ("Created %u unique globals, crc=%x",
23145 : : fixed_trees->length (), global_crc);
23146 : 895974 : for (unsigned ix = fixed_trees->length (); ix--;)
23147 : 891324 : TREE_VISITED ((*fixed_trees)[ix]) = false;
23148 : :
23149 : 4650 : dump.pop (0);
23150 : :
23151 : 4650 : if (!flag_module_lazy)
23152 : : /* Get the mapper now, if we're not being lazy. */
23153 : 290 : get_mapper (cpp_main_loc (reader), cpp_get_deps (reader));
23154 : :
23155 : 4650 : if (!flag_preprocess_only)
23156 : : {
23157 : 4515 : pending_table = new pending_map_t (EXPERIMENT (1, 400));
23158 : 4515 : entity_map = new entity_map_t (EXPERIMENT (1, 400));
23159 : 4515 : vec_safe_reserve (entity_ary, EXPERIMENT (1, 400));
23160 : 4515 : imported_temploid_friends
23161 : 4515 : = decl_tree_cache_map::create_ggc (EXPERIMENT (1, 400));
23162 : : }
23163 : :
23164 : : #if CHECKING_P
23165 : 4650 : note_defs = note_defs_table_t::create_ggc (1000);
23166 : : #endif
23167 : :
23168 : 4650 : if (flag_header_unit && cpp_get_options (reader)->preprocessed)
23169 : 9 : begin_header_unit (reader);
23170 : :
23171 : : /* Collect here to make sure things are tagged correctly (when
23172 : : aggressively GC'd). */
23173 : 4650 : ggc_collect ();
23174 : 4650 : }
23175 : :
23176 : : /* If NODE is a deferred macro, load it. */
23177 : :
23178 : : static int
23179 : 82736 : load_macros (cpp_reader *reader, cpp_hashnode *node, void *)
23180 : : {
23181 : 82736 : location_t main_loc
23182 : 82736 : = MAP_START_LOCATION (LINEMAPS_ORDINARY_MAP_AT (line_table, 0));
23183 : :
23184 : 82736 : if (cpp_user_macro_p (node)
23185 : 82736 : && !node->value.macro)
23186 : : {
23187 : 72 : cpp_macro *macro = cpp_get_deferred_macro (reader, node, main_loc);
23188 : 72 : dump () && dump ("Loaded macro #%s %I",
23189 : : macro ? "define" : "undef", identifier (node));
23190 : : }
23191 : :
23192 : 82736 : return 1;
23193 : : }
23194 : :
23195 : : /* At the end of tokenizing, we no longer need the macro tables of
23196 : : imports. But the user might have requested some checking. */
23197 : :
23198 : : void
23199 : 95380 : maybe_check_all_macros (cpp_reader *reader)
23200 : : {
23201 : 95380 : if (!warn_imported_macros)
23202 : : return;
23203 : :
23204 : : /* Force loading of any remaining deferred macros. This will
23205 : : produce diagnostics if they are ill-formed. */
23206 : 21 : unsigned n = dump.push (NULL);
23207 : 21 : cpp_forall_identifiers (reader, load_macros, NULL);
23208 : 21 : dump.pop (n);
23209 : : }
23210 : :
23211 : : // State propagated from finish_module_processing to fini_modules
23212 : :
23213 : : struct module_processing_cookie
23214 : : {
23215 : : elf_out out;
23216 : : module_state_config config;
23217 : : char *cmi_name;
23218 : : char *tmp_name;
23219 : : unsigned crc;
23220 : : bool began;
23221 : :
23222 : 2718 : module_processing_cookie (char *cmi, char *tmp, int fd, int e)
23223 : 2718 : : out (fd, e), cmi_name (cmi), tmp_name (tmp), crc (0), began (false)
23224 : : {
23225 : : }
23226 : 2718 : ~module_processing_cookie ()
23227 : : {
23228 : 2718 : XDELETEVEC (tmp_name);
23229 : 2718 : XDELETEVEC (cmi_name);
23230 : 2718 : }
23231 : : };
23232 : :
23233 : : /* Write the CMI, if we're a module interface. */
23234 : :
23235 : : void *
23236 : 95173 : finish_module_processing (cpp_reader *reader)
23237 : : {
23238 : 95173 : module_processing_cookie *cookie = nullptr;
23239 : :
23240 : 95173 : if (header_module_p ())
23241 : 889 : module_kind &= ~MK_EXPORTING;
23242 : :
23243 : 95173 : if (!modules || !this_module ()->name)
23244 : : {
23245 : 92452 : if (flag_module_only)
23246 : 6 : warning (0, "%<-fmodule-only%> used for non-interface");
23247 : : }
23248 : 2721 : else if (!flag_syntax_only)
23249 : : {
23250 : 2718 : int fd = -1;
23251 : 2718 : int e = -1;
23252 : :
23253 : 2718 : timevar_start (TV_MODULE_EXPORT);
23254 : :
23255 : : /* Force a valid but empty line map at the end. This simplifies
23256 : : the line table preparation and writing logic. */
23257 : 2718 : linemap_add (line_table, LC_ENTER, false, "", 0);
23258 : :
23259 : : /* We write to a tmpname, and then atomically rename. */
23260 : 2718 : char *cmi_name = NULL;
23261 : 2718 : char *tmp_name = NULL;
23262 : 2718 : module_state *state = this_module ();
23263 : :
23264 : 2718 : unsigned n = dump.push (state);
23265 : 2718 : state->announce ("creating");
23266 : 2718 : if (state->filename)
23267 : : {
23268 : 2718 : size_t len = 0;
23269 : 2718 : cmi_name = xstrdup (maybe_add_cmi_prefix (state->filename, &len));
23270 : 2718 : tmp_name = XNEWVEC (char, len + 3);
23271 : 2718 : memcpy (tmp_name, cmi_name, len);
23272 : 2718 : strcpy (&tmp_name[len], "~");
23273 : :
23274 : 2718 : if (!errorcount)
23275 : 37 : for (unsigned again = 2; ; again--)
23276 : : {
23277 : 2652 : fd = open (tmp_name,
23278 : : O_RDWR | O_CREAT | O_TRUNC | O_CLOEXEC | O_BINARY,
23279 : : S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP|S_IROTH|S_IWOTH);
23280 : 2652 : e = errno;
23281 : 2652 : if (fd >= 0 || !again || e != ENOENT)
23282 : : break;
23283 : 37 : create_dirs (tmp_name);
23284 : : }
23285 : 2718 : if (note_module_cmi_yes || state->inform_cmi_p)
23286 : 3 : inform (state->loc, "writing CMI %qs", cmi_name);
23287 : 2994 : dump () && dump ("CMI is %s", cmi_name);
23288 : : }
23289 : :
23290 : 2718 : cookie = new module_processing_cookie (cmi_name, tmp_name, fd, e);
23291 : :
23292 : 2718 : if (errorcount)
23293 : : /* Don't write the module if we have reported errors. */;
23294 : 2615 : else if (erroneous_templates
23295 : 2615 : && !erroneous_templates->is_empty ())
23296 : : {
23297 : : /* Don't write the module if it contains an erroneous template.
23298 : : Also emit notes about where errors occurred in case
23299 : : -Wno-template-body was passed. */
23300 : 6 : auto_diagnostic_group d;
23301 : 6 : error_at (state->loc, "not writing module %qs due to errors "
23302 : : "in template bodies", state->get_flatname ());
23303 : 6 : if (!warn_template_body)
23304 : 3 : inform (state->loc, "enable %<-Wtemplate-body%> for more details");
23305 : 12 : for (auto e : *erroneous_templates)
23306 : 6 : inform (e.second, "first error in %qD appeared here", e.first);
23307 : 6 : }
23308 : 2609 : else if (cookie->out.begin ())
23309 : : {
23310 : : /* So crashes finger-point the module decl. */
23311 : 2609 : iloc_sentinel ils = state->loc;
23312 : 2609 : if (state->write_begin (&cookie->out, reader, cookie->config,
23313 : 2609 : cookie->crc))
23314 : 2580 : cookie->began = true;
23315 : 2609 : }
23316 : :
23317 : 2718 : dump.pop (n);
23318 : 2718 : timevar_stop (TV_MODULE_EXPORT);
23319 : :
23320 : 2718 : ggc_collect ();
23321 : : }
23322 : :
23323 : 95173 : if (modules)
23324 : : {
23325 : 4475 : unsigned n = dump.push (NULL);
23326 : 5022 : dump () && dump ("Imported %u modules", modules->length () - 1);
23327 : 5022 : dump () && dump ("Containing %u clusters", available_clusters);
23328 : 4475 : dump () && dump ("Loaded %u clusters (%u%%)", loaded_clusters,
23329 : 547 : (loaded_clusters * 100 + available_clusters / 2) /
23330 : 547 : (available_clusters + !available_clusters));
23331 : 4475 : dump.pop (n);
23332 : : }
23333 : :
23334 : 95173 : return cookie;
23335 : : }
23336 : :
23337 : : // Do the final emission of a module. At this point we know whether
23338 : : // the module static initializer is a NOP or not.
23339 : :
23340 : : static void
23341 : 2718 : late_finish_module (cpp_reader *reader, module_processing_cookie *cookie,
23342 : : bool init_fn_non_empty)
23343 : : {
23344 : 2718 : timevar_start (TV_MODULE_EXPORT);
23345 : :
23346 : 2718 : module_state *state = this_module ();
23347 : 2718 : unsigned n = dump.push (state);
23348 : 2718 : state->announce ("finishing");
23349 : :
23350 : 2718 : cookie->config.active_init = init_fn_non_empty;
23351 : 2718 : if (cookie->began)
23352 : 2580 : state->write_end (&cookie->out, reader, cookie->config, cookie->crc);
23353 : :
23354 : 2718 : if (cookie->out.end () && cookie->cmi_name)
23355 : : {
23356 : : /* Some OS's do not replace NEWNAME if it already exists.
23357 : : This'll have a race condition in erroneous concurrent
23358 : : builds. */
23359 : 2615 : unlink (cookie->cmi_name);
23360 : 2615 : if (rename (cookie->tmp_name, cookie->cmi_name))
23361 : : {
23362 : 0 : dump () && dump ("Rename ('%s','%s') errno=%u",
23363 : 0 : cookie->tmp_name, cookie->cmi_name, errno);
23364 : 0 : cookie->out.set_error (errno);
23365 : : }
23366 : : }
23367 : :
23368 : 2718 : if (cookie->out.get_error () && cookie->began)
23369 : : {
23370 : 0 : error_at (state->loc, "failed to write compiled module: %s",
23371 : 0 : cookie->out.get_error (state->filename));
23372 : 0 : state->note_cmi_name ();
23373 : : }
23374 : :
23375 : 2718 : if (!errorcount)
23376 : : {
23377 : 2574 : auto *mapper = get_mapper (cpp_main_loc (reader), cpp_get_deps (reader));
23378 : 2574 : mapper->ModuleCompiled (state->get_flatname ());
23379 : : }
23380 : 144 : else if (cookie->cmi_name)
23381 : : {
23382 : : /* We failed, attempt to erase all evidence we even tried. */
23383 : 144 : unlink (cookie->tmp_name);
23384 : 144 : unlink (cookie->cmi_name);
23385 : : }
23386 : :
23387 : 2718 : delete cookie;
23388 : 2718 : dump.pop (n);
23389 : 2718 : timevar_stop (TV_MODULE_EXPORT);
23390 : 2718 : }
23391 : :
23392 : : void
23393 : 95173 : fini_modules (cpp_reader *reader, void *cookie, bool has_inits)
23394 : : {
23395 : 95173 : if (cookie)
23396 : 2718 : late_finish_module (reader,
23397 : : static_cast<module_processing_cookie *> (cookie),
23398 : : has_inits);
23399 : :
23400 : : /* We're done with the macro tables now. */
23401 : 95173 : vec_free (macro_exports);
23402 : 95173 : vec_free (macro_imports);
23403 : 95173 : headers = NULL;
23404 : :
23405 : : /* We're now done with everything but the module names. */
23406 : 95173 : set_cmi_repo (NULL);
23407 : 95173 : if (mapper)
23408 : : {
23409 : 4475 : timevar_start (TV_MODULE_MAPPER);
23410 : 4475 : module_client::close_module_client (0, mapper);
23411 : 4475 : mapper = nullptr;
23412 : 4475 : timevar_stop (TV_MODULE_MAPPER);
23413 : : }
23414 : 95173 : module_state_config::release ();
23415 : :
23416 : : #if CHECKING_P
23417 : 95173 : note_defs = NULL;
23418 : : #endif
23419 : :
23420 : 95173 : if (modules)
23421 : 7241 : for (unsigned ix = modules->length (); --ix;)
23422 : 2766 : if (module_state *state = (*modules)[ix])
23423 : 2766 : state->release ();
23424 : :
23425 : : /* No need to lookup modules anymore. */
23426 : 95173 : modules_hash = NULL;
23427 : :
23428 : : /* Or entity array. We still need the entity map to find import numbers. */
23429 : 95173 : vec_free (entity_ary);
23430 : 95173 : entity_ary = NULL;
23431 : :
23432 : : /* Or remember any pending entities. */
23433 : 99648 : delete pending_table;
23434 : 95173 : pending_table = NULL;
23435 : :
23436 : : /* Or any keys -- Let it go! */
23437 : 97438 : delete keyed_table;
23438 : 95173 : keyed_table = NULL;
23439 : :
23440 : : /* Allow a GC, we've possibly made much data unreachable. */
23441 : 95173 : ggc_collect ();
23442 : 95173 : }
23443 : :
23444 : : /* If CODE is a module option, handle it & return true. Otherwise
23445 : : return false. For unknown reasons I cannot get the option
23446 : : generation machinery to set fmodule-mapper or -fmodule-header to
23447 : : make a string type option variable. */
23448 : :
23449 : : bool
23450 : 1891622 : handle_module_option (unsigned code, const char *str, int)
23451 : : {
23452 : 1891622 : auto hdr = CMS_header;
23453 : :
23454 : 1891622 : switch (opt_code (code))
23455 : : {
23456 : 42 : case OPT_fmodule_mapper_:
23457 : 42 : module_mapper_name = str;
23458 : 42 : return true;
23459 : :
23460 : 12 : case OPT_fmodule_header_:
23461 : 12 : {
23462 : 12 : if (!strcmp (str, "user"))
23463 : : hdr = CMS_user;
23464 : 12 : else if (!strcmp (str, "system"))
23465 : : hdr = CMS_system;
23466 : : else
23467 : 0 : error ("unknown header kind %qs", str);
23468 : : }
23469 : : /* Fallthrough. */
23470 : :
23471 : 904 : case OPT_fmodule_header:
23472 : 904 : flag_header_unit = hdr;
23473 : 904 : flag_modules = 1;
23474 : 904 : return true;
23475 : :
23476 : 1 : case OPT_flang_info_include_translate_:
23477 : 1 : vec_safe_push (note_includes, str);
23478 : 1 : return true;
23479 : :
23480 : 6 : case OPT_flang_info_module_cmi_:
23481 : 6 : vec_safe_push (note_cmis, str);
23482 : 6 : return true;
23483 : :
23484 : : default:
23485 : : return false;
23486 : : }
23487 : : }
23488 : :
23489 : : /* Set preprocessor callbacks and options for modules. */
23490 : :
23491 : : void
23492 : 96923 : module_preprocess_options (cpp_reader *reader)
23493 : : {
23494 : 96923 : gcc_checking_assert (!lang_hooks.preprocess_undef);
23495 : 96923 : if (modules_p ())
23496 : : {
23497 : 4650 : auto *cb = cpp_get_callbacks (reader);
23498 : :
23499 : 4650 : cb->translate_include = maybe_translate_include;
23500 : 4650 : cb->user_deferred_macro = module_state::deferred_macro;
23501 : 4650 : if (flag_header_unit)
23502 : : {
23503 : : /* If the preprocessor hook is already in use, that
23504 : : implementation will call the undef langhook. */
23505 : 901 : if (cb->undef)
23506 : 0 : lang_hooks.preprocess_undef = module_state::undef_macro;
23507 : : else
23508 : 901 : cb->undef = module_state::undef_macro;
23509 : : }
23510 : 4650 : auto *opt = cpp_get_options (reader);
23511 : 4650 : opt->module_directives = true;
23512 : 4650 : if (flag_no_output)
23513 : 12 : opt->directives_only = true;
23514 : 4650 : if (opt->main_search == CMS_none)
23515 : 4648 : opt->main_search = cpp_main_search (flag_header_unit);
23516 : : }
23517 : 96923 : }
23518 : :
23519 : : #include "gt-cp-module.h"
|