Branch data Line data Source code
1 : : /* C++ modules. Experimental!
2 : : Copyright (C) 2017-2025 Free Software Foundation, Inc.
3 : : Written by Nathan Sidwell <nathan@acm.org> while at FaceBook
4 : :
5 : : This file is part of GCC.
6 : :
7 : : GCC is free software; you can redistribute it and/or modify it
8 : : under the terms of the GNU General Public License as published by
9 : : the Free Software Foundation; either version 3, or (at your option)
10 : : any later version.
11 : :
12 : : GCC is distributed in the hope that it will be useful, but
13 : : WITHOUT ANY WARRANTY; without even the implied warranty of
14 : : MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 : : General Public License for more details.
16 : :
17 : : You should have received a copy of the GNU General Public License
18 : : along with GCC; see the file COPYING3. If not see
19 : : <http://www.gnu.org/licenses/>. */
20 : :
21 : : /* Comments in this file have a non-negligible chance of being wrong
22 : : or at least inaccurate. Due to (a) my misunderstanding, (b)
23 : : ambiguities that I have interpretted differently to original intent
24 : : (c) changes in the specification, (d) my poor wording, (e) source
25 : : changes. */
26 : :
27 : : /* (Incomplete) Design Notes
28 : :
29 : : A hash table contains all module names. Imported modules are
30 : : present in a modules array, which by construction places an
31 : : import's dependencies before the import itself. The single
32 : : exception is the current TU, which always occupies slot zero (even
33 : : when it is not a module).
34 : :
35 : : Imported decls occupy an entity_ary, an array of binding_slots, indexed
36 : : by importing module and index within that module. A flat index is
37 : : used, as each module reserves a contiguous range of indices.
38 : : Initially each slot indicates the CMI section containing the
39 : : streamed decl. When the decl is imported it will point to the decl
40 : : itself.
41 : :
42 : : Additionally each imported decl is mapped in the entity_map via its
43 : : DECL_UID to the flat index in the entity_ary. Thus we can locate
44 : : the index for any imported decl by using this map and then
45 : : de-flattening the index via a binary seach of the module vector.
46 : : Cross-module references are by (remapped) module number and
47 : : module-local index.
48 : :
49 : : Each importable DECL contains several flags. The simple set are
50 : : DECL_MODULE_EXPORT_P, DECL_MODULE_PURVIEW_P, DECL_MODULE_ATTACH_P
51 : : and DECL_MODULE_IMPORT_P. The first indicates whether it is
52 : : exported, the second whether it is in module or header-unit
53 : : purview. The third indicates it is attached to the named module in
54 : : whose purview it resides and the fourth indicates whether it was an
55 : : import into this TU or not. DECL_MODULE_ATTACH_P will be false for
56 : : all decls in a header-unit, and for those in a named module inside
57 : : a linkage declaration.
58 : :
59 : : The more detailed flags are DECL_MODULE_PARTITION_P,
60 : : DECL_MODULE_ENTITY_P. The first is set in a primary interface unit
61 : : on decls that were read from module partitions (these will have
62 : : DECL_MODULE_IMPORT_P set too). Such decls will be streamed out to
63 : : the primary's CMI. DECL_MODULE_ENTITY_P is set when an entity is
64 : : imported, even if it matched a non-imported entity. Such a decl
65 : : will not have DECL_MODULE_IMPORT_P set, even though it has an entry
66 : : in the entity map and array.
67 : :
68 : : Header units are module-like.
69 : :
70 : : For namespace-scope lookup, the decls for a particular module are
71 : : held located in a sparse array hanging off the binding of the name.
72 : : This is partitioned into two: a few fixed slots at the start
73 : : followed by the sparse slots afterwards. By construction we only
74 : : need to append new slots to the end -- there is never a need to
75 : : insert in the middle. The fixed slots are MODULE_SLOT_CURRENT for
76 : : the current TU (regardless of whether it is a module or not),
77 : : MODULE_SLOT_GLOBAL and MODULE_SLOT_PARTITION. These latter two
78 : : slots are used for merging entities across the global module and
79 : : module partitions respectively. MODULE_SLOT_PARTITION is only
80 : : present in a module. Neither of those two slots is searched during
81 : : name lookup -- they are internal use only. This vector is created
82 : : lazily once we require it, if there is only a declaration from the
83 : : current TU, a regular binding is present. It is converted on
84 : : demand.
85 : :
86 : : OPTIMIZATION: Outside of the current TU, we only need ADL to work.
87 : : We could optimize regular lookup for the current TU by glomming all
88 : : the visible decls on its slot. Perhaps wait until design is a
89 : : little more settled though.
90 : :
91 : : There is only one instance of each extern-linkage namespace. It
92 : : appears in every module slot that makes it visible. It also
93 : : appears in MODULE_SLOT_GLOBAL. (It is an ODR violation if they
94 : : collide with some other global module entity.) We also have an
95 : : optimization that shares the slot for adjacent modules that declare
96 : : the same such namespace.
97 : :
98 : : A module interface compilation produces a Compiled Module Interface
99 : : (CMI). The format used is Encapsulated Lazy Records Of Numbered
100 : : Declarations, which is essentially ELF's section encapsulation. (As
101 : : all good nerds are aware, Elrond is half Elf.) Some sections are
102 : : named, and contain information about the module as a whole (indices
103 : : etc), and other sections are referenced by number. Although I
104 : : don't defend against actively hostile CMIs, there is some
105 : : checksumming involved to verify data integrity. When dumping out
106 : : an interface, we generate a graph of all the
107 : : independently-redeclarable DECLS that are needed, and the decls
108 : : they reference. From that we determine the strongly connected
109 : : components (SCC) within this TU. Each SCC is dumped to a separate
110 : : numbered section of the CMI. We generate a binding table section,
111 : : mapping each namespace&name to a defining section. This allows
112 : : lazy loading.
113 : :
114 : : Lazy loading employs mmap to map a read-only image of the CMI.
115 : : It thus only occupies address space and is paged in on demand,
116 : : backed by the CMI file itself. If mmap is unavailable, regular
117 : : FILEIO is used. Also, there's a bespoke ELF reader/writer here,
118 : : which implements just the section table and sections (including
119 : : string sections) of a 32-bit ELF in host byte-order. You can of
120 : : course inspect it with readelf. I figured 32-bit is sufficient,
121 : : for a single module. I detect running out of section numbers, but
122 : : do not implement the ELF overflow mechanism. At least you'll get
123 : : an error if that happens.
124 : :
125 : : We do not separate declarations and definitions. My guess is that
126 : : if you refer to the declaration, you'll also need the definition
127 : : (template body, inline function, class definition etc). But this
128 : : does mean we can get larger SCCs than if we separated them. It is
129 : : unclear whether this is a win or not.
130 : :
131 : : Notice that we embed section indices into the contents of other
132 : : sections. Thus random manipulation of the CMI file by ELF tools
133 : : may well break it. The kosher way would probably be to introduce
134 : : indirection via section symbols, but that would require defining a
135 : : relocation type.
136 : :
137 : : Notice that lazy loading of one module's decls can cause lazy
138 : : loading of other decls in the same or another module. Clearly we
139 : : want to avoid loops. In a correct program there can be no loops in
140 : : the module dependency graph, and the above-mentioned SCC algorithm
141 : : places all intra-module circular dependencies in the same SCC. It
142 : : also orders the SCCs wrt each other, so dependent SCCs come first.
143 : : As we load dependent modules first, we know there can be no
144 : : reference to a higher-numbered module, and because we write out
145 : : dependent SCCs first, likewise for SCCs within the module. This
146 : : allows us to immediately detect broken references. When loading,
147 : : we must ensure the rest of the compiler doesn't cause some
148 : : unconnected load to occur (for instance, instantiate a template).
149 : :
150 : : Classes used:
151 : :
152 : : dumper - logger
153 : :
154 : : data - buffer
155 : :
156 : : bytes_in : data - scalar reader
157 : : bytes_out : data - scalar writer
158 : :
159 : : bytes_in::bits_in - bit stream reader
160 : : bytes_out::bits_out - bit stream writer
161 : :
162 : : elf - ELROND format
163 : : elf_in : elf - ELROND reader
164 : : elf_out : elf - ELROND writer
165 : :
166 : : trees_in : bytes_in - tree reader
167 : : trees_out : bytes_out - tree writer
168 : :
169 : : depset - dependency set
170 : : depset::hash - hash table of depsets
171 : : depset::tarjan - SCC determinator
172 : :
173 : : uidset<T> - set T's related to a UID
174 : : uidset<T>::hash hash table of uidset<T>
175 : :
176 : : loc_spans - location map data
177 : :
178 : : module_state - module object
179 : :
180 : : slurping - data needed during loading
181 : :
182 : : macro_import - imported macro data
183 : : macro_export - exported macro data
184 : :
185 : : The ELROND objects use mmap, for both reading and writing. If mmap
186 : : is unavailable, fileno IO is used to read and write blocks of data.
187 : :
188 : : The mapper object uses fileno IO to communicate with the server or
189 : : program. */
190 : :
191 : : /* In expermental (trunk) sources, MODULE_VERSION is a #define passed
192 : : in from the Makefile. It records the modification date of the
193 : : source directory -- that's the only way to stay sane. In release
194 : : sources, we (plan to) use the compiler's major.minor versioning.
195 : : While the format might not change between at minor versions, it
196 : : seems simplest to tie the two together. There's no concept of
197 : : inter-version compatibility. */
198 : : #define IS_EXPERIMENTAL(V) ((V) >= (1U << 20))
199 : : #define MODULE_MAJOR(V) ((V) / 10000)
200 : : #define MODULE_MINOR(V) ((V) % 10000)
201 : : #define EXPERIMENT(A,B) (IS_EXPERIMENTAL (MODULE_VERSION) ? (A) : (B))
202 : : #ifndef MODULE_VERSION
203 : : #include "bversion.h"
204 : : #define MODULE_VERSION (BUILDING_GCC_MAJOR * 10000U + BUILDING_GCC_MINOR)
205 : : #elif !IS_EXPERIMENTAL (MODULE_VERSION)
206 : : #error "This is not the version I was looking for."
207 : : #endif
208 : :
209 : : #define _DEFAULT_SOURCE 1 /* To get TZ field of struct tm, if available. */
210 : : #include "config.h"
211 : : #define INCLUDE_STRING
212 : : #define INCLUDE_VECTOR
213 : : #include "system.h"
214 : : #include "coretypes.h"
215 : : #include "cp-tree.h"
216 : : #include "timevar.h"
217 : : #include "stringpool.h"
218 : : #include "dumpfile.h"
219 : : #include "bitmap.h"
220 : : #include "cgraph.h"
221 : : #include "varasm.h"
222 : : #include "tree-iterator.h"
223 : : #include "cpplib.h"
224 : : #include "mkdeps.h"
225 : : #include "incpath.h"
226 : : #include "libiberty.h"
227 : : #include "stor-layout.h"
228 : : #include "version.h"
229 : : #include "tree-diagnostic.h"
230 : : #include "toplev.h"
231 : : #include "opts.h"
232 : : #include "attribs.h"
233 : : #include "intl.h"
234 : : #include "langhooks.h"
235 : : /* This TU doesn't need or want to see the networking. */
236 : : #define CODY_NETWORKING 0
237 : : #include "mapper-client.h"
238 : : #include <zlib.h> // for crc32, crc32_combine
239 : :
240 : : #if 0 // 1 for testing no mmap
241 : : #define MAPPED_READING 0
242 : : #define MAPPED_WRITING 0
243 : : #else
244 : : #if HAVE_MMAP_FILE && HAVE_MUNMAP && HAVE_MSYNC
245 : : /* mmap, munmap, msync. */
246 : : #define MAPPED_READING 1
247 : : #if HAVE_SYSCONF && defined (_SC_PAGE_SIZE)
248 : : /* sysconf (_SC_PAGE_SIZE), ftruncate */
249 : : /* posix_fallocate used if available. */
250 : : #define MAPPED_WRITING 1
251 : : #else
252 : : #define MAPPED_WRITING 0
253 : : #endif
254 : : #else
255 : : #define MAPPED_READING 0
256 : : #define MAPPED_WRITING 0
257 : : #endif
258 : : #endif
259 : :
260 : : /* Some open(2) flag differences, what a colourful world it is! */
261 : : #if defined (O_CLOEXEC)
262 : : // OK
263 : : #elif defined (_O_NOINHERIT)
264 : : /* Windows' _O_NOINHERIT matches O_CLOEXEC flag */
265 : : #define O_CLOEXEC _O_NOINHERIT
266 : : #else
267 : : #define O_CLOEXEC 0
268 : : #endif
269 : : #if defined (O_BINARY)
270 : : // Ok?
271 : : #elif defined (_O_BINARY)
272 : : /* Windows' open(2) call defaults to text! */
273 : : #define O_BINARY _O_BINARY
274 : : #else
275 : : #define O_BINARY 0
276 : : #endif
277 : :
278 : 219402 : static inline cpp_hashnode *cpp_node (tree id)
279 : : {
280 : 219402 : return CPP_HASHNODE (GCC_IDENT_TO_HT_IDENT (id));
281 : : }
282 : :
283 : 134104 : static inline tree identifier (const cpp_hashnode *node)
284 : : {
285 : : /* HT_NODE() expands to node->ident that HT_IDENT_TO_GCC_IDENT()
286 : : then subtracts a nonzero constant, deriving a pointer to
287 : : a different member than ident. That's strictly undefined
288 : : and detected by -Warray-bounds. Suppress it. See PR 101372. */
289 : 134104 : #pragma GCC diagnostic push
290 : 134104 : #pragma GCC diagnostic ignored "-Warray-bounds"
291 : 134104 : return HT_IDENT_TO_GCC_IDENT (HT_NODE (const_cast<cpp_hashnode *> (node)));
292 : 134104 : #pragma GCC diagnostic pop
293 : : }
294 : :
295 : : /* Id for dumping module information. */
296 : : int module_dump_id;
297 : :
298 : : /* We have a special module owner. */
299 : : #define MODULE_UNKNOWN (~0U) /* Not yet known. */
300 : :
301 : : /* Prefix for section names. */
302 : : #define MOD_SNAME_PFX ".gnu.c++"
303 : :
304 : : /* Format a version for user consumption. */
305 : :
306 : : typedef char verstr_t[32];
307 : : static void
308 : 3639 : version2string (unsigned version, verstr_t &out)
309 : : {
310 : 3639 : unsigned major = MODULE_MAJOR (version);
311 : 3639 : unsigned minor = MODULE_MINOR (version);
312 : :
313 : 3639 : if (IS_EXPERIMENTAL (version))
314 : 3639 : sprintf (out, "%04u/%02u/%02u-%02u:%02u%s",
315 : 3639 : 2000 + major / 10000, (major / 100) % 100, (major % 100),
316 : : minor / 100, minor % 100,
317 : : EXPERIMENT ("", " (experimental)"));
318 : : else
319 : 0 : sprintf (out, "%u.%u", major, minor);
320 : 3639 : }
321 : :
322 : : /* Include files to note translation for. */
323 : : static vec<const char *, va_heap, vl_embed> *note_includes;
324 : :
325 : : /* Modules to note CMI pathames. */
326 : : static vec<const char *, va_heap, vl_embed> *note_cmis;
327 : :
328 : : /* Traits to hash an arbitrary pointer. Entries are not deletable,
329 : : and removal is a noop (removal needed upon destruction). */
330 : : template <typename T>
331 : : struct nodel_ptr_hash : pointer_hash<T>, typed_noop_remove <T *> {
332 : : /* Nothing is deletable. Everything is insertable. */
333 : : static bool is_deleted (T *) { return false; }
334 : : static void mark_deleted (T *) { gcc_unreachable (); }
335 : : };
336 : :
337 : : /* Map from pointer to signed integer. */
338 : : typedef simple_hashmap_traits<nodel_ptr_hash<void>, int> ptr_int_traits;
339 : : typedef hash_map<void *,signed,ptr_int_traits> ptr_int_hash_map;
340 : :
341 : : /********************************************************************/
342 : : /* Basic streaming & ELF. Serialization is usually via mmap. For
343 : : writing we slide a buffer over the output file, syncing it
344 : : approproiately. For reading we simply map the whole file (as a
345 : : file-backed read-only map -- it's just address space, leaving the
346 : : OS pager to deal with getting the data to us). Some buffers need
347 : : to be more conventional malloc'd contents. */
348 : :
349 : : /* Variable length buffer. */
350 : :
351 : : namespace {
352 : :
353 : : constexpr line_map_uint_t loc_one = 1;
354 : :
355 : : class data {
356 : : public:
357 : 2535 : class allocator {
358 : : public:
359 : : /* Tools tend to moan if the dtor's not virtual. */
360 : 97816 : virtual ~allocator () {}
361 : :
362 : : public:
363 : : void grow (data &obj, unsigned needed, bool exact);
364 : : void shrink (data &obj);
365 : :
366 : : public:
367 : : virtual char *grow (char *ptr, unsigned needed);
368 : : virtual void shrink (char *ptr);
369 : : };
370 : :
371 : : public:
372 : : char *buffer; /* Buffer being transferred. */
373 : : /* Although size_t would be the usual size, we know we never get
374 : : more than 4GB of buffer -- because that's the limit of the
375 : : encapsulation format. And if you need bigger imports, you're
376 : : doing it wrong. */
377 : : unsigned size; /* Allocated size of buffer. */
378 : : unsigned pos; /* Position in buffer. */
379 : :
380 : : public:
381 : 668941 : data ()
382 : 668941 : :buffer (NULL), size (0), pos (0)
383 : : {
384 : : }
385 : 681557 : ~data ()
386 : : {
387 : : /* Make sure the derived and/or using class know what they're
388 : : doing. */
389 : 681557 : gcc_checking_assert (!buffer);
390 : 681557 : }
391 : :
392 : : protected:
393 : 433125106 : char *use (unsigned count)
394 : : {
395 : 433125106 : if (size < pos + count)
396 : : return NULL;
397 : 433125106 : char *res = &buffer[pos];
398 : 433125106 : pos += count;
399 : 233068582 : return res;
400 : : }
401 : :
402 : : unsigned calc_crc (unsigned) const;
403 : :
404 : : public:
405 : 32832066 : void unuse (unsigned count)
406 : : {
407 : 32832066 : pos -= count;
408 : 24535 : }
409 : :
410 : : public:
411 : : static allocator simple_memory;
412 : : };
413 : : } // anon namespace
414 : :
415 : : /* The simple data allocator. */
416 : : data::allocator data::simple_memory;
417 : :
418 : : /* Grow buffer to at least size NEEDED. */
419 : :
420 : : void
421 : 589784 : data::allocator::grow (data &obj, unsigned needed, bool exact)
422 : : {
423 : 589784 : gcc_checking_assert (needed ? needed > obj.size : !obj.size);
424 : 589784 : if (!needed)
425 : : /* Pick a default size. */
426 : 249718 : needed = EXPERIMENT (100, 1000);
427 : :
428 : 589784 : if (!exact)
429 : 582494 : needed *= 2;
430 : 589784 : obj.buffer = grow (obj.buffer, needed);
431 : 589784 : if (obj.buffer)
432 : 589784 : obj.size = needed;
433 : : else
434 : 0 : obj.pos = obj.size = 0;
435 : 589784 : }
436 : :
437 : : /* Free a buffer. */
438 : :
439 : : void
440 : 262405 : data::allocator::shrink (data &obj)
441 : : {
442 : 0 : shrink (obj.buffer);
443 : 262405 : obj.buffer = NULL;
444 : 262405 : obj.size = 0;
445 : 0 : }
446 : :
447 : : char *
448 : 10036 : data::allocator::grow (char *ptr, unsigned needed)
449 : : {
450 : 10036 : return XRESIZEVAR (char, ptr, needed);
451 : : }
452 : :
453 : : void
454 : 12675 : data::allocator::shrink (char *ptr)
455 : : {
456 : 12675 : XDELETEVEC (ptr);
457 : 12675 : }
458 : :
459 : : /* Calculate the crc32 of the buffer. Note the CRC is stored in the
460 : : first 4 bytes, so don't include them. */
461 : :
462 : : unsigned
463 : 426105 : data::calc_crc (unsigned l) const
464 : : {
465 : 426105 : return crc32 (0, (unsigned char *)buffer + 4, l - 4);
466 : : }
467 : :
468 : : class elf_in;
469 : :
470 : : /* Byte stream reader. */
471 : :
472 : : namespace {
473 : : class bytes_in : public data {
474 : : typedef data parent;
475 : :
476 : : protected:
477 : : bool overrun; /* Sticky read-too-much flag. */
478 : :
479 : : public:
480 : 185212 : bytes_in ()
481 : 185212 : : parent (), overrun (false)
482 : : {
483 : : }
484 : 187702 : ~bytes_in ()
485 : : {
486 : 13784 : }
487 : :
488 : : public:
489 : : /* Begin reading a named section. */
490 : : bool begin (location_t loc, elf_in *src, const char *name);
491 : : /* Begin reading a numbered section with optional name. */
492 : : bool begin (location_t loc, elf_in *src, unsigned, const char * = NULL);
493 : : /* Complete reading a buffer. Propagate errors and return true on
494 : : success. */
495 : : bool end (elf_in *src);
496 : : /* Return true if there is unread data. */
497 : 1305628 : bool more_p () const
498 : : {
499 : 1305628 : return pos != size;
500 : : }
501 : :
502 : : public:
503 : : /* Start reading at OFFSET. */
504 : 323 : void random_access (unsigned offset)
505 : : {
506 : 323 : if (offset > size)
507 : 0 : set_overrun ();
508 : 323 : pos = offset;
509 : : }
510 : :
511 : : public:
512 : 1209343 : void align (unsigned boundary)
513 : : {
514 : 1209343 : if (unsigned pad = pos & (boundary - 1))
515 : 2342589 : read (boundary - pad);
516 : : }
517 : :
518 : : public:
519 : 200056524 : const char *read (unsigned count)
520 : : {
521 : 1133246 : char *ptr = use (count);
522 : 200056524 : if (!ptr)
523 : 0 : set_overrun ();
524 : 163129319 : return ptr;
525 : : }
526 : :
527 : : public:
528 : : bool check_crc () const;
529 : : /* We store the CRC in the first 4 bytes, using host endianness. */
530 : 186261 : unsigned get_crc () const
531 : : {
532 : 186261 : return *(const unsigned *)&buffer[0];
533 : : }
534 : :
535 : : public:
536 : : /* Manipulate the overrun flag. */
537 : 117185857 : bool get_overrun () const
538 : : {
539 : 117185857 : return overrun;
540 : : }
541 : 26 : void set_overrun ()
542 : : {
543 : 26 : overrun = true;
544 : 0 : }
545 : :
546 : : public:
547 : : unsigned u32 (); /* Read uncompressed integer. */
548 : :
549 : : public:
550 : : int c () ATTRIBUTE_UNUSED; /* Read a char. */
551 : : int i (); /* Read a signed int. */
552 : : unsigned u (); /* Read an unsigned int. */
553 : : size_t z (); /* Read a size_t. */
554 : : location_t loc (); /* Read a location_t. */
555 : : HOST_WIDE_INT wi (); /* Read a HOST_WIDE_INT. */
556 : : unsigned HOST_WIDE_INT wu (); /* Read an unsigned HOST_WIDE_INT. */
557 : : const char *str (size_t * = NULL); /* Read a string. */
558 : : const void *buf (size_t); /* Read a fixed-length buffer. */
559 : : cpp_hashnode *cpp_node (); /* Read a cpp node. */
560 : :
561 : : struct bits_in;
562 : : bits_in stream_bits ();
563 : : };
564 : : } // anon namespace
565 : :
566 : : /* Verify the buffer's CRC is correct. */
567 : :
568 : : bool
569 : 183637 : bytes_in::check_crc () const
570 : : {
571 : 183637 : if (size < 4)
572 : : return false;
573 : :
574 : 183637 : unsigned c_crc = calc_crc (size);
575 : 183637 : if (c_crc != get_crc ())
576 : : return false;
577 : :
578 : : return true;
579 : : }
580 : :
581 : : class elf_out;
582 : :
583 : : /* Byte stream writer. */
584 : :
585 : : namespace {
586 : : class bytes_out : public data {
587 : : typedef data parent;
588 : :
589 : : public:
590 : : allocator *memory; /* Obtainer of memory. */
591 : :
592 : : public:
593 : 478552 : bytes_out (allocator *memory)
594 : 478552 : : parent (), memory (memory)
595 : : {
596 : : }
597 : 478552 : ~bytes_out ()
598 : : {
599 : 504239 : }
600 : :
601 : : public:
602 : 558866471 : bool streaming_p () const
603 : : {
604 : 558866471 : return memory != NULL;
605 : : }
606 : :
607 : : public:
608 : : void set_crc (unsigned *crc_ptr);
609 : :
610 : : public:
611 : : /* Begin writing, maybe reserve space for CRC. */
612 : : void begin (bool need_crc = true);
613 : : /* Finish writing. Spill to section by number. */
614 : : unsigned end (elf_out *, unsigned, unsigned *crc_ptr = NULL);
615 : :
616 : : public:
617 : 1541627 : void align (unsigned boundary)
618 : : {
619 : 1541627 : if (unsigned pad = pos & (boundary - 1))
620 : 1442836 : write (boundary - pad);
621 : 1541627 : }
622 : :
623 : : public:
624 : 233068582 : char *write (unsigned count, bool exact = false)
625 : : {
626 : 233068582 : if (size < pos + count)
627 : 327592 : memory->grow (*this, pos + count, exact);
628 : 233068582 : return use (count);
629 : : }
630 : :
631 : : public:
632 : : void u32 (unsigned); /* Write uncompressed integer. */
633 : :
634 : : public:
635 : : void c (unsigned char) ATTRIBUTE_UNUSED; /* Write unsigned char. */
636 : : void i (int); /* Write signed int. */
637 : : void u (unsigned); /* Write unsigned int. */
638 : : void z (size_t s); /* Write size_t. */
639 : : void loc (location_t); /* Write location_t. */
640 : : void wi (HOST_WIDE_INT); /* Write HOST_WIDE_INT. */
641 : : void wu (unsigned HOST_WIDE_INT); /* Write unsigned HOST_WIDE_INT. */
642 : 15423 : void str (const char *ptr)
643 : : {
644 : 15423 : str (ptr, strlen (ptr));
645 : 15423 : }
646 : 240037 : void cpp_node (const cpp_hashnode *node)
647 : : {
648 : 240037 : str ((const char *)NODE_NAME (node), NODE_LEN (node));
649 : 12002 : }
650 : : void str (const char *, size_t); /* Write string of known length. */
651 : : void buf (const void *, size_t); /* Write fixed length buffer. */
652 : : void *buf (size_t); /* Create a writable buffer */
653 : :
654 : : struct bits_out;
655 : : bits_out stream_bits ();
656 : :
657 : : public:
658 : : /* Format a NUL-terminated raw string. */
659 : : void printf (const char *, ...) ATTRIBUTE_PRINTF_2;
660 : : void print_time (const char *, const tm *, const char *);
661 : :
662 : : public:
663 : : /* Dump instrumentation. */
664 : : static void instrument ();
665 : :
666 : : protected:
667 : : /* Instrumentation. */
668 : : static unsigned spans[4];
669 : : static unsigned lengths[4];
670 : : };
671 : : } // anon namespace
672 : :
673 : : /* Finish bit packet. Rewind the bytes not used. */
674 : :
675 : : static unsigned
676 : 32783311 : bit_flush (data& bits, uint32_t& bit_val, unsigned& bit_pos)
677 : : {
678 : 32783311 : gcc_assert (bit_pos);
679 : 32783311 : unsigned bytes = (bit_pos + 7) / 8;
680 : 32783311 : bits.unuse (4 - bytes);
681 : 32783311 : bit_pos = 0;
682 : 32783311 : bit_val = 0;
683 : 32783311 : return bytes;
684 : : }
685 : :
686 : : /* Bit stream reader (RAII-enabled). Bools are packed into bytes. You
687 : : cannot mix bools and non-bools. Use bflush to flush the current stream
688 : : of bools on demand. Upon destruction bflush is called.
689 : :
690 : : When reading, we don't know how many bools we'll read in. So read
691 : : 4 bytes-worth, and then rewind when flushing if we didn't need them
692 : : all. You can't have a block of bools closer than 4 bytes to the
693 : : end of the buffer.
694 : :
695 : : Both bits_in and bits_out maintain the necessary state for bit packing,
696 : : and since these objects are locally constructed the compiler can more
697 : : easily track their state across consecutive reads/writes and optimize
698 : : away redundant buffering checks. */
699 : :
700 : : struct bytes_in::bits_in {
701 : : bytes_in& in;
702 : : uint32_t bit_val = 0;
703 : : unsigned bit_pos = 0;
704 : :
705 : 11200534 : bits_in (bytes_in& in)
706 : 11200534 : : in (in)
707 : : { }
708 : :
709 : 11200534 : ~bits_in ()
710 : : {
711 : 10480922 : bflush ();
712 : 11200534 : }
713 : :
714 : : bits_in(bits_in&&) = default;
715 : : bits_in(const bits_in&) = delete;
716 : : bits_in& operator=(const bits_in&) = delete;
717 : :
718 : : /* Completed a block of bools. */
719 : 23255844 : void bflush ()
720 : : {
721 : 23255844 : if (bit_pos)
722 : 12774922 : bit_flush (in, bit_val, bit_pos);
723 : 23255844 : }
724 : :
725 : : /* Read one bit. */
726 : 382474877 : bool b ()
727 : : {
728 : 382474877 : if (!bit_pos)
729 : 16657303 : bit_val = in.u32 ();
730 : 382474877 : bool x = (bit_val >> bit_pos) & 1;
731 : 382474877 : bit_pos = (bit_pos + 1) % 32;
732 : 382474877 : return x;
733 : : }
734 : : };
735 : :
736 : : /* Factory function for bits_in. */
737 : :
738 : : bytes_in::bits_in
739 : 11200534 : bytes_in::stream_bits ()
740 : : {
741 : 11200534 : return bits_in (*this);
742 : : }
743 : :
744 : : /* Bit stream writer (RAII-enabled), counterpart to bits_in. */
745 : :
746 : : struct bytes_out::bits_out {
747 : : bytes_out& out;
748 : : uint32_t bit_val = 0;
749 : : unsigned bit_pos = 0;
750 : : char is_set = -1;
751 : :
752 : 13333765 : bits_out (bytes_out& out)
753 : 13333765 : : out (out)
754 : : { }
755 : :
756 : 13333765 : ~bits_out ()
757 : : {
758 : 67009 : bflush ();
759 : : }
760 : :
761 : : bits_out(bits_out&&) = default;
762 : : bits_out(const bits_out&) = delete;
763 : : bits_out& operator=(const bits_out&) = delete;
764 : :
765 : : /* Completed a block of bools. */
766 : 27685600 : void bflush ()
767 : : {
768 : 27685600 : if (bit_pos)
769 : : {
770 : 15253122 : out.u32 (bit_val);
771 : 15253122 : out.lengths[2] += bit_flush (out, bit_val, bit_pos);
772 : : }
773 : 27685600 : out.spans[2]++;
774 : 27685600 : is_set = -1;
775 : 27685600 : }
776 : :
777 : : /* Write one bit.
778 : :
779 : : It may be worth optimizing for most bools being zero. Some kind of
780 : : run-length encoding? */
781 : 455530546 : void b (bool x)
782 : : {
783 : 455530546 : if (is_set != x)
784 : : {
785 : 48961320 : is_set = x;
786 : 48961320 : out.spans[x]++;
787 : : }
788 : 455530546 : out.lengths[x]++;
789 : 455530546 : bit_val |= unsigned (x) << bit_pos++;
790 : 455530546 : if (bit_pos == 32)
791 : : {
792 : 4755267 : out.u32 (bit_val);
793 : 4755267 : out.lengths[2] += bit_flush (out, bit_val, bit_pos);
794 : : }
795 : 455530546 : }
796 : : };
797 : :
798 : : /* Factory function for bits_out. */
799 : :
800 : : bytes_out::bits_out
801 : 13333765 : bytes_out::stream_bits ()
802 : : {
803 : 13333765 : return bits_out (*this);
804 : : }
805 : :
806 : : /* Instrumentation. */
807 : : unsigned bytes_out::spans[4];
808 : : unsigned bytes_out::lengths[4];
809 : :
810 : : /* If CRC_PTR non-null, set the CRC of the buffer. Mix the CRC into
811 : : that pointed to by CRC_PTR. */
812 : :
813 : : void
814 : 244866 : bytes_out::set_crc (unsigned *crc_ptr)
815 : : {
816 : 244866 : if (crc_ptr)
817 : : {
818 : 242468 : gcc_checking_assert (pos >= 4);
819 : :
820 : 242468 : unsigned crc = calc_crc (pos);
821 : 242468 : unsigned accum = *crc_ptr;
822 : : /* Only mix the existing *CRC_PTR if it is non-zero. */
823 : 242468 : accum = accum ? crc32_combine (accum, crc, pos - 4) : crc;
824 : 242468 : *crc_ptr = accum;
825 : :
826 : : /* Buffer will be sufficiently aligned. */
827 : 242468 : *(unsigned *)buffer = crc;
828 : : }
829 : 244866 : }
830 : :
831 : : /* Exactly 4 bytes. Used internally for bool packing and a few other
832 : : places. We can't simply use uint32_t because (a) alignment and
833 : : (b) we need little-endian for the bool streaming rewinding to make
834 : : sense. */
835 : :
836 : : void
837 : 20016143 : bytes_out::u32 (unsigned val)
838 : : {
839 : 20016143 : if (char *ptr = write (4))
840 : : {
841 : 20016143 : ptr[0] = val;
842 : 20016143 : ptr[1] = val >> 8;
843 : 20016143 : ptr[2] = val >> 16;
844 : 20016143 : ptr[3] = val >> 24;
845 : : }
846 : 20016143 : }
847 : :
848 : : unsigned
849 : 16665522 : bytes_in::u32 ()
850 : : {
851 : 16665522 : unsigned val = 0;
852 : 16665522 : if (const char *ptr = read (4))
853 : : {
854 : 16665522 : val |= (unsigned char)ptr[0];
855 : 16665522 : val |= (unsigned char)ptr[1] << 8;
856 : 16665522 : val |= (unsigned char)ptr[2] << 16;
857 : 16665522 : val |= (unsigned char)ptr[3] << 24;
858 : : }
859 : :
860 : 16665522 : return val;
861 : : }
862 : :
863 : : /* Chars are unsigned and written as single bytes. */
864 : :
865 : : void
866 : 0 : bytes_out::c (unsigned char v)
867 : : {
868 : 0 : if (char *ptr = write (1))
869 : 0 : *ptr = v;
870 : 0 : }
871 : :
872 : : int
873 : 0 : bytes_in::c ()
874 : : {
875 : 0 : int v = 0;
876 : 0 : if (const char *ptr = read (1))
877 : 0 : v = (unsigned char)ptr[0];
878 : 0 : return v;
879 : : }
880 : :
881 : : /* Ints 7-bit as a byte. Otherwise a 3bit count of following bytes in
882 : : big-endian form. 4 bits are in the first byte. */
883 : :
884 : : void
885 : 82892302 : bytes_out::i (int v)
886 : : {
887 : 82892302 : if (char *ptr = write (1))
888 : : {
889 : 82892302 : if (v <= 0x3f && v >= -0x40)
890 : 65842869 : *ptr = v & 0x7f;
891 : : else
892 : : {
893 : 17049433 : unsigned bytes = 0;
894 : 17049433 : int probe;
895 : 17049433 : if (v >= 0)
896 : 0 : for (probe = v >> 8; probe > 0x7; probe >>= 8)
897 : 0 : bytes++;
898 : : else
899 : 26289221 : for (probe = v >> 8; probe < -0x8; probe >>= 8)
900 : 9239788 : bytes++;
901 : 17049433 : *ptr = 0x80 | bytes << 4 | (probe & 0xf);
902 : 17049433 : if ((ptr = write (++bytes)))
903 : 43338654 : for (; bytes--; v >>= 8)
904 : 26289221 : ptr[bytes] = v & 0xff;
905 : : }
906 : : }
907 : 82892302 : }
908 : :
909 : : int
910 : 70278426 : bytes_in::i ()
911 : : {
912 : 70278426 : int v = 0;
913 : 70278426 : if (const char *ptr = read (1))
914 : : {
915 : 70278426 : v = *ptr & 0xff;
916 : 70278426 : if (v & 0x80)
917 : : {
918 : 15267431 : unsigned bytes = (v >> 4) & 0x7;
919 : 15267431 : v &= 0xf;
920 : 15267431 : if (v & 0x8)
921 : 15267431 : v |= -1 ^ 0x7;
922 : : /* unsigned necessary due to left shifts of -ve values. */
923 : 15267431 : unsigned uv = unsigned (v);
924 : 15267431 : if ((ptr = read (++bytes)))
925 : 39501288 : while (bytes--)
926 : 24233857 : uv = (uv << 8) | (*ptr++ & 0xff);
927 : 15267431 : v = int (uv);
928 : : }
929 : 55010995 : else if (v & 0x40)
930 : 7418368 : v |= -1 ^ 0x3f;
931 : : }
932 : :
933 : 70278426 : return v;
934 : : }
935 : :
936 : : void
937 : 70656473 : bytes_out::u (unsigned v)
938 : : {
939 : 70656473 : if (char *ptr = write (1))
940 : : {
941 : 70656473 : if (v <= 0x7f)
942 : 61573724 : *ptr = v;
943 : : else
944 : : {
945 : 9082749 : unsigned bytes = 0;
946 : 9082749 : unsigned probe;
947 : 10664131 : for (probe = v >> 8; probe > 0xf; probe >>= 8)
948 : 1581382 : bytes++;
949 : 9082749 : *ptr = 0x80 | bytes << 4 | probe;
950 : 9082749 : if ((ptr = write (++bytes)))
951 : 19746880 : for (; bytes--; v >>= 8)
952 : 10664131 : ptr[bytes] = v & 0xff;
953 : : }
954 : : }
955 : 70656473 : }
956 : :
957 : : unsigned
958 : 61061245 : bytes_in::u ()
959 : : {
960 : 61061245 : unsigned v = 0;
961 : :
962 : 61061245 : if (const char *ptr = read (1))
963 : : {
964 : 61061245 : v = *ptr & 0xff;
965 : 61061245 : if (v & 0x80)
966 : : {
967 : 8072949 : unsigned bytes = (v >> 4) & 0x7;
968 : 8072949 : v &= 0xf;
969 : 8072949 : if ((ptr = read (++bytes)))
970 : 17656398 : while (bytes--)
971 : 9583449 : v = (v << 8) | (*ptr++ & 0xff);
972 : : }
973 : : }
974 : :
975 : 61061245 : return v;
976 : : }
977 : :
978 : : void
979 : 16871239 : bytes_out::wi (HOST_WIDE_INT v)
980 : : {
981 : 16871239 : if (char *ptr = write (1))
982 : : {
983 : 16871239 : if (v <= 0x3f && v >= -0x40)
984 : 3404214 : *ptr = v & 0x7f;
985 : : else
986 : : {
987 : 13467025 : unsigned bytes = 0;
988 : 13467025 : HOST_WIDE_INT probe;
989 : 13467025 : if (v >= 0)
990 : 48296400 : for (probe = v >> 8; probe > 0x7; probe >>= 8)
991 : 34831757 : bytes++;
992 : : else
993 : 7791 : for (probe = v >> 8; probe < -0x8; probe >>= 8)
994 : 5409 : bytes++;
995 : 13467025 : *ptr = 0x80 | bytes << 4 | (probe & 0xf);
996 : 13467025 : if ((ptr = write (++bytes)))
997 : 61771216 : for (; bytes--; v >>= 8)
998 : 48304191 : ptr[bytes] = v & 0xff;
999 : : }
1000 : : }
1001 : 16871239 : }
1002 : :
1003 : : HOST_WIDE_INT
1004 : 13914783 : bytes_in::wi ()
1005 : : {
1006 : 13914783 : HOST_WIDE_INT v = 0;
1007 : 13914783 : if (const char *ptr = read (1))
1008 : : {
1009 : 13914783 : v = *ptr & 0xff;
1010 : 13914783 : if (v & 0x80)
1011 : : {
1012 : 12453579 : unsigned bytes = (v >> 4) & 0x7;
1013 : 12453579 : v &= 0xf;
1014 : 12453579 : if (v & 0x8)
1015 : 1624 : v |= -1 ^ 0x7;
1016 : : /* unsigned necessary due to left shifts of -ve values. */
1017 : 12453579 : unsigned HOST_WIDE_INT uv = (unsigned HOST_WIDE_INT) v;
1018 : 12453579 : if ((ptr = read (++bytes)))
1019 : 56971780 : while (bytes--)
1020 : 44518201 : uv = (uv << 8) | (*ptr++ & 0xff);
1021 : 12453579 : v = (HOST_WIDE_INT) uv;
1022 : : }
1023 : 1461204 : else if (v & 0x40)
1024 : 6884 : v |= -1 ^ 0x3f;
1025 : : }
1026 : :
1027 : 13914783 : return v;
1028 : : }
1029 : :
1030 : : /* unsigned wide ints are just written as signed wide ints. */
1031 : :
1032 : : inline void
1033 : 16870675 : bytes_out::wu (unsigned HOST_WIDE_INT v)
1034 : : {
1035 : 16870675 : wi ((HOST_WIDE_INT) v);
1036 : : }
1037 : :
1038 : : inline unsigned HOST_WIDE_INT
1039 : 13914293 : bytes_in::wu ()
1040 : : {
1041 : 27361245 : return (unsigned HOST_WIDE_INT) wi ();
1042 : : }
1043 : :
1044 : : /* size_t written as unsigned or unsigned wide int. */
1045 : :
1046 : : inline void
1047 : 1513023 : bytes_out::z (size_t s)
1048 : : {
1049 : 1513023 : if (sizeof (s) == sizeof (unsigned))
1050 : : u (s);
1051 : : else
1052 : 2995909 : wu (s);
1053 : 12 : }
1054 : :
1055 : : inline size_t
1056 : 1195429 : bytes_in::z ()
1057 : : {
1058 : 1195429 : if (sizeof (size_t) == sizeof (unsigned))
1059 : : return u ();
1060 : : else
1061 : 2390858 : return wu ();
1062 : : }
1063 : :
1064 : : /* location_t written as 32- or 64-bit as needed. */
1065 : :
1066 : 14798646 : inline void bytes_out::loc (location_t l)
1067 : : {
1068 : 14798646 : if (sizeof (location_t) > sizeof (unsigned))
1069 : 28039712 : wu (l);
1070 : : else
1071 : : u (l);
1072 : 1555182 : }
1073 : :
1074 : 12254104 : inline location_t bytes_in::loc ()
1075 : : {
1076 : 12254104 : if (sizeof (location_t) > sizeof (unsigned))
1077 : 24505597 : return wu ();
1078 : : else
1079 : : return u ();
1080 : : }
1081 : :
1082 : : /* Buffer simply memcpied. */
1083 : : void *
1084 : 1541627 : bytes_out::buf (size_t len)
1085 : : {
1086 : 1541627 : align (sizeof (void *) * 2);
1087 : 1541627 : return write (len);
1088 : : }
1089 : :
1090 : : void
1091 : 1497304 : bytes_out::buf (const void *src, size_t len)
1092 : : {
1093 : 1497304 : if (void *ptr = buf (len))
1094 : 1497304 : memcpy (ptr, src, len);
1095 : 1497304 : }
1096 : :
1097 : : const void *
1098 : 1209343 : bytes_in::buf (size_t len)
1099 : : {
1100 : 1209343 : align (sizeof (void *) * 2);
1101 : 1209343 : const char *ptr = read (len);
1102 : :
1103 : 1209343 : return ptr;
1104 : : }
1105 : :
1106 : : /* strings as an size_t length, followed by the buffer. Make sure
1107 : : there's a NUL terminator on read. */
1108 : :
1109 : : void
1110 : 1513005 : bytes_out::str (const char *string, size_t len)
1111 : : {
1112 : 1482880 : z (len);
1113 : 1482880 : if (len)
1114 : : {
1115 : 1482880 : gcc_checking_assert (!string[len]);
1116 : 1482880 : buf (string, len + 1);
1117 : : }
1118 : 30125 : }
1119 : :
1120 : : const char *
1121 : 1195423 : bytes_in::str (size_t *len_p)
1122 : : {
1123 : 1195423 : size_t len = z ();
1124 : :
1125 : : /* We're about to trust some user data. */
1126 : 1195423 : if (overrun)
1127 : 0 : len = 0;
1128 : 1195423 : if (len_p)
1129 : 1192074 : *len_p = len;
1130 : 1195423 : const char *str = NULL;
1131 : 1195423 : if (len)
1132 : : {
1133 : 1195214 : str = reinterpret_cast<const char *> (buf (len + 1));
1134 : 1195214 : if (!str || str[len])
1135 : : {
1136 : 0 : set_overrun ();
1137 : 0 : str = NULL;
1138 : : }
1139 : : }
1140 : 0 : return str ? str : "";
1141 : : }
1142 : :
1143 : : cpp_hashnode *
1144 : 219611 : bytes_in::cpp_node ()
1145 : : {
1146 : 219611 : size_t len;
1147 : 219611 : const char *s = str (&len);
1148 : 219611 : if (!len)
1149 : : return NULL;
1150 : 219402 : return ::cpp_node (get_identifier_with_length (s, len));
1151 : : }
1152 : :
1153 : : /* Format a string directly to the buffer, including a terminating
1154 : : NUL. Intended for human consumption. */
1155 : :
1156 : : void
1157 : 24535 : bytes_out::printf (const char *format, ...)
1158 : : {
1159 : 24535 : va_list args;
1160 : : /* Exercise buffer expansion. */
1161 : 24535 : size_t len = EXPERIMENT (10, 500);
1162 : :
1163 : 48755 : while (char *ptr = write (len))
1164 : : {
1165 : 48755 : va_start (args, format);
1166 : 48755 : size_t actual = vsnprintf (ptr, len, format, args) + 1;
1167 : 48755 : va_end (args);
1168 : 48755 : if (actual <= len)
1169 : : {
1170 : 24535 : unuse (len - actual);
1171 : 24535 : break;
1172 : : }
1173 : 24220 : unuse (len);
1174 : 24220 : len = actual;
1175 : 24220 : }
1176 : 24535 : }
1177 : :
1178 : : void
1179 : 4796 : bytes_out::print_time (const char *kind, const tm *time, const char *tz)
1180 : : {
1181 : 4796 : printf ("%stime: %4u/%02u/%02u %02u:%02u:%02u %s",
1182 : 4796 : kind, time->tm_year + 1900, time->tm_mon + 1, time->tm_mday,
1183 : 4796 : time->tm_hour, time->tm_min, time->tm_sec, tz);
1184 : 4796 : }
1185 : :
1186 : : /* Encapsulated Lazy Records Of Named Declarations.
1187 : : Header: Stunningly Elf32_Ehdr-like
1188 : : Sections: Sectional data
1189 : : [1-N) : User data sections
1190 : : N .strtab : strings, stunningly ELF STRTAB-like
1191 : : Index: Section table, stunningly ELF32_Shdr-like. */
1192 : :
1193 : : class elf {
1194 : : protected:
1195 : : /* Constants used within the format. */
1196 : : enum private_constants {
1197 : : /* File kind. */
1198 : : ET_NONE = 0,
1199 : : EM_NONE = 0,
1200 : : OSABI_NONE = 0,
1201 : :
1202 : : /* File format. */
1203 : : EV_CURRENT = 1,
1204 : : CLASS32 = 1,
1205 : : DATA2LSB = 1,
1206 : : DATA2MSB = 2,
1207 : :
1208 : : /* Section numbering. */
1209 : : SHN_UNDEF = 0,
1210 : : SHN_LORESERVE = 0xff00,
1211 : : SHN_XINDEX = 0xffff,
1212 : :
1213 : : /* Section types. */
1214 : : SHT_NONE = 0, /* No contents. */
1215 : : SHT_PROGBITS = 1, /* Random bytes. */
1216 : : SHT_STRTAB = 3, /* A string table. */
1217 : :
1218 : : /* Section flags. */
1219 : : SHF_NONE = 0x00, /* Nothing. */
1220 : : SHF_STRINGS = 0x20, /* NUL-Terminated strings. */
1221 : :
1222 : : /* I really hope we do not get CMI files larger than 4GB. */
1223 : : MY_CLASS = CLASS32,
1224 : : /* It is host endianness that is relevant. */
1225 : : MY_ENDIAN = DATA2LSB
1226 : : #ifdef WORDS_BIGENDIAN
1227 : : ^ DATA2LSB ^ DATA2MSB
1228 : : #endif
1229 : : };
1230 : :
1231 : : public:
1232 : : /* Constants visible to users. */
1233 : : enum public_constants {
1234 : : /* Special error codes. Breaking layering a bit. */
1235 : : E_BAD_DATA = -1, /* Random unexpected data errors. */
1236 : : E_BAD_LAZY = -2, /* Badly ordered laziness. */
1237 : : E_BAD_IMPORT = -3 /* A nested import failed. */
1238 : : };
1239 : :
1240 : : protected:
1241 : : /* File identification. On-disk representation. */
1242 : : struct ident {
1243 : : uint8_t magic[4]; /* 0x7f, 'E', 'L', 'F' */
1244 : : uint8_t klass; /* 4:CLASS32 */
1245 : : uint8_t data; /* 5:DATA2[LM]SB */
1246 : : uint8_t version; /* 6:EV_CURRENT */
1247 : : uint8_t osabi; /* 7:OSABI_NONE */
1248 : : uint8_t abiver; /* 8: 0 */
1249 : : uint8_t pad[7]; /* 9-15 */
1250 : : };
1251 : : /* File header. On-disk representation. */
1252 : : struct header {
1253 : : struct ident ident;
1254 : : uint16_t type; /* ET_NONE */
1255 : : uint16_t machine; /* EM_NONE */
1256 : : uint32_t version; /* EV_CURRENT */
1257 : : uint32_t entry; /* 0 */
1258 : : uint32_t phoff; /* 0 */
1259 : : uint32_t shoff; /* Section Header Offset in file */
1260 : : uint32_t flags;
1261 : : uint16_t ehsize; /* ELROND Header SIZE -- sizeof (header) */
1262 : : uint16_t phentsize; /* 0 */
1263 : : uint16_t phnum; /* 0 */
1264 : : uint16_t shentsize; /* Section Header SIZE -- sizeof (section) */
1265 : : uint16_t shnum; /* Section Header NUM */
1266 : : uint16_t shstrndx; /* Section Header STRing iNDeX */
1267 : : };
1268 : : /* File section. On-disk representation. */
1269 : : struct section {
1270 : : uint32_t name; /* String table offset. */
1271 : : uint32_t type; /* SHT_* */
1272 : : uint32_t flags; /* SHF_* */
1273 : : uint32_t addr; /* 0 */
1274 : : uint32_t offset; /* OFFSET in file */
1275 : : uint32_t size; /* SIZE of section */
1276 : : uint32_t link; /* 0 */
1277 : : uint32_t info; /* 0 */
1278 : : uint32_t addralign; /* 0 */
1279 : : uint32_t entsize; /* ENTry SIZE, usually 0 */
1280 : : };
1281 : :
1282 : : protected:
1283 : : data hdr; /* The header. */
1284 : : data sectab; /* The section table. */
1285 : : data strtab; /* String table. */
1286 : : int fd; /* File descriptor we're reading or writing. */
1287 : : int err; /* Sticky error code. */
1288 : :
1289 : : public:
1290 : : /* Construct from STREAM. E is errno if STREAM NULL. */
1291 : 5177 : elf (int fd, int e)
1292 : 10354 : :hdr (), sectab (), strtab (), fd (fd), err (fd >= 0 ? 0 : e)
1293 : : {}
1294 : 5101 : ~elf ()
1295 : : {
1296 : 5101 : gcc_checking_assert (fd < 0 && !hdr.buffer
1297 : : && !sectab.buffer && !strtab.buffer);
1298 : 5101 : }
1299 : :
1300 : : public:
1301 : : /* Return the error, if we have an error. */
1302 : 358536 : int get_error () const
1303 : : {
1304 : 358536 : return err;
1305 : : }
1306 : : /* Set the error, unless it's already been set. */
1307 : 32 : void set_error (int e = E_BAD_DATA)
1308 : : {
1309 : 32 : if (!err)
1310 : 19 : err = e;
1311 : 0 : }
1312 : : /* Get an error string. */
1313 : : const char *get_error (const char *) const;
1314 : :
1315 : : public:
1316 : : /* Begin reading/writing file. Return false on error. */
1317 : 5050 : bool begin () const
1318 : : {
1319 : 5050 : return !get_error ();
1320 : : }
1321 : : /* Finish reading/writing file. Return false on error. */
1322 : : bool end ();
1323 : : };
1324 : :
1325 : : /* Return error string. */
1326 : :
1327 : : const char *
1328 : 37 : elf::get_error (const char *name) const
1329 : : {
1330 : 37 : if (!name)
1331 : : return "Unknown CMI mapping";
1332 : :
1333 : 37 : switch (err)
1334 : : {
1335 : 0 : case 0:
1336 : 0 : gcc_unreachable ();
1337 : : case E_BAD_DATA:
1338 : : return "Bad file data";
1339 : 6 : case E_BAD_IMPORT:
1340 : 6 : return "Bad import dependency";
1341 : 0 : case E_BAD_LAZY:
1342 : 0 : return "Bad lazy ordering";
1343 : 18 : default:
1344 : 18 : return xstrerror (err);
1345 : : }
1346 : : }
1347 : :
1348 : : /* Finish file, return true if there's an error. */
1349 : :
1350 : : bool
1351 : 7230 : elf::end ()
1352 : : {
1353 : : /* Close the stream and free the section table. */
1354 : 7230 : if (fd >= 0 && close (fd))
1355 : 0 : set_error (errno);
1356 : 7230 : fd = -1;
1357 : :
1358 : 7230 : return !get_error ();
1359 : : }
1360 : :
1361 : : /* ELROND reader. */
1362 : :
1363 : : class elf_in : public elf {
1364 : : typedef elf parent;
1365 : :
1366 : : private:
1367 : : /* For freezing & defrosting. */
1368 : : #if !defined (HOST_LACKS_INODE_NUMBERS)
1369 : : dev_t device;
1370 : : ino_t inode;
1371 : : #endif
1372 : :
1373 : : public:
1374 : 2642 : elf_in (int fd, int e)
1375 : 5284 : :parent (fd, e)
1376 : : {
1377 : : }
1378 : 2566 : ~elf_in ()
1379 : : {
1380 : 2566 : }
1381 : :
1382 : : public:
1383 : 167679 : bool is_frozen () const
1384 : : {
1385 : 18 : return fd < 0 && hdr.pos;
1386 : : }
1387 : 18 : bool is_freezable () const
1388 : : {
1389 : 9 : return fd >= 0 && hdr.pos;
1390 : : }
1391 : : void freeze ();
1392 : : bool defrost (const char *);
1393 : :
1394 : : /* If BYTES is in the mmapped area, allocate a new buffer for it. */
1395 : 0 : void preserve (bytes_in &bytes ATTRIBUTE_UNUSED)
1396 : : {
1397 : : #if MAPPED_READING
1398 : 0 : if (hdr.buffer && bytes.buffer >= hdr.buffer
1399 : 0 : && bytes.buffer < hdr.buffer + hdr.pos)
1400 : : {
1401 : 0 : char *buf = bytes.buffer;
1402 : 0 : bytes.buffer = data::simple_memory.grow (NULL, bytes.size);
1403 : 0 : memcpy (bytes.buffer, buf, bytes.size);
1404 : : }
1405 : : #endif
1406 : 0 : }
1407 : : /* If BYTES is not in SELF's mmapped area, free it. SELF might be
1408 : : NULL. */
1409 : 1028 : static void release (elf_in *self ATTRIBUTE_UNUSED, bytes_in &bytes)
1410 : : {
1411 : : #if MAPPED_READING
1412 : 1028 : if (!(self && self->hdr.buffer && bytes.buffer >= self->hdr.buffer
1413 : 1028 : && bytes.buffer < self->hdr.buffer + self->hdr.pos))
1414 : : #endif
1415 : 0 : data::simple_memory.shrink (bytes.buffer);
1416 : 1028 : bytes.buffer = NULL;
1417 : 1028 : bytes.size = 0;
1418 : 1028 : }
1419 : :
1420 : : public:
1421 : 188885 : static void grow (data &data, unsigned needed)
1422 : : {
1423 : 188885 : gcc_checking_assert (!data.buffer);
1424 : : #if !MAPPED_READING
1425 : : data.buffer = XNEWVEC (char, needed);
1426 : : #endif
1427 : 188885 : data.size = needed;
1428 : 188885 : }
1429 : 194559 : static void shrink (data &data)
1430 : : {
1431 : : #if !MAPPED_READING
1432 : : XDELETEVEC (data.buffer);
1433 : : #endif
1434 : 194559 : data.buffer = NULL;
1435 : 194559 : data.size = 0;
1436 : 0 : }
1437 : :
1438 : : public:
1439 : 186261 : const section *get_section (unsigned s) const
1440 : : {
1441 : 186261 : if (s * sizeof (section) < sectab.size)
1442 : 186261 : return reinterpret_cast<const section *>
1443 : 186261 : (§ab.buffer[s * sizeof (section)]);
1444 : : else
1445 : : return NULL;
1446 : : }
1447 : : unsigned get_section_limit () const
1448 : : {
1449 : : return sectab.size / sizeof (section);
1450 : : }
1451 : :
1452 : : protected:
1453 : : const char *read (data *, unsigned, unsigned);
1454 : :
1455 : : public:
1456 : : /* Read section by number. */
1457 : 186261 : bool read (data *d, const section *s)
1458 : : {
1459 : 186261 : return s && read (d, s->offset, s->size);
1460 : : }
1461 : :
1462 : : /* Find section by name. */
1463 : : unsigned find (const char *name);
1464 : : /* Find section by index. */
1465 : : const section *find (unsigned snum, unsigned type = SHT_PROGBITS);
1466 : :
1467 : : public:
1468 : : /* Release the string table, when we're done with it. */
1469 : 7294 : void release ()
1470 : : {
1471 : 7294 : shrink (strtab);
1472 : 39 : }
1473 : :
1474 : : public:
1475 : : bool begin (location_t);
1476 : 4695 : bool end ()
1477 : : {
1478 : 4695 : release ();
1479 : : #if MAPPED_READING
1480 : 4695 : if (hdr.buffer)
1481 : 2566 : munmap (hdr.buffer, hdr.pos);
1482 : 4695 : hdr.buffer = NULL;
1483 : : #endif
1484 : 4695 : shrink (sectab);
1485 : :
1486 : 4695 : return parent::end ();
1487 : : }
1488 : :
1489 : : public:
1490 : : /* Return string name at OFFSET. Checks OFFSET range. Always
1491 : : returns non-NULL. We know offset 0 is an empty string. */
1492 : 268058 : const char *name (unsigned offset)
1493 : : {
1494 : 536116 : return &strtab.buffer[offset < strtab.size ? offset : 0];
1495 : : }
1496 : : };
1497 : :
1498 : : /* ELROND writer. */
1499 : :
1500 : : class elf_out : public elf, public data::allocator {
1501 : : typedef elf parent;
1502 : : /* Desired section alignment on disk. */
1503 : : static const int SECTION_ALIGN = 16;
1504 : :
1505 : : private:
1506 : : ptr_int_hash_map identtab; /* Map of IDENTIFIERS to strtab offsets. */
1507 : : unsigned pos; /* Write position in file. */
1508 : : #if MAPPED_WRITING
1509 : : unsigned offset; /* Offset of the mapping. */
1510 : : unsigned extent; /* Length of mapping. */
1511 : : unsigned page_size; /* System page size. */
1512 : : #endif
1513 : :
1514 : : public:
1515 : 2535 : elf_out (int fd, int e)
1516 : 4967 : :parent (fd, e), identtab (500), pos (0)
1517 : : {
1518 : : #if MAPPED_WRITING
1519 : 2535 : offset = extent = 0;
1520 : 2535 : page_size = sysconf (_SC_PAGE_SIZE);
1521 : 2535 : if (page_size < SECTION_ALIGN)
1522 : : /* Something really strange. */
1523 : 0 : set_error (EINVAL);
1524 : : #endif
1525 : 2535 : }
1526 : 2535 : ~elf_out ()
1527 : 2535 : {
1528 : 2535 : data::simple_memory.shrink (hdr);
1529 : 2535 : data::simple_memory.shrink (sectab);
1530 : 2535 : data::simple_memory.shrink (strtab);
1531 : 2535 : }
1532 : :
1533 : : #if MAPPED_WRITING
1534 : : private:
1535 : : void create_mapping (unsigned ext, bool extending = true);
1536 : : void remove_mapping ();
1537 : : #endif
1538 : :
1539 : : protected:
1540 : : using allocator::grow;
1541 : : char *grow (char *, unsigned needed) final override;
1542 : : #if MAPPED_WRITING
1543 : : using allocator::shrink;
1544 : : void shrink (char *) final override;
1545 : : #endif
1546 : :
1547 : : public:
1548 : 5048 : unsigned get_section_limit () const
1549 : : {
1550 : 5048 : return sectab.pos / sizeof (section);
1551 : : }
1552 : :
1553 : : protected:
1554 : : unsigned add (unsigned type, unsigned name = 0,
1555 : : unsigned off = 0, unsigned size = 0, unsigned flags = SHF_NONE);
1556 : : unsigned write (const data &);
1557 : : #if MAPPED_WRITING
1558 : : unsigned write (const bytes_out &);
1559 : : #endif
1560 : :
1561 : : public:
1562 : : /* IDENTIFIER to strtab offset. */
1563 : : unsigned name (tree ident);
1564 : : /* String literal to strtab offset. */
1565 : : unsigned name (const char *n);
1566 : : /* Qualified name of DECL to strtab offset. */
1567 : : unsigned qualified_name (tree decl, bool is_defn);
1568 : :
1569 : : private:
1570 : : unsigned strtab_write (const char *s, unsigned l);
1571 : : void strtab_write (tree decl, int);
1572 : :
1573 : : public:
1574 : : /* Add a section with contents or strings. */
1575 : : unsigned add (const bytes_out &, bool string_p, unsigned name);
1576 : :
1577 : : public:
1578 : : /* Begin and end writing. */
1579 : : bool begin ();
1580 : : bool end ();
1581 : : };
1582 : :
1583 : : /* Begin reading section NAME (of type PROGBITS) from SOURCE.
1584 : : Data always checked for CRC. */
1585 : :
1586 : : bool
1587 : 18555 : bytes_in::begin (location_t loc, elf_in *source, const char *name)
1588 : : {
1589 : 18555 : unsigned snum = source->find (name);
1590 : :
1591 : 18555 : return begin (loc, source, snum, name);
1592 : : }
1593 : :
1594 : : /* Begin reading section numbered SNUM with NAME (may be NULL). */
1595 : :
1596 : : bool
1597 : 183637 : bytes_in::begin (location_t loc, elf_in *source, unsigned snum, const char *name)
1598 : : {
1599 : 183637 : if (!source->read (this, source->find (snum))
1600 : 183637 : || !size || !check_crc ())
1601 : : {
1602 : 0 : source->set_error (elf::E_BAD_DATA);
1603 : 0 : source->shrink (*this);
1604 : 0 : if (name)
1605 : 0 : error_at (loc, "section %qs is missing or corrupted", name);
1606 : : else
1607 : 0 : error_at (loc, "section #%u is missing or corrupted", snum);
1608 : 0 : return false;
1609 : : }
1610 : 183637 : pos = 4;
1611 : 183637 : return true;
1612 : : }
1613 : :
1614 : : /* Finish reading a section. */
1615 : :
1616 : : bool
1617 : 182570 : bytes_in::end (elf_in *src)
1618 : : {
1619 : 182570 : if (more_p ())
1620 : 13 : set_overrun ();
1621 : 182570 : if (overrun)
1622 : 13 : src->set_error ();
1623 : :
1624 : 182570 : src->shrink (*this);
1625 : :
1626 : 182570 : return !overrun;
1627 : : }
1628 : :
1629 : : /* Begin writing buffer. */
1630 : :
1631 : : void
1632 : 244866 : bytes_out::begin (bool need_crc)
1633 : : {
1634 : 0 : if (need_crc)
1635 : 0 : pos = 4;
1636 : 0 : memory->grow (*this, 0, false);
1637 : 226441 : }
1638 : :
1639 : : /* Finish writing buffer. Stream out to SINK as named section NAME.
1640 : : Return section number or 0 on failure. If CRC_PTR is true, crc
1641 : : the data. Otherwise it is a string section. */
1642 : :
1643 : : unsigned
1644 : 244866 : bytes_out::end (elf_out *sink, unsigned name, unsigned *crc_ptr)
1645 : : {
1646 : 244866 : lengths[3] += pos;
1647 : 244866 : spans[3]++;
1648 : :
1649 : 244866 : set_crc (crc_ptr);
1650 : 244866 : unsigned sec_num = sink->add (*this, !crc_ptr, name);
1651 : 244866 : memory->shrink (*this);
1652 : :
1653 : 244866 : return sec_num;
1654 : : }
1655 : :
1656 : : /* Close and open the file, without destroying it. */
1657 : :
1658 : : void
1659 : 9 : elf_in::freeze ()
1660 : : {
1661 : 9 : gcc_checking_assert (!is_frozen ());
1662 : : #if MAPPED_READING
1663 : 9 : if (munmap (hdr.buffer, hdr.pos) < 0)
1664 : 0 : set_error (errno);
1665 : : #endif
1666 : 9 : if (close (fd) < 0)
1667 : 0 : set_error (errno);
1668 : 9 : fd = -1;
1669 : 9 : }
1670 : :
1671 : : bool
1672 : 9 : elf_in::defrost (const char *name)
1673 : : {
1674 : 9 : gcc_checking_assert (is_frozen ());
1675 : 9 : struct stat stat;
1676 : :
1677 : 9 : fd = open (name, O_RDONLY | O_CLOEXEC | O_BINARY);
1678 : 9 : if (fd < 0 || fstat (fd, &stat) < 0)
1679 : 0 : set_error (errno);
1680 : : else
1681 : : {
1682 : 9 : bool ok = hdr.pos == unsigned (stat.st_size);
1683 : : #ifndef HOST_LACKS_INODE_NUMBERS
1684 : 9 : if (device != stat.st_dev
1685 : 9 : || inode != stat.st_ino)
1686 : : ok = false;
1687 : : #endif
1688 : 9 : if (!ok)
1689 : 0 : set_error (EMFILE);
1690 : : #if MAPPED_READING
1691 : 0 : if (ok)
1692 : : {
1693 : 9 : char *mapping = reinterpret_cast<char *>
1694 : 9 : (mmap (NULL, hdr.pos, PROT_READ, MAP_SHARED, fd, 0));
1695 : 9 : if (mapping == MAP_FAILED)
1696 : 0 : fail:
1697 : 0 : set_error (errno);
1698 : : else
1699 : : {
1700 : 9 : if (madvise (mapping, hdr.pos, MADV_RANDOM))
1701 : 0 : goto fail;
1702 : :
1703 : : /* These buffers are never NULL in this case. */
1704 : 9 : strtab.buffer = mapping + strtab.pos;
1705 : 9 : sectab.buffer = mapping + sectab.pos;
1706 : 9 : hdr.buffer = mapping;
1707 : : }
1708 : : }
1709 : : #endif
1710 : : }
1711 : :
1712 : 9 : return !get_error ();
1713 : : }
1714 : :
1715 : : /* Read at current position into BUFFER. Return true on success. */
1716 : :
1717 : : const char *
1718 : 188885 : elf_in::read (data *data, unsigned pos, unsigned length)
1719 : : {
1720 : : #if MAPPED_READING
1721 : 188885 : if (pos + length > hdr.pos)
1722 : : {
1723 : 0 : set_error (EINVAL);
1724 : 0 : return NULL;
1725 : : }
1726 : : #else
1727 : : if (pos != ~0u && lseek (fd, pos, SEEK_SET) < 0)
1728 : : {
1729 : : set_error (errno);
1730 : : return NULL;
1731 : : }
1732 : : #endif
1733 : 188885 : grow (*data, length);
1734 : : #if MAPPED_READING
1735 : 188885 : data->buffer = hdr.buffer + pos;
1736 : : #else
1737 : : if (::read (fd, data->buffer, data->size) != ssize_t (length))
1738 : : {
1739 : : set_error (errno);
1740 : : shrink (*data);
1741 : : return NULL;
1742 : : }
1743 : : #endif
1744 : :
1745 : 188885 : return data->buffer;
1746 : : }
1747 : :
1748 : : /* Read section SNUM of TYPE. Return section pointer or NULL on error. */
1749 : :
1750 : : const elf::section *
1751 : 186261 : elf_in::find (unsigned snum, unsigned type)
1752 : : {
1753 : 186261 : const section *sec = get_section (snum);
1754 : 186261 : if (!snum || !sec || sec->type != type)
1755 : 0 : return NULL;
1756 : : return sec;
1757 : : }
1758 : :
1759 : : /* Find a section NAME and TYPE. Return section number, or zero on
1760 : : failure. */
1761 : :
1762 : : unsigned
1763 : 18600 : elf_in::find (const char *sname)
1764 : : {
1765 : 117781 : for (unsigned pos = sectab.size; pos -= sizeof (section); )
1766 : : {
1767 : 117781 : const section *sec
1768 : 117781 : = reinterpret_cast<const section *> (§ab.buffer[pos]);
1769 : :
1770 : 235562 : if (0 == strcmp (sname, name (sec->name)))
1771 : 18600 : return pos / sizeof (section);
1772 : : }
1773 : :
1774 : : return 0;
1775 : : }
1776 : :
1777 : : /* Begin reading file. Verify header. Pull in section and string
1778 : : tables. Return true on success. */
1779 : :
1780 : : bool
1781 : 2624 : elf_in::begin (location_t loc)
1782 : : {
1783 : 2624 : if (!parent::begin ())
1784 : : return false;
1785 : :
1786 : 2624 : struct stat stat;
1787 : 2624 : unsigned size = 0;
1788 : 2624 : if (!fstat (fd, &stat))
1789 : : {
1790 : : #if !defined (HOST_LACKS_INODE_NUMBERS)
1791 : 2624 : device = stat.st_dev;
1792 : 2624 : inode = stat.st_ino;
1793 : : #endif
1794 : : /* Never generate files > 4GB, check we've not been given one. */
1795 : 2624 : if (stat.st_size == unsigned (stat.st_size))
1796 : 2624 : size = unsigned (stat.st_size);
1797 : : }
1798 : :
1799 : : #if MAPPED_READING
1800 : : /* MAP_SHARED so that the file is backing store. If someone else
1801 : : concurrently writes it, they're wrong. */
1802 : 2624 : void *mapping = mmap (NULL, size, PROT_READ, MAP_SHARED, fd, 0);
1803 : 2624 : if (mapping == MAP_FAILED)
1804 : : {
1805 : 0 : fail:
1806 : 0 : set_error (errno);
1807 : 0 : return false;
1808 : : }
1809 : : /* We'll be hopping over this randomly. Some systems declare the
1810 : : first parm as char *, and other declare it as void *. */
1811 : 2624 : if (madvise (reinterpret_cast <char *> (mapping), size, MADV_RANDOM))
1812 : 0 : goto fail;
1813 : :
1814 : 2624 : hdr.buffer = (char *)mapping;
1815 : : #else
1816 : : read (&hdr, 0, sizeof (header));
1817 : : #endif
1818 : 2624 : hdr.pos = size; /* Record size of the file. */
1819 : :
1820 : 2624 : const header *h = reinterpret_cast<const header *> (hdr.buffer);
1821 : 2624 : if (!h)
1822 : : return false;
1823 : :
1824 : 2624 : if (h->ident.magic[0] != 0x7f
1825 : 2624 : || h->ident.magic[1] != 'E'
1826 : 2624 : || h->ident.magic[2] != 'L'
1827 : 2624 : || h->ident.magic[3] != 'F')
1828 : : {
1829 : 0 : error_at (loc, "not Encapsulated Lazy Records of Named Declarations");
1830 : 0 : failed:
1831 : 0 : shrink (hdr);
1832 : 0 : return false;
1833 : : }
1834 : :
1835 : : /* We expect a particular format -- the ELF is not intended to be
1836 : : distributable. */
1837 : 2624 : if (h->ident.klass != MY_CLASS
1838 : 2624 : || h->ident.data != MY_ENDIAN
1839 : 2624 : || h->ident.version != EV_CURRENT
1840 : 2624 : || h->type != ET_NONE
1841 : 2624 : || h->machine != EM_NONE
1842 : 2624 : || h->ident.osabi != OSABI_NONE)
1843 : : {
1844 : 0 : error_at (loc, "unexpected encapsulation format or type");
1845 : 0 : goto failed;
1846 : : }
1847 : :
1848 : 2624 : int e = -1;
1849 : 2624 : if (!h->shoff || h->shentsize != sizeof (section))
1850 : : {
1851 : 0 : malformed:
1852 : 0 : set_error (e);
1853 : 0 : error_at (loc, "encapsulation is malformed");
1854 : 0 : goto failed;
1855 : : }
1856 : :
1857 : 2624 : unsigned strndx = h->shstrndx;
1858 : 2624 : unsigned shnum = h->shnum;
1859 : 2624 : if (shnum == SHN_XINDEX)
1860 : : {
1861 : 0 : if (!read (§ab, h->shoff, sizeof (section)))
1862 : : {
1863 : 0 : section_table_fail:
1864 : 0 : e = errno;
1865 : 0 : goto malformed;
1866 : : }
1867 : 0 : shnum = get_section (0)->size;
1868 : : /* Freeing does mean we'll re-read it in the case we're not
1869 : : mapping, but this is going to be rare. */
1870 : 0 : shrink (sectab);
1871 : : }
1872 : :
1873 : 2624 : if (!shnum)
1874 : 0 : goto malformed;
1875 : :
1876 : 2624 : if (!read (§ab, h->shoff, shnum * sizeof (section)))
1877 : 0 : goto section_table_fail;
1878 : :
1879 : 2624 : if (strndx == SHN_XINDEX)
1880 : 0 : strndx = get_section (0)->link;
1881 : :
1882 : 2624 : if (!read (&strtab, find (strndx, SHT_STRTAB)))
1883 : 0 : goto malformed;
1884 : :
1885 : : /* The string table should be at least one byte, with NUL chars
1886 : : at either end. */
1887 : 2624 : if (!(strtab.size && !strtab.buffer[0]
1888 : 2624 : && !strtab.buffer[strtab.size - 1]))
1889 : 0 : goto malformed;
1890 : :
1891 : : #if MAPPED_READING
1892 : : /* Record the offsets of the section and string tables. */
1893 : 2624 : sectab.pos = h->shoff;
1894 : 2624 : strtab.pos = shnum * sizeof (section);
1895 : : #else
1896 : : shrink (hdr);
1897 : : #endif
1898 : :
1899 : 2624 : return true;
1900 : : }
1901 : :
1902 : : /* Create a new mapping. */
1903 : :
1904 : : #if MAPPED_WRITING
1905 : : void
1906 : 3156 : elf_out::create_mapping (unsigned ext, bool extending)
1907 : : {
1908 : : /* A wrapper around posix_fallocate, falling back to ftruncate
1909 : : if the underlying filesystem does not support the operation. */
1910 : 6186 : auto allocate = [](int fd, off_t offset, off_t length)
1911 : : {
1912 : : #ifdef HAVE_POSIX_FALLOCATE
1913 : 3030 : int result = posix_fallocate (fd, offset, length);
1914 : 3030 : if (result != EINVAL)
1915 : 3030 : return result == 0;
1916 : : /* Not supported by the underlying filesystem, fallback to ftruncate. */
1917 : : #endif
1918 : 0 : return ftruncate (fd, offset + length) == 0;
1919 : : };
1920 : :
1921 : 3156 : void *mapping = MAP_FAILED;
1922 : 3156 : if (extending && ext < 1024 * 1024)
1923 : : {
1924 : 2918 : if (allocate (fd, offset, ext * 2))
1925 : 2918 : mapping = mmap (NULL, ext * 2, PROT_READ | PROT_WRITE,
1926 : 2918 : MAP_SHARED, fd, offset);
1927 : 2918 : if (mapping != MAP_FAILED)
1928 : : ext *= 2;
1929 : : }
1930 : : if (mapping == MAP_FAILED)
1931 : : {
1932 : 238 : if (!extending || allocate (fd, offset, ext))
1933 : 238 : mapping = mmap (NULL, ext, PROT_READ | PROT_WRITE,
1934 : 238 : MAP_SHARED, fd, offset);
1935 : 238 : if (mapping == MAP_FAILED)
1936 : : {
1937 : 0 : set_error (errno);
1938 : : mapping = NULL;
1939 : : ext = 0;
1940 : : }
1941 : : }
1942 : 3156 : hdr.buffer = (char *)mapping;
1943 : 3156 : extent = ext;
1944 : 3156 : }
1945 : : #endif
1946 : :
1947 : : /* Flush out the current mapping. */
1948 : :
1949 : : #if MAPPED_WRITING
1950 : : void
1951 : 3162 : elf_out::remove_mapping ()
1952 : : {
1953 : 3162 : if (hdr.buffer)
1954 : : {
1955 : : /* MS_ASYNC dtrt with the removed mapping, including a
1956 : : subsequent overlapping remap. */
1957 : 3156 : if (msync (hdr.buffer, extent, MS_ASYNC)
1958 : 3156 : || munmap (hdr.buffer, extent))
1959 : : /* We're somewhat screwed at this point. */
1960 : 0 : set_error (errno);
1961 : : }
1962 : :
1963 : 3162 : hdr.buffer = NULL;
1964 : 3162 : }
1965 : : #endif
1966 : :
1967 : : /* Grow a mapping of PTR to be NEEDED bytes long. This gets
1968 : : interesting if the new size grows the EXTENT. */
1969 : :
1970 : : char *
1971 : 579748 : elf_out::grow (char *data, unsigned needed)
1972 : : {
1973 : 579748 : if (!data)
1974 : : {
1975 : : /* First allocation, check we're aligned. */
1976 : 249730 : gcc_checking_assert (!(pos & (SECTION_ALIGN - 1)));
1977 : : #if MAPPED_WRITING
1978 : 249730 : data = hdr.buffer + (pos - offset);
1979 : : #endif
1980 : : }
1981 : :
1982 : : #if MAPPED_WRITING
1983 : 579748 : unsigned off = data - hdr.buffer;
1984 : 579748 : if (off + needed > extent)
1985 : : {
1986 : : /* We need to grow the mapping. */
1987 : 604 : unsigned lwm = off & ~(page_size - 1);
1988 : 604 : unsigned hwm = (off + needed + page_size - 1) & ~(page_size - 1);
1989 : :
1990 : 604 : gcc_checking_assert (hwm > extent);
1991 : :
1992 : 604 : remove_mapping ();
1993 : :
1994 : 604 : offset += lwm;
1995 : 604 : create_mapping (extent < hwm - lwm ? hwm - lwm : extent);
1996 : :
1997 : 604 : data = hdr.buffer + (off - lwm);
1998 : : }
1999 : : #else
2000 : : data = allocator::grow (data, needed);
2001 : : #endif
2002 : :
2003 : 579748 : return data;
2004 : : }
2005 : :
2006 : : #if MAPPED_WRITING
2007 : : /* Shrinking is a NOP. */
2008 : : void
2009 : 249730 : elf_out::shrink (char *)
2010 : : {
2011 : 249730 : }
2012 : : #endif
2013 : :
2014 : : /* Write S of length L to the strtab buffer. L must include the ending
2015 : : NUL, if that's what you want. */
2016 : :
2017 : : unsigned
2018 : 1080495 : elf_out::strtab_write (const char *s, unsigned l)
2019 : : {
2020 : 1080495 : if (strtab.pos + l > strtab.size)
2021 : 1055 : data::simple_memory.grow (strtab, strtab.pos + l, false);
2022 : 1080495 : memcpy (strtab.buffer + strtab.pos, s, l);
2023 : 1080495 : unsigned res = strtab.pos;
2024 : 1080495 : strtab.pos += l;
2025 : 1080495 : return res;
2026 : : }
2027 : :
2028 : : /* Write qualified name of decl. INNER >0 if this is a definition, <0
2029 : : if this is a qualifier of an outer name. */
2030 : :
2031 : : void
2032 : 426242 : elf_out::strtab_write (tree decl, int inner)
2033 : : {
2034 : 426242 : tree ctx = CP_DECL_CONTEXT (decl);
2035 : 426242 : if (TYPE_P (ctx))
2036 : 4645 : ctx = TYPE_NAME (ctx);
2037 : 426242 : if (ctx != global_namespace)
2038 : 210005 : strtab_write (ctx, -1);
2039 : :
2040 : 426242 : tree name = DECL_NAME (decl);
2041 : 426242 : if (!name)
2042 : 348 : name = DECL_ASSEMBLER_NAME_RAW (decl);
2043 : 426242 : strtab_write (IDENTIFIER_POINTER (name), IDENTIFIER_LENGTH (name));
2044 : :
2045 : 426242 : if (inner)
2046 : 301367 : strtab_write (&"::{}"[inner+1], 2);
2047 : 426242 : }
2048 : :
2049 : : /* Map IDENTIFIER IDENT to strtab offset. Inserts into strtab if not
2050 : : already there. */
2051 : :
2052 : : unsigned
2053 : 118280 : elf_out::name (tree ident)
2054 : : {
2055 : 118280 : unsigned res = 0;
2056 : 118280 : if (ident)
2057 : : {
2058 : 118240 : bool existed;
2059 : 118240 : int *slot = &identtab.get_or_insert (ident, &existed);
2060 : 118240 : if (!existed)
2061 : 212254 : *slot = strtab_write (IDENTIFIER_POINTER (ident),
2062 : 106127 : IDENTIFIER_LENGTH (ident) + 1);
2063 : 118240 : res = *slot;
2064 : : }
2065 : 118280 : return res;
2066 : : }
2067 : :
2068 : : /* Map LITERAL to strtab offset. Does not detect duplicates and
2069 : : expects LITERAL to remain live until strtab is written out. */
2070 : :
2071 : : unsigned
2072 : 30522 : elf_out::name (const char *literal)
2073 : : {
2074 : 30522 : return strtab_write (literal, strlen (literal) + 1);
2075 : : }
2076 : :
2077 : : /* Map a DECL's qualified name to strtab offset. Does not detect
2078 : : duplicates. */
2079 : :
2080 : : unsigned
2081 : 216237 : elf_out::qualified_name (tree decl, bool is_defn)
2082 : : {
2083 : 216237 : gcc_checking_assert (DECL_P (decl) && decl != global_namespace);
2084 : 216237 : unsigned result = strtab.pos;
2085 : :
2086 : 216237 : strtab_write (decl, is_defn);
2087 : 216237 : strtab_write ("", 1);
2088 : :
2089 : 216237 : return result;
2090 : : }
2091 : :
2092 : : /* Add section to file. Return section number. TYPE & NAME identify
2093 : : the section. OFF and SIZE identify the file location of its
2094 : : data. FLAGS contains additional info. */
2095 : :
2096 : : unsigned
2097 : 249724 : elf_out::add (unsigned type, unsigned name, unsigned off, unsigned size,
2098 : : unsigned flags)
2099 : : {
2100 : 249724 : gcc_checking_assert (!(off & (SECTION_ALIGN - 1)));
2101 : 249724 : if (sectab.pos + sizeof (section) > sectab.size)
2102 : 4129 : data::simple_memory.grow (sectab, sectab.pos + sizeof (section), false);
2103 : 249724 : section *sec = reinterpret_cast<section *> (sectab.buffer + sectab.pos);
2104 : 249724 : memset (sec, 0, sizeof (section));
2105 : 249724 : sec->type = type;
2106 : 249724 : sec->flags = flags;
2107 : 249724 : sec->name = name;
2108 : 249724 : sec->offset = off;
2109 : 249724 : sec->size = size;
2110 : 249724 : if (flags & SHF_STRINGS)
2111 : 4830 : sec->entsize = 1;
2112 : :
2113 : 249724 : unsigned res = sectab.pos;
2114 : 249724 : sectab.pos += sizeof (section);
2115 : 249724 : return res / sizeof (section);
2116 : : }
2117 : :
2118 : : /* Pad to the next alignment boundary, then write BUFFER to disk.
2119 : : Return the position of the start of the write, or zero on failure. */
2120 : :
2121 : : unsigned
2122 : 9722 : elf_out::write (const data &buffer)
2123 : : {
2124 : : #if MAPPED_WRITING
2125 : : /* HDR is always mapped. */
2126 : 9722 : if (&buffer != &hdr)
2127 : : {
2128 : 4864 : bytes_out out (this);
2129 : 4864 : grow (out, buffer.pos, true);
2130 : 4864 : if (out.buffer)
2131 : 4864 : memcpy (out.buffer, buffer.buffer, buffer.pos);
2132 : 4864 : shrink (out);
2133 : 4864 : }
2134 : : else
2135 : : /* We should have been aligned during the first allocation. */
2136 : 4858 : gcc_checking_assert (!(pos & (SECTION_ALIGN - 1)));
2137 : : #else
2138 : : if (::write (fd, buffer.buffer, buffer.pos) != ssize_t (buffer.pos))
2139 : : {
2140 : : set_error (errno);
2141 : : return 0;
2142 : : }
2143 : : #endif
2144 : 9722 : unsigned res = pos;
2145 : 9722 : pos += buffer.pos;
2146 : :
2147 : 9722 : if (unsigned padding = -pos & (SECTION_ALIGN - 1))
2148 : : {
2149 : : #if !MAPPED_WRITING
2150 : : /* Align the section on disk, should help the necessary copies.
2151 : : fseeking to extend is non-portable. */
2152 : : static char zero[SECTION_ALIGN];
2153 : : if (::write (fd, &zero, padding) != ssize_t (padding))
2154 : : set_error (errno);
2155 : : #endif
2156 : 8259 : pos += padding;
2157 : : }
2158 : 9722 : return res;
2159 : : }
2160 : :
2161 : : /* Write a streaming buffer. It must be using us as an allocator. */
2162 : :
2163 : : #if MAPPED_WRITING
2164 : : unsigned
2165 : 244866 : elf_out::write (const bytes_out &buf)
2166 : : {
2167 : 244866 : gcc_checking_assert (buf.memory == this);
2168 : : /* A directly mapped buffer. */
2169 : 244866 : gcc_checking_assert (buf.buffer - hdr.buffer >= 0
2170 : : && buf.buffer - hdr.buffer + buf.size <= extent);
2171 : 244866 : unsigned res = pos;
2172 : 244866 : pos += buf.pos;
2173 : :
2174 : : /* Align up. We're not going to advance into the next page. */
2175 : 244866 : pos += -pos & (SECTION_ALIGN - 1);
2176 : :
2177 : 244866 : return res;
2178 : : }
2179 : : #endif
2180 : :
2181 : : /* Write data and add section. STRING_P is true for a string
2182 : : section, false for PROGBITS. NAME identifies the section (0 is the
2183 : : empty name). DATA is the contents. Return section number or 0 on
2184 : : failure (0 is the undef section). */
2185 : :
2186 : : unsigned
2187 : 244866 : elf_out::add (const bytes_out &data, bool string_p, unsigned name)
2188 : : {
2189 : 244866 : unsigned off = write (data);
2190 : :
2191 : 489732 : return add (string_p ? SHT_STRTAB : SHT_PROGBITS, name,
2192 : 244866 : off, data.pos, string_p ? SHF_STRINGS : SHF_NONE);
2193 : : }
2194 : :
2195 : : /* Begin writing the file. Initialize the section table and write an
2196 : : empty header. Return false on failure. */
2197 : :
2198 : : bool
2199 : 2426 : elf_out::begin ()
2200 : : {
2201 : 2426 : if (!parent::begin ())
2202 : : return false;
2203 : :
2204 : : /* Let the allocators pick a default. */
2205 : 2426 : data::simple_memory.grow (strtab, 0, false);
2206 : 2426 : data::simple_memory.grow (sectab, 0, false);
2207 : :
2208 : : /* The string table starts with an empty string. */
2209 : 2426 : name ("");
2210 : :
2211 : : /* Create the UNDEF section. */
2212 : 2426 : add (SHT_NONE);
2213 : :
2214 : : #if MAPPED_WRITING
2215 : : /* Start a mapping. */
2216 : 2426 : create_mapping (EXPERIMENT (page_size,
2217 : : (32767 + page_size) & ~(page_size - 1)));
2218 : 2426 : if (!hdr.buffer)
2219 : : return false;
2220 : : #endif
2221 : :
2222 : : /* Write an empty header. */
2223 : 2426 : grow (hdr, sizeof (header), true);
2224 : 2426 : header *h = reinterpret_cast<header *> (hdr.buffer);
2225 : 2426 : memset (h, 0, sizeof (header));
2226 : 2426 : hdr.pos = hdr.size;
2227 : 2426 : write (hdr);
2228 : 2426 : return !get_error ();
2229 : : }
2230 : :
2231 : : /* Finish writing the file. Write out the string & section tables.
2232 : : Fill in the header. Return true on error. */
2233 : :
2234 : : bool
2235 : 2535 : elf_out::end ()
2236 : : {
2237 : 2535 : if (fd >= 0)
2238 : : {
2239 : : /* Write the string table. */
2240 : 2432 : unsigned strnam = name (".strtab");
2241 : 2432 : unsigned stroff = write (strtab);
2242 : 2432 : unsigned strndx = add (SHT_STRTAB, strnam, stroff, strtab.pos,
2243 : : SHF_STRINGS);
2244 : :
2245 : : /* Store escape values in section[0]. */
2246 : 2432 : if (strndx >= SHN_LORESERVE)
2247 : : {
2248 : 0 : reinterpret_cast<section *> (sectab.buffer)->link = strndx;
2249 : 0 : strndx = SHN_XINDEX;
2250 : : }
2251 : 2432 : unsigned shnum = sectab.pos / sizeof (section);
2252 : 2432 : if (shnum >= SHN_LORESERVE)
2253 : : {
2254 : 0 : reinterpret_cast<section *> (sectab.buffer)->size = shnum;
2255 : 0 : shnum = SHN_XINDEX;
2256 : : }
2257 : :
2258 : 2432 : unsigned shoff = write (sectab);
2259 : :
2260 : : #if MAPPED_WRITING
2261 : 2432 : if (offset)
2262 : : {
2263 : 126 : remove_mapping ();
2264 : 126 : offset = 0;
2265 : 126 : create_mapping ((sizeof (header) + page_size - 1) & ~(page_size - 1),
2266 : : false);
2267 : : }
2268 : 2432 : unsigned length = pos;
2269 : : #else
2270 : : if (lseek (fd, 0, SEEK_SET) < 0)
2271 : : set_error (errno);
2272 : : #endif
2273 : : /* Write header. */
2274 : 2432 : if (!get_error ())
2275 : : {
2276 : : /* Write the correct header now. */
2277 : 2432 : header *h = reinterpret_cast<header *> (hdr.buffer);
2278 : 2432 : h->ident.magic[0] = 0x7f;
2279 : 2432 : h->ident.magic[1] = 'E'; /* Elrond */
2280 : 2432 : h->ident.magic[2] = 'L'; /* is an */
2281 : 2432 : h->ident.magic[3] = 'F'; /* elf. */
2282 : 2432 : h->ident.klass = MY_CLASS;
2283 : 2432 : h->ident.data = MY_ENDIAN;
2284 : 2432 : h->ident.version = EV_CURRENT;
2285 : 2432 : h->ident.osabi = OSABI_NONE;
2286 : 2432 : h->type = ET_NONE;
2287 : 2432 : h->machine = EM_NONE;
2288 : 2432 : h->version = EV_CURRENT;
2289 : 2432 : h->shoff = shoff;
2290 : 2432 : h->ehsize = sizeof (header);
2291 : 2432 : h->shentsize = sizeof (section);
2292 : 2432 : h->shnum = shnum;
2293 : 2432 : h->shstrndx = strndx;
2294 : :
2295 : 2432 : pos = 0;
2296 : 2432 : write (hdr);
2297 : : }
2298 : :
2299 : : #if MAPPED_WRITING
2300 : 2432 : remove_mapping ();
2301 : 2432 : if (ftruncate (fd, length))
2302 : 0 : set_error (errno);
2303 : : #endif
2304 : : }
2305 : :
2306 : 2535 : data::simple_memory.shrink (sectab);
2307 : 2535 : data::simple_memory.shrink (strtab);
2308 : :
2309 : 2535 : return parent::end ();
2310 : : }
2311 : :
2312 : : /********************************************************************/
2313 : :
2314 : : /* A dependency set. This is used during stream out to determine the
2315 : : connectivity of the graph. Every namespace-scope declaration that
2316 : : needs writing has a depset. The depset is filled with the (depsets
2317 : : of) declarations within this module that it references. For a
2318 : : declaration that'll generally be named types. For definitions
2319 : : it'll also be declarations in the body.
2320 : :
2321 : : From that we can convert the graph to a DAG, via determining the
2322 : : Strongly Connected Clusters. Each cluster is streamed
2323 : : independently, and thus we achieve lazy loading.
2324 : :
2325 : : Other decls that get a depset are namespaces themselves and
2326 : : unnameable declarations. */
2327 : :
2328 : : class depset {
2329 : : private:
2330 : : tree entity; /* Entity, or containing namespace. */
2331 : : uintptr_t discriminator; /* Flags or identifier. */
2332 : :
2333 : : public:
2334 : : /* The kinds of entity the depset could describe. The ordering is
2335 : : significant, see entity_kind_name. */
2336 : : enum entity_kind
2337 : : {
2338 : : EK_DECL, /* A decl. */
2339 : : EK_SPECIALIZATION, /* A specialization. */
2340 : : EK_PARTIAL, /* A partial specialization. */
2341 : : EK_USING, /* A using declaration (at namespace scope). */
2342 : : EK_NAMESPACE, /* A namespace. */
2343 : : EK_REDIRECT, /* Redirect to a template_decl. */
2344 : : EK_EXPLICIT_HWM,
2345 : : EK_BINDING = EK_EXPLICIT_HWM, /* Implicitly encoded. */
2346 : : EK_FOR_BINDING, /* A decl being inserted for a binding. */
2347 : : EK_INNER_DECL, /* A decl defined outside of its imported
2348 : : context. */
2349 : : EK_DIRECT_HWM = EK_PARTIAL + 1,
2350 : :
2351 : : EK_BITS = 3 /* Only need to encode below EK_EXPLICIT_HWM. */
2352 : : };
2353 : :
2354 : : private:
2355 : : /* Placement of bit fields in discriminator. */
2356 : : enum disc_bits
2357 : : {
2358 : : DB_ZERO_BIT, /* Set to disambiguate identifier from flags */
2359 : : DB_SPECIAL_BIT, /* First dep slot is special. */
2360 : : DB_KIND_BIT, /* Kind of the entity. */
2361 : : DB_KIND_BITS = EK_BITS,
2362 : : DB_DEFN_BIT = DB_KIND_BIT + DB_KIND_BITS,
2363 : : DB_IS_PENDING_BIT, /* Is a maybe-pending entity. */
2364 : : DB_TU_LOCAL_BIT, /* It is a TU-local entity. */
2365 : : DB_REFS_TU_LOCAL_BIT, /* Refers to a TU-local entity (but is not
2366 : : necessarily an exposure.) */
2367 : : DB_EXPOSURE_BIT, /* Exposes a TU-local entity. */
2368 : : DB_IMPORTED_BIT, /* An imported entity. */
2369 : : DB_UNREACHED_BIT, /* A yet-to-be reached entity. */
2370 : : DB_MAYBE_RECURSIVE_BIT, /* An entity maybe in a recursive cluster. */
2371 : : DB_ENTRY_BIT, /* The first reached recursive dep. */
2372 : : DB_HIDDEN_BIT, /* A hidden binding. */
2373 : : /* The following bits are not independent, but enumerating them is
2374 : : awkward. */
2375 : : DB_TYPE_SPEC_BIT, /* Specialization in the type table. */
2376 : : DB_FRIEND_SPEC_BIT, /* An instantiated template friend. */
2377 : : };
2378 : :
2379 : : public:
2380 : : /* The first slot is special for EK_SPECIALIZATIONS it is a
2381 : : spec_entry pointer. It is not relevant for the SCC
2382 : : determination. */
2383 : : vec<depset *> deps; /* Depsets we reference. */
2384 : :
2385 : : public:
2386 : : unsigned cluster; /* Strongly connected cluster, later entity number */
2387 : : unsigned section; /* Section written to. */
2388 : : /* During SCC construction, section is lowlink, until the depset is
2389 : : removed from the stack. See Tarjan algorithm for details. */
2390 : :
2391 : : private:
2392 : : /* Construction via factories. Destruction via hash traits. */
2393 : : depset (tree entity);
2394 : : ~depset ();
2395 : :
2396 : : public:
2397 : : static depset *make_binding (tree, tree);
2398 : : static depset *make_entity (tree, entity_kind, bool = false);
2399 : : /* Late setting a binding name -- /then/ insert into hash! */
2400 : : inline void set_binding_name (tree name)
2401 : : {
2402 : : gcc_checking_assert (!get_name ());
2403 : : discriminator = reinterpret_cast<uintptr_t> (name);
2404 : : }
2405 : :
2406 : : private:
2407 : 3338904 : template<unsigned I> void set_flag_bit ()
2408 : : {
2409 : 0 : gcc_checking_assert (I < 2 || !is_binding ());
2410 : 3338904 : discriminator |= 1u << I;
2411 : 1999810 : }
2412 : 286690 : template<unsigned I> void clear_flag_bit ()
2413 : : {
2414 : 0 : gcc_checking_assert (I < 2 || !is_binding ());
2415 : 286690 : discriminator &= ~(1u << I);
2416 : 286690 : }
2417 : 359453584 : template<unsigned I> bool get_flag_bit () const
2418 : : {
2419 : 0 : gcc_checking_assert (I < 2 || !is_binding ());
2420 : 432207657 : return bool ((discriminator >> I) & 1);
2421 : : }
2422 : :
2423 : : public:
2424 : 351998005 : bool is_binding () const
2425 : : {
2426 : 75040573 : return !get_flag_bit<DB_ZERO_BIT> ();
2427 : : }
2428 : 193409237 : entity_kind get_entity_kind () const
2429 : : {
2430 : 13525 : if (is_binding ())
2431 : : return EK_BINDING;
2432 : 147025388 : return entity_kind ((discriminator >> DB_KIND_BIT) & ((1u << EK_BITS) - 1));
2433 : : }
2434 : : const char *entity_kind_name () const;
2435 : :
2436 : : public:
2437 : 6526496 : bool has_defn () const
2438 : : {
2439 : : /* Never consider TU-local entities as having definitions, since
2440 : : we will never be accessing them from importers anyway. */
2441 : 8697656 : return get_flag_bit<DB_DEFN_BIT> () && !is_tu_local ();
2442 : : }
2443 : :
2444 : : public:
2445 : : /* This entity might be found other than by namespace-scope lookup;
2446 : : see module_state::write_pendings for more details. */
2447 : 1600358 : bool is_pending_entity () const
2448 : : {
2449 : 2574463 : return (get_entity_kind () == EK_SPECIALIZATION
2450 : 974105 : || get_entity_kind () == EK_PARTIAL
2451 : 2544117 : || (get_entity_kind () == EK_DECL
2452 : 926148 : && get_flag_bit<DB_IS_PENDING_BIT> ()));
2453 : : }
2454 : : public:
2455 : 29114140 : bool is_tu_local () const
2456 : : {
2457 : 16967428 : return get_flag_bit<DB_TU_LOCAL_BIT> ();
2458 : : }
2459 : 695203 : bool refs_tu_local () const
2460 : : {
2461 : 81054 : return get_flag_bit<DB_REFS_TU_LOCAL_BIT> ();
2462 : : }
2463 : 1068024 : bool is_exposure () const
2464 : : {
2465 : 1068024 : return get_flag_bit<DB_EXPOSURE_BIT> ();
2466 : : }
2467 : 19224155 : bool is_import () const
2468 : : {
2469 : 5975500 : return get_flag_bit<DB_IMPORTED_BIT> ();
2470 : : }
2471 : 11719493 : bool is_unreached () const
2472 : : {
2473 : 804614 : return get_flag_bit<DB_UNREACHED_BIT> ();
2474 : : }
2475 : 957434 : bool is_hidden () const
2476 : : {
2477 : 957434 : return get_flag_bit<DB_HIDDEN_BIT> ();
2478 : : }
2479 : 643534 : bool is_maybe_recursive () const
2480 : : {
2481 : 643534 : return get_flag_bit<DB_MAYBE_RECURSIVE_BIT> ();
2482 : : }
2483 : 822 : bool is_entry () const
2484 : : {
2485 : 822 : return get_flag_bit<DB_ENTRY_BIT> ();
2486 : : }
2487 : 939312 : bool is_type_spec () const
2488 : : {
2489 : 939312 : return get_flag_bit<DB_TYPE_SPEC_BIT> ();
2490 : : }
2491 : 939312 : bool is_friend_spec () const
2492 : : {
2493 : 939312 : return get_flag_bit<DB_FRIEND_SPEC_BIT> ();
2494 : : }
2495 : :
2496 : : public:
2497 : : /* We set these bit outside of depset. */
2498 : 4238 : void set_hidden_binding ()
2499 : : {
2500 : 4238 : set_flag_bit<DB_HIDDEN_BIT> ();
2501 : 4238 : }
2502 : 30 : void clear_hidden_binding ()
2503 : : {
2504 : 30 : clear_flag_bit<DB_HIDDEN_BIT> ();
2505 : 30 : }
2506 : :
2507 : : public:
2508 : 7455579 : bool is_special () const
2509 : : {
2510 : 7455579 : return get_flag_bit<DB_SPECIAL_BIT> ();
2511 : : }
2512 : 1339094 : void set_special ()
2513 : : {
2514 : 1339094 : set_flag_bit<DB_SPECIAL_BIT> ();
2515 : 0 : }
2516 : :
2517 : : public:
2518 : 88218014 : tree get_entity () const
2519 : : {
2520 : 88218014 : return entity;
2521 : : }
2522 : 14476518 : tree get_name () const
2523 : : {
2524 : 14476518 : gcc_checking_assert (is_binding ());
2525 : 14476518 : return reinterpret_cast <tree> (discriminator);
2526 : : }
2527 : :
2528 : : public:
2529 : : /* Traits for a hash table of pointers to bindings. */
2530 : : struct traits {
2531 : : /* Each entry is a pointer to a depset. */
2532 : : typedef depset *value_type;
2533 : : /* We lookup by container:maybe-identifier pair. */
2534 : : typedef std::pair<tree,tree> compare_type;
2535 : :
2536 : : static const bool empty_zero_p = true;
2537 : :
2538 : : /* hash and equality for compare_type. */
2539 : 12020210 : inline static hashval_t hash (const compare_type &p)
2540 : : {
2541 : 12020210 : hashval_t h = pointer_hash<tree_node>::hash (p.first);
2542 : 12020210 : if (p.second)
2543 : : {
2544 : 149802 : hashval_t nh = IDENTIFIER_HASH_VALUE (p.second);
2545 : 149802 : h = iterative_hash_hashval_t (h, nh);
2546 : : }
2547 : 12020210 : return h;
2548 : : }
2549 : 57936329 : inline static bool equal (const value_type b, const compare_type &p)
2550 : : {
2551 : 57936329 : if (b->entity != p.first)
2552 : : return false;
2553 : :
2554 : 8597681 : if (p.second)
2555 : 15261 : return b->discriminator == reinterpret_cast<uintptr_t> (p.second);
2556 : : else
2557 : 8582420 : return !b->is_binding ();
2558 : : }
2559 : :
2560 : : /* (re)hasher for a binding itself. */
2561 : 44959109 : inline static hashval_t hash (const value_type b)
2562 : : {
2563 : 44959109 : hashval_t h = pointer_hash<tree_node>::hash (b->entity);
2564 : 44959109 : if (b->is_binding ())
2565 : : {
2566 : 2804260 : hashval_t nh = IDENTIFIER_HASH_VALUE (b->get_name ());
2567 : 2804260 : h = iterative_hash_hashval_t (h, nh);
2568 : : }
2569 : 44959109 : return h;
2570 : : }
2571 : :
2572 : : /* Empty via NULL. */
2573 : 0 : static inline void mark_empty (value_type &p) {p = NULL;}
2574 : : static inline bool is_empty (value_type p) {return !p;}
2575 : :
2576 : : /* Nothing is deletable. Everything is insertable. */
2577 : : static bool is_deleted (value_type) { return false; }
2578 : : static void mark_deleted (value_type) { gcc_unreachable (); }
2579 : :
2580 : : /* We own the entities in the hash table. */
2581 : 1975664 : static void remove (value_type p)
2582 : : {
2583 : 1975664 : delete (p);
2584 : 1975664 : }
2585 : : };
2586 : :
2587 : : public:
2588 : : class hash : public hash_table<traits> {
2589 : : typedef traits::compare_type key_t;
2590 : : typedef hash_table<traits> parent;
2591 : :
2592 : : public:
2593 : : vec<depset *> worklist; /* Worklist of decls to walk. */
2594 : : hash *chain; /* Original table. */
2595 : : depset *current; /* Current depset being depended. */
2596 : : unsigned section; /* When writing out, the section. */
2597 : : bool reached_unreached; /* We reached an unreached entity. */
2598 : : bool writing_merge_key; /* We're writing merge key information. */
2599 : : bool ignore_tu_local; /* In a context where referencing a TU-local
2600 : : entity is not an exposure. */
2601 : :
2602 : : public:
2603 : 226424 : hash (size_t size, hash *c = NULL)
2604 : 452848 : : parent (size), chain (c), current (NULL), section (0),
2605 : 226424 : reached_unreached (false), writing_merge_key (false),
2606 : 226424 : ignore_tu_local (false)
2607 : : {
2608 : 226424 : worklist.create (size);
2609 : 226424 : }
2610 : 226424 : ~hash ()
2611 : : {
2612 : 2426 : worklist.release ();
2613 : 226424 : }
2614 : :
2615 : : public:
2616 : 67678385 : bool is_key_order () const
2617 : : {
2618 : 67678385 : return chain != NULL;
2619 : : }
2620 : :
2621 : : private:
2622 : : depset **entity_slot (tree entity, bool = true);
2623 : : depset **binding_slot (tree ctx, tree name, bool = true);
2624 : : depset *maybe_add_declaration (tree decl);
2625 : :
2626 : : public:
2627 : : depset *find_dependency (tree entity);
2628 : : depset *find_binding (tree ctx, tree name);
2629 : : depset *make_dependency (tree decl, entity_kind);
2630 : : void add_dependency (depset *);
2631 : :
2632 : : public:
2633 : : void add_mergeable (depset *);
2634 : : depset *add_dependency (tree decl, entity_kind);
2635 : : void add_namespace_context (depset *, tree ns);
2636 : :
2637 : : private:
2638 : : bool has_tu_local_tmpl_arg (tree decl, tree args, bool explain);
2639 : : bool is_tu_local_entity (tree decl, bool explain = false);
2640 : : bool is_tu_local_value (tree decl, tree expr, bool explain = false);
2641 : :
2642 : : private:
2643 : : static bool add_binding_entity (tree, WMB_Flags, void *);
2644 : :
2645 : : public:
2646 : : bool add_namespace_entities (tree ns, bitmap partitions);
2647 : : void add_specializations (bool decl_p);
2648 : : void add_partial_entities (vec<tree, va_gc> *);
2649 : : void add_class_entities (vec<tree, va_gc> *);
2650 : :
2651 : : private:
2652 : : void add_deduction_guides (tree decl);
2653 : :
2654 : : public:
2655 : : void find_dependencies (module_state *);
2656 : : bool finalize_dependencies ();
2657 : : vec<depset *> connect ();
2658 : : };
2659 : :
2660 : : public:
2661 : : struct tarjan {
2662 : : vec<depset *> result;
2663 : : vec<depset *> stack;
2664 : : unsigned index;
2665 : :
2666 : 226396 : tarjan (unsigned size)
2667 : 226396 : : index (0)
2668 : : {
2669 : 226396 : result.create (size);
2670 : 226396 : stack.create (50);
2671 : 226396 : }
2672 : 226396 : ~tarjan ()
2673 : : {
2674 : 226396 : gcc_assert (!stack.length ());
2675 : 226396 : stack.release ();
2676 : 226396 : }
2677 : :
2678 : : public:
2679 : : void connect (depset *);
2680 : : };
2681 : : };
2682 : :
2683 : : inline
2684 : 1975664 : depset::depset (tree entity)
2685 : 1975664 : :entity (entity), discriminator (0), cluster (0), section (0)
2686 : : {
2687 : 1975664 : deps.create (0);
2688 : : }
2689 : :
2690 : : inline
2691 : 1975664 : depset::~depset ()
2692 : : {
2693 : 1975664 : deps.release ();
2694 : 1975664 : }
2695 : :
2696 : : const char *
2697 : 39739 : depset::entity_kind_name () const
2698 : : {
2699 : : /* Same order as entity_kind. */
2700 : 39739 : static const char *const names[] =
2701 : : {"decl", "specialization", "partial", "using",
2702 : : "namespace", "redirect", "binding"};
2703 : 39739 : entity_kind kind = get_entity_kind ();
2704 : 39544 : gcc_checking_assert (kind < ARRAY_SIZE (names));
2705 : 39739 : return names[kind];
2706 : : }
2707 : :
2708 : : /* Create a depset for a namespace binding NS::NAME. */
2709 : :
2710 : 116551 : depset *depset::make_binding (tree ns, tree name)
2711 : : {
2712 : 116551 : depset *binding = new depset (ns);
2713 : :
2714 : 116551 : binding->discriminator = reinterpret_cast <uintptr_t> (name);
2715 : :
2716 : 116551 : return binding;
2717 : : }
2718 : :
2719 : 1859113 : depset *depset::make_entity (tree entity, entity_kind ek, bool is_defn)
2720 : : {
2721 : 1859113 : depset *r = new depset (entity);
2722 : :
2723 : 1859113 : r->discriminator = ((1 << DB_ZERO_BIT)
2724 : 1859113 : | (ek << DB_KIND_BIT)
2725 : 1859113 : | is_defn << DB_DEFN_BIT);
2726 : :
2727 : 1859113 : return r;
2728 : : }
2729 : :
2730 : : class pending_key
2731 : : {
2732 : : public:
2733 : : tree ns;
2734 : : tree id;
2735 : : };
2736 : :
2737 : : template<>
2738 : : struct default_hash_traits<pending_key>
2739 : : {
2740 : : using value_type = pending_key;
2741 : :
2742 : : static const bool empty_zero_p = false;
2743 : 31247222 : static hashval_t hash (const value_type &k)
2744 : : {
2745 : 31247222 : hashval_t h = IDENTIFIER_HASH_VALUE (k.id);
2746 : 31247222 : h = iterative_hash_hashval_t (DECL_UID (k.ns), h);
2747 : :
2748 : 31247222 : return h;
2749 : : }
2750 : 8870054 : static bool equal (const value_type &k, const value_type &l)
2751 : : {
2752 : 8870054 : return k.ns == l.ns && k.id == l.id;
2753 : : }
2754 : 174510 : static void mark_empty (value_type &k)
2755 : : {
2756 : 174510 : k.ns = k.id = NULL_TREE;
2757 : : }
2758 : 4606 : static void mark_deleted (value_type &k)
2759 : : {
2760 : 4606 : k.ns = NULL_TREE;
2761 : 4606 : gcc_checking_assert (k.id);
2762 : 4606 : }
2763 : 244398041 : static bool is_empty (const value_type &k)
2764 : : {
2765 : 244358040 : return k.ns == NULL_TREE && k.id == NULL_TREE;
2766 : : }
2767 : 14111996 : static bool is_deleted (const value_type &k)
2768 : : {
2769 : 14111996 : return k.ns == NULL_TREE && k.id != NULL_TREE;
2770 : : }
2771 : : static void remove (value_type &)
2772 : : {
2773 : : }
2774 : : };
2775 : :
2776 : : typedef hash_map<pending_key, auto_vec<unsigned>> pending_map_t;
2777 : :
2778 : : /* Not-loaded entities that are keyed to a namespace-scope
2779 : : identifier. See module_state::write_pendings for details. */
2780 : : pending_map_t *pending_table;
2781 : :
2782 : : /* Decls that need some post processing once a batch of lazy loads has
2783 : : completed. */
2784 : : vec<tree, va_heap, vl_embed> *post_load_decls;
2785 : :
2786 : : /* Some entities are keyed to another entitity for ODR purposes.
2787 : : For example, at namespace scope, 'inline auto var = []{};', that
2788 : : lambda is keyed to 'var', and follows its ODRness. */
2789 : : typedef hash_map<tree, auto_vec<tree>> keyed_map_t;
2790 : : static keyed_map_t *keyed_table;
2791 : :
2792 : : /* Instantiations of temploid friends imported from another module
2793 : : need to be attached to the same module as the temploid. This maps
2794 : : these decls to the temploid they are instantiated from, as there is
2795 : : no other easy way to get this information. */
2796 : : static GTY((cache)) decl_tree_cache_map *imported_temploid_friends;
2797 : :
2798 : : /********************************************************************/
2799 : : /* Tree streaming. The tree streaming is very specific to the tree
2800 : : structures themselves. A tag indicates the kind of tree being
2801 : : streamed. -ve tags indicate backreferences to already-streamed
2802 : : trees. Backreferences are auto-numbered. */
2803 : :
2804 : : /* Tree tags. */
2805 : : enum tree_tag {
2806 : : tt_null, /* NULL_TREE. */
2807 : : tt_tu_local, /* A TU-local entity. */
2808 : : tt_fixed, /* Fixed vector index. */
2809 : :
2810 : : tt_node, /* By-value node. */
2811 : : tt_decl, /* By-value mergeable decl. */
2812 : : tt_tpl_parm, /* Template parm. */
2813 : :
2814 : : /* The ordering of the following 5 is relied upon in
2815 : : trees_out::tree_node. */
2816 : : tt_id, /* Identifier node. */
2817 : : tt_conv_id, /* Conversion operator name. */
2818 : : tt_anon_id, /* Anonymous name. */
2819 : : tt_lambda_id, /* Lambda name. */
2820 : : tt_internal_id, /* Internal name. */
2821 : :
2822 : : tt_typedef_type, /* A (possibly implicit) typedefed type. */
2823 : : tt_derived_type, /* A type derived from another type. */
2824 : : tt_variant_type, /* A variant of another type. */
2825 : :
2826 : : tt_tinfo_var, /* Typeinfo object. */
2827 : : tt_tinfo_typedef, /* Typeinfo typedef. */
2828 : : tt_ptrmem_type, /* Pointer to member type. */
2829 : : tt_nttp_var, /* NTTP_OBJECT VAR_DECL. */
2830 : :
2831 : : tt_parm, /* Function parameter or result. */
2832 : : tt_enum_value, /* An enum value. */
2833 : : tt_enum_decl, /* An enum decl. */
2834 : : tt_data_member, /* Data member/using-decl. */
2835 : :
2836 : : tt_binfo, /* A BINFO. */
2837 : : tt_vtable, /* A vtable. */
2838 : : tt_thunk, /* A thunk. */
2839 : : tt_clone_ref,
2840 : :
2841 : : tt_entity, /* A extra-cluster entity. */
2842 : :
2843 : : tt_template, /* The TEMPLATE_RESULT of a template. */
2844 : : };
2845 : :
2846 : : enum walk_kind {
2847 : : WK_none, /* No walk to do (a back- or fixed-ref happened). */
2848 : : WK_normal, /* Normal walk (by-name if possible). */
2849 : :
2850 : : WK_value, /* By-value walk. */
2851 : : };
2852 : :
2853 : : enum merge_kind
2854 : : {
2855 : : MK_unique, /* Known unique. */
2856 : : MK_named, /* Found by CTX, NAME + maybe_arg types etc. */
2857 : : MK_field, /* Found by CTX and index on TYPE_FIELDS */
2858 : : MK_vtable, /* Found by CTX and index on TYPE_VTABLES */
2859 : : MK_as_base, /* Found by CTX. */
2860 : :
2861 : : MK_partial,
2862 : :
2863 : : MK_enum, /* Found by CTX, & 1stMemberNAME. */
2864 : : MK_keyed, /* Found by key & index. */
2865 : : MK_local_type, /* Found by CTX, index. */
2866 : :
2867 : : MK_friend_spec, /* Like named, but has a tmpl & args too. */
2868 : : MK_local_friend, /* Found by CTX, index. */
2869 : :
2870 : : MK_indirect_lwm = MK_enum,
2871 : :
2872 : : /* Template specialization kinds below. These are all found via
2873 : : primary template and specialization args. */
2874 : : MK_template_mask = 0x10, /* A template specialization. */
2875 : :
2876 : : MK_tmpl_decl_mask = 0x4, /* In decl table. */
2877 : :
2878 : : MK_tmpl_tmpl_mask = 0x1, /* We want TEMPLATE_DECL. */
2879 : :
2880 : : MK_type_spec = MK_template_mask,
2881 : : MK_decl_spec = MK_template_mask | MK_tmpl_decl_mask,
2882 : :
2883 : : MK_hwm = 0x20
2884 : : };
2885 : : /* This is more than a debugging array. NULLs are used to determine
2886 : : an invalid merge_kind number. */
2887 : : static char const *const merge_kind_name[MK_hwm] =
2888 : : {
2889 : : "unique", "named", "field", "vtable", /* 0...3 */
2890 : : "asbase", "partial", "enum", "attached", /* 4...7 */
2891 : :
2892 : : "local type", "friend spec", "local friend", NULL, /* 8...11 */
2893 : : NULL, NULL, NULL, NULL,
2894 : :
2895 : : "type spec", "type tmpl spec", /* 16,17 type (template). */
2896 : : NULL, NULL,
2897 : :
2898 : : "decl spec", "decl tmpl spec", /* 20,21 decl (template). */
2899 : : NULL, NULL,
2900 : : NULL, NULL, NULL, NULL,
2901 : : NULL, NULL, NULL, NULL,
2902 : : };
2903 : :
2904 : : /* Mergeable entity location data. */
2905 : : struct merge_key {
2906 : : cp_ref_qualifier ref_q : 2;
2907 : : unsigned index;
2908 : :
2909 : : tree ret; /* Return type, if appropriate. */
2910 : : tree args; /* Arg types, if appropriate. */
2911 : :
2912 : : tree constraints; /* Constraints. */
2913 : :
2914 : 2113764 : merge_key ()
2915 : 2113764 : :ref_q (REF_QUAL_NONE), index (0),
2916 : 2113764 : ret (NULL_TREE), args (NULL_TREE),
2917 : 2113764 : constraints (NULL_TREE)
2918 : : {
2919 : : }
2920 : : };
2921 : :
2922 : : /* Hashmap of merged duplicates. Usually decls, but can contain
2923 : : BINFOs. */
2924 : : typedef hash_map<tree,uintptr_t,
2925 : : simple_hashmap_traits<nodel_ptr_hash<tree_node>,uintptr_t> >
2926 : : duplicate_hash_map;
2927 : :
2928 : : /* Data needed for post-processing. */
2929 : : struct post_process_data {
2930 : : tree decl;
2931 : : location_t start_locus;
2932 : : location_t end_locus;
2933 : : bool returns_value;
2934 : : bool returns_null;
2935 : : bool returns_abnormally;
2936 : : bool infinite_loop;
2937 : : };
2938 : :
2939 : : /* Tree stream reader. Note that reading a stream doesn't mark the
2940 : : read trees with TREE_VISITED. Thus it's quite safe to have
2941 : : multiple concurrent readers. Which is good, because lazy
2942 : : loading.
2943 : :
2944 : : It's important that trees_in/out have internal linkage so that the
2945 : : compiler knows core_bools, lang_type_bools and lang_decl_bools have
2946 : : only a single caller (tree_node_bools) and inlines them appropriately. */
2947 : : namespace {
2948 : : class trees_in : public bytes_in {
2949 : : typedef bytes_in parent;
2950 : :
2951 : : private:
2952 : : module_state *state; /* Module being imported. */
2953 : : vec<tree> back_refs; /* Back references. */
2954 : : duplicate_hash_map *duplicates; /* Map from existings to duplicate. */
2955 : : vec<post_process_data> post_decls; /* Decls to post process. */
2956 : : unsigned unused; /* Inhibit any interior TREE_USED
2957 : : marking. */
2958 : :
2959 : : public:
2960 : : trees_in (module_state *);
2961 : : ~trees_in ();
2962 : :
2963 : : public:
2964 : : int insert (tree);
2965 : : tree back_ref (int);
2966 : :
2967 : : private:
2968 : : tree start (unsigned = 0);
2969 : :
2970 : : public:
2971 : : /* Needed for binfo writing */
2972 : : bool core_bools (tree, bits_in&);
2973 : :
2974 : : private:
2975 : : /* Stream tree_core, lang_decl_specific and lang_type_specific
2976 : : bits. */
2977 : : bool core_vals (tree);
2978 : : bool lang_type_bools (tree, bits_in&);
2979 : : bool lang_type_vals (tree);
2980 : : bool lang_decl_bools (tree, bits_in&);
2981 : : bool lang_decl_vals (tree);
2982 : : bool lang_vals (tree);
2983 : : bool tree_node_bools (tree);
2984 : : bool tree_node_vals (tree);
2985 : : tree tree_value ();
2986 : : tree decl_value ();
2987 : : tree tpl_parm_value ();
2988 : :
2989 : : private:
2990 : : tree chained_decls (); /* Follow DECL_CHAIN. */
2991 : : vec<tree, va_heap> *vec_chained_decls ();
2992 : : vec<tree, va_gc> *tree_vec (); /* vec of tree. */
2993 : : vec<tree_pair_s, va_gc> *tree_pair_vec (); /* vec of tree_pair. */
2994 : : tree tree_list (bool has_purpose);
2995 : :
2996 : : public:
2997 : : /* Read a tree node. */
2998 : : tree tree_node (bool is_use = false);
2999 : :
3000 : : private:
3001 : : bool install_entity (tree decl);
3002 : : tree tpl_parms (unsigned &tpl_levels);
3003 : : bool tpl_parms_fini (tree decl, unsigned tpl_levels);
3004 : : bool tpl_header (tree decl, unsigned *tpl_levels);
3005 : : int fn_parms_init (tree);
3006 : : void fn_parms_fini (int tag, tree fn, tree existing, bool has_defn);
3007 : : unsigned add_indirect_tpl_parms (tree);
3008 : : public:
3009 : : bool add_indirects (tree);
3010 : :
3011 : : public:
3012 : : /* Serialize various definitions. */
3013 : : bool read_definition (tree decl);
3014 : :
3015 : : private:
3016 : : void check_abi_tags (tree existing, tree decl, tree &eattr, tree &dattr);
3017 : : bool is_matching_decl (tree existing, tree decl, bool is_typedef);
3018 : : static bool install_implicit_member (tree decl);
3019 : : bool read_function_def (tree decl, tree maybe_template);
3020 : : bool read_var_def (tree decl, tree maybe_template);
3021 : : bool read_class_def (tree decl, tree maybe_template);
3022 : : bool read_enum_def (tree decl, tree maybe_template);
3023 : :
3024 : : public:
3025 : : tree decl_container ();
3026 : : tree key_mergeable (int tag, merge_kind, tree decl, tree inner, tree type,
3027 : : tree container, bool is_attached,
3028 : : bool is_imported_temploid_friend);
3029 : : unsigned binfo_mergeable (tree *);
3030 : :
3031 : : private:
3032 : : tree key_local_type (const merge_key&, tree, tree);
3033 : : uintptr_t *find_duplicate (tree existing);
3034 : : void register_duplicate (tree decl, tree existing);
3035 : : /* Mark as an already diagnosed bad duplicate. */
3036 : 48 : void unmatched_duplicate (tree existing)
3037 : : {
3038 : 96 : *find_duplicate (existing) |= 1;
3039 : 48 : }
3040 : :
3041 : : public:
3042 : 128758 : bool is_duplicate (tree decl)
3043 : : {
3044 : 257516 : return find_duplicate (decl) != NULL;
3045 : : }
3046 : 113659 : tree maybe_duplicate (tree decl)
3047 : : {
3048 : 113659 : if (uintptr_t *dup = find_duplicate (decl))
3049 : 8504 : return reinterpret_cast<tree> (*dup & ~uintptr_t (1));
3050 : : return decl;
3051 : : }
3052 : : tree odr_duplicate (tree decl, bool has_defn);
3053 : :
3054 : : public:
3055 : : /* Return the decls to postprocess. */
3056 : : const vec<post_process_data>& post_process ()
3057 : : {
3058 : : return post_decls;
3059 : : }
3060 : : private:
3061 : : /* Register DATA for postprocessing. */
3062 : 71530 : void post_process (post_process_data data)
3063 : : {
3064 : 71530 : post_decls.safe_push (data);
3065 : : }
3066 : :
3067 : : private:
3068 : : void assert_definition (tree, bool installing);
3069 : : };
3070 : : } // anon namespace
3071 : :
3072 : 168786 : trees_in::trees_in (module_state *state)
3073 : 168786 : :parent (), state (state), unused (0)
3074 : : {
3075 : 168786 : duplicates = NULL;
3076 : 168786 : back_refs.create (500);
3077 : 168786 : post_decls.create (0);
3078 : 168786 : }
3079 : :
3080 : 168786 : trees_in::~trees_in ()
3081 : : {
3082 : 263685 : delete (duplicates);
3083 : 168786 : back_refs.release ();
3084 : 168786 : post_decls.release ();
3085 : 168786 : }
3086 : :
3087 : : /* Tree stream writer. */
3088 : : namespace {
3089 : : class trees_out : public bytes_out {
3090 : : typedef bytes_out parent;
3091 : :
3092 : : private:
3093 : : module_state *state; /* The module we are writing. */
3094 : : ptr_int_hash_map tree_map; /* Trees to references */
3095 : : depset::hash *dep_hash; /* Dependency table. */
3096 : : int ref_num; /* Back reference number. */
3097 : : unsigned section;
3098 : : bool writing_local_entities; /* Whether we might walk into a TU-local
3099 : : entity we need to emit placeholders for. */
3100 : : bool walking_bit_field_unit; /* Whether we're walking the underlying
3101 : : storage for a bit field. There's no other
3102 : : great way to detect this. */
3103 : : #if CHECKING_P
3104 : : int importedness; /* Checker that imports not occurring
3105 : : inappropriately. +ve imports ok,
3106 : : -ve imports not ok. */
3107 : : #endif
3108 : :
3109 : : public:
3110 : : trees_out (allocator *, module_state *, depset::hash &deps, unsigned sec = 0);
3111 : : ~trees_out ();
3112 : :
3113 : : private:
3114 : : void mark_trees ();
3115 : : void unmark_trees ();
3116 : :
3117 : : public:
3118 : : /* Hey, let's ignore the well known STL iterator idiom. */
3119 : : void begin ();
3120 : : unsigned end (elf_out *sink, unsigned name, unsigned *crc_ptr);
3121 : : void end ();
3122 : :
3123 : : public:
3124 : : enum tags
3125 : : {
3126 : : tag_backref = -1, /* Upper bound on the backrefs. */
3127 : : tag_value = 0, /* Write by value. */
3128 : : tag_fixed /* Lower bound on the fixed trees. */
3129 : : };
3130 : :
3131 : : public:
3132 : : /* The walk is used for three similar purposes:
3133 : :
3134 : : 1. The initial scan for dependencies.
3135 : : 2. Once dependencies have been found, ordering them.
3136 : : 3. Writing dependencies to file (streaming_p).
3137 : :
3138 : : For cases where it matters, these accessers can be used to determine
3139 : : which state we're in. */
3140 : 47728133 : bool is_initial_scan () const
3141 : : {
3142 : 80556871 : return !streaming_p () && !is_key_order ();
3143 : : }
3144 : 44761892 : bool is_key_order () const
3145 : : {
3146 : 32828738 : return dep_hash->is_key_order ();
3147 : : }
3148 : :
3149 : : public:
3150 : : int insert (tree, walk_kind = WK_normal);
3151 : :
3152 : : private:
3153 : : void start (tree, bool = false);
3154 : :
3155 : : private:
3156 : : walk_kind ref_node (tree);
3157 : : public:
3158 : : int get_tag (tree);
3159 : 447996 : void set_importing (int i ATTRIBUTE_UNUSED)
3160 : : {
3161 : : #if CHECKING_P
3162 : 447996 : importedness = i;
3163 : : #endif
3164 : : }
3165 : :
3166 : : private:
3167 : : void core_bools (tree, bits_out&);
3168 : : void core_vals (tree);
3169 : : void lang_type_bools (tree, bits_out&);
3170 : : void lang_type_vals (tree);
3171 : : void lang_decl_bools (tree, bits_out&);
3172 : : void lang_decl_vals (tree);
3173 : : void lang_vals (tree);
3174 : : void tree_node_bools (tree);
3175 : : void tree_node_vals (tree);
3176 : :
3177 : : private:
3178 : : void chained_decls (tree);
3179 : : void vec_chained_decls (tree);
3180 : : void tree_vec (vec<tree, va_gc> *);
3181 : : void tree_pair_vec (vec<tree_pair_s, va_gc> *);
3182 : : void tree_list (tree, bool has_purpose);
3183 : :
3184 : : private:
3185 : : bool has_tu_local_dep (tree) const;
3186 : : tree find_tu_local_decl (tree);
3187 : :
3188 : : public:
3189 : : /* Mark a node for by-value walking. */
3190 : : void mark_by_value (tree);
3191 : :
3192 : : public:
3193 : : void tree_node (tree);
3194 : :
3195 : : private:
3196 : : void install_entity (tree decl, depset *);
3197 : : void tpl_parms (tree parms, unsigned &tpl_levels);
3198 : : void tpl_parms_fini (tree decl, unsigned tpl_levels);
3199 : : void fn_parms_fini (tree) {}
3200 : : unsigned add_indirect_tpl_parms (tree);
3201 : : public:
3202 : : void add_indirects (tree);
3203 : : void fn_parms_init (tree);
3204 : : void tpl_header (tree decl, unsigned *tpl_levels);
3205 : :
3206 : : public:
3207 : : merge_kind get_merge_kind (tree decl, depset *maybe_dep);
3208 : : tree decl_container (tree decl);
3209 : : void key_mergeable (int tag, merge_kind, tree decl, tree inner,
3210 : : tree container, depset *maybe_dep);
3211 : : void binfo_mergeable (tree binfo);
3212 : :
3213 : : private:
3214 : : void key_local_type (merge_key&, tree, tree);
3215 : : bool decl_node (tree, walk_kind ref);
3216 : : void type_node (tree);
3217 : : void tree_value (tree);
3218 : : void tpl_parm_value (tree);
3219 : :
3220 : : public:
3221 : : void decl_value (tree, depset *);
3222 : :
3223 : : public:
3224 : : /* Serialize various definitions. */
3225 : : void write_definition (tree decl, bool refs_tu_local = false);
3226 : : void mark_declaration (tree decl, bool do_defn);
3227 : :
3228 : : private:
3229 : : void mark_function_def (tree decl);
3230 : : void mark_var_def (tree decl);
3231 : : void mark_class_def (tree decl);
3232 : : void mark_enum_def (tree decl);
3233 : : void mark_class_member (tree decl, bool do_defn = true);
3234 : : void mark_binfos (tree type);
3235 : :
3236 : : private:
3237 : : void write_var_def (tree decl);
3238 : : void write_function_def (tree decl);
3239 : : void write_class_def (tree decl);
3240 : : void write_enum_def (tree decl);
3241 : :
3242 : : private:
3243 : : static void assert_definition (tree);
3244 : :
3245 : : public:
3246 : : static void instrument ();
3247 : :
3248 : : private:
3249 : : /* Tree instrumentation. */
3250 : : static unsigned tree_val_count;
3251 : : static unsigned decl_val_count;
3252 : : static unsigned back_ref_count;
3253 : : static unsigned tu_local_count;
3254 : : static unsigned null_count;
3255 : : };
3256 : : } // anon namespace
3257 : :
3258 : : /* Instrumentation counters. */
3259 : : unsigned trees_out::tree_val_count;
3260 : : unsigned trees_out::decl_val_count;
3261 : : unsigned trees_out::back_ref_count;
3262 : : unsigned trees_out::tu_local_count;
3263 : : unsigned trees_out::null_count;
3264 : :
3265 : 452865 : trees_out::trees_out (allocator *mem, module_state *state, depset::hash &deps,
3266 : 452865 : unsigned section)
3267 : 905730 : :parent (mem), state (state), tree_map (500),
3268 : 452865 : dep_hash (&deps), ref_num (0), section (section),
3269 : 452865 : writing_local_entities (false), walking_bit_field_unit (false)
3270 : : {
3271 : : #if CHECKING_P
3272 : 452865 : importedness = 0;
3273 : : #endif
3274 : 452865 : }
3275 : :
3276 : 452865 : trees_out::~trees_out ()
3277 : : {
3278 : 452865 : }
3279 : :
3280 : : /********************************************************************/
3281 : : /* Location. We're aware of the line-map concept and reproduce it
3282 : : here. Each imported module allocates a contiguous span of ordinary
3283 : : maps, and of macro maps. adhoc maps are serialized by contents,
3284 : : not pre-allocated. The scattered linemaps of a module are
3285 : : coalesced when writing. */
3286 : :
3287 : :
3288 : : /* I use half-open [first,second) ranges. */
3289 : : typedef std::pair<line_map_uint_t,line_map_uint_t> range_t;
3290 : :
3291 : : /* A range of locations. */
3292 : : typedef std::pair<location_t,location_t> loc_range_t;
3293 : :
3294 : : /* Spans of the line maps that are occupied by this TU. I.e. not
3295 : : within imports. Only extended when in an interface unit.
3296 : : Interval zero corresponds to the forced header linemap(s). This
3297 : : is a singleton object. */
3298 : :
3299 : : class loc_spans {
3300 : : public:
3301 : : /* An interval of line maps. The line maps here represent a contiguous
3302 : : non-imported range. */
3303 : 5321 : struct span {
3304 : : loc_range_t ordinary; /* Ordinary map location range. */
3305 : : loc_range_t macro; /* Macro map location range. */
3306 : : /* Add to locs to get serialized loc. */
3307 : : location_diff_t ordinary_delta;
3308 : : location_diff_t macro_delta;
3309 : : };
3310 : :
3311 : : private:
3312 : : vec<span> *spans;
3313 : : bool locs_exhausted_p;
3314 : :
3315 : : public:
3316 : : loc_spans ()
3317 : : /* Do not preallocate spans, as that causes
3318 : : --enable-detailed-mem-stats problems. */
3319 : : : spans (nullptr), locs_exhausted_p (false)
3320 : : {
3321 : : }
3322 : 95281 : ~loc_spans ()
3323 : : {
3324 : 95281 : delete spans;
3325 : 95281 : }
3326 : :
3327 : : public:
3328 : 252 : span &operator[] (unsigned ix)
3329 : : {
3330 : 504 : return (*spans)[ix];
3331 : : }
3332 : : unsigned length () const
3333 : : {
3334 : : return spans->length ();
3335 : : }
3336 : :
3337 : : public:
3338 : 13828 : bool init_p () const
3339 : : {
3340 : 13828 : return spans != nullptr;
3341 : : }
3342 : : /* Initializer. */
3343 : : void init (const line_maps *lmaps, const line_map_ordinary *map);
3344 : :
3345 : : /* Slightly skewed preprocessed files can cause us to miss an
3346 : : initialization in some places. Fallback initializer. */
3347 : 5171 : void maybe_init ()
3348 : : {
3349 : 5171 : if (!init_p ())
3350 : 6 : init (line_table, nullptr);
3351 : 5171 : }
3352 : :
3353 : : public:
3354 : : enum {
3355 : : SPAN_RESERVED = 0, /* Reserved (fixed) locations. */
3356 : : SPAN_FIRST = 1, /* LWM of locations to stream */
3357 : : SPAN_MAIN = 2 /* Main file and onwards. */
3358 : : };
3359 : :
3360 : : public:
3361 : 433506 : location_t main_start () const
3362 : : {
3363 : 433506 : return (*spans)[SPAN_MAIN].ordinary.first;
3364 : : }
3365 : :
3366 : : public:
3367 : : void open (location_t);
3368 : : void close ();
3369 : :
3370 : : public:
3371 : : /* Propagate imported linemaps to us, if needed. */
3372 : : bool maybe_propagate (module_state *import, location_t loc);
3373 : :
3374 : : public:
3375 : : /* Whether we can no longer represent new imported locations. */
3376 : 0 : bool locations_exhausted_p () const
3377 : : {
3378 : 0 : return locs_exhausted_p;
3379 : : }
3380 : 0 : void report_location_exhaustion (location_t loc)
3381 : : {
3382 : 0 : if (!locs_exhausted_p)
3383 : : {
3384 : : /* Just give the notice once. */
3385 : 0 : locs_exhausted_p = true;
3386 : 0 : inform (loc, "unable to represent further imported source locations");
3387 : : }
3388 : : }
3389 : :
3390 : : public:
3391 : : const span *ordinary (location_t);
3392 : : const span *macro (location_t);
3393 : : };
3394 : :
3395 : : static loc_spans spans;
3396 : :
3397 : : /* Information about ordinary locations we stream out. */
3398 : : struct ord_loc_info
3399 : : {
3400 : : const line_map_ordinary *src; // line map we're based on
3401 : : line_map_uint_t offset; // offset to this line
3402 : : line_map_uint_t span; // number of locs we span
3403 : : line_map_uint_t remap; // serialization
3404 : :
3405 : 195873474 : static int compare (const void *a_, const void *b_)
3406 : : {
3407 : 195873474 : auto *a = static_cast<const ord_loc_info *> (a_);
3408 : 195873474 : auto *b = static_cast<const ord_loc_info *> (b_);
3409 : :
3410 : 195873474 : if (a->src != b->src)
3411 : 43927625 : return a->src < b->src ? -1 : +1;
3412 : :
3413 : : // Ensure no overlap
3414 : 166176093 : gcc_checking_assert (a->offset + a->span <= b->offset
3415 : : || b->offset + b->span <= a->offset);
3416 : :
3417 : 166176093 : gcc_checking_assert (a->offset != b->offset);
3418 : 166176093 : return a->offset < b->offset ? -1 : +1;
3419 : : }
3420 : : };
3421 : : struct ord_loc_traits
3422 : : {
3423 : : typedef ord_loc_info value_type;
3424 : : typedef value_type compare_type;
3425 : :
3426 : : static const bool empty_zero_p = false;
3427 : :
3428 : 81961712 : static hashval_t hash (const value_type &v)
3429 : : {
3430 : 69674486 : auto h = pointer_hash<const line_map_ordinary>::hash (v.src);
3431 : 81961712 : return iterative_hash_hashval_t (v.offset, h);
3432 : : }
3433 : 90351023 : static bool equal (const value_type &v, const compare_type p)
3434 : : {
3435 : 90351023 : return v.src == p.src && v.offset == p.offset;
3436 : : }
3437 : :
3438 : 5896024 : static void mark_empty (value_type &v)
3439 : : {
3440 : 5896024 : v.src = nullptr;
3441 : : }
3442 : 262340117 : static bool is_empty (value_type &v)
3443 : : {
3444 : 262340117 : return !v.src;
3445 : : }
3446 : :
3447 : : static bool is_deleted (value_type &) { return false; }
3448 : : static void mark_deleted (value_type &) { gcc_unreachable (); }
3449 : :
3450 : 1489410 : static void remove (value_type &) {}
3451 : : };
3452 : : /* Table keyed by ord_loc_info, used for noting. */
3453 : : static hash_table<ord_loc_traits> *ord_loc_table;
3454 : : /* Sorted vector, used for writing. */
3455 : : static vec<ord_loc_info> *ord_loc_remap;
3456 : :
3457 : : /* Information about macro locations we stream out. */
3458 : : struct macro_loc_info
3459 : : {
3460 : : const line_map_macro *src; // original expansion
3461 : : line_map_uint_t remap; // serialization
3462 : :
3463 : 6768495 : static int compare (const void *a_, const void *b_)
3464 : : {
3465 : 6768495 : auto *a = static_cast<const macro_loc_info *> (a_);
3466 : 6768495 : auto *b = static_cast<const macro_loc_info *> (b_);
3467 : :
3468 : 6768495 : gcc_checking_assert (MAP_START_LOCATION (a->src)
3469 : : != MAP_START_LOCATION (b->src));
3470 : 6768495 : if (MAP_START_LOCATION (a->src) < MAP_START_LOCATION (b->src))
3471 : : return -1;
3472 : : else
3473 : 3264670 : return +1;
3474 : : }
3475 : : };
3476 : : struct macro_loc_traits
3477 : : {
3478 : : typedef macro_loc_info value_type;
3479 : : typedef const line_map_macro *compare_type;
3480 : :
3481 : : static const bool empty_zero_p = false;
3482 : :
3483 : 6802334 : static hashval_t hash (compare_type p)
3484 : : {
3485 : 6802334 : return pointer_hash<const line_map_macro>::hash (p);
3486 : : }
3487 : 5775813 : static hashval_t hash (const value_type &v)
3488 : : {
3489 : 5775813 : return hash (v.src);
3490 : : }
3491 : : static bool equal (const value_type &v, const compare_type p)
3492 : : {
3493 : : return v.src == p;
3494 : : }
3495 : :
3496 : 491174 : static void mark_empty (value_type &v)
3497 : : {
3498 : 491174 : v.src = nullptr;
3499 : : }
3500 : 21522365 : static bool is_empty (value_type &v)
3501 : : {
3502 : 21522365 : return !v.src;
3503 : : }
3504 : :
3505 : : static bool is_deleted (value_type &) { return false; }
3506 : : static void mark_deleted (value_type &) { gcc_unreachable (); }
3507 : :
3508 : 117192 : static void remove (value_type &) {}
3509 : : };
3510 : : /* Table keyed by line_map_macro, used for noting. */
3511 : : static hash_table<macro_loc_traits> *macro_loc_table;
3512 : : /* Sorted vector, used for writing. */
3513 : : static vec<macro_loc_info> *macro_loc_remap;
3514 : :
3515 : : /* Indirection to allow bsearching imports by ordinary location. */
3516 : : static vec<module_state *> *ool;
3517 : :
3518 : : /********************************************************************/
3519 : : /* Data needed by a module during the process of loading. */
3520 : : struct GTY(()) slurping {
3521 : :
3522 : : /* Remap import's module numbering to our numbering. Values are
3523 : : shifted by 1. Bit0 encodes if the import is direct. */
3524 : : vec<unsigned, va_heap, vl_embed> *
3525 : : GTY((skip)) remap; /* Module owner remapping. */
3526 : :
3527 : : elf_in *GTY((skip)) from; /* The elf loader. */
3528 : :
3529 : : /* This map is only for header imports themselves -- the global
3530 : : headers bitmap hold it for the current TU. */
3531 : : bitmap headers; /* Transitive set of direct imports, including
3532 : : self. Used for macro visibility and
3533 : : priority. */
3534 : :
3535 : : /* These objects point into the mmapped area, unless we're not doing
3536 : : that, or we got frozen or closed. In those cases they point to
3537 : : buffers we own. */
3538 : : bytes_in macro_defs; /* Macro definitions. */
3539 : : bytes_in macro_tbl; /* Macro table. */
3540 : :
3541 : : /* Location remapping. first->ordinary, second->macro. */
3542 : : range_t GTY((skip)) loc_deltas;
3543 : :
3544 : : unsigned current; /* Section currently being loaded. */
3545 : : unsigned remaining; /* Number of lazy sections yet to read. */
3546 : : unsigned lru; /* An LRU counter. */
3547 : :
3548 : : public:
3549 : : slurping (elf_in *);
3550 : : ~slurping ();
3551 : :
3552 : : public:
3553 : : /* Close the ELF file, if it's open. */
3554 : 4695 : void close ()
3555 : : {
3556 : 4695 : if (from)
3557 : : {
3558 : 2566 : from->end ();
3559 : 5132 : delete from;
3560 : 2566 : from = NULL;
3561 : : }
3562 : 4695 : }
3563 : :
3564 : : public:
3565 : : void release_macros ();
3566 : :
3567 : : public:
3568 : 2624 : void alloc_remap (unsigned size)
3569 : : {
3570 : 2624 : gcc_assert (!remap);
3571 : 2624 : vec_safe_reserve (remap, size);
3572 : 5568 : for (unsigned ix = size; ix--;)
3573 : 2944 : remap->quick_push (0);
3574 : 2624 : }
3575 : 879031 : unsigned remap_module (unsigned owner)
3576 : : {
3577 : 879031 : if (owner < remap->length ())
3578 : 879031 : return (*remap)[owner] >> 1;
3579 : : return 0;
3580 : : }
3581 : :
3582 : : public:
3583 : : /* GC allocation. But we must explicitly delete it. */
3584 : 2642 : static void *operator new (size_t x)
3585 : : {
3586 : 5284 : return ggc_alloc_atomic (x);
3587 : : }
3588 : 2566 : static void operator delete (void *p)
3589 : : {
3590 : 2566 : ggc_free (p);
3591 : 2566 : }
3592 : : };
3593 : :
3594 : 2642 : slurping::slurping (elf_in *from)
3595 : 2642 : : remap (NULL), from (from),
3596 : 2642 : headers (BITMAP_GGC_ALLOC ()), macro_defs (), macro_tbl (),
3597 : 2642 : loc_deltas (0, 0),
3598 : 2642 : current (~0u), remaining (0), lru (0)
3599 : : {
3600 : 2642 : }
3601 : :
3602 : 2566 : slurping::~slurping ()
3603 : : {
3604 : 2566 : vec_free (remap);
3605 : 2566 : remap = NULL;
3606 : 2566 : release_macros ();
3607 : 2566 : close ();
3608 : 2566 : }
3609 : :
3610 : 4695 : void slurping::release_macros ()
3611 : : {
3612 : 4695 : if (macro_defs.size)
3613 : 850 : elf_in::release (from, macro_defs);
3614 : 4695 : if (macro_tbl.size)
3615 : 0 : elf_in::release (from, macro_tbl);
3616 : 4695 : }
3617 : :
3618 : : /* Flags for extensions that end up being streamed. */
3619 : :
3620 : : enum streamed_extensions {
3621 : : SE_OPENMP_SIMD = 1 << 0,
3622 : : SE_OPENMP = 1 << 1,
3623 : : SE_OPENACC = 1 << 2,
3624 : : SE_BITS = 3
3625 : : };
3626 : :
3627 : : /* Counter indices. */
3628 : : enum module_state_counts
3629 : : {
3630 : : MSC_sec_lwm,
3631 : : MSC_sec_hwm,
3632 : : MSC_pendings,
3633 : : MSC_entities,
3634 : : MSC_namespaces,
3635 : : MSC_bindings,
3636 : : MSC_macros,
3637 : : MSC_inits,
3638 : : MSC_HWM
3639 : : };
3640 : :
3641 : : /********************************************************************/
3642 : : struct module_state_config;
3643 : :
3644 : : /* Increasing levels of loadedness. */
3645 : : enum module_loadedness {
3646 : : ML_NONE, /* Not loaded. */
3647 : : ML_CONFIG, /* Config loaed. */
3648 : : ML_PREPROCESSOR, /* Preprocessor loaded. */
3649 : : ML_LANGUAGE, /* Language loaded. */
3650 : : };
3651 : :
3652 : : /* Increasing levels of directness (toplevel) of import. */
3653 : : enum module_directness {
3654 : : MD_NONE, /* Not direct. */
3655 : : MD_PARTITION_DIRECT, /* Direct import of a partition. */
3656 : : MD_DIRECT, /* Direct import. */
3657 : : MD_PURVIEW_DIRECT, /* direct import in purview. */
3658 : : };
3659 : :
3660 : : /* State of a particular module. */
3661 : :
3662 : : class GTY((chain_next ("%h.parent"), for_user)) module_state {
3663 : : public:
3664 : : /* We always import & export ourselves. */
3665 : : bitmap imports; /* Transitive modules we're importing. */
3666 : : bitmap exports; /* Subset of that, that we're exporting. */
3667 : :
3668 : : module_state *parent;
3669 : : tree name; /* Name of the module. */
3670 : :
3671 : : slurping *slurp; /* Data for loading. */
3672 : :
3673 : : const char *flatname; /* Flatname of module. */
3674 : : char *filename; /* CMI Filename */
3675 : :
3676 : : /* Indices into the entity_ary. */
3677 : : unsigned entity_lwm;
3678 : : unsigned entity_num;
3679 : :
3680 : : /* Location ranges for this module. adhoc-locs are decomposed, so
3681 : : don't have a range. */
3682 : : loc_range_t GTY((skip)) ordinary_locs;
3683 : : loc_range_t GTY((skip)) macro_locs; // [lwm,num)
3684 : :
3685 : : /* LOC is first set too the importing location. When initially
3686 : : loaded it refers to a module loc whose parent is the importing
3687 : : location. */
3688 : : location_t loc; /* Location referring to module itself. */
3689 : : unsigned crc; /* CRC we saw reading it in. */
3690 : :
3691 : : unsigned mod; /* Module owner number. */
3692 : : unsigned remap; /* Remapping during writing. */
3693 : :
3694 : : unsigned short subst; /* Mangle subst if !0. */
3695 : :
3696 : : /* How loaded this module is. */
3697 : : enum module_loadedness loadedness : 2;
3698 : :
3699 : : bool module_p : 1; /* /The/ module of this TU. */
3700 : : bool header_p : 1; /* Is a header unit. */
3701 : : bool interface_p : 1; /* An interface. */
3702 : : bool partition_p : 1; /* A partition. */
3703 : :
3704 : : /* How directly this module is imported. */
3705 : : enum module_directness directness : 2;
3706 : :
3707 : : bool exported_p : 1; /* directness != MD_NONE && exported. */
3708 : : bool cmi_noted_p : 1; /* We've told the user about the CMI, don't
3709 : : do it again */
3710 : : bool active_init_p : 1; /* This module's global initializer needs
3711 : : calling. */
3712 : : bool inform_cmi_p : 1; /* Inform of a read/write. */
3713 : : bool visited_p : 1; /* A walk-once flag. */
3714 : : /* Record extensions emitted or permitted. */
3715 : : unsigned extensions : SE_BITS;
3716 : : /* 14 bits used, 2 bits remain */
3717 : :
3718 : : public:
3719 : : module_state (tree name, module_state *, bool);
3720 : : ~module_state ();
3721 : :
3722 : : public:
3723 : 2616 : void release ()
3724 : : {
3725 : 2616 : imports = exports = NULL;
3726 : 2616 : slurped ();
3727 : 2566 : }
3728 : 4745 : void slurped ()
3729 : : {
3730 : 4745 : delete slurp;
3731 : 4745 : slurp = NULL;
3732 : 4745 : }
3733 : 1027867 : elf_in *from () const
3734 : : {
3735 : 1027867 : return slurp->from;
3736 : : }
3737 : :
3738 : : public:
3739 : : /* Kind of this module. */
3740 : 97136 : bool is_module () const
3741 : : {
3742 : 97136 : return module_p;
3743 : : }
3744 : 1625917 : bool is_header () const
3745 : : {
3746 : 1625917 : return header_p;
3747 : : }
3748 : 513 : bool is_interface () const
3749 : : {
3750 : 513 : return interface_p;
3751 : : }
3752 : 146706 : bool is_partition () const
3753 : : {
3754 : 146706 : return partition_p;
3755 : : }
3756 : :
3757 : : /* How this module is used in the current TU. */
3758 : 33 : bool is_exported () const
3759 : : {
3760 : 33 : return exported_p;
3761 : : }
3762 : 18220 : bool is_direct () const
3763 : : {
3764 : 18220 : return directness >= MD_DIRECT;
3765 : : }
3766 : 236 : bool is_purview_direct () const
3767 : : {
3768 : 236 : return directness == MD_PURVIEW_DIRECT;
3769 : : }
3770 : 362 : bool is_partition_direct () const
3771 : : {
3772 : 362 : return directness == MD_PARTITION_DIRECT;
3773 : : }
3774 : :
3775 : : public:
3776 : : /* Is this a real module? */
3777 : 14317 : bool has_location () const
3778 : : {
3779 : 14317 : return loc != UNKNOWN_LOCATION;
3780 : : }
3781 : :
3782 : : public:
3783 : : bool check_not_purview (location_t loc);
3784 : :
3785 : : public:
3786 : : void mangle (bool include_partition);
3787 : :
3788 : : public:
3789 : : void set_import (module_state const *, bool is_export);
3790 : : void announce (const char *) const;
3791 : :
3792 : : public:
3793 : : /* Read and write module. */
3794 : : bool write_begin (elf_out *to, cpp_reader *,
3795 : : module_state_config &, unsigned &crc);
3796 : : void write_end (elf_out *to, cpp_reader *,
3797 : : module_state_config &, unsigned &crc);
3798 : : bool read_initial (cpp_reader *);
3799 : : bool read_preprocessor (bool);
3800 : : bool read_language (bool);
3801 : :
3802 : : public:
3803 : : /* Read a section. */
3804 : : bool load_section (unsigned snum, binding_slot *mslot);
3805 : : /* Lazily read a section. */
3806 : : bool lazy_load (unsigned index, binding_slot *mslot);
3807 : :
3808 : : public:
3809 : : /* Juggle a limited number of file numbers. */
3810 : : static void freeze_an_elf ();
3811 : : bool maybe_defrost ();
3812 : :
3813 : : public:
3814 : : void maybe_completed_reading ();
3815 : : bool check_read (bool outermost, bool ok);
3816 : :
3817 : : private:
3818 : : /* The README, for human consumption. */
3819 : : void write_readme (elf_out *to, cpp_reader *, const char *dialect);
3820 : : void write_env (elf_out *to);
3821 : :
3822 : : private:
3823 : : /* Import tables. */
3824 : : void write_imports (bytes_out &cfg, bool direct);
3825 : : unsigned read_imports (bytes_in &cfg, cpp_reader *, line_maps *maps);
3826 : :
3827 : : private:
3828 : : void write_imports (elf_out *to, unsigned *crc_ptr);
3829 : : bool read_imports (cpp_reader *, line_maps *);
3830 : :
3831 : : private:
3832 : : void write_partitions (elf_out *to, unsigned, unsigned *crc_ptr);
3833 : : bool read_partitions (unsigned);
3834 : :
3835 : : private:
3836 : : void write_config (elf_out *to, struct module_state_config &, unsigned crc);
3837 : : bool read_config (struct module_state_config &);
3838 : : static void write_counts (elf_out *to, unsigned [MSC_HWM], unsigned *crc_ptr);
3839 : : bool read_counts (unsigned *);
3840 : :
3841 : : public:
3842 : : void note_cmi_name ();
3843 : :
3844 : : private:
3845 : : static unsigned write_bindings (elf_out *to, vec<depset *> depsets,
3846 : : unsigned *crc_ptr);
3847 : : bool read_bindings (unsigned count, unsigned lwm, unsigned hwm);
3848 : :
3849 : : static void write_namespace (bytes_out &sec, depset *ns_dep);
3850 : : tree read_namespace (bytes_in &sec);
3851 : :
3852 : : void write_namespaces (elf_out *to, vec<depset *> spaces,
3853 : : unsigned, unsigned *crc_ptr);
3854 : : bool read_namespaces (unsigned);
3855 : :
3856 : : void intercluster_seed (trees_out &sec, unsigned index, depset *dep);
3857 : : unsigned write_cluster (elf_out *to, depset *depsets[], unsigned size,
3858 : : depset::hash &, unsigned *counts, unsigned *crc_ptr);
3859 : : bool read_cluster (unsigned snum);
3860 : :
3861 : : private:
3862 : : unsigned write_inits (elf_out *to, depset::hash &, unsigned *crc_ptr);
3863 : : bool read_inits (unsigned count);
3864 : :
3865 : : private:
3866 : : unsigned write_pendings (elf_out *to, vec<depset *> depsets,
3867 : : depset::hash &, unsigned *crc_ptr);
3868 : : bool read_pendings (unsigned count);
3869 : :
3870 : : private:
3871 : : void write_entities (elf_out *to, vec<depset *> depsets,
3872 : : unsigned count, unsigned *crc_ptr);
3873 : : bool read_entities (unsigned count, unsigned lwm, unsigned hwm);
3874 : :
3875 : : private:
3876 : : void write_init_maps ();
3877 : : range_t write_prepare_maps (module_state_config *, bool);
3878 : : bool read_prepare_maps (const module_state_config *);
3879 : :
3880 : : void write_ordinary_maps (elf_out *to, range_t &,
3881 : : bool, unsigned *crc_ptr);
3882 : : bool read_ordinary_maps (line_map_uint_t, unsigned);
3883 : : void write_macro_maps (elf_out *to, range_t &, unsigned *crc_ptr);
3884 : : bool read_macro_maps (line_map_uint_t);
3885 : :
3886 : : void write_diagnostic_classification (elf_out *, diagnostic_context *,
3887 : : unsigned *);
3888 : : bool read_diagnostic_classification (diagnostic_context *);
3889 : :
3890 : : private:
3891 : : void write_define (bytes_out &, const cpp_macro *);
3892 : : cpp_macro *read_define (bytes_in &, cpp_reader *) const;
3893 : : vec<cpp_hashnode *> *prepare_macros (cpp_reader *);
3894 : : unsigned write_macros (elf_out *to, vec<cpp_hashnode *> *, unsigned *crc_ptr);
3895 : : bool read_macros ();
3896 : : void install_macros ();
3897 : :
3898 : : public:
3899 : : void import_macros ();
3900 : :
3901 : : public:
3902 : : static void undef_macro (cpp_reader *, location_t, cpp_hashnode *);
3903 : : static cpp_macro *deferred_macro (cpp_reader *, location_t, cpp_hashnode *);
3904 : :
3905 : : public:
3906 : : static bool note_location (location_t);
3907 : : static void write_location (bytes_out &, location_t);
3908 : : location_t read_location (bytes_in &) const;
3909 : :
3910 : : public:
3911 : : void set_flatname ();
3912 : 42411 : const char *get_flatname () const
3913 : : {
3914 : 42411 : return flatname;
3915 : : }
3916 : : location_t imported_from () const;
3917 : :
3918 : : public:
3919 : : void set_filename (const Cody::Packet &);
3920 : : bool do_import (cpp_reader *, bool outermost);
3921 : : };
3922 : :
3923 : : /* Hash module state by name. This cannot be a member of
3924 : : module_state, because of GTY restrictions. We never delete from
3925 : : the hash table, but ggc_ptr_hash doesn't support that
3926 : : simplification. */
3927 : :
3928 : : struct module_state_hash : ggc_ptr_hash<module_state> {
3929 : : typedef std::pair<tree,uintptr_t> compare_type; /* {name,parent} */
3930 : :
3931 : : static inline hashval_t hash (const value_type m);
3932 : : static inline hashval_t hash (const compare_type &n);
3933 : : static inline bool equal (const value_type existing,
3934 : : const compare_type &candidate);
3935 : : };
3936 : :
3937 : 10186 : module_state::module_state (tree name, module_state *parent, bool partition)
3938 : 10186 : : imports (BITMAP_GGC_ALLOC ()), exports (BITMAP_GGC_ALLOC ()),
3939 : 10186 : parent (parent), name (name), slurp (NULL),
3940 : 10186 : flatname (NULL), filename (NULL),
3941 : 10186 : entity_lwm (~0u >> 1), entity_num (0),
3942 : 10186 : ordinary_locs (0, 0), macro_locs (0, 0),
3943 : 10186 : loc (UNKNOWN_LOCATION),
3944 : 10186 : crc (0), mod (MODULE_UNKNOWN), remap (0), subst (0)
3945 : : {
3946 : 10186 : loadedness = ML_NONE;
3947 : :
3948 : 10186 : module_p = header_p = interface_p = partition_p = false;
3949 : :
3950 : 10186 : directness = MD_NONE;
3951 : 10186 : exported_p = false;
3952 : :
3953 : 10186 : cmi_noted_p = false;
3954 : 10186 : active_init_p = false;
3955 : :
3956 : 10186 : partition_p = partition;
3957 : :
3958 : 10186 : inform_cmi_p = false;
3959 : 10186 : visited_p = false;
3960 : :
3961 : 10186 : extensions = 0;
3962 : 10186 : if (name && TREE_CODE (name) == STRING_CST)
3963 : : {
3964 : 1822 : header_p = true;
3965 : :
3966 : 1822 : const char *string = TREE_STRING_POINTER (name);
3967 : 1822 : gcc_checking_assert (string[0] == '.'
3968 : : ? IS_DIR_SEPARATOR (string[1])
3969 : : : IS_ABSOLUTE_PATH (string));
3970 : : }
3971 : :
3972 : 10186 : gcc_checking_assert (!(parent && header_p));
3973 : 10186 : }
3974 : :
3975 : 50 : module_state::~module_state ()
3976 : : {
3977 : 50 : release ();
3978 : 50 : }
3979 : :
3980 : : /* Hash module state. */
3981 : : static hashval_t
3982 : 14559 : module_name_hash (const_tree name)
3983 : : {
3984 : 14559 : if (TREE_CODE (name) == STRING_CST)
3985 : 3365 : return htab_hash_string (TREE_STRING_POINTER (name));
3986 : : else
3987 : 11194 : return IDENTIFIER_HASH_VALUE (name);
3988 : : }
3989 : :
3990 : : hashval_t
3991 : 3815 : module_state_hash::hash (const value_type m)
3992 : : {
3993 : 3815 : hashval_t ph = pointer_hash<void>::hash
3994 : 3815 : (reinterpret_cast<void *> (reinterpret_cast<uintptr_t> (m->parent)
3995 : 3815 : | m->is_partition ()));
3996 : 3815 : hashval_t nh = module_name_hash (m->name);
3997 : 3815 : return iterative_hash_hashval_t (ph, nh);
3998 : : }
3999 : :
4000 : : /* Hash a name. */
4001 : : hashval_t
4002 : 10744 : module_state_hash::hash (const compare_type &c)
4003 : : {
4004 : 10744 : hashval_t ph = pointer_hash<void>::hash (reinterpret_cast<void *> (c.second));
4005 : 10744 : hashval_t nh = module_name_hash (c.first);
4006 : :
4007 : 10744 : return iterative_hash_hashval_t (ph, nh);
4008 : : }
4009 : :
4010 : : bool
4011 : 7150 : module_state_hash::equal (const value_type existing,
4012 : : const compare_type &candidate)
4013 : : {
4014 : 7150 : uintptr_t ep = (reinterpret_cast<uintptr_t> (existing->parent)
4015 : 7150 : | existing->is_partition ());
4016 : 7150 : if (ep != candidate.second)
4017 : : return false;
4018 : :
4019 : : /* Identifier comparison is by pointer. If the string_csts happen
4020 : : to be the same object, then they're equal too. */
4021 : 5930 : if (existing->name == candidate.first)
4022 : : return true;
4023 : :
4024 : : /* If neither are string csts, they can't be equal. */
4025 : 1185 : if (TREE_CODE (candidate.first) != STRING_CST
4026 : 483 : || TREE_CODE (existing->name) != STRING_CST)
4027 : : return false;
4028 : :
4029 : : /* String equality. */
4030 : 417 : if (TREE_STRING_LENGTH (existing->name)
4031 : 417 : == TREE_STRING_LENGTH (candidate.first)
4032 : 417 : && !memcmp (TREE_STRING_POINTER (existing->name),
4033 : 414 : TREE_STRING_POINTER (candidate.first),
4034 : 414 : TREE_STRING_LENGTH (existing->name)))
4035 : 131 : return true;
4036 : :
4037 : : return false;
4038 : : }
4039 : :
4040 : : /********************************************************************/
4041 : : /* Global state */
4042 : :
4043 : : /* Mapper name. */
4044 : : static const char *module_mapper_name;
4045 : :
4046 : : /* Deferred import queue (FIFO). */
4047 : : static vec<module_state *, va_heap, vl_embed> *pending_imports;
4048 : :
4049 : : /* CMI repository path and workspace. */
4050 : : static char *cmi_repo;
4051 : : static size_t cmi_repo_length;
4052 : : static char *cmi_path;
4053 : : static size_t cmi_path_alloc;
4054 : :
4055 : : /* Count of available and loaded clusters. */
4056 : : static unsigned available_clusters;
4057 : : static unsigned loaded_clusters;
4058 : :
4059 : : /* What the current TU is. */
4060 : : unsigned module_kind;
4061 : :
4062 : : /* Global trees. */
4063 : : static const std::pair<tree *, unsigned> global_tree_arys[] =
4064 : : {
4065 : : std::pair<tree *, unsigned> (sizetype_tab, stk_type_kind_last),
4066 : : std::pair<tree *, unsigned> (integer_types, itk_none),
4067 : : std::pair<tree *, unsigned> (global_trees, TI_MODULE_HWM),
4068 : : std::pair<tree *, unsigned> (c_global_trees, CTI_MODULE_HWM),
4069 : : std::pair<tree *, unsigned> (cp_global_trees, CPTI_MODULE_HWM),
4070 : : std::pair<tree *, unsigned> (NULL, 0)
4071 : : };
4072 : : static GTY(()) vec<tree, va_gc> *fixed_trees;
4073 : : static unsigned global_crc;
4074 : :
4075 : : /* Lazy loading can open many files concurrently, there are
4076 : : per-process limits on that. We pay attention to the process limit,
4077 : : and attempt to increase it when we run out. Otherwise we use an
4078 : : LRU scheme to figure out who to flush. Note that if the import
4079 : : graph /depth/ exceeds lazy_limit, we'll exceed the limit. */
4080 : : static unsigned lazy_lru; /* LRU counter. */
4081 : : static unsigned lazy_open; /* Number of open modules */
4082 : : static unsigned lazy_limit; /* Current limit of open modules. */
4083 : : static unsigned lazy_hard_limit; /* Hard limit on open modules. */
4084 : : /* Account for source, assembler and dump files & directory searches.
4085 : : We don't keep the source file's open, so we don't have to account
4086 : : for #include depth. I think dump files are opened and closed per
4087 : : pass, but ICBW. */
4088 : : #define LAZY_HEADROOM 15 /* File descriptor headroom. */
4089 : :
4090 : : /* Vector of module state. Indexed by OWNER. Index 0 is reserved for the
4091 : : current TU; imports start at 1. */
4092 : : static GTY(()) vec<module_state *, va_gc> *modules;
4093 : :
4094 : : /* Hash of module state, findable by {name, parent}. */
4095 : : static GTY(()) hash_table<module_state_hash> *modules_hash;
4096 : :
4097 : : /* Map of imported entities. We map DECL_UID to index of entity
4098 : : vector. */
4099 : : typedef hash_map<unsigned/*UID*/, unsigned/*index*/,
4100 : : simple_hashmap_traits<int_hash<unsigned,0>, unsigned>
4101 : : > entity_map_t;
4102 : : static entity_map_t *entity_map;
4103 : : /* Doesn't need GTYing, because any tree referenced here is also
4104 : : findable by, symbol table, specialization table, return type of
4105 : : reachable function. */
4106 : : static vec<binding_slot, va_heap, vl_embed> *entity_ary;
4107 : :
4108 : : /* Members entities of imported classes that are defined in this TU.
4109 : : These are where the entity's context is not from the current TU.
4110 : : We need to emit the definition (but not the enclosing class).
4111 : :
4112 : : We could find these by walking ALL the imported classes that we
4113 : : could provide a member definition. But that's expensive,
4114 : : especially when you consider lazy implicit member declarations,
4115 : : which could be ANY imported class. */
4116 : : static GTY(()) vec<tree, va_gc> *class_members;
4117 : :
4118 : : /* The same problem exists for class template partial
4119 : : specializations. Now that we have constraints, the invariant of
4120 : : expecting them in the instantiation table no longer holds. One of
4121 : : the constrained partial specializations will be there, but the
4122 : : others not so much. It's not even an unconstrained partial
4123 : : spacialization in the table :( so any partial template declaration
4124 : : is added to this list too. */
4125 : : static GTY(()) vec<tree, va_gc> *partial_specializations;
4126 : :
4127 : : /********************************************************************/
4128 : :
4129 : : /* Our module mapper (created lazily). */
4130 : : module_client *mapper;
4131 : :
4132 : : static module_client *make_mapper (location_t loc, class mkdeps *deps);
4133 : 30086 : inline module_client *get_mapper (location_t loc, class mkdeps *deps)
4134 : : {
4135 : 30086 : auto *res = mapper;
4136 : 278 : if (!res)
4137 : 4318 : res = make_mapper (loc, deps);
4138 : 30086 : return res;
4139 : : }
4140 : :
4141 : : /********************************************************************/
4142 : : static tree
4143 : 256230 : get_clone_target (tree decl)
4144 : : {
4145 : 256230 : tree target;
4146 : :
4147 : 256230 : if (TREE_CODE (decl) == TEMPLATE_DECL)
4148 : : {
4149 : 28312 : tree res_orig = DECL_CLONED_FUNCTION (DECL_TEMPLATE_RESULT (decl));
4150 : :
4151 : 28312 : target = DECL_TI_TEMPLATE (res_orig);
4152 : : }
4153 : : else
4154 : 227918 : target = DECL_CLONED_FUNCTION (decl);
4155 : :
4156 : 256230 : gcc_checking_assert (DECL_MAYBE_IN_CHARGE_CDTOR_P (target));
4157 : :
4158 : 256230 : return target;
4159 : : }
4160 : :
4161 : : /* Like FOR_EACH_CLONE, but will walk cloned templates. */
4162 : : #define FOR_EVERY_CLONE(CLONE, FN) \
4163 : : if (!DECL_MAYBE_IN_CHARGE_CDTOR_P (FN)); \
4164 : : else \
4165 : : for (CLONE = DECL_CHAIN (FN); \
4166 : : CLONE && DECL_CLONED_FUNCTION_P (CLONE); \
4167 : : CLONE = DECL_CHAIN (CLONE))
4168 : :
4169 : : /* It'd be nice if USE_TEMPLATE was a field of template_info
4170 : : (a) it'd solve the enum case dealt with below,
4171 : : (b) both class templates and decl templates would store this in the
4172 : : same place
4173 : : (c) this function wouldn't need the by-ref arg, which is annoying. */
4174 : :
4175 : : static tree
4176 : 175441720 : node_template_info (tree decl, int &use)
4177 : : {
4178 : 175441720 : tree ti = NULL_TREE;
4179 : 175441720 : int use_tpl = -1;
4180 : 175441720 : if (DECL_IMPLICIT_TYPEDEF_P (decl))
4181 : : {
4182 : 18799262 : tree type = TREE_TYPE (decl);
4183 : :
4184 : 18799262 : ti = TYPE_TEMPLATE_INFO (type);
4185 : 18799262 : if (ti)
4186 : : {
4187 : 3317678 : if (TYPE_LANG_SPECIFIC (type))
4188 : 3312439 : use_tpl = CLASSTYPE_USE_TEMPLATE (type);
4189 : : else
4190 : : {
4191 : : /* An enum, where we don't explicitly encode use_tpl.
4192 : : If the containing context (a type or a function), is
4193 : : an ({im,ex}plicit) instantiation, then this is too.
4194 : : If it's a partial or explicit specialization, then
4195 : : this is not!. */
4196 : 5239 : tree ctx = CP_DECL_CONTEXT (decl);
4197 : 5239 : if (TYPE_P (ctx))
4198 : 5129 : ctx = TYPE_NAME (ctx);
4199 : 5239 : node_template_info (ctx, use);
4200 : 5239 : use_tpl = use != 2 ? use : 0;
4201 : : }
4202 : : }
4203 : : }
4204 : 156642458 : else if (DECL_LANG_SPECIFIC (decl)
4205 : 156642458 : && (VAR_P (decl)
4206 : : || TREE_CODE (decl) == TYPE_DECL
4207 : : || TREE_CODE (decl) == FUNCTION_DECL
4208 : : || TREE_CODE (decl) == FIELD_DECL
4209 : : || TREE_CODE (decl) == CONCEPT_DECL
4210 : : || TREE_CODE (decl) == TEMPLATE_DECL))
4211 : : {
4212 : 82586617 : use_tpl = DECL_USE_TEMPLATE (decl);
4213 : 82586617 : ti = DECL_TEMPLATE_INFO (decl);
4214 : : }
4215 : :
4216 : 175441720 : use = use_tpl;
4217 : 175441720 : return ti;
4218 : : }
4219 : :
4220 : : /* Find the index in entity_ary for an imported DECL. It should
4221 : : always be there, but bugs can cause it to be missing, and that can
4222 : : crash the crash reporting -- let's not do that! When streaming
4223 : : out we place entities from this module there too -- with negated
4224 : : indices. */
4225 : :
4226 : : static unsigned
4227 : 1034845 : import_entity_index (tree decl, bool null_ok = false)
4228 : : {
4229 : 1034845 : if (unsigned *slot = entity_map->get (DECL_UID (decl)))
4230 : 1034800 : return *slot;
4231 : :
4232 : 45 : gcc_checking_assert (null_ok);
4233 : : return ~(~0u >> 1);
4234 : : }
4235 : :
4236 : : /* Find the module for an imported entity at INDEX in the entity ary.
4237 : : There must be one. */
4238 : :
4239 : : static module_state *
4240 : 38092 : import_entity_module (unsigned index)
4241 : : {
4242 : 38092 : if (index > ~(~0u >> 1))
4243 : : /* This is an index for an exported entity. */
4244 : 18 : return (*modules)[0];
4245 : :
4246 : : /* Do not include the current TU (not an off-by-one error). */
4247 : 38074 : unsigned pos = 1;
4248 : 38074 : unsigned len = modules->length () - pos;
4249 : 83656 : while (len)
4250 : : {
4251 : 45582 : unsigned half = len / 2;
4252 : 45582 : module_state *probe = (*modules)[pos + half];
4253 : 45582 : if (index < probe->entity_lwm)
4254 : : len = half;
4255 : 38192 : else if (index < probe->entity_lwm + probe->entity_num)
4256 : : return probe;
4257 : : else
4258 : : {
4259 : 118 : pos += half + 1;
4260 : 118 : len = len - (half + 1);
4261 : : }
4262 : : }
4263 : 0 : gcc_unreachable ();
4264 : : }
4265 : :
4266 : :
4267 : : /********************************************************************/
4268 : : /* A dumping machinery. */
4269 : :
4270 : : class dumper {
4271 : : public:
4272 : : enum {
4273 : : LOCATION = TDF_LINENO, /* -lineno:Source location streaming. */
4274 : : DEPEND = TDF_GRAPH, /* -graph:Dependency graph construction. */
4275 : : CLUSTER = TDF_BLOCKS, /* -blocks:Clusters. */
4276 : : TREE = TDF_UID, /* -uid:Tree streaming. */
4277 : : MERGE = TDF_ALIAS, /* -alias:Mergeable Entities. */
4278 : : ELF = TDF_ASMNAME, /* -asmname:Elf data. */
4279 : : MACRO = TDF_VOPS /* -vops:Macros. */
4280 : : };
4281 : :
4282 : : private:
4283 : : struct impl {
4284 : : typedef vec<module_state *, va_heap, vl_embed> stack_t;
4285 : :
4286 : : FILE *stream; /* Dump stream. */
4287 : : unsigned indent; /* Local indentation. */
4288 : : bool bol; /* Beginning of line. */
4289 : : stack_t stack; /* Trailing array of module_state. */
4290 : :
4291 : : bool nested_name (tree); /* Dump a name following DECL_CONTEXT. */
4292 : : };
4293 : :
4294 : : public:
4295 : : /* The dumper. */
4296 : : impl *dumps;
4297 : : dump_flags_t flags;
4298 : :
4299 : : public:
4300 : : /* Push/pop module state dumping. */
4301 : : unsigned push (module_state *);
4302 : : void pop (unsigned);
4303 : :
4304 : : public:
4305 : : /* Change local indentation. */
4306 : 278104423 : void indent ()
4307 : : {
4308 : 282 : if (dumps)
4309 : 506748 : dumps->indent++;
4310 : : }
4311 : 278104423 : void outdent ()
4312 : : {
4313 : 278104423 : if (dumps)
4314 : : {
4315 : 506748 : gcc_checking_assert (dumps->indent);
4316 : 506748 : dumps->indent--;
4317 : : }
4318 : 278104423 : }
4319 : :
4320 : : public:
4321 : : /* Is dump enabled?. */
4322 : 171295267 : bool operator () (int mask = 0)
4323 : : {
4324 : 3529123 : if (!dumps || !dumps->stream)
4325 : : return false;
4326 : 382859 : if (mask && !(mask & flags))
4327 : 3641 : return false;
4328 : : return true;
4329 : : }
4330 : : /* Dump some information. */
4331 : : bool operator () (const char *, ...);
4332 : : };
4333 : :
4334 : : /* The dumper. */
4335 : : static dumper dump = {0, dump_flags_t (0)};
4336 : :
4337 : : /* Push to dumping M. Return previous indentation level. */
4338 : :
4339 : : unsigned
4340 : 87654 : dumper::push (module_state *m)
4341 : : {
4342 : 87654 : FILE *stream = NULL;
4343 : 87654 : if (!dumps || !dumps->stack.length ())
4344 : : {
4345 : 86528 : stream = dump_begin (module_dump_id, &flags);
4346 : 86528 : if (!stream)
4347 : : return 0;
4348 : : }
4349 : :
4350 : 6727 : if (!dumps || !dumps->stack.space (1))
4351 : : {
4352 : : /* Create or extend the dump implementor. */
4353 : 1095 : unsigned current = dumps ? dumps->stack.length () : 0;
4354 : 578 : unsigned count = current ? current * 2 : EXPERIMENT (1, 20);
4355 : 1095 : size_t alloc = (offsetof (impl, stack)
4356 : 1095 : + impl::stack_t::embedded_size (count));
4357 : 1095 : dumps = XRESIZEVAR (impl, dumps, alloc);
4358 : 1095 : dumps->stack.embedded_init (count, current);
4359 : : }
4360 : 6727 : if (stream)
4361 : 5601 : dumps->stream = stream;
4362 : :
4363 : 6727 : unsigned n = dumps->indent;
4364 : 6727 : dumps->indent = 0;
4365 : 6727 : dumps->bol = true;
4366 : 6727 : dumps->stack.quick_push (m);
4367 : 6727 : if (m)
4368 : : {
4369 : 1796 : module_state *from = NULL;
4370 : :
4371 : 1796 : if (dumps->stack.length () > 1)
4372 : 573 : from = dumps->stack[dumps->stack.length () - 2];
4373 : : else
4374 : 1223 : dump ("");
4375 : 3257 : dump (from ? "Starting module %M (from %M)"
4376 : : : "Starting module %M", m, from);
4377 : : }
4378 : :
4379 : : return n;
4380 : : }
4381 : :
4382 : : /* Pop from dumping. Restore indentation to N. */
4383 : :
4384 : 87614 : void dumper::pop (unsigned n)
4385 : : {
4386 : 87614 : if (!dumps)
4387 : : return;
4388 : :
4389 : 13454 : gcc_checking_assert (dump () && !dumps->indent);
4390 : 6727 : if (module_state *m = dumps->stack[dumps->stack.length () - 1])
4391 : : {
4392 : 1796 : module_state *from = (dumps->stack.length () > 1
4393 : 1796 : ? dumps->stack[dumps->stack.length () - 2] : NULL);
4394 : 2034 : dump (from ? "Finishing module %M (returning to %M)"
4395 : : : "Finishing module %M", m, from);
4396 : : }
4397 : 6727 : dumps->stack.pop ();
4398 : 6727 : dumps->indent = n;
4399 : 6727 : if (!dumps->stack.length ())
4400 : : {
4401 : 5601 : dump_end (module_dump_id, dumps->stream);
4402 : 5601 : dumps->stream = NULL;
4403 : : }
4404 : : }
4405 : :
4406 : : /* Dump a nested name for arbitrary tree T. Sometimes it won't have a
4407 : : name. */
4408 : :
4409 : : bool
4410 : 470002 : dumper::impl::nested_name (tree t)
4411 : : {
4412 : 470002 : tree ti = NULL_TREE;
4413 : 470002 : int origin = -1;
4414 : 470002 : tree name = NULL_TREE;
4415 : :
4416 : 470002 : if (t && TREE_CODE (t) == TU_LOCAL_ENTITY)
4417 : 0 : t = TU_LOCAL_ENTITY_NAME (t);
4418 : :
4419 : 469978 : if (t && TREE_CODE (t) == TREE_BINFO)
4420 : 384 : t = BINFO_TYPE (t);
4421 : :
4422 : 470002 : if (t && TYPE_P (t))
4423 : 234062 : t = TYPE_NAME (t);
4424 : :
4425 : 469966 : if (t && DECL_P (t))
4426 : : {
4427 : 397071 : if (t == global_namespace || DECL_TEMPLATE_PARM_P (t))
4428 : : ;
4429 : 366279 : else if (tree ctx = DECL_CONTEXT (t))
4430 : 280444 : if (TREE_CODE (ctx) == TRANSLATION_UNIT_DECL
4431 : 280444 : || nested_name (ctx))
4432 : 280444 : fputs ("::", stream);
4433 : :
4434 : 397071 : int use_tpl;
4435 : 397071 : ti = node_template_info (t, use_tpl);
4436 : 127846 : if (ti && TREE_CODE (TI_TEMPLATE (ti)) == TEMPLATE_DECL
4437 : 524875 : && (DECL_TEMPLATE_RESULT (TI_TEMPLATE (ti)) == t))
4438 : : t = TI_TEMPLATE (ti);
4439 : 397071 : tree not_tmpl = t;
4440 : 397071 : if (TREE_CODE (t) == TEMPLATE_DECL)
4441 : : {
4442 : 22152 : fputs ("template ", stream);
4443 : 22152 : not_tmpl = DECL_TEMPLATE_RESULT (t);
4444 : : }
4445 : :
4446 : 22152 : if (not_tmpl
4447 : 397067 : && DECL_P (not_tmpl)
4448 : 397067 : && DECL_LANG_SPECIFIC (not_tmpl)
4449 : 232046 : && DECL_MODULE_IMPORT_P (not_tmpl))
4450 : : {
4451 : : /* We need to be careful here, so as to not explode on
4452 : : inconsistent data -- we're probably debugging, because
4453 : : Something Is Wrong. */
4454 : 17596 : unsigned index = import_entity_index (t, true);
4455 : 17596 : if (!(index & ~(~0u >> 1)))
4456 : 17038 : origin = import_entity_module (index)->mod;
4457 : 558 : else if (index > ~(~0u >> 1))
4458 : : /* An imported partition member that we're emitting. */
4459 : : origin = 0;
4460 : : else
4461 : 45 : origin = -2;
4462 : : }
4463 : :
4464 : 400127 : name = DECL_NAME (t) ? DECL_NAME (t)
4465 : 4129 : : HAS_DECL_ASSEMBLER_NAME_P (t) ? DECL_ASSEMBLER_NAME_RAW (t)
4466 : : : NULL_TREE;
4467 : : }
4468 : : else
4469 : : name = t;
4470 : :
4471 : 397071 : if (name)
4472 : 439473 : switch (TREE_CODE (name))
4473 : : {
4474 : 13445 : default:
4475 : 13445 : fputs ("#unnamed#", stream);
4476 : 13445 : break;
4477 : :
4478 : 406600 : case IDENTIFIER_NODE:
4479 : 406600 : fwrite (IDENTIFIER_POINTER (name), 1, IDENTIFIER_LENGTH (name), stream);
4480 : 406600 : break;
4481 : :
4482 : 19336 : case INTEGER_CST:
4483 : 19336 : print_hex (wi::to_wide (name), stream);
4484 : 19336 : break;
4485 : :
4486 : 92 : case STRING_CST:
4487 : : /* If TREE_TYPE is NULL, this is a raw string. */
4488 : 184 : fwrite (TREE_STRING_POINTER (name), 1,
4489 : 92 : TREE_STRING_LENGTH (name) - (TREE_TYPE (name) != NULL_TREE),
4490 : : stream);
4491 : 92 : break;
4492 : : }
4493 : : else
4494 : 30529 : fputs ("#null#", stream);
4495 : :
4496 : 470002 : if (origin >= 0)
4497 : : {
4498 : 17551 : const module_state *module = (*modules)[origin];
4499 : 35102 : fprintf (stream, "@%s:%d", !module ? "" : !module->name ? "(unnamed)"
4500 : 17551 : : module->get_flatname (), origin);
4501 : : }
4502 : 452451 : else if (origin == -2)
4503 : 45 : fprintf (stream, "@???");
4504 : :
4505 : 470002 : if (ti)
4506 : : {
4507 : 127846 : tree args = INNERMOST_TEMPLATE_ARGS (TI_ARGS (ti));
4508 : 127846 : fputs ("<", stream);
4509 : 127846 : if (args)
4510 : 320661 : for (int ix = 0; ix != TREE_VEC_LENGTH (args); ix++)
4511 : : {
4512 : 192815 : if (ix)
4513 : 64969 : fputs (",", stream);
4514 : 192815 : nested_name (TREE_VEC_ELT (args, ix));
4515 : : }
4516 : 127846 : fputs (">", stream);
4517 : : }
4518 : :
4519 : 470002 : return true;
4520 : : }
4521 : :
4522 : : /* Formatted dumping. FORMAT begins with '+' do not emit a trailing
4523 : : new line. (Normally it is appended.)
4524 : : Escapes:
4525 : : %C - tree_code
4526 : : %I - identifier
4527 : : %K - location_t or line_map_uint_t
4528 : : %M - module_state
4529 : : %N - name -- DECL_NAME
4530 : : %P - context:name pair
4531 : : %R - unsigned:unsigned ratio
4532 : : %S - symbol -- DECL_ASSEMBLER_NAME
4533 : : %U - long unsigned
4534 : : %V - version
4535 : : --- the following are printf-like, but without its flexibility
4536 : : %d - decimal int
4537 : : %p - pointer
4538 : : %s - string
4539 : : %u - unsigned int
4540 : : %x - hex int
4541 : :
4542 : : We do not implement the printf modifiers. */
4543 : :
4544 : : bool
4545 : 398047 : dumper::operator () (const char *format, ...)
4546 : : {
4547 : 398047 : if (!(*this) ())
4548 : : return false;
4549 : :
4550 : 342249 : bool no_nl = format[0] == '+';
4551 : 342249 : format += no_nl;
4552 : :
4553 : 342249 : if (dumps->bol)
4554 : : {
4555 : : /* Module import indent. */
4556 : 178360 : if (unsigned depth = dumps->stack.length () - 1)
4557 : : {
4558 : 21231 : const char *prefix = ">>>>";
4559 : 42444 : fprintf (dumps->stream, (depth <= strlen (prefix)
4560 : 21213 : ? &prefix[strlen (prefix) - depth]
4561 : : : ">.%d.>"), depth);
4562 : : }
4563 : :
4564 : : /* Local indent. */
4565 : 178360 : if (unsigned indent = dumps->indent)
4566 : : {
4567 : 96456 : const char *prefix = " ";
4568 : 187924 : fprintf (dumps->stream, (indent <= strlen (prefix)
4569 : 91468 : ? &prefix[strlen (prefix) - indent]
4570 : : : " .%d. "), indent);
4571 : : }
4572 : 178360 : dumps->bol = false;
4573 : : }
4574 : :
4575 : 342249 : va_list args;
4576 : 342249 : va_start (args, format);
4577 : 1011002 : while (const char *esc = strchr (format, '%'))
4578 : : {
4579 : 668753 : fwrite (format, 1, (size_t)(esc - format), dumps->stream);
4580 : 668753 : format = ++esc;
4581 : 668753 : switch (*format++)
4582 : : {
4583 : 0 : default:
4584 : 0 : gcc_unreachable ();
4585 : :
4586 : 517 : case '%':
4587 : 517 : fputc ('%', dumps->stream);
4588 : 517 : break;
4589 : :
4590 : 104569 : case 'C': /* Code */
4591 : 104569 : {
4592 : 104569 : tree_code code = (tree_code)va_arg (args, unsigned);
4593 : 104569 : fputs (get_tree_code_name (code), dumps->stream);
4594 : : }
4595 : 104569 : break;
4596 : :
4597 : 81 : case 'I': /* Identifier. */
4598 : 81 : {
4599 : 81 : tree t = va_arg (args, tree);
4600 : 81 : dumps->nested_name (t);
4601 : : }
4602 : 81 : break;
4603 : :
4604 : 4074 : case 'K': /* location_t, either 32- or 64-bit. */
4605 : 4074 : {
4606 : 4074 : unsigned long long u = va_arg (args, location_t);
4607 : 4074 : fprintf (dumps->stream, "%llu", u);
4608 : : }
4609 : 4074 : break;
4610 : :
4611 : 6927 : case 'M': /* Module. */
4612 : 6927 : {
4613 : 6927 : const char *str = "(none)";
4614 : 6927 : if (module_state *m = va_arg (args, module_state *))
4615 : : {
4616 : 6927 : if (!m->has_location ())
4617 : : str = "(detached)";
4618 : : else
4619 : 6927 : str = m->get_flatname ();
4620 : : }
4621 : 6927 : fputs (str, dumps->stream);
4622 : : }
4623 : 6927 : break;
4624 : :
4625 : 114395 : case 'N': /* Name. */
4626 : 114395 : {
4627 : 114395 : tree t = va_arg (args, tree);
4628 : 229270 : while (t && TREE_CODE (t) == OVERLOAD)
4629 : 480 : t = OVL_FUNCTION (t);
4630 : 114395 : fputc ('\'', dumps->stream);
4631 : 114395 : dumps->nested_name (t);
4632 : 114395 : fputc ('\'', dumps->stream);
4633 : : }
4634 : 114395 : break;
4635 : :
4636 : 6505 : case 'P': /* Pair. */
4637 : 6505 : {
4638 : 6505 : tree ctx = va_arg (args, tree);
4639 : 6505 : tree name = va_arg (args, tree);
4640 : 6505 : fputc ('\'', dumps->stream);
4641 : 6505 : dumps->nested_name (ctx);
4642 : 6505 : if (ctx && ctx != global_namespace)
4643 : 768 : fputs ("::", dumps->stream);
4644 : 6505 : dumps->nested_name (name);
4645 : 6505 : fputc ('\'', dumps->stream);
4646 : : }
4647 : 6505 : break;
4648 : :
4649 : 756 : case 'R': /* Ratio */
4650 : 756 : {
4651 : 756 : unsigned a = va_arg (args, unsigned);
4652 : 756 : unsigned b = va_arg (args, unsigned);
4653 : 756 : fprintf (dumps->stream, "%.1f", (float) a / (b + !b));
4654 : : }
4655 : 756 : break;
4656 : :
4657 : 34727 : case 'S': /* Symbol name */
4658 : 34727 : {
4659 : 34727 : tree t = va_arg (args, tree);
4660 : 34727 : if (t && TYPE_P (t))
4661 : 12655 : t = TYPE_NAME (t);
4662 : 32945 : if (t && HAS_DECL_ASSEMBLER_NAME_P (t)
4663 : 31790 : && DECL_ASSEMBLER_NAME_SET_P (t))
4664 : : {
4665 : 169 : fputc ('(', dumps->stream);
4666 : 169 : fputs (IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (t)),
4667 : 169 : dumps->stream);
4668 : 169 : fputc (')', dumps->stream);
4669 : : }
4670 : : }
4671 : : break;
4672 : :
4673 : 0 : case 'U': /* long unsigned. */
4674 : 0 : {
4675 : 0 : unsigned long u = va_arg (args, unsigned long);
4676 : 0 : fprintf (dumps->stream, "%lu", u);
4677 : : }
4678 : 0 : break;
4679 : :
4680 : 724 : case 'V': /* Version. */
4681 : 724 : {
4682 : 724 : unsigned v = va_arg (args, unsigned);
4683 : 724 : verstr_t string;
4684 : :
4685 : 724 : version2string (v, string);
4686 : 724 : fputs (string, dumps->stream);
4687 : : }
4688 : 724 : break;
4689 : :
4690 : 0 : case 'c': /* Character. */
4691 : 0 : {
4692 : 0 : int c = va_arg (args, int);
4693 : 0 : fputc (c, dumps->stream);
4694 : : }
4695 : 0 : break;
4696 : :
4697 : 61369 : case 'd': /* Decimal Int. */
4698 : 61369 : {
4699 : 61369 : int d = va_arg (args, int);
4700 : 61369 : fprintf (dumps->stream, "%d", d);
4701 : : }
4702 : 61369 : break;
4703 : :
4704 : 0 : case 'p': /* Pointer. */
4705 : 0 : {
4706 : 0 : void *p = va_arg (args, void *);
4707 : 0 : fprintf (dumps->stream, "%p", p);
4708 : : }
4709 : 0 : break;
4710 : :
4711 : 114364 : case 's': /* String. */
4712 : 114364 : {
4713 : 114364 : const char *s = va_arg (args, char *);
4714 : 114364 : gcc_checking_assert (s);
4715 : 114364 : fputs (s, dumps->stream);
4716 : : }
4717 : 114364 : break;
4718 : :
4719 : 217423 : case 'u': /* Unsigned. */
4720 : 217423 : {
4721 : 217423 : unsigned u = va_arg (args, unsigned);
4722 : 217423 : fprintf (dumps->stream, "%u", u);
4723 : : }
4724 : 217423 : break;
4725 : :
4726 : 2322 : case 'x': /* Hex. */
4727 : 2322 : {
4728 : 2322 : unsigned x = va_arg (args, unsigned);
4729 : 2322 : fprintf (dumps->stream, "%x", x);
4730 : : }
4731 : 2322 : break;
4732 : : }
4733 : : }
4734 : 342249 : fputs (format, dumps->stream);
4735 : 342249 : va_end (args);
4736 : 342249 : if (!no_nl)
4737 : : {
4738 : 178360 : dumps->bol = true;
4739 : 178360 : fputc ('\n', dumps->stream);
4740 : : }
4741 : : return true;
4742 : : }
4743 : :
4744 : : struct note_def_cache_hasher : ggc_cache_ptr_hash<tree_node>
4745 : : {
4746 : 140907 : static int keep_cache_entry (tree t)
4747 : : {
4748 : 140907 : if (!CHECKING_P)
4749 : : /* GTY is unfortunately not clever enough to conditionalize
4750 : : this. */
4751 : : gcc_unreachable ();
4752 : :
4753 : 140907 : if (ggc_marked_p (t))
4754 : : return -1;
4755 : :
4756 : 0 : unsigned n = dump.push (NULL);
4757 : : /* This might or might not be an error. We should note its
4758 : : dropping whichever. */
4759 : 0 : dump () && dump ("Dropping %N from note_defs table", t);
4760 : 0 : dump.pop (n);
4761 : :
4762 : 0 : return 0;
4763 : : }
4764 : : };
4765 : :
4766 : : /* We should stream each definition at most once.
4767 : : This needs to be a cache because there are cases where a definition
4768 : : ends up being not retained, and we need to drop those so we don't
4769 : : get confused if memory is reallocated. */
4770 : : typedef hash_table<note_def_cache_hasher> note_defs_table_t;
4771 : : static GTY((cache)) note_defs_table_t *note_defs;
4772 : :
4773 : : void
4774 : 243372 : trees_in::assert_definition (tree decl ATTRIBUTE_UNUSED,
4775 : : bool installing ATTRIBUTE_UNUSED)
4776 : : {
4777 : : #if CHECKING_P
4778 : 243372 : tree *slot = note_defs->find_slot (decl, installing ? INSERT : NO_INSERT);
4779 : 243372 : tree not_tmpl = STRIP_TEMPLATE (decl);
4780 : 243372 : if (installing)
4781 : : {
4782 : : /* We must be inserting for the first time. */
4783 : 114614 : gcc_assert (!*slot);
4784 : 114614 : *slot = decl;
4785 : : }
4786 : : else
4787 : : /* If this is not the mergeable entity, it should not be in the
4788 : : table. If it is a non-global-module mergeable entity, it
4789 : : should be in the table. Global module entities could have been
4790 : : defined textually in the current TU and so might or might not
4791 : : be present. */
4792 : 128758 : gcc_assert (!is_duplicate (decl)
4793 : : ? !slot
4794 : : : (slot
4795 : : || !DECL_LANG_SPECIFIC (not_tmpl)
4796 : : || !DECL_MODULE_PURVIEW_P (not_tmpl)
4797 : : || (!DECL_MODULE_IMPORT_P (not_tmpl)
4798 : : && header_module_p ())));
4799 : :
4800 : 243372 : if (not_tmpl != decl)
4801 : 140113 : gcc_assert (!note_defs->find_slot (not_tmpl, NO_INSERT));
4802 : : #endif
4803 : 243372 : }
4804 : :
4805 : : void
4806 : 309732 : trees_out::assert_definition (tree decl ATTRIBUTE_UNUSED)
4807 : : {
4808 : : #if CHECKING_P
4809 : 309732 : tree *slot = note_defs->find_slot (decl, INSERT);
4810 : 309732 : gcc_assert (!*slot);
4811 : 309732 : *slot = decl;
4812 : 309732 : if (TREE_CODE (decl) == TEMPLATE_DECL)
4813 : 167564 : gcc_assert (!note_defs->find_slot (DECL_TEMPLATE_RESULT (decl), NO_INSERT));
4814 : : #endif
4815 : 309732 : }
4816 : :
4817 : : /********************************************************************/
4818 : : static bool
4819 : 11143 : noisy_p ()
4820 : : {
4821 : 0 : if (quiet_flag)
4822 : : return false;
4823 : :
4824 : 0 : pp_needs_newline (global_dc->get_reference_printer ()) = true;
4825 : 0 : diagnostic_set_last_function (global_dc, (diagnostic_info *) NULL);
4826 : :
4827 : 0 : return true;
4828 : : }
4829 : :
4830 : : /* Set the cmi repo. Strip trailing '/', '.' becomes NULL. */
4831 : :
4832 : : static void
4833 : 97972 : set_cmi_repo (const char *r)
4834 : : {
4835 : 97972 : XDELETEVEC (cmi_repo);
4836 : 97972 : XDELETEVEC (cmi_path);
4837 : 97972 : cmi_path_alloc = 0;
4838 : :
4839 : 97972 : cmi_repo = NULL;
4840 : 97972 : cmi_repo_length = 0;
4841 : :
4842 : 97972 : if (!r || !r[0])
4843 : : return;
4844 : :
4845 : 4315 : size_t len = strlen (r);
4846 : 4315 : cmi_repo = XNEWVEC (char, len + 1);
4847 : 4315 : memcpy (cmi_repo, r, len + 1);
4848 : :
4849 : 4315 : if (len > 1 && IS_DIR_SEPARATOR (cmi_repo[len-1]))
4850 : 4315 : len--;
4851 : 4315 : if (len == 1 && cmi_repo[0] == '.')
4852 : 21 : len--;
4853 : 4315 : cmi_repo[len] = 0;
4854 : 4315 : cmi_repo_length = len;
4855 : : }
4856 : :
4857 : : /* TO is a repo-relative name. Provide one that we may use from where
4858 : : we are. */
4859 : :
4860 : : static const char *
4861 : 5256 : maybe_add_cmi_prefix (const char *to, size_t *len_p = NULL)
4862 : : {
4863 : 5256 : size_t len = len_p || cmi_repo_length ? strlen (to) : 0;
4864 : :
4865 : 5256 : if (cmi_repo_length && !IS_ABSOLUTE_PATH (to))
4866 : : {
4867 : 5235 : if (cmi_path_alloc < cmi_repo_length + len + 2)
4868 : : {
4869 : 4231 : XDELETEVEC (cmi_path);
4870 : 4231 : cmi_path_alloc = cmi_repo_length + len * 2 + 2;
4871 : 4231 : cmi_path = XNEWVEC (char, cmi_path_alloc);
4872 : :
4873 : 4231 : memcpy (cmi_path, cmi_repo, cmi_repo_length);
4874 : 4231 : cmi_path[cmi_repo_length] = DIR_SEPARATOR;
4875 : : }
4876 : :
4877 : 5235 : memcpy (&cmi_path[cmi_repo_length + 1], to, len + 1);
4878 : 5235 : len += cmi_repo_length + 1;
4879 : 5235 : to = cmi_path;
4880 : : }
4881 : :
4882 : 5256 : if (len_p)
4883 : 2535 : *len_p = len;
4884 : :
4885 : 5256 : return to;
4886 : : }
4887 : :
4888 : : /* Try and create the directories of PATH. */
4889 : :
4890 : : static void
4891 : 34 : create_dirs (char *path)
4892 : : {
4893 : : /* Try and create the missing directories. */
4894 : 2150 : for (char *base = path; *base; base++)
4895 : 2116 : if (IS_DIR_SEPARATOR (*base))
4896 : : {
4897 : 213 : char sep = *base;
4898 : 213 : *base = 0;
4899 : 213 : int failed = mkdir (path, S_IRWXU | S_IRWXG | S_IRWXO);
4900 : 219 : dump () && dump ("Mkdir ('%s') errno:=%u", path, failed ? errno : 0);
4901 : 213 : *base = sep;
4902 : 213 : if (failed
4903 : : /* Maybe racing with another creator (of a *different*
4904 : : module). */
4905 : 41 : && errno != EEXIST)
4906 : : break;
4907 : : }
4908 : 34 : }
4909 : :
4910 : : /* Given a CLASSTYPE_DECL_LIST VALUE get the template friend decl,
4911 : : if that's what this is. */
4912 : :
4913 : : static tree
4914 : 62233 : friend_from_decl_list (tree frnd)
4915 : : {
4916 : 62233 : tree res = frnd;
4917 : :
4918 : 62233 : if (TREE_CODE (frnd) != TEMPLATE_DECL)
4919 : : {
4920 : 35783 : tree tmpl = NULL_TREE;
4921 : 35783 : if (TYPE_P (frnd))
4922 : : {
4923 : 5415 : res = TYPE_NAME (frnd);
4924 : 5331 : if (CLASS_TYPE_P (frnd)
4925 : 10746 : && CLASSTYPE_TEMPLATE_INFO (frnd))
4926 : 5322 : tmpl = CLASSTYPE_TI_TEMPLATE (frnd);
4927 : : }
4928 : 30368 : else if (DECL_TEMPLATE_INFO (frnd))
4929 : : {
4930 : 30368 : tmpl = DECL_TI_TEMPLATE (frnd);
4931 : 30368 : if (TREE_CODE (tmpl) != TEMPLATE_DECL)
4932 : : tmpl = NULL_TREE;
4933 : : }
4934 : :
4935 : 40616 : if (tmpl && DECL_TEMPLATE_RESULT (tmpl) == res)
4936 : : res = tmpl;
4937 : : }
4938 : :
4939 : 62233 : return res;
4940 : : }
4941 : :
4942 : : static tree
4943 : 20690 : find_enum_member (tree ctx, tree name)
4944 : : {
4945 : 20690 : for (tree values = TYPE_VALUES (ctx);
4946 : 424450 : values; values = TREE_CHAIN (values))
4947 : 419380 : if (DECL_NAME (TREE_VALUE (values)) == name)
4948 : : return TREE_VALUE (values);
4949 : :
4950 : : return NULL_TREE;
4951 : : }
4952 : :
4953 : : /********************************************************************/
4954 : : /* Instrumentation gathered writing bytes. */
4955 : :
4956 : : void
4957 : 252 : bytes_out::instrument ()
4958 : : {
4959 : 252 : dump ("Wrote %u bytes in %u blocks", lengths[3], spans[3]);
4960 : 252 : dump ("Wrote %u bits in %u bytes", lengths[0] + lengths[1], lengths[2]);
4961 : 756 : for (unsigned ix = 0; ix < 2; ix++)
4962 : 756 : dump (" %u %s spans of %R bits", spans[ix],
4963 : : ix ? "one" : "zero", lengths[ix], spans[ix]);
4964 : 252 : dump (" %u blocks with %R bits padding", spans[2],
4965 : 252 : lengths[2] * 8 - (lengths[0] + lengths[1]), spans[2]);
4966 : 252 : }
4967 : :
4968 : : /* Instrumentation gathered writing trees. */
4969 : : void
4970 : 2398 : trees_out::instrument ()
4971 : : {
4972 : 2398 : if (dump (""))
4973 : : {
4974 : 252 : bytes_out::instrument ();
4975 : 252 : dump ("Wrote:");
4976 : 252 : dump (" %u decl trees", decl_val_count);
4977 : 252 : dump (" %u other trees", tree_val_count);
4978 : 252 : dump (" %u back references", back_ref_count);
4979 : 252 : dump (" %u TU-local entities", tu_local_count);
4980 : 252 : dump (" %u null trees", null_count);
4981 : : }
4982 : 2398 : }
4983 : :
4984 : : /* Setup and teardown for a tree walk. */
4985 : :
4986 : : void
4987 : 1826848 : trees_out::begin ()
4988 : : {
4989 : 1826848 : gcc_assert (!streaming_p () || !tree_map.elements ());
4990 : :
4991 : 1826848 : mark_trees ();
4992 : 1826848 : if (streaming_p ())
4993 : 226441 : parent::begin ();
4994 : 1826848 : }
4995 : :
4996 : : unsigned
4997 : 226441 : trees_out::end (elf_out *sink, unsigned name, unsigned *crc_ptr)
4998 : : {
4999 : 226441 : gcc_checking_assert (streaming_p ());
5000 : :
5001 : 226441 : unmark_trees ();
5002 : 226441 : return parent::end (sink, name, crc_ptr);
5003 : : }
5004 : :
5005 : : void
5006 : 1600407 : trees_out::end ()
5007 : : {
5008 : 1600407 : gcc_assert (!streaming_p ());
5009 : :
5010 : 1600407 : unmark_trees ();
5011 : : /* Do not parent::end -- we weren't streaming. */
5012 : 1600407 : }
5013 : :
5014 : : void
5015 : 1826848 : trees_out::mark_trees ()
5016 : : {
5017 : 1826848 : if (size_t size = tree_map.elements ())
5018 : : {
5019 : : /* This isn't our first rodeo, destroy and recreate the
5020 : : tree_map. I'm a bad bad man. Use the previous size as a
5021 : : guess for the next one (so not all bad). */
5022 : 1381983 : tree_map.~ptr_int_hash_map ();
5023 : 1381983 : new (&tree_map) ptr_int_hash_map (size);
5024 : : }
5025 : :
5026 : : /* Install the fixed trees, with +ve references. */
5027 : 1826848 : unsigned limit = fixed_trees->length ();
5028 : 352374952 : for (unsigned ix = 0; ix != limit; ix++)
5029 : : {
5030 : 350548104 : tree val = (*fixed_trees)[ix];
5031 : 350548104 : bool existed = tree_map.put (val, ix + tag_fixed);
5032 : 350548104 : gcc_checking_assert (!TREE_VISITED (val) && !existed);
5033 : 350548104 : TREE_VISITED (val) = true;
5034 : : }
5035 : :
5036 : 1826848 : ref_num = 0;
5037 : 1826848 : }
5038 : :
5039 : : /* Unmark the trees we encountered */
5040 : :
5041 : : void
5042 : 1826848 : trees_out::unmark_trees ()
5043 : : {
5044 : 1826848 : ptr_int_hash_map::iterator end (tree_map.end ());
5045 : 413333509 : for (ptr_int_hash_map::iterator iter (tree_map.begin ()); iter != end; ++iter)
5046 : : {
5047 : 411506661 : tree node = reinterpret_cast<tree> ((*iter).first);
5048 : 411506661 : int ref = (*iter).second;
5049 : : /* We should have visited the node, and converted its mergeable
5050 : : reference to a regular reference. */
5051 : 411506661 : gcc_checking_assert (TREE_VISITED (node)
5052 : : && (ref <= tag_backref || ref >= tag_fixed));
5053 : 411506661 : TREE_VISITED (node) = false;
5054 : : }
5055 : 1826848 : }
5056 : :
5057 : : /* Mark DECL for by-value walking. We do this by inserting it into
5058 : : the tree map with a reference of zero. May be called multiple
5059 : : times on the same node. */
5060 : :
5061 : : void
5062 : 2844496 : trees_out::mark_by_value (tree decl)
5063 : : {
5064 : 2844496 : gcc_checking_assert (DECL_P (decl)
5065 : : /* Enum consts are INTEGER_CSTS. */
5066 : : || TREE_CODE (decl) == INTEGER_CST
5067 : : || TREE_CODE (decl) == TREE_BINFO);
5068 : :
5069 : 2844496 : if (TREE_VISITED (decl))
5070 : : /* Must already be forced or fixed. */
5071 : 2264 : gcc_checking_assert (*tree_map.get (decl) >= tag_value);
5072 : : else
5073 : : {
5074 : 2842232 : bool existed = tree_map.put (decl, tag_value);
5075 : 2842232 : gcc_checking_assert (!existed);
5076 : 2842232 : TREE_VISITED (decl) = true;
5077 : : }
5078 : 2844496 : }
5079 : :
5080 : : int
5081 : 74424615 : trees_out::get_tag (tree t)
5082 : : {
5083 : 74424615 : gcc_checking_assert (TREE_VISITED (t));
5084 : 74424615 : return *tree_map.get (t);
5085 : : }
5086 : :
5087 : : /* Insert T into the map, return its tag number. */
5088 : :
5089 : : int
5090 : 60958557 : trees_out::insert (tree t, walk_kind walk)
5091 : : {
5092 : 60958557 : gcc_checking_assert (walk != WK_normal || !TREE_VISITED (t));
5093 : 60958557 : int tag = --ref_num;
5094 : 60958557 : bool existed;
5095 : 60958557 : int &slot = tree_map.get_or_insert (t, &existed);
5096 : 60958557 : gcc_checking_assert (TREE_VISITED (t) == existed
5097 : : && (!existed
5098 : : || (walk == WK_value && slot == tag_value)));
5099 : 60958557 : TREE_VISITED (t) = true;
5100 : 60958557 : slot = tag;
5101 : :
5102 : 60958557 : return tag;
5103 : : }
5104 : :
5105 : : /* Insert T into the backreference array. Return its back reference
5106 : : number. */
5107 : :
5108 : : int
5109 : 14745757 : trees_in::insert (tree t)
5110 : : {
5111 : 14745757 : gcc_checking_assert (t || get_overrun ());
5112 : 14745757 : back_refs.safe_push (t);
5113 : 14745757 : return -(int)back_refs.length ();
5114 : : }
5115 : :
5116 : : /* A chained set of decls. */
5117 : :
5118 : : void
5119 : 89498 : trees_out::chained_decls (tree decls)
5120 : : {
5121 : 209748 : for (; decls; decls = DECL_CHAIN (decls))
5122 : 120250 : tree_node (decls);
5123 : 89498 : tree_node (NULL_TREE);
5124 : 89498 : }
5125 : :
5126 : : tree
5127 : 35469 : trees_in::chained_decls ()
5128 : : {
5129 : 35469 : tree decls = NULL_TREE;
5130 : 35469 : for (tree *chain = &decls;;)
5131 : 87709 : if (tree decl = tree_node ())
5132 : : {
5133 : 52240 : if (!DECL_P (decl) || DECL_CHAIN (decl))
5134 : : {
5135 : 0 : set_overrun ();
5136 : 0 : break;
5137 : : }
5138 : 52240 : *chain = decl;
5139 : 52240 : chain = &DECL_CHAIN (decl);
5140 : : }
5141 : : else
5142 : 52240 : break;
5143 : :
5144 : 35469 : return decls;
5145 : : }
5146 : :
5147 : : /* A vector of decls following DECL_CHAIN. */
5148 : :
5149 : : void
5150 : 286288 : trees_out::vec_chained_decls (tree decls)
5151 : : {
5152 : 286288 : if (streaming_p ())
5153 : : {
5154 : : unsigned len = 0;
5155 : :
5156 : 817405 : for (tree decl = decls; decl; decl = DECL_CHAIN (decl))
5157 : 674301 : len++;
5158 : 143104 : u (len);
5159 : : }
5160 : :
5161 : 1635216 : for (tree decl = decls; decl; decl = DECL_CHAIN (decl))
5162 : : {
5163 : 295891 : if (DECL_IMPLICIT_TYPEDEF_P (decl)
5164 : 1359627 : && TYPE_NAME (TREE_TYPE (decl)) != decl)
5165 : : /* An anonynmous struct with a typedef name. An odd thing to
5166 : : write. */
5167 : 8 : tree_node (NULL_TREE);
5168 : : else
5169 : 1348920 : tree_node (decl);
5170 : : }
5171 : 286288 : }
5172 : :
5173 : : vec<tree, va_heap> *
5174 : 103188 : trees_in::vec_chained_decls ()
5175 : : {
5176 : 103188 : vec<tree, va_heap> *v = NULL;
5177 : :
5178 : 103188 : if (unsigned len = u ())
5179 : : {
5180 : 54151 : vec_alloc (v, len);
5181 : :
5182 : 604711 : for (unsigned ix = 0; ix < len; ix++)
5183 : : {
5184 : 550560 : tree decl = tree_node ();
5185 : 550560 : if (decl && !DECL_P (decl))
5186 : : {
5187 : 0 : set_overrun ();
5188 : 0 : break;
5189 : : }
5190 : 550560 : v->quick_push (decl);
5191 : : }
5192 : :
5193 : 54151 : if (get_overrun ())
5194 : : {
5195 : 0 : vec_free (v);
5196 : 0 : v = NULL;
5197 : : }
5198 : : }
5199 : :
5200 : 103188 : return v;
5201 : : }
5202 : :
5203 : : /* A vector of trees. */
5204 : :
5205 : : void
5206 : 204268 : trees_out::tree_vec (vec<tree, va_gc> *v)
5207 : : {
5208 : 204268 : unsigned len = vec_safe_length (v);
5209 : 204268 : if (streaming_p ())
5210 : 102114 : u (len);
5211 : 263940 : for (unsigned ix = 0; ix != len; ix++)
5212 : 59672 : tree_node ((*v)[ix]);
5213 : 204268 : }
5214 : :
5215 : : vec<tree, va_gc> *
5216 : 73605 : trees_in::tree_vec ()
5217 : : {
5218 : 73605 : vec<tree, va_gc> *v = NULL;
5219 : 73605 : if (unsigned len = u ())
5220 : : {
5221 : 19630 : vec_alloc (v, len);
5222 : 41148 : for (unsigned ix = 0; ix != len; ix++)
5223 : 21518 : v->quick_push (tree_node ());
5224 : : }
5225 : 73605 : return v;
5226 : : }
5227 : :
5228 : : /* A vector of tree pairs. */
5229 : :
5230 : : void
5231 : 4820 : trees_out::tree_pair_vec (vec<tree_pair_s, va_gc> *v)
5232 : : {
5233 : 4820 : unsigned len = vec_safe_length (v);
5234 : 4820 : if (streaming_p ())
5235 : 2410 : u (len);
5236 : 4820 : if (len)
5237 : 25734 : for (unsigned ix = 0; ix != len; ix++)
5238 : : {
5239 : 21022 : tree_pair_s const &s = (*v)[ix];
5240 : 21022 : tree_node (s.purpose);
5241 : 21022 : tree_node (s.value);
5242 : : }
5243 : 4820 : }
5244 : :
5245 : : vec<tree_pair_s, va_gc> *
5246 : 2078 : trees_in::tree_pair_vec ()
5247 : : {
5248 : 2078 : vec<tree_pair_s, va_gc> *v = NULL;
5249 : 2078 : if (unsigned len = u ())
5250 : : {
5251 : 2032 : vec_alloc (v, len);
5252 : 11293 : for (unsigned ix = 0; ix != len; ix++)
5253 : : {
5254 : 9261 : tree_pair_s s;
5255 : 9261 : s.purpose = tree_node ();
5256 : 9261 : s.value = tree_node ();
5257 : 9261 : v->quick_push (s);
5258 : : }
5259 : : }
5260 : 2078 : return v;
5261 : : }
5262 : :
5263 : : void
5264 : 301274 : trees_out::tree_list (tree list, bool has_purpose)
5265 : : {
5266 : 1240673 : for (; list; list = TREE_CHAIN (list))
5267 : : {
5268 : 939399 : gcc_checking_assert (TREE_VALUE (list));
5269 : 939399 : tree_node (TREE_VALUE (list));
5270 : 939399 : if (has_purpose)
5271 : 909963 : tree_node (TREE_PURPOSE (list));
5272 : : }
5273 : 301274 : tree_node (NULL_TREE);
5274 : 301274 : }
5275 : :
5276 : : tree
5277 : 110055 : trees_in::tree_list (bool has_purpose)
5278 : : {
5279 : 110055 : tree res = NULL_TREE;
5280 : :
5281 : 492222 : for (tree *chain = &res; tree value = tree_node ();
5282 : 764334 : chain = &TREE_CHAIN (*chain))
5283 : : {
5284 : 382167 : tree purpose = has_purpose ? tree_node () : NULL_TREE;
5285 : 382167 : *chain = build_tree_list (purpose, value);
5286 : 382167 : }
5287 : :
5288 : 110055 : return res;
5289 : : }
5290 : :
5291 : : #define CASE_OMP_SIMD_CODE \
5292 : : case OMP_SIMD: \
5293 : : case OMP_STRUCTURED_BLOCK: \
5294 : : case OMP_LOOP: \
5295 : : case OMP_ORDERED: \
5296 : : case OMP_TILE: \
5297 : : case OMP_UNROLL
5298 : : #define CASE_OMP_CODE \
5299 : : case OMP_PARALLEL: \
5300 : : case OMP_TASK: \
5301 : : case OMP_FOR: \
5302 : : case OMP_DISTRIBUTE: \
5303 : : case OMP_TASKLOOP: \
5304 : : case OMP_TEAMS: \
5305 : : case OMP_TARGET_DATA: \
5306 : : case OMP_TARGET: \
5307 : : case OMP_SECTIONS: \
5308 : : case OMP_CRITICAL: \
5309 : : case OMP_SINGLE: \
5310 : : case OMP_SCOPE: \
5311 : : case OMP_TASKGROUP: \
5312 : : case OMP_MASKED: \
5313 : : case OMP_DISPATCH: \
5314 : : case OMP_INTEROP: \
5315 : : case OMP_MASTER: \
5316 : : case OMP_TARGET_UPDATE: \
5317 : : case OMP_TARGET_ENTER_DATA: \
5318 : : case OMP_TARGET_EXIT_DATA: \
5319 : : case OMP_METADIRECTIVE: \
5320 : : case OMP_ATOMIC: \
5321 : : case OMP_ATOMIC_READ: \
5322 : : case OMP_ATOMIC_CAPTURE_OLD: \
5323 : : case OMP_ATOMIC_CAPTURE_NEW
5324 : : #define CASE_OACC_CODE \
5325 : : case OACC_PARALLEL: \
5326 : : case OACC_KERNELS: \
5327 : : case OACC_SERIAL: \
5328 : : case OACC_DATA: \
5329 : : case OACC_HOST_DATA: \
5330 : : case OACC_LOOP: \
5331 : : case OACC_CACHE: \
5332 : : case OACC_DECLARE: \
5333 : : case OACC_ENTER_DATA: \
5334 : : case OACC_EXIT_DATA: \
5335 : : case OACC_UPDATE
5336 : :
5337 : : /* Start tree write. Write information to allocate the receiving
5338 : : node. */
5339 : :
5340 : : void
5341 : 12346932 : trees_out::start (tree t, bool code_streamed)
5342 : : {
5343 : 12346932 : if (TYPE_P (t))
5344 : : {
5345 : 501799 : enum tree_code code = TREE_CODE (t);
5346 : 501799 : gcc_checking_assert (TYPE_MAIN_VARIANT (t) == t);
5347 : : /* All these types are TYPE_NON_COMMON. */
5348 : 501799 : gcc_checking_assert (code == RECORD_TYPE
5349 : : || code == UNION_TYPE
5350 : : || code == ENUMERAL_TYPE
5351 : : || code == TEMPLATE_TYPE_PARM
5352 : : || code == TEMPLATE_TEMPLATE_PARM
5353 : : || code == BOUND_TEMPLATE_TEMPLATE_PARM);
5354 : : }
5355 : :
5356 : 12346932 : if (!code_streamed)
5357 : 11896771 : u (TREE_CODE (t));
5358 : :
5359 : 12346932 : switch (TREE_CODE (t))
5360 : : {
5361 : 10948533 : default:
5362 : 10948533 : if (VL_EXP_CLASS_P (t))
5363 : 460121 : u (VL_EXP_OPERAND_LENGTH (t));
5364 : : break;
5365 : :
5366 : 557323 : case INTEGER_CST:
5367 : 557323 : u (TREE_INT_CST_NUNITS (t));
5368 : 557323 : u (TREE_INT_CST_EXT_NUNITS (t));
5369 : 557323 : break;
5370 : :
5371 : 12 : case OMP_CLAUSE:
5372 : 12 : u (OMP_CLAUSE_CODE (t));
5373 : 12 : break;
5374 : :
5375 : 6 : CASE_OMP_SIMD_CODE:
5376 : 6 : state->extensions |= SE_OPENMP_SIMD;
5377 : 6 : break;
5378 : :
5379 : 3 : CASE_OMP_CODE:
5380 : 3 : state->extensions |= SE_OPENMP;
5381 : 3 : break;
5382 : :
5383 : 6 : CASE_OACC_CODE:
5384 : 6 : state->extensions |= SE_OPENACC;
5385 : 6 : break;
5386 : :
5387 : 40582 : case STRING_CST:
5388 : 40582 : str (TREE_STRING_POINTER (t), TREE_STRING_LENGTH (t));
5389 : 40582 : break;
5390 : :
5391 : 18 : case RAW_DATA_CST:
5392 : 18 : if (RAW_DATA_OWNER (t) == NULL_TREE)
5393 : : {
5394 : : /* Stream RAW_DATA_CST with no owner (i.e. data pointing
5395 : : into libcpp buffers) as something we can stream in as
5396 : : STRING_CST which owns the data. */
5397 : 6 : u (0);
5398 : : /* Can't use str (RAW_DATA_POINTER (t), RAW_DATA_LENGTH (t));
5399 : : here as there isn't a null termination after it. */
5400 : 6 : z (RAW_DATA_LENGTH (t));
5401 : 6 : if (RAW_DATA_LENGTH (t))
5402 : 6 : if (void *ptr = buf (RAW_DATA_LENGTH (t) + 1))
5403 : : {
5404 : 6 : memcpy (ptr, RAW_DATA_POINTER (t), RAW_DATA_LENGTH (t));
5405 : 6 : ((char *) ptr)[RAW_DATA_LENGTH (t)] = '\0';
5406 : : }
5407 : : }
5408 : : else
5409 : : {
5410 : 12 : gcc_assert (RAW_DATA_LENGTH (t));
5411 : 12 : u (RAW_DATA_LENGTH (t));
5412 : : }
5413 : : break;
5414 : :
5415 : 18 : case VECTOR_CST:
5416 : 18 : u (VECTOR_CST_LOG2_NPATTERNS (t));
5417 : 18 : u (VECTOR_CST_NELTS_PER_PATTERN (t));
5418 : 18 : break;
5419 : :
5420 : 99704 : case TREE_BINFO:
5421 : 99704 : u (BINFO_N_BASE_BINFOS (t));
5422 : 99704 : break;
5423 : :
5424 : 700727 : case TREE_VEC:
5425 : 700727 : u (TREE_VEC_LENGTH (t));
5426 : 700727 : break;
5427 : :
5428 : 0 : case FIXED_CST:
5429 : 0 : gcc_unreachable (); /* Not supported in C++. */
5430 : 0 : break;
5431 : :
5432 : 0 : case IDENTIFIER_NODE:
5433 : 0 : case SSA_NAME:
5434 : 0 : case TARGET_MEM_REF:
5435 : 0 : case TRANSLATION_UNIT_DECL:
5436 : : /* We shouldn't meet these. */
5437 : 0 : gcc_unreachable ();
5438 : 12346932 : break;
5439 : : }
5440 : 12346932 : }
5441 : :
5442 : : /* Start tree read. Allocate the receiving node. */
5443 : :
5444 : : tree
5445 : 10465858 : trees_in::start (unsigned code)
5446 : : {
5447 : 10465858 : tree t = NULL_TREE;
5448 : :
5449 : 10465858 : if (!code)
5450 : 9457888 : code = u ();
5451 : :
5452 : 10465858 : switch (code)
5453 : : {
5454 : 9317554 : default:
5455 : 9317554 : if (code >= MAX_TREE_CODES)
5456 : : {
5457 : 0 : fail:
5458 : 0 : set_overrun ();
5459 : 0 : return NULL_TREE;
5460 : : }
5461 : 9317554 : else if (TREE_CODE_CLASS (code) == tcc_vl_exp)
5462 : : {
5463 : 397135 : unsigned ops = u ();
5464 : 397135 : t = build_vl_exp (tree_code (code), ops);
5465 : : }
5466 : : else
5467 : 8920419 : t = make_node (tree_code (code));
5468 : : break;
5469 : :
5470 : 463411 : case INTEGER_CST:
5471 : 463411 : {
5472 : 463411 : unsigned n = u ();
5473 : 463411 : unsigned e = u ();
5474 : 463411 : t = make_int_cst (n, e);
5475 : : }
5476 : 463411 : break;
5477 : :
5478 : 12 : case OMP_CLAUSE:
5479 : 12 : t = build_omp_clause (UNKNOWN_LOCATION, omp_clause_code (u ()));
5480 : 12 : break;
5481 : :
5482 : 9 : CASE_OMP_SIMD_CODE:
5483 : 9 : if (!(state->extensions & SE_OPENMP_SIMD))
5484 : 0 : goto fail;
5485 : 9 : t = make_node (tree_code (code));
5486 : 9 : break;
5487 : :
5488 : 3 : CASE_OMP_CODE:
5489 : 3 : if (!(state->extensions & SE_OPENMP))
5490 : 0 : goto fail;
5491 : 3 : t = make_node (tree_code (code));
5492 : 3 : break;
5493 : :
5494 : 6 : CASE_OACC_CODE:
5495 : 6 : if (!(state->extensions & SE_OPENACC))
5496 : 0 : goto fail;
5497 : 6 : t = make_node (tree_code (code));
5498 : 6 : break;
5499 : :
5500 : 38592 : case STRING_CST:
5501 : 38592 : {
5502 : 38592 : size_t l;
5503 : 38592 : const char *chars = str (&l);
5504 : 38592 : t = build_string (l, chars);
5505 : : }
5506 : 38592 : break;
5507 : :
5508 : 9 : case RAW_DATA_CST:
5509 : 9 : {
5510 : 9 : size_t l = u ();
5511 : 9 : if (l == 0)
5512 : : {
5513 : : /* Stream in RAW_DATA_CST with no owner as STRING_CST
5514 : : which owns the data. */
5515 : 3 : const char *chars = str (&l);
5516 : 3 : t = build_string (l, chars);
5517 : : }
5518 : : else
5519 : : {
5520 : 6 : t = make_node (RAW_DATA_CST);
5521 : 6 : RAW_DATA_LENGTH (t) = l;
5522 : : }
5523 : : }
5524 : 9 : break;
5525 : :
5526 : 24 : case VECTOR_CST:
5527 : 24 : {
5528 : 24 : unsigned log2_npats = u ();
5529 : 24 : unsigned elts_per = u ();
5530 : 24 : t = make_vector (log2_npats, elts_per);
5531 : : }
5532 : 24 : break;
5533 : :
5534 : 71527 : case TREE_BINFO:
5535 : 71527 : t = make_tree_binfo (u ());
5536 : 71527 : break;
5537 : :
5538 : 574711 : case TREE_VEC:
5539 : 574711 : t = make_tree_vec (u ());
5540 : 574711 : break;
5541 : :
5542 : 0 : case FIXED_CST:
5543 : 0 : case IDENTIFIER_NODE:
5544 : 0 : case SSA_NAME:
5545 : 0 : case TARGET_MEM_REF:
5546 : 0 : case TRANSLATION_UNIT_DECL:
5547 : 0 : goto fail;
5548 : : }
5549 : :
5550 : : return t;
5551 : : }
5552 : :
5553 : : /* The kinds of interface an importer could have for a decl. */
5554 : :
5555 : : enum class importer_interface {
5556 : : unknown, /* The definition may or may not need to be emitted. */
5557 : : external, /* The definition can always be found in another TU. */
5558 : : internal, /* The definition should be emitted in the importer's TU. */
5559 : : always_emit, /* The definition must be emitted in the importer's TU,
5560 : : regardless of if it's used or not. */
5561 : : };
5562 : :
5563 : : /* Returns what kind of interface an importer will have of DECL. */
5564 : :
5565 : : static importer_interface
5566 : 472283 : get_importer_interface (tree decl)
5567 : : {
5568 : : /* Internal linkage entities must be emitted in each importer if
5569 : : there is a definition available. */
5570 : 472283 : if (!TREE_PUBLIC (decl))
5571 : : return importer_interface::internal;
5572 : :
5573 : : /* Other entities that aren't vague linkage are either not definitions
5574 : : or will be publicly emitted in this TU, so importers can just refer
5575 : : to an external definition. */
5576 : 247631 : if (!vague_linkage_p (decl))
5577 : : return importer_interface::external;
5578 : :
5579 : : /* For explicit instantiations, importers can always rely on there
5580 : : being a definition in another TU, unless this is a definition
5581 : : in a header module: in which case the importer will always need
5582 : : to emit it. */
5583 : 243164 : if (DECL_LANG_SPECIFIC (decl)
5584 : 243164 : && DECL_EXPLICIT_INSTANTIATION (decl))
5585 : 18971 : return (header_module_p () && !DECL_EXTERNAL (decl)
5586 : 18971 : ? importer_interface::always_emit
5587 : : : importer_interface::external);
5588 : :
5589 : : /* A gnu_inline function is never emitted in any TU. */
5590 : 224193 : if (TREE_CODE (decl) == FUNCTION_DECL
5591 : 153841 : && DECL_DECLARED_INLINE_P (decl)
5592 : 373076 : && lookup_attribute ("gnu_inline", DECL_ATTRIBUTES (decl)))
5593 : : return importer_interface::external;
5594 : :
5595 : : /* Everything else has vague linkage. */
5596 : : return importer_interface::unknown;
5597 : : }
5598 : :
5599 : : /* The structure streamers access the raw fields, because the
5600 : : alternative, of using the accessor macros can require using
5601 : : different accessors for the same underlying field, depending on the
5602 : : tree code. That's both confusing and annoying. */
5603 : :
5604 : : /* Read & write the core boolean flags. */
5605 : :
5606 : : void
5607 : 12365469 : trees_out::core_bools (tree t, bits_out& bits)
5608 : : {
5609 : : #define WB(X) (bits.b (X))
5610 : : /* Stream X if COND holds, and if !COND stream a dummy value so that the
5611 : : overall number of bits streamed is independent of the runtime value
5612 : : of COND, which allows the compiler to better optimize this function. */
5613 : : #define WB_IF(COND, X) WB ((COND) ? (X) : false)
5614 : 12365469 : tree_code code = TREE_CODE (t);
5615 : :
5616 : 12365469 : WB (t->base.side_effects_flag);
5617 : 12365469 : WB (t->base.constant_flag);
5618 : 12365469 : WB (t->base.addressable_flag);
5619 : 12365469 : WB (t->base.volatile_flag);
5620 : 12365469 : WB (t->base.readonly_flag);
5621 : : /* base.asm_written_flag is a property of the current TU's use of
5622 : : this decl. */
5623 : 12365469 : WB (t->base.nowarning_flag);
5624 : : /* base.visited read as zero (it's set for writer, because that's
5625 : : how we mark nodes). */
5626 : : /* base.used_flag is not streamed. Readers may set TREE_USED of
5627 : : decls they use. */
5628 : 12365469 : WB (t->base.nothrow_flag);
5629 : 12365469 : WB (t->base.static_flag);
5630 : : /* This is TYPE_CACHED_VALUES_P for types. */
5631 : 12365469 : WB_IF (TREE_CODE_CLASS (code) != tcc_type, t->base.public_flag);
5632 : 12365469 : WB (t->base.private_flag);
5633 : 12365469 : WB (t->base.protected_flag);
5634 : 12365469 : WB (t->base.deprecated_flag);
5635 : 12365469 : WB (t->base.default_def_flag);
5636 : :
5637 : 12365469 : switch (code)
5638 : : {
5639 : : case CALL_EXPR:
5640 : : case INTEGER_CST:
5641 : : case SSA_NAME:
5642 : : case TARGET_MEM_REF:
5643 : : case TREE_VEC:
5644 : : /* These use different base.u fields. */
5645 : : return;
5646 : :
5647 : 10658588 : default:
5648 : 10658588 : WB (t->base.u.bits.lang_flag_0);
5649 : 10658588 : bool flag_1 = t->base.u.bits.lang_flag_1;
5650 : 10658588 : if (!flag_1)
5651 : : ;
5652 : 332603 : else if (code == TEMPLATE_INFO)
5653 : : /* This is TI_PENDING_TEMPLATE_FLAG, not relevant to reader. */
5654 : : flag_1 = false;
5655 : 329314 : else if (code == VAR_DECL)
5656 : : {
5657 : : /* This is DECL_INITIALIZED_P. */
5658 : 68962 : if (TREE_CODE (DECL_CONTEXT (t)) != FUNCTION_DECL)
5659 : : /* We'll set this when reading the definition. */
5660 : 10658588 : flag_1 = false;
5661 : : }
5662 : 10658588 : WB (flag_1);
5663 : 10658588 : WB (t->base.u.bits.lang_flag_2);
5664 : 10658588 : WB (t->base.u.bits.lang_flag_3);
5665 : 10658588 : WB (t->base.u.bits.lang_flag_4);
5666 : 10658588 : WB (t->base.u.bits.lang_flag_5);
5667 : 10658588 : WB (t->base.u.bits.lang_flag_6);
5668 : 10658588 : WB (t->base.u.bits.saturating_flag);
5669 : 10658588 : WB (t->base.u.bits.unsigned_flag);
5670 : 10658588 : WB (t->base.u.bits.packed_flag);
5671 : 10658588 : WB (t->base.u.bits.user_align);
5672 : 10658588 : WB (t->base.u.bits.nameless_flag);
5673 : 10658588 : WB (t->base.u.bits.atomic_flag);
5674 : 10658588 : WB (t->base.u.bits.unavailable_flag);
5675 : 10658588 : break;
5676 : : }
5677 : :
5678 : 10658588 : if (TREE_CODE_CLASS (code) == tcc_type)
5679 : : {
5680 : 520336 : WB (t->type_common.no_force_blk_flag);
5681 : 520336 : WB (t->type_common.needs_constructing_flag);
5682 : 520336 : WB (t->type_common.transparent_aggr_flag);
5683 : 520336 : WB (t->type_common.restrict_flag);
5684 : 520336 : WB (t->type_common.string_flag);
5685 : 520336 : WB (t->type_common.lang_flag_0);
5686 : 520336 : WB (t->type_common.lang_flag_1);
5687 : 520336 : WB (t->type_common.lang_flag_2);
5688 : 520336 : WB (t->type_common.lang_flag_3);
5689 : 520336 : WB (t->type_common.lang_flag_4);
5690 : 520336 : WB (t->type_common.lang_flag_5);
5691 : 520336 : WB (t->type_common.lang_flag_6);
5692 : 520336 : WB (t->type_common.typeless_storage);
5693 : : }
5694 : :
5695 : 10658588 : if (TREE_CODE_CLASS (code) != tcc_declaration)
5696 : : return;
5697 : :
5698 : 2771625 : if (CODE_CONTAINS_STRUCT (code, TS_DECL_COMMON))
5699 : : {
5700 : 2771625 : WB (t->decl_common.nonlocal_flag);
5701 : 2771625 : WB (t->decl_common.virtual_flag);
5702 : 2771625 : WB (t->decl_common.ignored_flag);
5703 : 2771625 : WB (t->decl_common.abstract_flag);
5704 : 2771625 : WB (t->decl_common.artificial_flag);
5705 : 2771625 : WB (t->decl_common.preserve_flag);
5706 : 2771625 : WB (t->decl_common.debug_expr_is_from);
5707 : 2771625 : WB (t->decl_common.lang_flag_0);
5708 : 2771625 : WB (t->decl_common.lang_flag_1);
5709 : 2771625 : WB (t->decl_common.lang_flag_2);
5710 : 2771625 : WB (t->decl_common.lang_flag_3);
5711 : 2771625 : WB (t->decl_common.lang_flag_4);
5712 : :
5713 : 2771625 : {
5714 : : /* This is DECL_INTERFACE_KNOWN: We should redetermine whether
5715 : : we need to import or export any vague-linkage entities on
5716 : : stream-in. */
5717 : 2771625 : bool interface_known = t->decl_common.lang_flag_5;
5718 : 2771625 : if (interface_known
5719 : 2771625 : && get_importer_interface (t) == importer_interface::unknown)
5720 : : interface_known = false;
5721 : 2771625 : WB (interface_known);
5722 : : }
5723 : :
5724 : 2771625 : WB (t->decl_common.lang_flag_6);
5725 : 2771625 : WB (t->decl_common.lang_flag_7);
5726 : 2771625 : WB (t->decl_common.lang_flag_8);
5727 : 2771625 : WB (t->decl_common.decl_flag_0);
5728 : :
5729 : 2771625 : {
5730 : : /* DECL_EXTERNAL -> decl_flag_1
5731 : : == it is defined elsewhere
5732 : : DECL_NOT_REALLY_EXTERN -> base.not_really_extern
5733 : : == that was a lie, it is here */
5734 : :
5735 : 2771625 : bool is_external = t->decl_common.decl_flag_1;
5736 : : /* maybe_emit_vtables relies on vtables being marked as
5737 : : DECL_EXTERNAL and DECL_NOT_REALLY_EXTERN before processing. */
5738 : 2771625 : if (!is_external && VAR_P (t) && DECL_VTABLE_OR_VTT_P (t))
5739 : : is_external = true;
5740 : : /* Things we emit here might well be external from the POV of an
5741 : : importer. */
5742 : 2771418 : if (!is_external
5743 : 2329629 : && VAR_OR_FUNCTION_DECL_P (t)
5744 : 2978296 : && get_importer_interface (t) == importer_interface::external)
5745 : : is_external = true;
5746 : 2771625 : WB (is_external);
5747 : : }
5748 : :
5749 : 2771625 : WB (t->decl_common.decl_flag_2);
5750 : 2771625 : WB (t->decl_common.decl_flag_3);
5751 : 2771625 : WB (t->decl_common.not_gimple_reg_flag);
5752 : 2771625 : WB (t->decl_common.decl_by_reference_flag);
5753 : 2771625 : WB (t->decl_common.decl_read_flag);
5754 : 2771625 : WB (t->decl_common.decl_nonshareable_flag);
5755 : 2771625 : WB (t->decl_common.decl_not_flexarray);
5756 : : }
5757 : : else
5758 : : return;
5759 : :
5760 : 2771625 : if (CODE_CONTAINS_STRUCT (code, TS_DECL_WITH_VIS))
5761 : : {
5762 : 1318984 : WB (t->decl_with_vis.defer_output);
5763 : 1318984 : WB (t->decl_with_vis.hard_register);
5764 : 1318984 : WB (t->decl_with_vis.common_flag);
5765 : 1318984 : WB (t->decl_with_vis.in_text_section);
5766 : 1318984 : WB (t->decl_with_vis.in_constant_pool);
5767 : 1318984 : WB (t->decl_with_vis.dllimport_flag);
5768 : 1318984 : WB (t->decl_with_vis.weak_flag);
5769 : 1318984 : WB (t->decl_with_vis.seen_in_bind_expr);
5770 : 1318984 : WB (t->decl_with_vis.comdat_flag);
5771 : 1318984 : WB (t->decl_with_vis.visibility_specified);
5772 : 1318984 : WB (t->decl_with_vis.init_priority_p);
5773 : 1318984 : WB (t->decl_with_vis.shadowed_for_var_p);
5774 : 1318984 : WB (t->decl_with_vis.cxx_constructor);
5775 : 1318984 : WB (t->decl_with_vis.cxx_destructor);
5776 : 1318984 : WB (t->decl_with_vis.final);
5777 : 1318984 : WB (t->decl_with_vis.regdecl_flag);
5778 : : }
5779 : : else
5780 : : return;
5781 : :
5782 : 1318984 : if (CODE_CONTAINS_STRUCT (code, TS_FUNCTION_DECL))
5783 : : {
5784 : 377900 : WB (t->function_decl.static_ctor_flag);
5785 : 377900 : WB (t->function_decl.static_dtor_flag);
5786 : 377900 : WB (t->function_decl.uninlinable);
5787 : 377900 : WB (t->function_decl.possibly_inlined);
5788 : 377900 : WB (t->function_decl.novops_flag);
5789 : 377900 : WB (t->function_decl.returns_twice_flag);
5790 : 377900 : WB (t->function_decl.malloc_flag);
5791 : 377900 : WB (t->function_decl.declared_inline_flag);
5792 : 377900 : WB (t->function_decl.no_inline_warning_flag);
5793 : 377900 : WB (t->function_decl.no_instrument_function_entry_exit);
5794 : 377900 : WB (t->function_decl.no_limit_stack);
5795 : 377900 : WB (t->function_decl.disregard_inline_limits);
5796 : 377900 : WB (t->function_decl.pure_flag);
5797 : 377900 : WB (t->function_decl.looping_const_or_pure_flag);
5798 : :
5799 : 377900 : WB (t->function_decl.has_debug_args_flag);
5800 : 377900 : WB (t->function_decl.versioned_function);
5801 : 377900 : WB (t->function_decl.replaceable_operator);
5802 : :
5803 : : /* decl_type is a (misnamed) 2 bit discriminator. */
5804 : 377900 : unsigned kind = (unsigned)t->function_decl.decl_type;
5805 : 377900 : WB ((kind >> 0) & 1);
5806 : 377900 : WB ((kind >> 1) & 1);
5807 : : }
5808 : : #undef WB_IF
5809 : : #undef WB
5810 : : }
5811 : :
5812 : : bool
5813 : 10480605 : trees_in::core_bools (tree t, bits_in& bits)
5814 : : {
5815 : : #define RB(X) ((X) = bits.b ())
5816 : : /* See the comment for WB_IF in trees_out::core_bools. */
5817 : : #define RB_IF(COND, X) ((COND) ? RB (X) : bits.b ())
5818 : :
5819 : 10480605 : tree_code code = TREE_CODE (t);
5820 : :
5821 : 10480605 : RB (t->base.side_effects_flag);
5822 : 10480605 : RB (t->base.constant_flag);
5823 : 10480605 : RB (t->base.addressable_flag);
5824 : 10480605 : RB (t->base.volatile_flag);
5825 : 10480605 : RB (t->base.readonly_flag);
5826 : : /* base.asm_written_flag is not streamed. */
5827 : 10480605 : RB (t->base.nowarning_flag);
5828 : : /* base.visited is not streamed. */
5829 : : /* base.used_flag is not streamed. */
5830 : 10480605 : RB (t->base.nothrow_flag);
5831 : 10480605 : RB (t->base.static_flag);
5832 : 10480605 : RB_IF (TREE_CODE_CLASS (code) != tcc_type, t->base.public_flag);
5833 : 10480605 : RB (t->base.private_flag);
5834 : 10480605 : RB (t->base.protected_flag);
5835 : 10480605 : RB (t->base.deprecated_flag);
5836 : 10480605 : RB (t->base.default_def_flag);
5837 : :
5838 : 10480605 : switch (code)
5839 : : {
5840 : 1425596 : case CALL_EXPR:
5841 : 1425596 : case INTEGER_CST:
5842 : 1425596 : case SSA_NAME:
5843 : 1425596 : case TARGET_MEM_REF:
5844 : 1425596 : case TREE_VEC:
5845 : : /* These use different base.u fields. */
5846 : 1425596 : goto done;
5847 : :
5848 : 9055009 : default:
5849 : 9055009 : RB (t->base.u.bits.lang_flag_0);
5850 : 9055009 : RB (t->base.u.bits.lang_flag_1);
5851 : 9055009 : RB (t->base.u.bits.lang_flag_2);
5852 : 9055009 : RB (t->base.u.bits.lang_flag_3);
5853 : 9055009 : RB (t->base.u.bits.lang_flag_4);
5854 : 9055009 : RB (t->base.u.bits.lang_flag_5);
5855 : 9055009 : RB (t->base.u.bits.lang_flag_6);
5856 : 9055009 : RB (t->base.u.bits.saturating_flag);
5857 : 9055009 : RB (t->base.u.bits.unsigned_flag);
5858 : 9055009 : RB (t->base.u.bits.packed_flag);
5859 : 9055009 : RB (t->base.u.bits.user_align);
5860 : 9055009 : RB (t->base.u.bits.nameless_flag);
5861 : 9055009 : RB (t->base.u.bits.atomic_flag);
5862 : 9055009 : RB (t->base.u.bits.unavailable_flag);
5863 : 9055009 : break;
5864 : : }
5865 : :
5866 : 9055009 : if (TREE_CODE_CLASS (code) == tcc_type)
5867 : : {
5868 : 408419 : RB (t->type_common.no_force_blk_flag);
5869 : 408419 : RB (t->type_common.needs_constructing_flag);
5870 : 408419 : RB (t->type_common.transparent_aggr_flag);
5871 : 408419 : RB (t->type_common.restrict_flag);
5872 : 408419 : RB (t->type_common.string_flag);
5873 : 408419 : RB (t->type_common.lang_flag_0);
5874 : 408419 : RB (t->type_common.lang_flag_1);
5875 : 408419 : RB (t->type_common.lang_flag_2);
5876 : 408419 : RB (t->type_common.lang_flag_3);
5877 : 408419 : RB (t->type_common.lang_flag_4);
5878 : 408419 : RB (t->type_common.lang_flag_5);
5879 : 408419 : RB (t->type_common.lang_flag_6);
5880 : 408419 : RB (t->type_common.typeless_storage);
5881 : : }
5882 : :
5883 : 9055009 : if (TREE_CODE_CLASS (code) != tcc_declaration)
5884 : 6758854 : goto done;
5885 : :
5886 : 2296155 : if (CODE_CONTAINS_STRUCT (code, TS_DECL_COMMON))
5887 : : {
5888 : 2296155 : RB (t->decl_common.nonlocal_flag);
5889 : 2296155 : RB (t->decl_common.virtual_flag);
5890 : 2296155 : RB (t->decl_common.ignored_flag);
5891 : 2296155 : RB (t->decl_common.abstract_flag);
5892 : 2296155 : RB (t->decl_common.artificial_flag);
5893 : 2296155 : RB (t->decl_common.preserve_flag);
5894 : 2296155 : RB (t->decl_common.debug_expr_is_from);
5895 : 2296155 : RB (t->decl_common.lang_flag_0);
5896 : 2296155 : RB (t->decl_common.lang_flag_1);
5897 : 2296155 : RB (t->decl_common.lang_flag_2);
5898 : 2296155 : RB (t->decl_common.lang_flag_3);
5899 : 2296155 : RB (t->decl_common.lang_flag_4);
5900 : 2296155 : RB (t->decl_common.lang_flag_5);
5901 : 2296155 : RB (t->decl_common.lang_flag_6);
5902 : 2296155 : RB (t->decl_common.lang_flag_7);
5903 : 2296155 : RB (t->decl_common.lang_flag_8);
5904 : 2296155 : RB (t->decl_common.decl_flag_0);
5905 : 2296155 : RB (t->decl_common.decl_flag_1);
5906 : 2296155 : RB (t->decl_common.decl_flag_2);
5907 : 2296155 : RB (t->decl_common.decl_flag_3);
5908 : 2296155 : RB (t->decl_common.not_gimple_reg_flag);
5909 : 2296155 : RB (t->decl_common.decl_by_reference_flag);
5910 : 2296155 : RB (t->decl_common.decl_read_flag);
5911 : 2296155 : RB (t->decl_common.decl_nonshareable_flag);
5912 : 2296155 : RB (t->decl_common.decl_not_flexarray);
5913 : : }
5914 : : else
5915 : 0 : goto done;
5916 : :
5917 : 2296155 : if (CODE_CONTAINS_STRUCT (code, TS_DECL_WITH_VIS))
5918 : : {
5919 : 1066902 : RB (t->decl_with_vis.defer_output);
5920 : 1066902 : RB (t->decl_with_vis.hard_register);
5921 : 1066902 : RB (t->decl_with_vis.common_flag);
5922 : 1066902 : RB (t->decl_with_vis.in_text_section);
5923 : 1066902 : RB (t->decl_with_vis.in_constant_pool);
5924 : 1066902 : RB (t->decl_with_vis.dllimport_flag);
5925 : 1066902 : RB (t->decl_with_vis.weak_flag);
5926 : 1066902 : RB (t->decl_with_vis.seen_in_bind_expr);
5927 : 1066902 : RB (t->decl_with_vis.comdat_flag);
5928 : 1066902 : RB (t->decl_with_vis.visibility_specified);
5929 : 1066902 : RB (t->decl_with_vis.init_priority_p);
5930 : 1066902 : RB (t->decl_with_vis.shadowed_for_var_p);
5931 : 1066902 : RB (t->decl_with_vis.cxx_constructor);
5932 : 1066902 : RB (t->decl_with_vis.cxx_destructor);
5933 : 1066902 : RB (t->decl_with_vis.final);
5934 : 1066902 : RB (t->decl_with_vis.regdecl_flag);
5935 : : }
5936 : : else
5937 : 1229253 : goto done;
5938 : :
5939 : 1066902 : if (CODE_CONTAINS_STRUCT (code, TS_FUNCTION_DECL))
5940 : : {
5941 : 322959 : RB (t->function_decl.static_ctor_flag);
5942 : 322959 : RB (t->function_decl.static_dtor_flag);
5943 : 322959 : RB (t->function_decl.uninlinable);
5944 : 322959 : RB (t->function_decl.possibly_inlined);
5945 : 322959 : RB (t->function_decl.novops_flag);
5946 : 322959 : RB (t->function_decl.returns_twice_flag);
5947 : 322959 : RB (t->function_decl.malloc_flag);
5948 : 322959 : RB (t->function_decl.declared_inline_flag);
5949 : 322959 : RB (t->function_decl.no_inline_warning_flag);
5950 : 322959 : RB (t->function_decl.no_instrument_function_entry_exit);
5951 : 322959 : RB (t->function_decl.no_limit_stack);
5952 : 322959 : RB (t->function_decl.disregard_inline_limits);
5953 : 322959 : RB (t->function_decl.pure_flag);
5954 : 322959 : RB (t->function_decl.looping_const_or_pure_flag);
5955 : :
5956 : 322959 : RB (t->function_decl.has_debug_args_flag);
5957 : 322959 : RB (t->function_decl.versioned_function);
5958 : 322959 : RB (t->function_decl.replaceable_operator);
5959 : :
5960 : : /* decl_type is a (misnamed) 2 bit discriminator. */
5961 : 322959 : unsigned kind = 0;
5962 : 322959 : kind |= unsigned (bits.b ()) << 0;
5963 : 322959 : kind |= unsigned (bits.b ()) << 1;
5964 : 322959 : t->function_decl.decl_type = function_decl_type (kind);
5965 : : }
5966 : : #undef RB_IF
5967 : : #undef RB
5968 : 743943 : done:
5969 : 10480605 : return !get_overrun ();
5970 : : }
5971 : :
5972 : : void
5973 : 1775035 : trees_out::lang_decl_bools (tree t, bits_out& bits)
5974 : : {
5975 : : #define WB(X) (bits.b (X))
5976 : 1775035 : const struct lang_decl *lang = DECL_LANG_SPECIFIC (t);
5977 : :
5978 : 1775035 : bits.bflush ();
5979 : 1775035 : WB (lang->u.base.language == lang_cplusplus);
5980 : 1775035 : WB ((lang->u.base.use_template >> 0) & 1);
5981 : 1775035 : WB ((lang->u.base.use_template >> 1) & 1);
5982 : : /* Do not write lang->u.base.not_really_extern, importer will set
5983 : : when reading the definition (if any). */
5984 : 1775035 : WB (lang->u.base.initialized_in_class);
5985 : 1775035 : WB (lang->u.base.threadprivate_or_deleted_p);
5986 : : /* Do not write lang->u.base.anticipated_p, it is a property of the
5987 : : current TU. */
5988 : 1775035 : WB (lang->u.base.friend_or_tls);
5989 : 1775035 : WB (lang->u.base.unknown_bound_p);
5990 : : /* Do not write lang->u.base.odr_used, importer will recalculate if
5991 : : they do ODR use this decl. */
5992 : 1775035 : WB (lang->u.base.concept_p);
5993 : 1775035 : WB (lang->u.base.var_declared_inline_p);
5994 : 1775035 : WB (lang->u.base.dependent_init_p);
5995 : : /* When building a header unit, everthing is marked as purview, (so
5996 : : we know which decls to write). But when we import them we do not
5997 : : want to mark them as in module purview. */
5998 : 3474179 : WB (lang->u.base.module_purview_p && !header_module_p ());
5999 : 1775035 : WB (lang->u.base.module_attach_p);
6000 : 1775035 : WB (lang->u.base.module_keyed_decls_p);
6001 : 1775035 : switch (lang->u.base.selector)
6002 : : {
6003 : 0 : default:
6004 : 0 : gcc_unreachable ();
6005 : :
6006 : 377900 : case lds_fn: /* lang_decl_fn. */
6007 : 377900 : WB (lang->u.fn.global_ctor_p);
6008 : 377900 : WB (lang->u.fn.global_dtor_p);
6009 : 377900 : WB (lang->u.fn.static_function);
6010 : 377900 : WB (lang->u.fn.pure_virtual);
6011 : 377900 : WB (lang->u.fn.defaulted_p);
6012 : 377900 : WB (lang->u.fn.has_in_charge_parm_p);
6013 : 377900 : WB (lang->u.fn.has_vtt_parm_p);
6014 : : /* There shouldn't be a pending inline at this point. */
6015 : 377900 : gcc_assert (!lang->u.fn.pending_inline_p);
6016 : 377900 : WB (lang->u.fn.nonconverting);
6017 : 377900 : WB (lang->u.fn.thunk_p);
6018 : 377900 : WB (lang->u.fn.this_thunk_p);
6019 : : /* Do not stream lang->u.hidden_friend_p, it is a property of
6020 : : the TU. */
6021 : 377900 : WB (lang->u.fn.omp_declare_reduction_p);
6022 : 377900 : WB (lang->u.fn.has_dependent_explicit_spec_p);
6023 : 377900 : WB (lang->u.fn.immediate_fn_p);
6024 : 377900 : WB (lang->u.fn.maybe_deleted);
6025 : 377900 : WB (lang->u.fn.implicit_constexpr);
6026 : 377900 : WB (lang->u.fn.escalated_p);
6027 : 377900 : WB (lang->u.fn.xobj_func);
6028 : 377900 : goto lds_min;
6029 : :
6030 : 1378 : case lds_decomp: /* lang_decl_decomp. */
6031 : : /* No bools. */
6032 : 1378 : goto lds_min;
6033 : :
6034 : : case lds_min: /* lang_decl_min. */
6035 : 1775035 : lds_min:
6036 : : /* No bools. */
6037 : : break;
6038 : :
6039 : : case lds_ns: /* lang_decl_ns. */
6040 : : /* No bools. */
6041 : : break;
6042 : :
6043 : : case lds_parm: /* lang_decl_parm. */
6044 : : /* No bools. */
6045 : : break;
6046 : : }
6047 : : #undef WB
6048 : 1775035 : }
6049 : :
6050 : : bool
6051 : 1463483 : trees_in::lang_decl_bools (tree t, bits_in& bits)
6052 : : {
6053 : : #define RB(X) ((X) = bits.b ())
6054 : 1463483 : struct lang_decl *lang = DECL_LANG_SPECIFIC (t);
6055 : :
6056 : 1463483 : bits.bflush ();
6057 : 1463483 : lang->u.base.language = bits.b () ? lang_cplusplus : lang_c;
6058 : 1463483 : unsigned v;
6059 : 1463483 : v = bits.b () << 0;
6060 : 1463483 : v |= bits.b () << 1;
6061 : 1463483 : lang->u.base.use_template = v;
6062 : : /* lang->u.base.not_really_extern is not streamed. */
6063 : 1463483 : RB (lang->u.base.initialized_in_class);
6064 : 1463483 : RB (lang->u.base.threadprivate_or_deleted_p);
6065 : : /* lang->u.base.anticipated_p is not streamed. */
6066 : 1463483 : RB (lang->u.base.friend_or_tls);
6067 : 1463483 : RB (lang->u.base.unknown_bound_p);
6068 : : /* lang->u.base.odr_used is not streamed. */
6069 : 1463483 : RB (lang->u.base.concept_p);
6070 : 1463483 : RB (lang->u.base.var_declared_inline_p);
6071 : 1463483 : RB (lang->u.base.dependent_init_p);
6072 : 1463483 : RB (lang->u.base.module_purview_p);
6073 : 1463483 : RB (lang->u.base.module_attach_p);
6074 : 1463483 : RB (lang->u.base.module_keyed_decls_p);
6075 : 1463483 : switch (lang->u.base.selector)
6076 : : {
6077 : 0 : default:
6078 : 0 : gcc_unreachable ();
6079 : :
6080 : 322959 : case lds_fn: /* lang_decl_fn. */
6081 : 322959 : RB (lang->u.fn.global_ctor_p);
6082 : 322959 : RB (lang->u.fn.global_dtor_p);
6083 : 322959 : RB (lang->u.fn.static_function);
6084 : 322959 : RB (lang->u.fn.pure_virtual);
6085 : 322959 : RB (lang->u.fn.defaulted_p);
6086 : 322959 : RB (lang->u.fn.has_in_charge_parm_p);
6087 : 322959 : RB (lang->u.fn.has_vtt_parm_p);
6088 : 322959 : RB (lang->u.fn.nonconverting);
6089 : 322959 : RB (lang->u.fn.thunk_p);
6090 : 322959 : RB (lang->u.fn.this_thunk_p);
6091 : : /* lang->u.fn.hidden_friend_p is not streamed. */
6092 : 322959 : RB (lang->u.fn.omp_declare_reduction_p);
6093 : 322959 : RB (lang->u.fn.has_dependent_explicit_spec_p);
6094 : 322959 : RB (lang->u.fn.immediate_fn_p);
6095 : 322959 : RB (lang->u.fn.maybe_deleted);
6096 : 322959 : RB (lang->u.fn.implicit_constexpr);
6097 : 322959 : RB (lang->u.fn.escalated_p);
6098 : 322959 : RB (lang->u.fn.xobj_func);
6099 : 322959 : goto lds_min;
6100 : :
6101 : 1303 : case lds_decomp: /* lang_decl_decomp. */
6102 : : /* No bools. */
6103 : 1303 : goto lds_min;
6104 : :
6105 : : case lds_min: /* lang_decl_min. */
6106 : 1463483 : lds_min:
6107 : : /* No bools. */
6108 : : break;
6109 : :
6110 : : case lds_ns: /* lang_decl_ns. */
6111 : : /* No bools. */
6112 : : break;
6113 : :
6114 : : case lds_parm: /* lang_decl_parm. */
6115 : : /* No bools. */
6116 : : break;
6117 : : }
6118 : : #undef RB
6119 : 1463483 : return !get_overrun ();
6120 : : }
6121 : :
6122 : : void
6123 : 144322 : trees_out::lang_type_bools (tree t, bits_out& bits)
6124 : : {
6125 : : #define WB(X) (bits.b (X))
6126 : 144322 : const struct lang_type *lang = TYPE_LANG_SPECIFIC (t);
6127 : :
6128 : 144322 : bits.bflush ();
6129 : 144322 : WB (lang->has_type_conversion);
6130 : 144322 : WB (lang->has_copy_ctor);
6131 : 144322 : WB (lang->has_default_ctor);
6132 : 144322 : WB (lang->const_needs_init);
6133 : 144322 : WB (lang->ref_needs_init);
6134 : 144322 : WB (lang->has_const_copy_assign);
6135 : 144322 : WB ((lang->use_template >> 0) & 1);
6136 : 144322 : WB ((lang->use_template >> 1) & 1);
6137 : :
6138 : 144322 : WB (lang->has_mutable);
6139 : 144322 : WB (lang->com_interface);
6140 : 144322 : WB (lang->non_pod_class);
6141 : 144322 : WB (lang->nearly_empty_p);
6142 : 144322 : WB (lang->user_align);
6143 : 144322 : WB (lang->has_copy_assign);
6144 : 144322 : WB (lang->has_new);
6145 : 144322 : WB (lang->has_array_new);
6146 : :
6147 : 144322 : WB ((lang->gets_delete >> 0) & 1);
6148 : 144322 : WB ((lang->gets_delete >> 1) & 1);
6149 : 144322 : WB (lang->interface_only);
6150 : 144322 : WB (lang->interface_unknown);
6151 : 144322 : WB (lang->contains_empty_class_p);
6152 : 144322 : WB (lang->anon_aggr);
6153 : 144322 : WB (lang->non_zero_init);
6154 : 144322 : WB (lang->empty_p);
6155 : :
6156 : 144322 : WB (lang->vec_new_uses_cookie);
6157 : 144322 : WB (lang->declared_class);
6158 : 144322 : WB (lang->diamond_shaped);
6159 : 144322 : WB (lang->repeated_base);
6160 : 144322 : gcc_assert (!lang->being_defined);
6161 : : // lang->debug_requested
6162 : 144322 : WB (lang->fields_readonly);
6163 : 144322 : WB (lang->ptrmemfunc_flag);
6164 : :
6165 : 144322 : WB (lang->lazy_default_ctor);
6166 : 144322 : WB (lang->lazy_copy_ctor);
6167 : 144322 : WB (lang->lazy_copy_assign);
6168 : 144322 : WB (lang->lazy_destructor);
6169 : 144322 : WB (lang->has_const_copy_ctor);
6170 : 144322 : WB (lang->has_complex_copy_ctor);
6171 : 144322 : WB (lang->has_complex_copy_assign);
6172 : 144322 : WB (lang->non_aggregate);
6173 : :
6174 : 144322 : WB (lang->has_complex_dflt);
6175 : 144322 : WB (lang->has_list_ctor);
6176 : 144322 : WB (lang->non_std_layout);
6177 : 144322 : WB (lang->is_literal);
6178 : 144322 : WB (lang->lazy_move_ctor);
6179 : 144322 : WB (lang->lazy_move_assign);
6180 : 144322 : WB (lang->has_complex_move_ctor);
6181 : 144322 : WB (lang->has_complex_move_assign);
6182 : :
6183 : 144322 : WB (lang->has_constexpr_ctor);
6184 : 144322 : WB (lang->unique_obj_representations);
6185 : 144322 : WB (lang->unique_obj_representations_set);
6186 : : #undef WB
6187 : 144322 : }
6188 : :
6189 : : bool
6190 : 110905 : trees_in::lang_type_bools (tree t, bits_in& bits)
6191 : : {
6192 : : #define RB(X) ((X) = bits.b ())
6193 : 110905 : struct lang_type *lang = TYPE_LANG_SPECIFIC (t);
6194 : :
6195 : 110905 : bits.bflush ();
6196 : 110905 : RB (lang->has_type_conversion);
6197 : 110905 : RB (lang->has_copy_ctor);
6198 : 110905 : RB (lang->has_default_ctor);
6199 : 110905 : RB (lang->const_needs_init);
6200 : 110905 : RB (lang->ref_needs_init);
6201 : 110905 : RB (lang->has_const_copy_assign);
6202 : 110905 : unsigned v;
6203 : 110905 : v = bits.b () << 0;
6204 : 110905 : v |= bits.b () << 1;
6205 : 110905 : lang->use_template = v;
6206 : :
6207 : 110905 : RB (lang->has_mutable);
6208 : 110905 : RB (lang->com_interface);
6209 : 110905 : RB (lang->non_pod_class);
6210 : 110905 : RB (lang->nearly_empty_p);
6211 : 110905 : RB (lang->user_align);
6212 : 110905 : RB (lang->has_copy_assign);
6213 : 110905 : RB (lang->has_new);
6214 : 110905 : RB (lang->has_array_new);
6215 : :
6216 : 110905 : v = bits.b () << 0;
6217 : 110905 : v |= bits.b () << 1;
6218 : 110905 : lang->gets_delete = v;
6219 : 110905 : RB (lang->interface_only);
6220 : 110905 : RB (lang->interface_unknown);
6221 : 110905 : RB (lang->contains_empty_class_p);
6222 : 110905 : RB (lang->anon_aggr);
6223 : 110905 : RB (lang->non_zero_init);
6224 : 110905 : RB (lang->empty_p);
6225 : :
6226 : 110905 : RB (lang->vec_new_uses_cookie);
6227 : 110905 : RB (lang->declared_class);
6228 : 110905 : RB (lang->diamond_shaped);
6229 : 110905 : RB (lang->repeated_base);
6230 : 110905 : gcc_assert (!lang->being_defined);
6231 : 110905 : gcc_assert (!lang->debug_requested);
6232 : 110905 : RB (lang->fields_readonly);
6233 : 110905 : RB (lang->ptrmemfunc_flag);
6234 : :
6235 : 110905 : RB (lang->lazy_default_ctor);
6236 : 110905 : RB (lang->lazy_copy_ctor);
6237 : 110905 : RB (lang->lazy_copy_assign);
6238 : 110905 : RB (lang->lazy_destructor);
6239 : 110905 : RB (lang->has_const_copy_ctor);
6240 : 110905 : RB (lang->has_complex_copy_ctor);
6241 : 110905 : RB (lang->has_complex_copy_assign);
6242 : 110905 : RB (lang->non_aggregate);
6243 : :
6244 : 110905 : RB (lang->has_complex_dflt);
6245 : 110905 : RB (lang->has_list_ctor);
6246 : 110905 : RB (lang->non_std_layout);
6247 : 110905 : RB (lang->is_literal);
6248 : 110905 : RB (lang->lazy_move_ctor);
6249 : 110905 : RB (lang->lazy_move_assign);
6250 : 110905 : RB (lang->has_complex_move_ctor);
6251 : 110905 : RB (lang->has_complex_move_assign);
6252 : :
6253 : 110905 : RB (lang->has_constexpr_ctor);
6254 : 110905 : RB (lang->unique_obj_representations);
6255 : 110905 : RB (lang->unique_obj_representations_set);
6256 : : #undef RB
6257 : 110905 : return !get_overrun ();
6258 : : }
6259 : :
6260 : : /* Read & write the core values and pointers. */
6261 : :
6262 : : void
6263 : 31886026 : trees_out::core_vals (tree t)
6264 : : {
6265 : : #define WU(X) (u (X))
6266 : : #define WT(X) (tree_node (X))
6267 : 31886026 : tree_code code = TREE_CODE (t);
6268 : :
6269 : : /* First by shape of the tree. */
6270 : :
6271 : 31886026 : if (CODE_CONTAINS_STRUCT (code, TS_DECL_MINIMAL))
6272 : : {
6273 : : /* Write this early, for better log information. */
6274 : 7217015 : WT (t->decl_minimal.name);
6275 : 7217015 : if (!DECL_TEMPLATE_PARM_P (t))
6276 : 5592102 : WT (t->decl_minimal.context);
6277 : :
6278 : 7217015 : if (state)
6279 : 5955158 : state->write_location (*this, t->decl_minimal.locus);
6280 : :
6281 : 7217015 : if (streaming_p ())
6282 : 2771625 : if (has_warning_spec (t))
6283 : 483 : u (get_warning_spec (t));
6284 : : }
6285 : :
6286 : 31886026 : if (CODE_CONTAINS_STRUCT (code, TS_TYPE_COMMON))
6287 : : {
6288 : : /* The only types we write also have TYPE_NON_COMMON. */
6289 : 1804216 : gcc_checking_assert (CODE_CONTAINS_STRUCT (code, TS_TYPE_NON_COMMON));
6290 : :
6291 : : /* We only stream the main variant. */
6292 : 1804216 : gcc_checking_assert (TYPE_MAIN_VARIANT (t) == t);
6293 : :
6294 : : /* Stream the name & context first, for better log information */
6295 : 1804216 : WT (t->type_common.name);
6296 : 1804216 : WT (t->type_common.context);
6297 : :
6298 : : /* By construction we want to make sure we have the canonical
6299 : : and main variants already in the type table, so emit them
6300 : : now. */
6301 : 1804216 : WT (t->type_common.main_variant);
6302 : :
6303 : 1804216 : tree canonical = t->type_common.canonical;
6304 : 1804216 : if (canonical && DECL_TEMPLATE_PARM_P (TYPE_NAME (t)))
6305 : : /* We do not want to wander into different templates.
6306 : : Reconstructed on stream in. */
6307 : : canonical = t;
6308 : 1804216 : WT (canonical);
6309 : :
6310 : : /* type_common.next_variant is internally manipulated. */
6311 : : /* type_common.pointer_to, type_common.reference_to. */
6312 : :
6313 : 1804216 : if (streaming_p ())
6314 : : {
6315 : 501799 : WU (t->type_common.precision);
6316 : 501799 : WU (t->type_common.contains_placeholder_bits);
6317 : 501799 : WU (t->type_common.mode);
6318 : 501799 : WU (t->type_common.align);
6319 : : }
6320 : :
6321 : 1804216 : if (!RECORD_OR_UNION_CODE_P (code))
6322 : : {
6323 : 1472850 : WT (t->type_common.size);
6324 : 1472850 : WT (t->type_common.size_unit);
6325 : : }
6326 : 1804216 : WT (t->type_common.attributes);
6327 : :
6328 : 1804216 : WT (t->type_common.common.chain); /* TYPE_STUB_DECL. */
6329 : : }
6330 : :
6331 : 31886026 : if (CODE_CONTAINS_STRUCT (code, TS_DECL_COMMON))
6332 : : {
6333 : 7217015 : if (streaming_p ())
6334 : : {
6335 : 2771625 : WU (t->decl_common.mode);
6336 : 2771625 : WU (t->decl_common.off_align);
6337 : 2771625 : WU (t->decl_common.align);
6338 : : }
6339 : :
6340 : : /* For templates these hold instantiation (partial and/or
6341 : : specialization) information. */
6342 : 7217015 : if (code != TEMPLATE_DECL)
6343 : : {
6344 : 6652180 : WT (t->decl_common.size);
6345 : 6652180 : WT (t->decl_common.size_unit);
6346 : : }
6347 : :
6348 : 7217015 : WT (t->decl_common.attributes);
6349 : : // FIXME: Does this introduce cross-decl links? For instance
6350 : : // from instantiation to the template. If so, we'll need more
6351 : : // deduplication logic. I think we'll need to walk the blocks
6352 : : // of the owning function_decl's abstract origin in tandem, to
6353 : : // generate the locating data needed?
6354 : 7217015 : WT (t->decl_common.abstract_origin);
6355 : : }
6356 : :
6357 : 31886026 : if (CODE_CONTAINS_STRUCT (code, TS_DECL_WITH_VIS))
6358 : : {
6359 : 3447966 : WT (t->decl_with_vis.assembler_name);
6360 : 3447966 : if (streaming_p ())
6361 : 1318984 : WU (t->decl_with_vis.visibility);
6362 : : }
6363 : :
6364 : 31886026 : if (CODE_CONTAINS_STRUCT (code, TS_TYPE_NON_COMMON))
6365 : : {
6366 : 1804216 : if (code == ENUMERAL_TYPE)
6367 : : {
6368 : : /* These fields get set even for opaque enums that lack a
6369 : : definition, so we stream them directly for each ENUMERAL_TYPE.
6370 : : We stream TYPE_VALUES as part of the definition. */
6371 : 7601 : WT (t->type_non_common.maxval);
6372 : 7601 : WT (t->type_non_common.minval);
6373 : : }
6374 : : /* Records and unions hold FIELDS, VFIELD & BINFO on these
6375 : : things. */
6376 : 1796615 : else if (!RECORD_OR_UNION_CODE_P (code))
6377 : : {
6378 : : // FIXME: These are from tpl_parm_value's 'type' writing.
6379 : : // Perhaps it should just be doing them directly?
6380 : 1465249 : gcc_checking_assert (code == TEMPLATE_TYPE_PARM
6381 : : || code == TEMPLATE_TEMPLATE_PARM
6382 : : || code == BOUND_TEMPLATE_TEMPLATE_PARM);
6383 : 1465249 : gcc_checking_assert (!TYPE_CACHED_VALUES_P (t));
6384 : 1465249 : WT (t->type_non_common.values);
6385 : 1465249 : WT (t->type_non_common.maxval);
6386 : 1465249 : WT (t->type_non_common.minval);
6387 : : }
6388 : :
6389 : 1804216 : WT (t->type_non_common.lang_1);
6390 : : }
6391 : :
6392 : 31886026 : if (CODE_CONTAINS_STRUCT (code, TS_EXP))
6393 : : {
6394 : 9390422 : if (state)
6395 : 9204550 : state->write_location (*this, t->exp.locus);
6396 : :
6397 : 9390422 : if (streaming_p ())
6398 : 4558557 : if (has_warning_spec (t))
6399 : 362181 : u (get_warning_spec (t));
6400 : :
6401 : : /* Walk in forward order, as (for instance) REQUIRES_EXPR has a
6402 : : bunch of unscoped parms on its first operand. It's safer to
6403 : : create those in order. */
6404 : 9390422 : bool vl = TREE_CODE_CLASS (code) == tcc_vl_exp;
6405 : 26047720 : for (unsigned limit = (vl ? VL_EXP_OPERAND_LENGTH (t)
6406 : 9390422 : : TREE_OPERAND_LENGTH (t)),
6407 : 26047720 : ix = unsigned (vl); ix != limit; ix++)
6408 : 16657298 : WT (TREE_OPERAND (t, ix));
6409 : : }
6410 : : else
6411 : : /* The CODE_CONTAINS tables were inaccurate when I started. */
6412 : 22495604 : gcc_checking_assert (TREE_CODE_CLASS (code) != tcc_expression
6413 : : && TREE_CODE_CLASS (code) != tcc_binary
6414 : : && TREE_CODE_CLASS (code) != tcc_unary
6415 : : && TREE_CODE_CLASS (code) != tcc_reference
6416 : : && TREE_CODE_CLASS (code) != tcc_comparison
6417 : : && TREE_CODE_CLASS (code) != tcc_statement
6418 : : && TREE_CODE_CLASS (code) != tcc_vl_exp);
6419 : :
6420 : : /* Then by CODE. Special cases and/or 1:1 tree shape
6421 : : correspondance. */
6422 : 31886026 : switch (code)
6423 : : {
6424 : : default:
6425 : : break;
6426 : :
6427 : 0 : case ARGUMENT_PACK_SELECT: /* Transient during instantiation. */
6428 : 0 : case DEFERRED_PARSE: /* Expanded upon completion of
6429 : : outermost class. */
6430 : 0 : case IDENTIFIER_NODE: /* Streamed specially. */
6431 : 0 : case BINDING_VECTOR: /* Only in namespace-scope symbol
6432 : : table. */
6433 : 0 : case SSA_NAME:
6434 : 0 : case TRANSLATION_UNIT_DECL: /* There is only one, it is a
6435 : : global_tree. */
6436 : 0 : case USERDEF_LITERAL: /* Expanded during parsing. */
6437 : 0 : gcc_unreachable (); /* Should never meet. */
6438 : :
6439 : : /* Constants. */
6440 : 18 : case COMPLEX_CST:
6441 : 18 : WT (TREE_REALPART (t));
6442 : 18 : WT (TREE_IMAGPART (t));
6443 : 18 : break;
6444 : :
6445 : 0 : case FIXED_CST:
6446 : 0 : gcc_unreachable (); /* Not supported in C++. */
6447 : :
6448 : 3031228 : case INTEGER_CST:
6449 : 3031228 : if (streaming_p ())
6450 : : {
6451 : 557323 : unsigned num = TREE_INT_CST_EXT_NUNITS (t);
6452 : 1116308 : for (unsigned ix = 0; ix != num; ix++)
6453 : 558985 : wu (TREE_INT_CST_ELT (t, ix));
6454 : : }
6455 : : break;
6456 : :
6457 : 0 : case POLY_INT_CST:
6458 : 0 : if (streaming_p ())
6459 : 0 : for (unsigned ix = 0; ix != NUM_POLY_INT_COEFFS; ix++)
6460 : 0 : WT (POLY_INT_CST_COEFF (t, ix));
6461 : : break;
6462 : :
6463 : 28896 : case REAL_CST:
6464 : 28896 : if (streaming_p ())
6465 : 14424 : buf (TREE_REAL_CST_PTR (t), sizeof (real_value));
6466 : : break;
6467 : :
6468 : : case STRING_CST:
6469 : : /* Streamed during start. */
6470 : : break;
6471 : :
6472 : 36 : case RAW_DATA_CST:
6473 : 36 : if (RAW_DATA_OWNER (t) == NULL_TREE)
6474 : : break; /* Streamed as STRING_CST during start. */
6475 : 24 : WT (RAW_DATA_OWNER (t));
6476 : 24 : if (streaming_p ())
6477 : : {
6478 : 12 : if (TREE_CODE (RAW_DATA_OWNER (t)) == RAW_DATA_CST)
6479 : 6 : z (RAW_DATA_POINTER (t) - RAW_DATA_POINTER (RAW_DATA_OWNER (t)));
6480 : 6 : else if (TREE_CODE (RAW_DATA_OWNER (t)) == STRING_CST)
6481 : 6 : z (RAW_DATA_POINTER (t)
6482 : 6 : - TREE_STRING_POINTER (RAW_DATA_OWNER (t)));
6483 : : else
6484 : 0 : gcc_unreachable ();
6485 : : }
6486 : : break;
6487 : :
6488 : 36 : case VECTOR_CST:
6489 : 102 : for (unsigned ix = vector_cst_encoded_nelts (t); ix--;)
6490 : 66 : WT (VECTOR_CST_ENCODED_ELT (t, ix));
6491 : : break;
6492 : :
6493 : : /* Decls. */
6494 : 381971 : case VAR_DECL:
6495 : 381971 : if (DECL_CONTEXT (t)
6496 : 381971 : && TREE_CODE (DECL_CONTEXT (t)) != FUNCTION_DECL)
6497 : : {
6498 : 102705 : if (DECL_HAS_VALUE_EXPR_P (t))
6499 : 18 : WT (DECL_VALUE_EXPR (t));
6500 : : break;
6501 : : }
6502 : : /* FALLTHROUGH */
6503 : :
6504 : 3206114 : case RESULT_DECL:
6505 : 3206114 : case PARM_DECL:
6506 : 3206114 : if (DECL_HAS_VALUE_EXPR_P (t))
6507 : 25631 : WT (DECL_VALUE_EXPR (t));
6508 : : /* FALLTHROUGH */
6509 : :
6510 : 3343987 : case CONST_DECL:
6511 : 3343987 : case IMPORTED_DECL:
6512 : 3343987 : WT (t->decl_common.initial);
6513 : 3343987 : break;
6514 : :
6515 : 98633 : case FIELD_DECL:
6516 : 98633 : WT (t->field_decl.offset);
6517 : 98633 : WT (t->field_decl.bit_field_type);
6518 : 98633 : {
6519 : 98633 : auto ovr = make_temp_override (walking_bit_field_unit, true);
6520 : 98633 : WT (t->field_decl.qualifier); /* bitfield unit. */
6521 : 98633 : }
6522 : 98633 : WT (t->field_decl.bit_offset);
6523 : 98633 : WT (t->field_decl.fcontext);
6524 : 98633 : WT (t->decl_common.initial);
6525 : 98633 : break;
6526 : :
6527 : 29472 : case LABEL_DECL:
6528 : 29472 : if (streaming_p ())
6529 : : {
6530 : 14736 : WU (t->label_decl.label_decl_uid);
6531 : 14736 : WU (t->label_decl.eh_landing_pad_nr);
6532 : : }
6533 : : break;
6534 : :
6535 : 755967 : case FUNCTION_DECL:
6536 : 755967 : if (streaming_p ())
6537 : : {
6538 : : /* Builtins can be streamed by value when a header declares
6539 : : them. */
6540 : 377900 : WU (DECL_BUILT_IN_CLASS (t));
6541 : 377900 : if (DECL_BUILT_IN_CLASS (t) != NOT_BUILT_IN)
6542 : 7682 : WU (DECL_UNCHECKED_FUNCTION_CODE (t));
6543 : : }
6544 : :
6545 : 755967 : WT (t->function_decl.personality);
6546 : 755967 : WT (t->function_decl.function_specific_target);
6547 : 755967 : WT (t->function_decl.function_specific_optimization);
6548 : 755967 : WT (t->function_decl.vindex);
6549 : :
6550 : 755967 : if (DECL_HAS_DEPENDENT_EXPLICIT_SPEC_P (t))
6551 : 4462 : WT (lookup_explicit_specifier (t));
6552 : : break;
6553 : :
6554 : 94861 : case USING_DECL:
6555 : : /* USING_DECL_DECLS */
6556 : 94861 : WT (t->decl_common.initial);
6557 : : /* FALLTHROUGH */
6558 : :
6559 : 2309795 : case TYPE_DECL:
6560 : : /* USING_DECL: USING_DECL_SCOPE */
6561 : : /* TYPE_DECL: DECL_ORIGINAL_TYPE */
6562 : 2309795 : WT (t->decl_non_common.result);
6563 : 2309795 : break;
6564 : :
6565 : : /* Miscellaneous common nodes. */
6566 : 483230 : case BLOCK:
6567 : 483230 : if (state)
6568 : : {
6569 : 483230 : state->write_location (*this, t->block.locus);
6570 : 483230 : state->write_location (*this, t->block.end_locus);
6571 : : }
6572 : :
6573 : : /* DECL_LOCAL_DECL_P decls are first encountered here and
6574 : : streamed by value. */
6575 : 716722 : for (tree decls = t->block.vars; decls; decls = DECL_CHAIN (decls))
6576 : : {
6577 : 233492 : if (VAR_OR_FUNCTION_DECL_P (decls)
6578 : 233492 : && DECL_LOCAL_DECL_P (decls))
6579 : : {
6580 : : /* Make sure this is the first encounter, and mark for
6581 : : walk-by-value. */
6582 : 228 : gcc_checking_assert (!TREE_VISITED (decls)
6583 : : && !DECL_TEMPLATE_INFO (decls));
6584 : 228 : mark_by_value (decls);
6585 : : }
6586 : 233492 : tree_node (decls);
6587 : : }
6588 : 483230 : tree_node (NULL_TREE);
6589 : :
6590 : : /* nonlocalized_vars is a middle-end thing. */
6591 : 483230 : WT (t->block.subblocks);
6592 : 483230 : WT (t->block.supercontext);
6593 : : // FIXME: As for decl's abstract_origin, does this introduce crosslinks?
6594 : 483230 : WT (t->block.abstract_origin);
6595 : : /* fragment_origin, fragment_chain are middle-end things. */
6596 : 483230 : WT (t->block.chain);
6597 : : /* nonlocalized_vars, block_num & die are middle endy/debug
6598 : : things. */
6599 : 483230 : break;
6600 : :
6601 : 940919 : case CALL_EXPR:
6602 : 940919 : if (streaming_p ())
6603 : 448831 : WU (t->base.u.ifn);
6604 : : break;
6605 : :
6606 : : case CONSTRUCTOR:
6607 : : // This must be streamed /after/ we've streamed the type,
6608 : : // because it can directly refer to elements of the type. Eg,
6609 : : // FIELD_DECLs of a RECORD_TYPE.
6610 : : break;
6611 : :
6612 : 24 : case OMP_CLAUSE:
6613 : 24 : {
6614 : : /* The ompcode is serialized in start. */
6615 : 24 : if (streaming_p ())
6616 : 12 : WU (t->omp_clause.subcode.map_kind);
6617 : 24 : if (state)
6618 : 24 : state->write_location (*this, t->omp_clause.locus);
6619 : :
6620 : 24 : unsigned len = omp_clause_num_ops[OMP_CLAUSE_CODE (t)];
6621 : 78 : for (unsigned ix = 0; ix != len; ix++)
6622 : 54 : WT (t->omp_clause.ops[ix]);
6623 : : }
6624 : : break;
6625 : :
6626 : 351183 : case STATEMENT_LIST:
6627 : 1378375 : for (tree stmt : tsi_range (t))
6628 : 1027192 : if (stmt)
6629 : 1027192 : WT (stmt);
6630 : 351183 : WT (NULL_TREE);
6631 : 351183 : break;
6632 : :
6633 : 0 : case OPTIMIZATION_NODE:
6634 : 0 : case TARGET_OPTION_NODE:
6635 : : // FIXME: Our representation for these two nodes is a cache of
6636 : : // the resulting set of options. Not a record of the options
6637 : : // that got changed by a particular attribute or pragma. Should
6638 : : // we record that, or should we record the diff from the command
6639 : : // line options? The latter seems the right behaviour, but is
6640 : : // (a) harder, and I guess could introduce strangeness if the
6641 : : // importer has set some incompatible set of optimization flags?
6642 : 0 : gcc_unreachable ();
6643 : 199448 : break;
6644 : :
6645 : 199448 : case TREE_BINFO:
6646 : 199448 : {
6647 : 199448 : WT (t->binfo.common.chain);
6648 : 199448 : WT (t->binfo.offset);
6649 : 199448 : WT (t->binfo.inheritance);
6650 : 199448 : WT (t->binfo.vptr_field);
6651 : :
6652 : 199448 : WT (t->binfo.vtable);
6653 : 199448 : WT (t->binfo.virtuals);
6654 : 199448 : WT (t->binfo.vtt_subvtt);
6655 : 199448 : WT (t->binfo.vtt_vptr);
6656 : :
6657 : 199448 : tree_vec (BINFO_BASE_ACCESSES (t));
6658 : 199448 : unsigned num = vec_safe_length (BINFO_BASE_ACCESSES (t));
6659 : 256980 : for (unsigned ix = 0; ix != num; ix++)
6660 : 57532 : WT (BINFO_BASE_BINFO (t, ix));
6661 : : }
6662 : : break;
6663 : :
6664 : 2465200 : case TREE_LIST:
6665 : 2465200 : WT (t->list.purpose);
6666 : 2465200 : WT (t->list.value);
6667 : 2465200 : WT (t->list.common.chain);
6668 : 2465200 : break;
6669 : :
6670 : 2115167 : case TREE_VEC:
6671 : 5792900 : for (unsigned ix = TREE_VEC_LENGTH (t); ix--;)
6672 : 3677733 : WT (TREE_VEC_ELT (t, ix));
6673 : : /* We stash NON_DEFAULT_TEMPLATE_ARGS_COUNT on TREE_CHAIN! */
6674 : 2115167 : gcc_checking_assert (!t->type_common.common.chain
6675 : : || (TREE_CODE (t->type_common.common.chain)
6676 : : == INTEGER_CST));
6677 : 2115167 : WT (t->type_common.common.chain);
6678 : 2115167 : break;
6679 : :
6680 : : /* C++-specific nodes ... */
6681 : 166163 : case BASELINK:
6682 : 166163 : WT (((lang_tree_node *)t)->baselink.binfo);
6683 : 166163 : WT (((lang_tree_node *)t)->baselink.functions);
6684 : 166163 : WT (((lang_tree_node *)t)->baselink.access_binfo);
6685 : 166163 : break;
6686 : :
6687 : 68763 : case CONSTRAINT_INFO:
6688 : 68763 : WT (((lang_tree_node *)t)->constraint_info.template_reqs);
6689 : 68763 : WT (((lang_tree_node *)t)->constraint_info.declarator_reqs);
6690 : 68763 : WT (((lang_tree_node *)t)->constraint_info.associated_constr);
6691 : 68763 : break;
6692 : :
6693 : 11474 : case DEFERRED_NOEXCEPT:
6694 : 11474 : WT (((lang_tree_node *)t)->deferred_noexcept.pattern);
6695 : 11474 : WT (((lang_tree_node *)t)->deferred_noexcept.args);
6696 : 11474 : break;
6697 : :
6698 : 8806 : case LAMBDA_EXPR:
6699 : 8806 : WT (((lang_tree_node *)t)->lambda_expression.capture_list);
6700 : 8806 : WT (((lang_tree_node *)t)->lambda_expression.this_capture);
6701 : 8806 : WT (((lang_tree_node *)t)->lambda_expression.extra_scope);
6702 : 8806 : WT (((lang_tree_node *)t)->lambda_expression.regen_info);
6703 : 8806 : WT (((lang_tree_node *)t)->lambda_expression.extra_args);
6704 : : /* pending_proxies is a parse-time thing. */
6705 : 8806 : gcc_assert (!((lang_tree_node *)t)->lambda_expression.pending_proxies);
6706 : 8806 : if (state)
6707 : 8806 : state->write_location
6708 : 8806 : (*this, ((lang_tree_node *)t)->lambda_expression.locus);
6709 : 8806 : if (streaming_p ())
6710 : : {
6711 : 3013 : WU (((lang_tree_node *)t)->lambda_expression.default_capture_mode);
6712 : 3013 : WU (((lang_tree_node *)t)->lambda_expression.discriminator_scope);
6713 : 3013 : WU (((lang_tree_node *)t)->lambda_expression.discriminator_sig);
6714 : : }
6715 : : break;
6716 : :
6717 : 1486102 : case OVERLOAD:
6718 : 1486102 : WT (((lang_tree_node *)t)->overload.function);
6719 : 1486102 : WT (t->common.chain);
6720 : 1486102 : break;
6721 : :
6722 : 0 : case PTRMEM_CST:
6723 : 0 : WT (((lang_tree_node *)t)->ptrmem.member);
6724 : 0 : break;
6725 : :
6726 : 13040 : case STATIC_ASSERT:
6727 : 13040 : WT (((lang_tree_node *)t)->static_assertion.condition);
6728 : 13040 : WT (((lang_tree_node *)t)->static_assertion.message);
6729 : 13040 : if (state)
6730 : 13040 : state->write_location
6731 : 13040 : (*this, ((lang_tree_node *)t)->static_assertion.location);
6732 : : break;
6733 : :
6734 : 564835 : case TEMPLATE_DECL:
6735 : : /* Streamed with the template_decl node itself. */
6736 : 564835 : gcc_checking_assert
6737 : : (TREE_VISITED (((lang_tree_node *)t)->template_decl.arguments));
6738 : 564835 : gcc_checking_assert
6739 : : (TREE_VISITED (((lang_tree_node *)t)->template_decl.result));
6740 : 564835 : if (DECL_UNINSTANTIATED_TEMPLATE_FRIEND_P (t))
6741 : 9948 : WT (DECL_CHAIN (t));
6742 : : break;
6743 : :
6744 : 1265622 : case TEMPLATE_INFO:
6745 : 1265622 : {
6746 : 1265622 : WT (((lang_tree_node *)t)->template_info.tmpl);
6747 : 1265622 : WT (((lang_tree_node *)t)->template_info.args);
6748 : 1265622 : WT (((lang_tree_node *)t)->template_info.partial);
6749 : :
6750 : 1265622 : const auto *ac = (((lang_tree_node *)t)
6751 : : ->template_info.deferred_access_checks);
6752 : 1265622 : unsigned len = vec_safe_length (ac);
6753 : 1265622 : if (streaming_p ())
6754 : 631367 : u (len);
6755 : 1265622 : if (len)
6756 : : {
6757 : 0 : for (unsigned ix = 0; ix != len; ix++)
6758 : : {
6759 : 0 : const auto &m = (*ac)[ix];
6760 : 0 : WT (m.binfo);
6761 : 0 : WT (m.decl);
6762 : 0 : WT (m.diag_decl);
6763 : 0 : if (state)
6764 : 0 : state->write_location (*this, m.loc);
6765 : : }
6766 : : }
6767 : : }
6768 : : break;
6769 : :
6770 : 1567475 : case TEMPLATE_PARM_INDEX:
6771 : 1567475 : if (streaming_p ())
6772 : : {
6773 : 352759 : WU (((lang_tree_node *)t)->tpi.index);
6774 : 352759 : WU (((lang_tree_node *)t)->tpi.level);
6775 : 352759 : WU (((lang_tree_node *)t)->tpi.orig_level);
6776 : : }
6777 : 1567475 : WT (((lang_tree_node *)t)->tpi.decl);
6778 : : /* TEMPLATE_PARM_DESCENDANTS (AKA TREE_CHAIN) is an internal
6779 : : cache, do not stream. */
6780 : 1567475 : break;
6781 : :
6782 : 24441 : case TRAIT_EXPR:
6783 : 24441 : WT (((lang_tree_node *)t)->trait_expression.type1);
6784 : 24441 : WT (((lang_tree_node *)t)->trait_expression.type2);
6785 : 24441 : if (streaming_p ())
6786 : 9401 : WU (((lang_tree_node *)t)->trait_expression.kind);
6787 : : break;
6788 : : }
6789 : :
6790 : 31886026 : if (CODE_CONTAINS_STRUCT (code, TS_TYPED))
6791 : : {
6792 : : /* We want to stream the type of a expression-like nodes /after/
6793 : : we've streamed the operands. The type often contains (bits
6794 : : of the) types of the operands, and with things like decltype
6795 : : and noexcept in play, we really want to stream the decls
6796 : : defining the type before we try and stream the type on its
6797 : : own. Otherwise we can find ourselves trying to read in a
6798 : : decl, when we're already partially reading in a component of
6799 : : its type. And that's bad. */
6800 : 30043897 : tree type = t->typed.type;
6801 : 30043897 : unsigned prec = 0;
6802 : :
6803 : 30043897 : switch (code)
6804 : : {
6805 : : default:
6806 : : break;
6807 : :
6808 : : case TEMPLATE_DECL:
6809 : : /* We fill in the template's type separately. */
6810 : 30043897 : type = NULL_TREE;
6811 : : break;
6812 : :
6813 : 2214934 : case TYPE_DECL:
6814 : 2214934 : if (DECL_ORIGINAL_TYPE (t) && t == TYPE_NAME (type))
6815 : : /* This is a typedef. We set its type separately. */
6816 : : type = NULL_TREE;
6817 : : break;
6818 : :
6819 : 7601 : case ENUMERAL_TYPE:
6820 : 7601 : if (type && !ENUM_FIXED_UNDERLYING_TYPE_P (t))
6821 : : {
6822 : : /* Type is a restricted range integer type derived from the
6823 : : integer_types. Find the right one. */
6824 : 4797 : prec = TYPE_PRECISION (type);
6825 : 4797 : tree name = DECL_NAME (TYPE_NAME (type));
6826 : :
6827 : 62685 : for (unsigned itk = itk_none; itk--;)
6828 : 62685 : if (integer_types[itk]
6829 : 62685 : && DECL_NAME (TYPE_NAME (integer_types[itk])) == name)
6830 : : {
6831 : : type = integer_types[itk];
6832 : : break;
6833 : : }
6834 : 4797 : gcc_assert (type != t->typed.type);
6835 : : }
6836 : : break;
6837 : : }
6838 : :
6839 : 30043897 : WT (type);
6840 : 30043897 : if (prec && streaming_p ())
6841 : 2397 : WU (prec);
6842 : : }
6843 : :
6844 : 31886026 : if (TREE_CODE (t) == CONSTRUCTOR)
6845 : : {
6846 : 101232 : unsigned len = vec_safe_length (t->constructor.elts);
6847 : 101232 : if (streaming_p ())
6848 : 49953 : WU (len);
6849 : 101232 : if (len)
6850 : 320724 : for (unsigned ix = 0; ix != len; ix++)
6851 : : {
6852 : 273174 : const constructor_elt &elt = (*t->constructor.elts)[ix];
6853 : :
6854 : 273174 : WT (elt.index);
6855 : 273174 : WT (elt.value);
6856 : : }
6857 : : }
6858 : :
6859 : : #undef WT
6860 : : #undef WU
6861 : 31886026 : }
6862 : :
6863 : : // Streaming in a reference to a decl can cause that decl to be
6864 : : // TREE_USED, which is the mark_used behaviour we need most of the
6865 : : // time. The trees_in::unused can be incremented to inhibit this,
6866 : : // which is at least needed for vtables.
6867 : :
6868 : : bool
6869 : 10465858 : trees_in::core_vals (tree t)
6870 : : {
6871 : : #define RU(X) ((X) = u ())
6872 : : #define RUC(T,X) ((X) = T (u ()))
6873 : : #define RT(X) ((X) = tree_node ())
6874 : : #define RTU(X) ((X) = tree_node (true))
6875 : 10465858 : tree_code code = TREE_CODE (t);
6876 : :
6877 : : /* First by tree shape. */
6878 : 10465858 : if (CODE_CONTAINS_STRUCT (code, TS_DECL_MINIMAL))
6879 : : {
6880 : 2296155 : RT (t->decl_minimal.name);
6881 : 2296155 : if (!DECL_TEMPLATE_PARM_P (t))
6882 : 2001145 : RT (t->decl_minimal.context);
6883 : :
6884 : : /* Don't zap the locus just yet, we don't record it correctly
6885 : : and thus lose all location information. */
6886 : 2296155 : t->decl_minimal.locus = state->read_location (*this);
6887 : 2296155 : if (has_warning_spec (t))
6888 : 364 : put_warning_spec (t, u ());
6889 : : }
6890 : :
6891 : 10465858 : if (CODE_CONTAINS_STRUCT (code, TS_TYPE_COMMON))
6892 : : {
6893 : 393672 : RT (t->type_common.name);
6894 : 393672 : RT (t->type_common.context);
6895 : :
6896 : 393672 : RT (t->type_common.main_variant);
6897 : 393672 : RT (t->type_common.canonical);
6898 : :
6899 : : /* type_common.next_variant is internally manipulated. */
6900 : : /* type_common.pointer_to, type_common.reference_to. */
6901 : :
6902 : 393672 : RU (t->type_common.precision);
6903 : 393672 : RU (t->type_common.contains_placeholder_bits);
6904 : 393672 : RUC (machine_mode, t->type_common.mode);
6905 : 393672 : RU (t->type_common.align);
6906 : :
6907 : 393672 : if (!RECORD_OR_UNION_CODE_P (code))
6908 : : {
6909 : 267854 : RT (t->type_common.size);
6910 : 267854 : RT (t->type_common.size_unit);
6911 : : }
6912 : 393672 : RT (t->type_common.attributes);
6913 : :
6914 : 393672 : RT (t->type_common.common.chain); /* TYPE_STUB_DECL. */
6915 : : }
6916 : :
6917 : 10465858 : if (CODE_CONTAINS_STRUCT (code, TS_DECL_COMMON))
6918 : : {
6919 : 2296155 : RUC (machine_mode, t->decl_common.mode);
6920 : 2296155 : RU (t->decl_common.off_align);
6921 : 2296155 : RU (t->decl_common.align);
6922 : :
6923 : 2296155 : if (code != TEMPLATE_DECL)
6924 : : {
6925 : 2065938 : RT (t->decl_common.size);
6926 : 2065938 : RT (t->decl_common.size_unit);
6927 : : }
6928 : :
6929 : 2296155 : RT (t->decl_common.attributes);
6930 : 2296155 : RT (t->decl_common.abstract_origin);
6931 : : }
6932 : :
6933 : 10465858 : if (CODE_CONTAINS_STRUCT (code, TS_DECL_WITH_VIS))
6934 : : {
6935 : 1066902 : RT (t->decl_with_vis.assembler_name);
6936 : 1066902 : RUC (symbol_visibility, t->decl_with_vis.visibility);
6937 : : }
6938 : :
6939 : 10465858 : if (CODE_CONTAINS_STRUCT (code, TS_TYPE_NON_COMMON))
6940 : : {
6941 : 393672 : if (code == ENUMERAL_TYPE)
6942 : : {
6943 : : /* These fields get set even for opaque enums that lack a
6944 : : definition, so we stream them directly for each ENUMERAL_TYPE.
6945 : : We stream TYPE_VALUES as part of the definition. */
6946 : 2504 : RT (t->type_non_common.maxval);
6947 : 2504 : RT (t->type_non_common.minval);
6948 : : }
6949 : : /* Records and unions hold FIELDS, VFIELD & BINFO on these
6950 : : things. */
6951 : 391168 : else if (!RECORD_OR_UNION_CODE_P (code))
6952 : : {
6953 : : /* This is not clobbering TYPE_CACHED_VALUES, because this
6954 : : is a type that doesn't have any. */
6955 : 265350 : gcc_checking_assert (!TYPE_CACHED_VALUES_P (t));
6956 : 265350 : RT (t->type_non_common.values);
6957 : 265350 : RT (t->type_non_common.maxval);
6958 : 265350 : RT (t->type_non_common.minval);
6959 : : }
6960 : :
6961 : 393672 : RT (t->type_non_common.lang_1);
6962 : : }
6963 : :
6964 : 10465858 : if (CODE_CONTAINS_STRUCT (code, TS_EXP))
6965 : : {
6966 : 4000151 : t->exp.locus = state->read_location (*this);
6967 : 4000151 : if (has_warning_spec (t))
6968 : 317625 : put_warning_spec (t, u ());
6969 : :
6970 : 4000151 : bool vl = TREE_CODE_CLASS (code) == tcc_vl_exp;
6971 : 4000151 : for (unsigned limit = (vl ? VL_EXP_OPERAND_LENGTH (t)
6972 : 4000151 : : TREE_OPERAND_LENGTH (t)),
6973 : 11084077 : ix = unsigned (vl); ix != limit; ix++)
6974 : 7083926 : RTU (TREE_OPERAND (t, ix));
6975 : : }
6976 : :
6977 : : /* Then by CODE. Special cases and/or 1:1 tree shape
6978 : : correspondance. */
6979 : 10465858 : switch (code)
6980 : : {
6981 : : default:
6982 : : break;
6983 : :
6984 : : case ARGUMENT_PACK_SELECT:
6985 : : case DEFERRED_PARSE:
6986 : : case IDENTIFIER_NODE:
6987 : : case BINDING_VECTOR:
6988 : : case SSA_NAME:
6989 : : case TRANSLATION_UNIT_DECL:
6990 : : case USERDEF_LITERAL:
6991 : : return false; /* Should never meet. */
6992 : :
6993 : : /* Constants. */
6994 : 9 : case COMPLEX_CST:
6995 : 9 : RT (TREE_REALPART (t));
6996 : 9 : RT (TREE_IMAGPART (t));
6997 : 9 : break;
6998 : :
6999 : : case FIXED_CST:
7000 : : /* Not suported in C++. */
7001 : : return false;
7002 : :
7003 : 463411 : case INTEGER_CST:
7004 : 463411 : {
7005 : 463411 : unsigned num = TREE_INT_CST_EXT_NUNITS (t);
7006 : 928141 : for (unsigned ix = 0; ix != num; ix++)
7007 : 464730 : TREE_INT_CST_ELT (t, ix) = wu ();
7008 : : }
7009 : : break;
7010 : :
7011 : : case POLY_INT_CST:
7012 : 0 : for (unsigned ix = 0; ix != NUM_POLY_INT_COEFFS; ix++)
7013 : 0 : RT (POLY_INT_CST_COEFF (t, ix));
7014 : : break;
7015 : :
7016 : 13954 : case REAL_CST:
7017 : 13954 : if (const void *bytes = buf (sizeof (real_value)))
7018 : 13954 : memcpy (TREE_REAL_CST_PTR (t), bytes, sizeof (real_value));
7019 : : break;
7020 : :
7021 : : case STRING_CST:
7022 : : /* Streamed during start. */
7023 : : break;
7024 : :
7025 : 6 : case RAW_DATA_CST:
7026 : 6 : RT (RAW_DATA_OWNER (t));
7027 : 6 : gcc_assert (TREE_CODE (RAW_DATA_OWNER (t)) == STRING_CST
7028 : : && TREE_STRING_LENGTH (RAW_DATA_OWNER (t)));
7029 : 6 : RAW_DATA_POINTER (t) = TREE_STRING_POINTER (RAW_DATA_OWNER (t)) + z ();
7030 : 6 : break;
7031 : :
7032 : 24 : case VECTOR_CST:
7033 : 63 : for (unsigned ix = vector_cst_encoded_nelts (t); ix--;)
7034 : 39 : RT (VECTOR_CST_ENCODED_ELT (t, ix));
7035 : : break;
7036 : :
7037 : : /* Decls. */
7038 : 156176 : case VAR_DECL:
7039 : 156176 : if (DECL_CONTEXT (t)
7040 : 156176 : && TREE_CODE (DECL_CONTEXT (t)) != FUNCTION_DECL)
7041 : : {
7042 : 33284 : if (DECL_HAS_VALUE_EXPR_P (t))
7043 : : {
7044 : 9 : tree val = tree_node ();
7045 : 9 : SET_DECL_VALUE_EXPR (t, val);
7046 : : }
7047 : : break;
7048 : : }
7049 : : /* FALLTHROUGH */
7050 : :
7051 : 1031371 : case RESULT_DECL:
7052 : 1031371 : case PARM_DECL:
7053 : 1031371 : if (DECL_HAS_VALUE_EXPR_P (t))
7054 : : {
7055 : : /* The DECL_VALUE hash table is a cache, thus if we're
7056 : : reading a duplicate (which we end up discarding), the
7057 : : value expr will also be cleaned up at the next gc. */
7058 : 10116 : tree val = tree_node ();
7059 : 10116 : SET_DECL_VALUE_EXPR (t, val);
7060 : : }
7061 : : /* FALLTHROUGH */
7062 : :
7063 : 1064175 : case CONST_DECL:
7064 : 1064175 : case IMPORTED_DECL:
7065 : 1064175 : RT (t->decl_common.initial);
7066 : 1064175 : break;
7067 : :
7068 : 40700 : case FIELD_DECL:
7069 : 40700 : RT (t->field_decl.offset);
7070 : 40700 : RT (t->field_decl.bit_field_type);
7071 : 40700 : RT (t->field_decl.qualifier);
7072 : 40700 : RT (t->field_decl.bit_offset);
7073 : 40700 : RT (t->field_decl.fcontext);
7074 : 40700 : RT (t->decl_common.initial);
7075 : 40700 : break;
7076 : :
7077 : 12491 : case LABEL_DECL:
7078 : 12491 : RU (t->label_decl.label_decl_uid);
7079 : 12491 : RU (t->label_decl.eh_landing_pad_nr);
7080 : 12491 : break;
7081 : :
7082 : 322959 : case FUNCTION_DECL:
7083 : 322959 : {
7084 : 322959 : unsigned bltin = u ();
7085 : 322959 : t->function_decl.built_in_class = built_in_class (bltin);
7086 : 322959 : if (bltin != NOT_BUILT_IN)
7087 : : {
7088 : 6653 : bltin = u ();
7089 : 6653 : DECL_UNCHECKED_FUNCTION_CODE (t) = built_in_function (bltin);
7090 : : }
7091 : :
7092 : 322959 : RT (t->function_decl.personality);
7093 : 322959 : RT (t->function_decl.function_specific_target);
7094 : 322959 : RT (t->function_decl.function_specific_optimization);
7095 : 322959 : RT (t->function_decl.vindex);
7096 : :
7097 : 322959 : if (DECL_HAS_DEPENDENT_EXPLICIT_SPEC_P (t))
7098 : : {
7099 : 2104 : tree spec;
7100 : 2104 : RT (spec);
7101 : 2104 : store_explicit_specifier (t, spec);
7102 : : }
7103 : : }
7104 : : break;
7105 : :
7106 : 37626 : case USING_DECL:
7107 : : /* USING_DECL_DECLS */
7108 : 37626 : RT (t->decl_common.initial);
7109 : : /* FALLTHROUGH */
7110 : :
7111 : 587642 : case TYPE_DECL:
7112 : : /* USING_DECL: USING_DECL_SCOPE */
7113 : : /* TYPE_DECL: DECL_ORIGINAL_TYPE */
7114 : 587642 : RT (t->decl_non_common.result);
7115 : 587642 : break;
7116 : :
7117 : : /* Miscellaneous common nodes. */
7118 : 208765 : case BLOCK:
7119 : 208765 : t->block.locus = state->read_location (*this);
7120 : 208765 : t->block.end_locus = state->read_location (*this);
7121 : :
7122 : 208765 : for (tree *chain = &t->block.vars;;)
7123 : 315730 : if (tree decl = tree_node ())
7124 : : {
7125 : : /* For a deduplicated local type or enumerator, chain the
7126 : : duplicate decl instead of the canonical in-TU decl. Seeing
7127 : : a duplicate here means the containing function whose body
7128 : : we're streaming in is a duplicate too, so we'll end up
7129 : : discarding this BLOCK (and the rest of the duplicate function
7130 : : body) anyway. */
7131 : 106965 : decl = maybe_duplicate (decl);
7132 : :
7133 : 106965 : if (!DECL_P (decl))
7134 : : {
7135 : 0 : set_overrun ();
7136 : 0 : break;
7137 : : }
7138 : :
7139 : : /* If DECL_CHAIN is already set then this was a backreference to a
7140 : : local type or enumerator from a previous read (PR c++/114630).
7141 : : Let's copy the node so we can keep building the chain for ODR
7142 : : checking later. */
7143 : 106965 : if (DECL_CHAIN (decl))
7144 : : {
7145 : 12 : gcc_checking_assert (TREE_CODE (decl) == TYPE_DECL
7146 : : && find_duplicate (DECL_CONTEXT (decl)));
7147 : 6 : decl = copy_decl (decl);
7148 : : }
7149 : :
7150 : 106965 : *chain = decl;
7151 : 106965 : chain = &DECL_CHAIN (decl);
7152 : : }
7153 : : else
7154 : 106965 : break;
7155 : :
7156 : : /* nonlocalized_vars is middle-end. */
7157 : 208765 : RT (t->block.subblocks);
7158 : 208765 : RT (t->block.supercontext);
7159 : 208765 : RT (t->block.abstract_origin);
7160 : : /* fragment_origin, fragment_chain are middle-end. */
7161 : 208765 : RT (t->block.chain);
7162 : : /* nonlocalized_vars, block_num, die are middle endy/debug
7163 : : things. */
7164 : 208765 : break;
7165 : :
7166 : 387474 : case CALL_EXPR:
7167 : 387474 : RUC (internal_fn, t->base.u.ifn);
7168 : 387474 : break;
7169 : :
7170 : : case CONSTRUCTOR:
7171 : : // Streamed after the node's type.
7172 : : break;
7173 : :
7174 : 12 : case OMP_CLAUSE:
7175 : 12 : {
7176 : 12 : RU (t->omp_clause.subcode.map_kind);
7177 : 12 : t->omp_clause.locus = state->read_location (*this);
7178 : :
7179 : 12 : unsigned len = omp_clause_num_ops[OMP_CLAUSE_CODE (t)];
7180 : 45 : for (unsigned ix = 0; ix != len; ix++)
7181 : 33 : RT (t->omp_clause.ops[ix]);
7182 : : }
7183 : : break;
7184 : :
7185 : 150809 : case STATEMENT_LIST:
7186 : 150809 : {
7187 : 150809 : tree_stmt_iterator iter = tsi_start (t);
7188 : 602467 : for (tree stmt; RT (stmt);)
7189 : : {
7190 : 451658 : if (TREE_CODE (stmt) == DEBUG_BEGIN_STMT
7191 : 0 : && !MAY_HAVE_DEBUG_MARKER_STMTS)
7192 : 0 : continue;
7193 : 451658 : tsi_link_after (&iter, stmt, TSI_CONTINUE_LINKING);
7194 : : }
7195 : : }
7196 : 150809 : break;
7197 : :
7198 : 0 : case OPTIMIZATION_NODE:
7199 : 0 : case TARGET_OPTION_NODE:
7200 : : /* Not yet implemented, see trees_out::core_vals. */
7201 : 0 : gcc_unreachable ();
7202 : 71527 : break;
7203 : :
7204 : 71527 : case TREE_BINFO:
7205 : 71527 : RT (t->binfo.common.chain);
7206 : 71527 : RT (t->binfo.offset);
7207 : 71527 : RT (t->binfo.inheritance);
7208 : 71527 : RT (t->binfo.vptr_field);
7209 : :
7210 : : /* Do not mark the vtables as USED in the address expressions
7211 : : here. */
7212 : 71527 : unused++;
7213 : 71527 : RT (t->binfo.vtable);
7214 : 71527 : RT (t->binfo.virtuals);
7215 : 71527 : RT (t->binfo.vtt_subvtt);
7216 : 71527 : RT (t->binfo.vtt_vptr);
7217 : 71527 : unused--;
7218 : :
7219 : 71527 : BINFO_BASE_ACCESSES (t) = tree_vec ();
7220 : 71527 : if (!get_overrun ())
7221 : : {
7222 : 71527 : unsigned num = vec_safe_length (BINFO_BASE_ACCESSES (t));
7223 : 92030 : for (unsigned ix = 0; ix != num; ix++)
7224 : 20503 : BINFO_BASE_APPEND (t, tree_node ());
7225 : : }
7226 : : break;
7227 : :
7228 : 905069 : case TREE_LIST:
7229 : 905069 : RT (t->list.purpose);
7230 : 905069 : RT (t->list.value);
7231 : 905069 : RT (t->list.common.chain);
7232 : 905069 : break;
7233 : :
7234 : 574711 : case TREE_VEC:
7235 : 1540541 : for (unsigned ix = TREE_VEC_LENGTH (t); ix--;)
7236 : 965830 : RT (TREE_VEC_ELT (t, ix));
7237 : 574711 : RT (t->type_common.common.chain);
7238 : 574711 : break;
7239 : :
7240 : : /* C++-specific nodes ... */
7241 : 67359 : case BASELINK:
7242 : 67359 : RT (((lang_tree_node *)t)->baselink.binfo);
7243 : 67359 : RTU (((lang_tree_node *)t)->baselink.functions);
7244 : 67359 : RT (((lang_tree_node *)t)->baselink.access_binfo);
7245 : 67359 : break;
7246 : :
7247 : 26657 : case CONSTRAINT_INFO:
7248 : 26657 : RT (((lang_tree_node *)t)->constraint_info.template_reqs);
7249 : 26657 : RT (((lang_tree_node *)t)->constraint_info.declarator_reqs);
7250 : 26657 : RT (((lang_tree_node *)t)->constraint_info.associated_constr);
7251 : 26657 : break;
7252 : :
7253 : 4996 : case DEFERRED_NOEXCEPT:
7254 : 4996 : RT (((lang_tree_node *)t)->deferred_noexcept.pattern);
7255 : 4996 : RT (((lang_tree_node *)t)->deferred_noexcept.args);
7256 : 4996 : break;
7257 : :
7258 : 2705 : case LAMBDA_EXPR:
7259 : 2705 : RT (((lang_tree_node *)t)->lambda_expression.capture_list);
7260 : 2705 : RT (((lang_tree_node *)t)->lambda_expression.this_capture);
7261 : 2705 : RT (((lang_tree_node *)t)->lambda_expression.extra_scope);
7262 : 2705 : RT (((lang_tree_node *)t)->lambda_expression.regen_info);
7263 : 2705 : RT (((lang_tree_node *)t)->lambda_expression.extra_args);
7264 : : /* lambda_expression.pending_proxies is NULL */
7265 : 2705 : ((lang_tree_node *)t)->lambda_expression.locus
7266 : 2705 : = state->read_location (*this);
7267 : 2705 : RUC (cp_lambda_default_capture_mode_type,
7268 : : ((lang_tree_node *)t)->lambda_expression.default_capture_mode);
7269 : 2705 : RU (((lang_tree_node *)t)->lambda_expression.discriminator_scope);
7270 : 2705 : RU (((lang_tree_node *)t)->lambda_expression.discriminator_sig);
7271 : 2705 : break;
7272 : :
7273 : 392815 : case OVERLOAD:
7274 : 392815 : RT (((lang_tree_node *)t)->overload.function);
7275 : 392815 : RT (t->common.chain);
7276 : 392815 : break;
7277 : :
7278 : 0 : case PTRMEM_CST:
7279 : 0 : RT (((lang_tree_node *)t)->ptrmem.member);
7280 : 0 : break;
7281 : :
7282 : 5367 : case STATIC_ASSERT:
7283 : 5367 : RT (((lang_tree_node *)t)->static_assertion.condition);
7284 : 5367 : RT (((lang_tree_node *)t)->static_assertion.message);
7285 : 5367 : ((lang_tree_node *)t)->static_assertion.location
7286 : 5367 : = state->read_location (*this);
7287 : 5367 : break;
7288 : :
7289 : 230217 : case TEMPLATE_DECL:
7290 : : /* Streamed when reading the raw template decl itself. */
7291 : 230217 : gcc_assert (((lang_tree_node *)t)->template_decl.arguments);
7292 : 230217 : gcc_assert (((lang_tree_node *)t)->template_decl.result);
7293 : 230217 : if (DECL_UNINSTANTIATED_TEMPLATE_FRIEND_P (t))
7294 : 4657 : RT (DECL_CHAIN (t));
7295 : : break;
7296 : :
7297 : 514553 : case TEMPLATE_INFO:
7298 : 514553 : RT (((lang_tree_node *)t)->template_info.tmpl);
7299 : 514553 : RT (((lang_tree_node *)t)->template_info.args);
7300 : 514553 : RT (((lang_tree_node *)t)->template_info.partial);
7301 : 514553 : if (unsigned len = u ())
7302 : : {
7303 : 0 : auto &ac = (((lang_tree_node *)t)
7304 : : ->template_info.deferred_access_checks);
7305 : 0 : vec_alloc (ac, len);
7306 : 0 : for (unsigned ix = 0; ix != len; ix++)
7307 : : {
7308 : 0 : deferred_access_check m;
7309 : :
7310 : 0 : RT (m.binfo);
7311 : 0 : RT (m.decl);
7312 : 0 : RT (m.diag_decl);
7313 : 0 : m.loc = state->read_location (*this);
7314 : 0 : ac->quick_push (m);
7315 : : }
7316 : : }
7317 : : break;
7318 : :
7319 : 282400 : case TEMPLATE_PARM_INDEX:
7320 : 282400 : RU (((lang_tree_node *)t)->tpi.index);
7321 : 282400 : RU (((lang_tree_node *)t)->tpi.level);
7322 : 282400 : RU (((lang_tree_node *)t)->tpi.orig_level);
7323 : 282400 : RT (((lang_tree_node *)t)->tpi.decl);
7324 : 282400 : break;
7325 : :
7326 : 6620 : case TRAIT_EXPR:
7327 : 6620 : RT (((lang_tree_node *)t)->trait_expression.type1);
7328 : 6620 : RT (((lang_tree_node *)t)->trait_expression.type2);
7329 : 6620 : RUC (cp_trait_kind, ((lang_tree_node *)t)->trait_expression.kind);
7330 : 6620 : break;
7331 : : }
7332 : :
7333 : 10465858 : if (CODE_CONTAINS_STRUCT (code, TS_TYPED))
7334 : : {
7335 : 9705520 : tree type = tree_node ();
7336 : :
7337 : 9705520 : if (type && code == ENUMERAL_TYPE && !ENUM_FIXED_UNDERLYING_TYPE_P (t))
7338 : : {
7339 : 1445 : unsigned precision = u ();
7340 : :
7341 : 1445 : type = build_distinct_type_copy (type);
7342 : 1445 : TYPE_PRECISION (type) = precision;
7343 : 2890 : set_min_and_max_values_for_integral_type (type, precision,
7344 : 1445 : TYPE_SIGN (type));
7345 : : }
7346 : :
7347 : 9705520 : if (code != TEMPLATE_DECL)
7348 : 9475303 : t->typed.type = type;
7349 : : }
7350 : :
7351 : 10465858 : if (TREE_CODE (t) == CONSTRUCTOR)
7352 : 45516 : if (unsigned len = u ())
7353 : : {
7354 : 23952 : vec_alloc (t->constructor.elts, len);
7355 : 162082 : for (unsigned ix = 0; ix != len; ix++)
7356 : : {
7357 : 138130 : constructor_elt elt;
7358 : :
7359 : 138130 : RT (elt.index);
7360 : 138130 : RTU (elt.value);
7361 : 138130 : t->constructor.elts->quick_push (elt);
7362 : : }
7363 : : }
7364 : :
7365 : : #undef RT
7366 : : #undef RM
7367 : : #undef RU
7368 : 10465858 : return !get_overrun ();
7369 : : }
7370 : :
7371 : : void
7372 : 4090565 : trees_out::lang_decl_vals (tree t)
7373 : : {
7374 : 4090565 : const struct lang_decl *lang = DECL_LANG_SPECIFIC (t);
7375 : : #define WU(X) (u (X))
7376 : : #define WT(X) (tree_node (X))
7377 : : /* Module index already written. */
7378 : 4090565 : switch (lang->u.base.selector)
7379 : : {
7380 : 0 : default:
7381 : 0 : gcc_unreachable ();
7382 : :
7383 : 755967 : case lds_fn: /* lang_decl_fn. */
7384 : 755967 : if (streaming_p ())
7385 : : {
7386 : 377900 : if (DECL_NAME (t) && IDENTIFIER_OVL_OP_P (DECL_NAME (t)))
7387 : 62027 : WU (lang->u.fn.ovl_op_code);
7388 : : }
7389 : :
7390 : 956562 : if (DECL_CLASS_SCOPE_P (t) || DECL_UNIQUE_FRIEND_P (t))
7391 : 574544 : WT (lang->u.fn.context);
7392 : :
7393 : 755967 : if (lang->u.fn.thunk_p)
7394 : : {
7395 : : /* The thunked-to function. */
7396 : 1128 : WT (lang->u.fn.befriending_classes);
7397 : 1128 : if (streaming_p ())
7398 : 564 : wi (lang->u.fn.u5.fixed_offset);
7399 : : }
7400 : 754839 : else if (decl_tls_wrapper_p (t))
7401 : : /* The wrapped variable. */
7402 : 18 : WT (lang->u.fn.befriending_classes);
7403 : : else
7404 : 754821 : WT (lang->u.fn.u5.cloned_function);
7405 : :
7406 : 755967 : if (FNDECL_USED_AUTO (t))
7407 : 2278 : WT (lang->u.fn.u.saved_auto_return_type);
7408 : :
7409 : 755967 : goto lds_min;
7410 : :
7411 : 2766 : case lds_decomp: /* lang_decl_decomp. */
7412 : 2766 : WT (lang->u.decomp.base);
7413 : 2766 : goto lds_min;
7414 : :
7415 : 2434121 : case lds_min: /* lang_decl_min. */
7416 : 2434121 : lds_min:
7417 : 2434121 : WT (lang->u.min.template_info);
7418 : 2434121 : {
7419 : 2434121 : tree access = lang->u.min.access;
7420 : :
7421 : : /* DECL_ACCESS needs to be maintained by the definition of the
7422 : : (derived) class that changes the access. The other users
7423 : : of DECL_ACCESS need to write it here. */
7424 : 755967 : if (!DECL_THUNK_P (t)
7425 : 3188960 : && (DECL_CONTEXT (t) && TYPE_P (DECL_CONTEXT (t))))
7426 : : access = NULL_TREE;
7427 : :
7428 : 2434121 : WT (access);
7429 : : }
7430 : 2434121 : break;
7431 : :
7432 : : case lds_ns: /* lang_decl_ns. */
7433 : : break;
7434 : :
7435 : 1656279 : case lds_parm: /* lang_decl_parm. */
7436 : 1656279 : if (streaming_p ())
7437 : : {
7438 : 564931 : WU (lang->u.parm.level);
7439 : 564931 : WU (lang->u.parm.index);
7440 : : }
7441 : : break;
7442 : : }
7443 : : #undef WU
7444 : : #undef WT
7445 : 4090565 : }
7446 : :
7447 : : bool
7448 : 1463483 : trees_in::lang_decl_vals (tree t)
7449 : : {
7450 : 1463483 : struct lang_decl *lang = DECL_LANG_SPECIFIC (t);
7451 : : #define RU(X) ((X) = u ())
7452 : : #define RT(X) ((X) = tree_node ())
7453 : :
7454 : : /* Module index already read. */
7455 : 1463483 : switch (lang->u.base.selector)
7456 : : {
7457 : 0 : default:
7458 : 0 : gcc_unreachable ();
7459 : :
7460 : 322959 : case lds_fn: /* lang_decl_fn. */
7461 : 322959 : if (DECL_NAME (t) && IDENTIFIER_OVL_OP_P (DECL_NAME (t)))
7462 : : {
7463 : 55094 : unsigned code = u ();
7464 : :
7465 : : /* Check consistency. */
7466 : 55094 : if (code >= OVL_OP_MAX
7467 : 55094 : || (ovl_op_info[IDENTIFIER_ASSIGN_OP_P (DECL_NAME (t))][code]
7468 : 55094 : .ovl_op_code) == OVL_OP_ERROR_MARK)
7469 : 0 : set_overrun ();
7470 : : else
7471 : 55094 : lang->u.fn.ovl_op_code = code;
7472 : : }
7473 : :
7474 : 406640 : if (DECL_CLASS_SCOPE_P (t) || DECL_UNIQUE_FRIEND_P (t))
7475 : 248141 : RT (lang->u.fn.context);
7476 : :
7477 : 322959 : if (lang->u.fn.thunk_p)
7478 : : {
7479 : 490 : RT (lang->u.fn.befriending_classes);
7480 : 490 : lang->u.fn.u5.fixed_offset = wi ();
7481 : : }
7482 : 322469 : else if (decl_tls_wrapper_p (t))
7483 : 15 : RT (lang->u.fn.befriending_classes);
7484 : : else
7485 : 322454 : RT (lang->u.fn.u5.cloned_function);
7486 : :
7487 : 322959 : if (FNDECL_USED_AUTO (t))
7488 : 961 : RT (lang->u.fn.u.saved_auto_return_type);
7489 : 322959 : goto lds_min;
7490 : :
7491 : 1303 : case lds_decomp: /* lang_decl_decomp. */
7492 : 1303 : RT (lang->u.decomp.base);
7493 : 1303 : goto lds_min;
7494 : :
7495 : 985716 : case lds_min: /* lang_decl_min. */
7496 : 985716 : lds_min:
7497 : 985716 : RT (lang->u.min.template_info);
7498 : 985716 : RT (lang->u.min.access);
7499 : 985716 : break;
7500 : :
7501 : : case lds_ns: /* lang_decl_ns. */
7502 : : break;
7503 : :
7504 : 477669 : case lds_parm: /* lang_decl_parm. */
7505 : 477669 : RU (lang->u.parm.level);
7506 : 477669 : RU (lang->u.parm.index);
7507 : 477669 : break;
7508 : : }
7509 : : #undef RU
7510 : : #undef RT
7511 : 1463483 : return !get_overrun ();
7512 : : }
7513 : :
7514 : : /* Most of the value contents of lang_type is streamed in
7515 : : define_class. */
7516 : :
7517 : : void
7518 : 288684 : trees_out::lang_type_vals (tree t)
7519 : : {
7520 : 288684 : const struct lang_type *lang = TYPE_LANG_SPECIFIC (t);
7521 : : #define WU(X) (u (X))
7522 : : #define WT(X) (tree_node (X))
7523 : 288684 : if (streaming_p ())
7524 : 144322 : WU (lang->align);
7525 : : #undef WU
7526 : : #undef WT
7527 : 288684 : }
7528 : :
7529 : : bool
7530 : 110905 : trees_in::lang_type_vals (tree t)
7531 : : {
7532 : 110905 : struct lang_type *lang = TYPE_LANG_SPECIFIC (t);
7533 : : #define RU(X) ((X) = u ())
7534 : : #define RT(X) ((X) = tree_node ())
7535 : 110905 : RU (lang->align);
7536 : : #undef RU
7537 : : #undef RT
7538 : 110905 : return !get_overrun ();
7539 : : }
7540 : :
7541 : : /* Write out the bools of T, including information about any
7542 : : LANG_SPECIFIC information. Including allocation of any lang
7543 : : specific object. */
7544 : :
7545 : : void
7546 : 12365469 : trees_out::tree_node_bools (tree t)
7547 : : {
7548 : 12365469 : gcc_checking_assert (streaming_p ());
7549 : :
7550 : : /* We should never stream a namespace. */
7551 : 12365469 : gcc_checking_assert (TREE_CODE (t) != NAMESPACE_DECL
7552 : : || DECL_NAMESPACE_ALIAS (t));
7553 : :
7554 : 12365469 : bits_out bits = stream_bits ();
7555 : 12365469 : core_bools (t, bits);
7556 : :
7557 : 12365469 : switch (TREE_CODE_CLASS (TREE_CODE (t)))
7558 : : {
7559 : 2771625 : case tcc_declaration:
7560 : 2771625 : {
7561 : 2771625 : bool specific = DECL_LANG_SPECIFIC (t) != NULL;
7562 : 2771625 : bits.b (specific);
7563 : 2771625 : if (specific && VAR_P (t))
7564 : 278378 : bits.b (DECL_DECOMPOSITION_P (t));
7565 : 1775035 : if (specific)
7566 : 1775035 : lang_decl_bools (t, bits);
7567 : : }
7568 : : break;
7569 : :
7570 : 520336 : case tcc_type:
7571 : 520336 : {
7572 : 520336 : bool specific = (TYPE_MAIN_VARIANT (t) == t
7573 : 520336 : && TYPE_LANG_SPECIFIC (t) != NULL);
7574 : 520336 : gcc_assert (TYPE_LANG_SPECIFIC (t)
7575 : : == TYPE_LANG_SPECIFIC (TYPE_MAIN_VARIANT (t)));
7576 : :
7577 : 520336 : bits.b (specific);
7578 : 520336 : if (specific)
7579 : 144322 : lang_type_bools (t, bits);
7580 : : }
7581 : : break;
7582 : :
7583 : : default:
7584 : : break;
7585 : : }
7586 : :
7587 : 12365469 : bits.bflush ();
7588 : 12365469 : }
7589 : :
7590 : : bool
7591 : 10480605 : trees_in::tree_node_bools (tree t)
7592 : : {
7593 : 10480605 : bits_in bits = stream_bits ();
7594 : 10480605 : bool ok = core_bools (t, bits);
7595 : :
7596 : 10480605 : if (ok)
7597 : 10480605 : switch (TREE_CODE_CLASS (TREE_CODE (t)))
7598 : : {
7599 : 2296155 : case tcc_declaration:
7600 : 2296155 : if (bits.b ())
7601 : : {
7602 : 1463483 : bool decomp = VAR_P (t) && bits.b ();
7603 : :
7604 : 1463483 : ok = maybe_add_lang_decl_raw (t, decomp);
7605 : 1463483 : if (ok)
7606 : 1463483 : ok = lang_decl_bools (t, bits);
7607 : : }
7608 : : break;
7609 : :
7610 : 408419 : case tcc_type:
7611 : 408419 : if (bits.b ())
7612 : : {
7613 : 110905 : ok = maybe_add_lang_type_raw (t);
7614 : 110905 : if (ok)
7615 : 110905 : ok = lang_type_bools (t, bits);
7616 : : }
7617 : : break;
7618 : :
7619 : : default:
7620 : : break;
7621 : : }
7622 : :
7623 : 10480605 : bits.bflush ();
7624 : 10480605 : if (!ok || get_overrun ())
7625 : 0 : return false;
7626 : :
7627 : : return true;
7628 : 10480605 : }
7629 : :
7630 : :
7631 : : /* Write out the lang-specifc vals of node T. */
7632 : :
7633 : : void
7634 : 31886026 : trees_out::lang_vals (tree t)
7635 : : {
7636 : 31886026 : switch (TREE_CODE_CLASS (TREE_CODE (t)))
7637 : : {
7638 : 7217015 : case tcc_declaration:
7639 : 7217015 : if (DECL_LANG_SPECIFIC (t))
7640 : 4090565 : lang_decl_vals (t);
7641 : : break;
7642 : :
7643 : 1804216 : case tcc_type:
7644 : 1804216 : if (TYPE_MAIN_VARIANT (t) == t && TYPE_LANG_SPECIFIC (t))
7645 : 288684 : lang_type_vals (t);
7646 : : break;
7647 : :
7648 : : default:
7649 : : break;
7650 : : }
7651 : 31886026 : }
7652 : :
7653 : : bool
7654 : 10465858 : trees_in::lang_vals (tree t)
7655 : : {
7656 : 10465858 : bool ok = true;
7657 : :
7658 : 10465858 : switch (TREE_CODE_CLASS (TREE_CODE (t)))
7659 : : {
7660 : 2296155 : case tcc_declaration:
7661 : 2296155 : if (DECL_LANG_SPECIFIC (t))
7662 : 1463483 : ok = lang_decl_vals (t);
7663 : : break;
7664 : :
7665 : 393672 : case tcc_type:
7666 : 393672 : if (TYPE_LANG_SPECIFIC (t))
7667 : 110905 : ok = lang_type_vals (t);
7668 : : else
7669 : 282767 : TYPE_LANG_SPECIFIC (t) = TYPE_LANG_SPECIFIC (TYPE_MAIN_VARIANT (t));
7670 : : break;
7671 : :
7672 : : default:
7673 : : break;
7674 : : }
7675 : :
7676 : 10465858 : return ok;
7677 : : }
7678 : :
7679 : : /* Write out the value fields of node T. */
7680 : :
7681 : : void
7682 : 31886026 : trees_out::tree_node_vals (tree t)
7683 : : {
7684 : 31886026 : core_vals (t);
7685 : 31886026 : lang_vals (t);
7686 : 31886026 : }
7687 : :
7688 : : bool
7689 : 10465858 : trees_in::tree_node_vals (tree t)
7690 : : {
7691 : 10465858 : bool ok = core_vals (t);
7692 : 10465858 : if (ok)
7693 : 10465858 : ok = lang_vals (t);
7694 : :
7695 : 10465858 : return ok;
7696 : : }
7697 : :
7698 : :
7699 : : /* If T is a back reference, fixed reference or NULL, write out its
7700 : : code and return WK_none. Otherwise return WK_value if we must write
7701 : : by value, or WK_normal otherwise. */
7702 : :
7703 : : walk_kind
7704 : 214350607 : trees_out::ref_node (tree t)
7705 : : {
7706 : 214350607 : if (!t)
7707 : : {
7708 : 88056112 : if (streaming_p ())
7709 : : {
7710 : : /* NULL_TREE -> tt_null. */
7711 : 33854876 : null_count++;
7712 : 33854876 : i (tt_null);
7713 : : }
7714 : 88056112 : return WK_none;
7715 : : }
7716 : :
7717 : 126294495 : if (!TREE_VISITED (t))
7718 : : return WK_normal;
7719 : :
7720 : : /* An already-visited tree. It must be in the map. */
7721 : 74424615 : int val = get_tag (t);
7722 : :
7723 : 74424615 : if (val == tag_value)
7724 : : /* An entry we should walk into. */
7725 : : return WK_value;
7726 : :
7727 : 73184436 : const char *kind;
7728 : :
7729 : 73184436 : if (val <= tag_backref)
7730 : : {
7731 : : /* Back reference -> -ve number */
7732 : 55917097 : if (streaming_p ())
7733 : 26519535 : i (val);
7734 : : kind = "backref";
7735 : : }
7736 : 17267339 : else if (val >= tag_fixed)
7737 : : {
7738 : : /* Fixed reference -> tt_fixed */
7739 : 17267339 : val -= tag_fixed;
7740 : 17267339 : if (streaming_p ())
7741 : 6136489 : i (tt_fixed), u (val);
7742 : : kind = "fixed";
7743 : : }
7744 : :
7745 : 73184436 : if (streaming_p ())
7746 : : {
7747 : 32656024 : back_ref_count++;
7748 : 32656024 : dump (dumper::TREE)
7749 : 14197 : && dump ("Wrote %s:%d %C:%N%S", kind, val, TREE_CODE (t), t, t);
7750 : : }
7751 : : return WK_none;
7752 : : }
7753 : :
7754 : : tree
7755 : 22298619 : trees_in::back_ref (int tag)
7756 : : {
7757 : 22298619 : tree res = NULL_TREE;
7758 : :
7759 : 22298619 : if (tag < 0 && unsigned (~tag) < back_refs.length ())
7760 : 22298619 : res = back_refs[~tag];
7761 : :
7762 : 22298619 : if (!res
7763 : : /* Checking TREE_CODE is a dereference, so we know this is not a
7764 : : wild pointer. Checking the code provides evidence we've not
7765 : : corrupted something. */
7766 : 22298619 : || TREE_CODE (res) >= MAX_TREE_CODES)
7767 : 0 : set_overrun ();
7768 : : else
7769 : 22313161 : dump (dumper::TREE) && dump ("Read backref:%d found %C:%N%S", tag,
7770 : : TREE_CODE (res), res, res);
7771 : 22298619 : return res;
7772 : : }
7773 : :
7774 : : unsigned
7775 : 2762369 : trees_out::add_indirect_tpl_parms (tree parms)
7776 : : {
7777 : 2762369 : unsigned len = 0;
7778 : 5165330 : for (; parms; parms = TREE_CHAIN (parms), len++)
7779 : : {
7780 : 2917079 : if (TREE_VISITED (parms))
7781 : : break;
7782 : :
7783 : 2402961 : int tag = insert (parms);
7784 : 2402961 : if (streaming_p ())
7785 : 2403240 : dump (dumper::TREE)
7786 : 153 : && dump ("Indirect:%d template's parameter %u %C:%N",
7787 : 153 : tag, len, TREE_CODE (parms), parms);
7788 : : }
7789 : :
7790 : 2762369 : if (streaming_p ())
7791 : 465501 : u (len);
7792 : :
7793 : 2762369 : return len;
7794 : : }
7795 : :
7796 : : unsigned
7797 : 380157 : trees_in::add_indirect_tpl_parms (tree parms)
7798 : : {
7799 : 380157 : unsigned len = u ();
7800 : 685705 : for (unsigned ix = 0; ix != len; parms = TREE_CHAIN (parms), ix++)
7801 : : {
7802 : 305548 : int tag = insert (parms);
7803 : 305986 : dump (dumper::TREE)
7804 : 159 : && dump ("Indirect:%d template's parameter %u %C:%N",
7805 : 159 : tag, ix, TREE_CODE (parms), parms);
7806 : : }
7807 : :
7808 : 380157 : return len;
7809 : : }
7810 : :
7811 : : /* We've just found DECL by name. Insert nodes that come with it, but
7812 : : cannot be found by name, so we'll not accidentally walk into them. */
7813 : :
7814 : : void
7815 : 6437804 : trees_out::add_indirects (tree decl)
7816 : : {
7817 : 6437804 : unsigned count = 0;
7818 : :
7819 : : // FIXME:OPTIMIZATION We'll eventually want default fn parms of
7820 : : // templates and perhaps default template parms too. The former can
7821 : : // be referenced from instantiations (as they are lazily
7822 : : // instantiated). Also (deferred?) exception specifications of
7823 : : // templates. See the note about PARM_DECLs in trees_out::decl_node.
7824 : 6437804 : tree inner = decl;
7825 : 6437804 : if (TREE_CODE (decl) == TEMPLATE_DECL)
7826 : : {
7827 : 2762369 : count += add_indirect_tpl_parms (DECL_TEMPLATE_PARMS (decl));
7828 : :
7829 : 2762369 : inner = DECL_TEMPLATE_RESULT (decl);
7830 : 2762369 : int tag = insert (inner);
7831 : 2762369 : if (streaming_p ())
7832 : 465501 : dump (dumper::TREE)
7833 : 219 : && dump ("Indirect:%d template's result %C:%N",
7834 : 219 : tag, TREE_CODE (inner), inner);
7835 : 2762369 : count++;
7836 : : }
7837 : :
7838 : 6437804 : if (TREE_CODE (inner) == TYPE_DECL)
7839 : : {
7840 : : /* Make sure the type is in the map too. Otherwise we get
7841 : : different RECORD_TYPEs for the same type, and things go
7842 : : south. */
7843 : 3553919 : tree type = TREE_TYPE (inner);
7844 : 3553919 : gcc_checking_assert (DECL_ORIGINAL_TYPE (inner)
7845 : : || TYPE_NAME (type) == inner);
7846 : 3553919 : int tag = insert (type);
7847 : 3553919 : if (streaming_p ())
7848 : 412561 : dump (dumper::TREE) && dump ("Indirect:%d decl's type %C:%N", tag,
7849 : 362 : TREE_CODE (type), type);
7850 : 3553919 : count++;
7851 : : }
7852 : :
7853 : 6437804 : if (streaming_p ())
7854 : : {
7855 : 999947 : u (count);
7856 : 1000476 : dump (dumper::TREE) && dump ("Inserted %u indirects", count);
7857 : : }
7858 : 6437804 : }
7859 : :
7860 : : bool
7861 : 786519 : trees_in::add_indirects (tree decl)
7862 : : {
7863 : 786519 : unsigned count = 0;
7864 : :
7865 : 786519 : tree inner = decl;
7866 : 786519 : if (TREE_CODE (inner) == TEMPLATE_DECL)
7867 : : {
7868 : 380157 : count += add_indirect_tpl_parms (DECL_TEMPLATE_PARMS (decl));
7869 : :
7870 : 380157 : inner = DECL_TEMPLATE_RESULT (decl);
7871 : 380157 : int tag = insert (inner);
7872 : 380157 : dump (dumper::TREE)
7873 : 228 : && dump ("Indirect:%d templates's result %C:%N", tag,
7874 : 228 : TREE_CODE (inner), inner);
7875 : 380157 : count++;
7876 : : }
7877 : :
7878 : 786519 : if (TREE_CODE (inner) == TYPE_DECL)
7879 : : {
7880 : 303109 : tree type = TREE_TYPE (inner);
7881 : 303109 : gcc_checking_assert (DECL_ORIGINAL_TYPE (inner)
7882 : : || TYPE_NAME (type) == inner);
7883 : 303109 : int tag = insert (type);
7884 : 303109 : dump (dumper::TREE)
7885 : 362 : && dump ("Indirect:%d decl's type %C:%N", tag, TREE_CODE (type), type);
7886 : 303109 : count++;
7887 : : }
7888 : :
7889 : 787124 : dump (dumper::TREE) && dump ("Inserted %u indirects", count);
7890 : 786519 : return count == u ();
7891 : : }
7892 : :
7893 : : /* Stream a template parameter. There are 4.5 kinds of parameter:
7894 : : a) Template - TEMPLATE_DECL->TYPE_DECL->TEMPLATE_TEMPLATE_PARM
7895 : : TEMPLATE_TYPE_PARM_INDEX TPI
7896 : : b) Type - TYPE_DECL->TEMPLATE_TYPE_PARM TEMPLATE_TYPE_PARM_INDEX TPI
7897 : : c.1) NonTYPE - PARM_DECL DECL_INITIAL TPI We meet this first
7898 : : c.2) NonTYPE - CONST_DECL DECL_INITIAL Same TPI
7899 : : d) BoundTemplate - TYPE_DECL->BOUND_TEMPLATE_TEMPLATE_PARM
7900 : : TEMPLATE_TYPE_PARM_INDEX->TPI
7901 : : TEMPLATE_TEMPLATE_PARM_INFO->TEMPLATE_INFO
7902 : :
7903 : : All of these point to a TEMPLATE_PARM_INDEX, and #B also has a TEMPLATE_INFO
7904 : : */
7905 : :
7906 : : void
7907 : 1624913 : trees_out::tpl_parm_value (tree parm)
7908 : : {
7909 : 1624913 : gcc_checking_assert (DECL_P (parm) && DECL_TEMPLATE_PARM_P (parm));
7910 : :
7911 : 1624913 : int parm_tag = insert (parm);
7912 : 1624913 : if (streaming_p ())
7913 : : {
7914 : 368104 : i (tt_tpl_parm);
7915 : 368104 : dump (dumper::TREE) && dump ("Writing template parm:%d %C:%N",
7916 : 114 : parm_tag, TREE_CODE (parm), parm);
7917 : 368104 : start (parm);
7918 : 368104 : tree_node_bools (parm);
7919 : : }
7920 : :
7921 : 1624913 : tree inner = parm;
7922 : 1624913 : if (TREE_CODE (inner) == TEMPLATE_DECL)
7923 : : {
7924 : 4786 : inner = DECL_TEMPLATE_RESULT (inner);
7925 : 4786 : int inner_tag = insert (inner);
7926 : 4786 : if (streaming_p ())
7927 : : {
7928 : 1234 : dump (dumper::TREE) && dump ("Writing inner template parm:%d %C:%N",
7929 : 0 : inner_tag, TREE_CODE (inner), inner);
7930 : 1234 : start (inner);
7931 : 1234 : tree_node_bools (inner);
7932 : : }
7933 : : }
7934 : :
7935 : 1624913 : tree type = NULL_TREE;
7936 : 1624913 : if (TREE_CODE (inner) == TYPE_DECL)
7937 : : {
7938 : 1465249 : type = TREE_TYPE (inner);
7939 : 1465249 : int type_tag = insert (type);
7940 : 1465249 : if (streaming_p ())
7941 : : {
7942 : 332346 : dump (dumper::TREE) && dump ("Writing template parm type:%d %C:%N",
7943 : 108 : type_tag, TREE_CODE (type), type);
7944 : 332346 : start (type);
7945 : 332346 : tree_node_bools (type);
7946 : : }
7947 : : }
7948 : :
7949 : 1624913 : if (inner != parm)
7950 : : {
7951 : : /* This is a template-template parameter. */
7952 : 4786 : unsigned tpl_levels = 0;
7953 : 4786 : tpl_header (parm, &tpl_levels);
7954 : 4786 : tpl_parms_fini (parm, tpl_levels);
7955 : : }
7956 : :
7957 : 1624913 : tree_node_vals (parm);
7958 : 1624913 : if (inner != parm)
7959 : 4786 : tree_node_vals (inner);
7960 : 1624913 : if (type)
7961 : : {
7962 : 1465249 : tree_node_vals (type);
7963 : 1465249 : if (DECL_NAME (inner) == auto_identifier
7964 : 1465249 : || DECL_NAME (inner) == decltype_auto_identifier)
7965 : : {
7966 : : /* Placeholder auto. */
7967 : 57996 : tree_node (DECL_INITIAL (inner));
7968 : 57996 : tree_node (DECL_SIZE_UNIT (inner));
7969 : : }
7970 : : }
7971 : :
7972 : 1624913 : if (streaming_p ())
7973 : 368104 : dump (dumper::TREE) && dump ("Wrote template parm:%d %C:%N",
7974 : 114 : parm_tag, TREE_CODE (parm), parm);
7975 : 1624913 : }
7976 : :
7977 : : tree
7978 : 295010 : trees_in::tpl_parm_value ()
7979 : : {
7980 : 295010 : tree parm = start ();
7981 : 295010 : if (!parm || !tree_node_bools (parm))
7982 : 0 : return NULL_TREE;
7983 : :
7984 : 295010 : int parm_tag = insert (parm);
7985 : 295010 : dump (dumper::TREE) && dump ("Reading template parm:%d %C:%N",
7986 : 198 : parm_tag, TREE_CODE (parm), parm);
7987 : :
7988 : 295010 : tree inner = parm;
7989 : 295010 : if (TREE_CODE (inner) == TEMPLATE_DECL)
7990 : : {
7991 : 824 : inner = start ();
7992 : 824 : if (!inner || !tree_node_bools (inner))
7993 : 0 : return NULL_TREE;
7994 : 824 : int inner_tag = insert (inner);
7995 : 824 : dump (dumper::TREE) && dump ("Reading inner template parm:%d %C:%N",
7996 : 0 : inner_tag, TREE_CODE (inner), inner);
7997 : 824 : DECL_TEMPLATE_RESULT (parm) = inner;
7998 : : }
7999 : :
8000 : 295010 : tree type = NULL_TREE;
8001 : 295010 : if (TREE_CODE (inner) == TYPE_DECL)
8002 : : {
8003 : 265350 : type = start ();
8004 : 265350 : if (!type || !tree_node_bools (type))
8005 : 0 : return NULL_TREE;
8006 : 265350 : int type_tag = insert (type);
8007 : 265350 : dump (dumper::TREE) && dump ("Reading template parm type:%d %C:%N",
8008 : 120 : type_tag, TREE_CODE (type), type);
8009 : :
8010 : 265350 : TREE_TYPE (inner) = TREE_TYPE (parm) = type;
8011 : 265350 : TYPE_NAME (type) = parm;
8012 : : }
8013 : :
8014 : 295010 : if (inner != parm)
8015 : : {
8016 : : /* A template template parameter. */
8017 : 824 : unsigned tpl_levels = 0;
8018 : 824 : tpl_header (parm, &tpl_levels);
8019 : 824 : tpl_parms_fini (parm, tpl_levels);
8020 : : }
8021 : :
8022 : 295010 : tree_node_vals (parm);
8023 : 295010 : if (inner != parm)
8024 : 824 : tree_node_vals (inner);
8025 : 295010 : if (type)
8026 : : {
8027 : 265350 : tree_node_vals (type);
8028 : 265350 : if (DECL_NAME (inner) == auto_identifier
8029 : 265350 : || DECL_NAME (inner) == decltype_auto_identifier)
8030 : : {
8031 : : /* Placeholder auto. */
8032 : 21575 : DECL_INITIAL (inner) = tree_node ();
8033 : 21575 : DECL_SIZE_UNIT (inner) = tree_node ();
8034 : : }
8035 : 265350 : if (TYPE_CANONICAL (type))
8036 : : {
8037 : 265350 : gcc_checking_assert (TYPE_CANONICAL (type) == type);
8038 : 265350 : TYPE_CANONICAL (type) = canonical_type_parameter (type);
8039 : : }
8040 : : }
8041 : :
8042 : 295010 : dump (dumper::TREE) && dump ("Read template parm:%d %C:%N",
8043 : 198 : parm_tag, TREE_CODE (parm), parm);
8044 : :
8045 : : return parm;
8046 : : }
8047 : :
8048 : : void
8049 : 1058398 : trees_out::install_entity (tree decl, depset *dep)
8050 : : {
8051 : 1058398 : gcc_checking_assert (streaming_p ());
8052 : :
8053 : : /* Write the entity index, so we can insert it as soon as we
8054 : : know this is new. */
8055 : 1058398 : u (dep ? dep->cluster + 1 : 0);
8056 : 1058398 : if (CHECKING_P && dep)
8057 : : {
8058 : : /* Add it to the entity map, such that we can tell it is
8059 : : part of us. */
8060 : 791089 : bool existed;
8061 : 791089 : unsigned *slot = &entity_map->get_or_insert
8062 : 791089 : (DECL_UID (decl), &existed);
8063 : 791089 : if (existed)
8064 : : /* If it existed, it should match. */
8065 : 813 : gcc_checking_assert (decl == (*entity_ary)[*slot]);
8066 : 791089 : *slot = ~dep->cluster;
8067 : : }
8068 : 1058398 : }
8069 : :
8070 : : bool
8071 : 861824 : trees_in::install_entity (tree decl)
8072 : : {
8073 : 861824 : unsigned entity_index = u ();
8074 : 861824 : if (!entity_index)
8075 : : return false;
8076 : :
8077 : 629867 : if (entity_index > state->entity_num)
8078 : : {
8079 : 0 : set_overrun ();
8080 : 0 : return false;
8081 : : }
8082 : :
8083 : : /* Insert the real decl into the entity ary. */
8084 : 629867 : unsigned ident = state->entity_lwm + entity_index - 1;
8085 : 629867 : (*entity_ary)[ident] = decl;
8086 : :
8087 : : /* And into the entity map, if it's not already there. */
8088 : 629867 : tree not_tmpl = STRIP_TEMPLATE (decl);
8089 : 629867 : if (!DECL_LANG_SPECIFIC (not_tmpl)
8090 : 1175561 : || !DECL_MODULE_ENTITY_P (not_tmpl))
8091 : : {
8092 : 629250 : retrofit_lang_decl (not_tmpl);
8093 : 629250 : DECL_MODULE_ENTITY_P (not_tmpl) = true;
8094 : :
8095 : : /* Insert into the entity hash (it cannot already be there). */
8096 : 629250 : bool existed;
8097 : 629250 : unsigned &slot = entity_map->get_or_insert (DECL_UID (decl), &existed);
8098 : 629250 : gcc_checking_assert (!existed);
8099 : 629250 : slot = ident;
8100 : : }
8101 : : else
8102 : : {
8103 : 617 : unsigned *slot = entity_map->get (DECL_UID (decl));
8104 : :
8105 : : /* The entity must be in the entity map already. However, DECL may
8106 : : be the DECL_TEMPLATE_RESULT of an existing partial specialisation
8107 : : if we matched it while streaming another instantiation; in this
8108 : : case we already registered that TEMPLATE_DECL. */
8109 : 617 : if (!slot)
8110 : : {
8111 : 9 : tree type = TREE_TYPE (decl);
8112 : 9 : gcc_checking_assert (TREE_CODE (decl) == TYPE_DECL
8113 : : && CLASS_TYPE_P (type)
8114 : : && CLASSTYPE_TEMPLATE_SPECIALIZATION (type));
8115 : 9 : slot = entity_map->get (DECL_UID (CLASSTYPE_TI_TEMPLATE (type)));
8116 : : }
8117 : 9 : gcc_checking_assert (slot);
8118 : :
8119 : 617 : if (state->is_partition ())
8120 : : {
8121 : : /* The decl is already in the entity map, but we see it again now
8122 : : from a partition: we want to overwrite if the original decl
8123 : : wasn't also from a (possibly different) partition. Otherwise,
8124 : : for things like template instantiations, make_dependency might
8125 : : not realise that this is also provided from a partition and
8126 : : should be considered part of this module (and thus always
8127 : : emitted into the primary interface's CMI). */
8128 : 150 : module_state *imp = import_entity_module (*slot);
8129 : 150 : if (!imp->is_partition ())
8130 : 24 : *slot = ident;
8131 : : }
8132 : : }
8133 : :
8134 : : return true;
8135 : : }
8136 : :
8137 : : static bool has_definition (tree decl);
8138 : :
8139 : : /* DECL is a decl node that must be written by value. DEP is the
8140 : : decl's depset. */
8141 : :
8142 : : void
8143 : 2918928 : trees_out::decl_value (tree decl, depset *dep)
8144 : : {
8145 : : /* We should not be writing clones or template parms. */
8146 : 2918928 : gcc_checking_assert (DECL_P (decl)
8147 : : && !DECL_CLONED_FUNCTION_P (decl)
8148 : : && !DECL_TEMPLATE_PARM_P (decl));
8149 : :
8150 : : /* We should never be writing non-typedef ptrmemfuncs by value. */
8151 : 2918928 : gcc_checking_assert (TREE_CODE (decl) != TYPE_DECL
8152 : : || DECL_ORIGINAL_TYPE (decl)
8153 : : || !TYPE_PTRMEMFUNC_P (TREE_TYPE (decl)));
8154 : :
8155 : : /* There's no need to walk any of the contents of a known TU-local entity,
8156 : : since importers should never see any of it regardless. But make sure we
8157 : : at least note its location so importers can use it for diagnostics. */
8158 : 5293021 : if (dep && dep->is_tu_local ())
8159 : : {
8160 : 437 : gcc_checking_assert (is_initial_scan ());
8161 : 437 : insert (decl, WK_value);
8162 : 437 : state->note_location (DECL_SOURCE_LOCATION (decl));
8163 : 437 : return;
8164 : : }
8165 : :
8166 : 2918491 : merge_kind mk = get_merge_kind (decl, dep);
8167 : :
8168 : 2918491 : if (CHECKING_P)
8169 : : {
8170 : : /* Never start in the middle of a template. */
8171 : 2918491 : int use_tpl = -1;
8172 : 2918491 : if (tree ti = node_template_info (decl, use_tpl))
8173 : 990721 : gcc_checking_assert (TREE_CODE (TI_TEMPLATE (ti)) == OVERLOAD
8174 : : || TREE_CODE (TI_TEMPLATE (ti)) == FIELD_DECL
8175 : : || (DECL_TEMPLATE_RESULT (TI_TEMPLATE (ti))
8176 : : != decl));
8177 : : }
8178 : :
8179 : 2918491 : if (streaming_p ())
8180 : : {
8181 : : /* A new node -> tt_decl. */
8182 : 1058398 : decl_val_count++;
8183 : 1058398 : i (tt_decl);
8184 : 1058398 : u (mk);
8185 : 1058398 : start (decl);
8186 : :
8187 : 1058398 : if (mk != MK_unique)
8188 : : {
8189 : 901287 : bits_out bits = stream_bits ();
8190 : 901287 : if (!(mk & MK_template_mask) && !state->is_header ())
8191 : : {
8192 : : /* Tell the importer whether this is a global module entity,
8193 : : or a module entity. */
8194 : 114694 : tree o = get_originating_module_decl (decl);
8195 : 114694 : bool is_attached = false;
8196 : :
8197 : 114694 : tree not_tmpl = STRIP_TEMPLATE (o);
8198 : 114694 : if (DECL_LANG_SPECIFIC (not_tmpl)
8199 : 174136 : && DECL_MODULE_ATTACH_P (not_tmpl))
8200 : : is_attached = true;
8201 : :
8202 : 114694 : bits.b (is_attached);
8203 : : }
8204 : 901287 : bits.b (dep && dep->has_defn ());
8205 : 901287 : }
8206 : 1058398 : tree_node_bools (decl);
8207 : : }
8208 : :
8209 : 2918491 : int tag = insert (decl, WK_value);
8210 : 2918491 : if (streaming_p ())
8211 : 1058398 : dump (dumper::TREE)
8212 : 683 : && dump ("Writing %s:%d %C:%N%S", merge_kind_name[mk], tag,
8213 : 683 : TREE_CODE (decl), decl, decl);
8214 : :
8215 : 2918491 : tree inner = decl;
8216 : 2918491 : int inner_tag = 0;
8217 : 2918491 : if (TREE_CODE (decl) == TEMPLATE_DECL)
8218 : : {
8219 : 840049 : inner = DECL_TEMPLATE_RESULT (decl);
8220 : 840049 : inner_tag = insert (inner, WK_value);
8221 : :
8222 : : /* On stream-in we assume that a template and its result will
8223 : : have the same type. */
8224 : 840049 : gcc_checking_assert (TREE_TYPE (decl) == TREE_TYPE (inner));
8225 : :
8226 : 840049 : if (streaming_p ())
8227 : : {
8228 : 280000 : int code = TREE_CODE (inner);
8229 : 280000 : u (code);
8230 : 280000 : start (inner, true);
8231 : 280000 : tree_node_bools (inner);
8232 : 280000 : dump (dumper::TREE)
8233 : 132 : && dump ("Writing %s:%d %C:%N%S", merge_kind_name[mk], inner_tag,
8234 : 132 : TREE_CODE (inner), inner, inner);
8235 : : }
8236 : : }
8237 : :
8238 : 2918491 : tree type = NULL_TREE;
8239 : 2918491 : int type_tag = 0;
8240 : 2918491 : tree stub_decl = NULL_TREE;
8241 : 2918491 : int stub_tag = 0;
8242 : 2918491 : if (TREE_CODE (inner) == TYPE_DECL)
8243 : : {
8244 : 1108283 : type = TREE_TYPE (inner);
8245 : 1108283 : bool has_type = (type == TYPE_MAIN_VARIANT (type)
8246 : 1108283 : && TYPE_NAME (type) == inner);
8247 : :
8248 : 1108283 : if (streaming_p ())
8249 : 373393 : u (has_type ? TREE_CODE (type) : 0);
8250 : :
8251 : 1108283 : if (has_type)
8252 : : {
8253 : 508378 : type_tag = insert (type, WK_value);
8254 : 508378 : if (streaming_p ())
8255 : : {
8256 : 169439 : start (type, true);
8257 : 169439 : tree_node_bools (type);
8258 : 169439 : dump (dumper::TREE)
8259 : 155 : && dump ("Writing type:%d %C:%N", type_tag,
8260 : 155 : TREE_CODE (type), type);
8261 : : }
8262 : :
8263 : 508378 : stub_decl = TYPE_STUB_DECL (type);
8264 : 508378 : bool has_stub = inner != stub_decl;
8265 : 508378 : if (streaming_p ())
8266 : 169439 : u (has_stub ? TREE_CODE (stub_decl) : 0);
8267 : 508378 : if (has_stub)
8268 : : {
8269 : 2169 : stub_tag = insert (stub_decl);
8270 : 2169 : if (streaming_p ())
8271 : : {
8272 : 722 : start (stub_decl, true);
8273 : 722 : tree_node_bools (stub_decl);
8274 : 722 : dump (dumper::TREE)
8275 : 0 : && dump ("Writing stub_decl:%d %C:%N", stub_tag,
8276 : 0 : TREE_CODE (stub_decl), stub_decl);
8277 : : }
8278 : : }
8279 : : else
8280 : : stub_decl = NULL_TREE;
8281 : : }
8282 : : else
8283 : : /* Regular typedef. */
8284 : : type = NULL_TREE;
8285 : : }
8286 : :
8287 : : /* Stream the container, we want it correctly canonicalized before
8288 : : we start emitting keys for this decl. */
8289 : 2918491 : tree container = decl_container (decl);
8290 : 2918491 : unsigned tpl_levels = 0;
8291 : :
8292 : : /* Also tell the importer whether this is a temploid friend attached
8293 : : to a different module (which has implications for merging), so that
8294 : : importers can reconstruct this information on stream-in. */
8295 : 2918491 : if (TREE_CODE (inner) == FUNCTION_DECL || TREE_CODE (inner) == TYPE_DECL)
8296 : : {
8297 : 2241481 : tree* temploid_friend_slot = imported_temploid_friends->get (decl);
8298 : 2241481 : gcc_checking_assert (!temploid_friend_slot || *temploid_friend_slot);
8299 : 2241481 : tree_node (temploid_friend_slot ? *temploid_friend_slot : NULL_TREE);
8300 : : }
8301 : :
8302 : 2918491 : {
8303 : 2918491 : auto wmk = make_temp_override (dep_hash->writing_merge_key, true);
8304 : 2918491 : if (decl != inner)
8305 : 840049 : tpl_header (decl, &tpl_levels);
8306 : 2918491 : if (TREE_CODE (inner) == FUNCTION_DECL)
8307 : 1133198 : fn_parms_init (inner);
8308 : :
8309 : : /* Now write out the merging information, and then really
8310 : : install the tag values. */
8311 : 2918491 : key_mergeable (tag, mk, decl, inner, container, dep);
8312 : :
8313 : 2918491 : if (streaming_p ())
8314 : 2921058 : dump (dumper::MERGE)
8315 : 798 : && dump ("Wrote:%d's %s merge key %C:%N", tag,
8316 : 798 : merge_kind_name[mk], TREE_CODE (decl), decl);
8317 : 2918491 : }
8318 : :
8319 : 2918491 : if (TREE_CODE (inner) == FUNCTION_DECL)
8320 : 2918491 : fn_parms_fini (inner);
8321 : :
8322 : 2918491 : if (!is_key_order ())
8323 : 2126254 : tree_node_vals (decl);
8324 : :
8325 : 2918491 : if (inner_tag)
8326 : : {
8327 : 840049 : if (!is_key_order ())
8328 : 560049 : tree_node_vals (inner);
8329 : 840049 : tpl_parms_fini (decl, tpl_levels);
8330 : : }
8331 : :
8332 : 2918491 : if (type && !is_key_order ())
8333 : : {
8334 : 338939 : tree_node_vals (type);
8335 : 338939 : if (stub_decl)
8336 : 1447 : tree_node_vals (stub_decl);
8337 : : }
8338 : :
8339 : 2918491 : if (!is_key_order ())
8340 : : {
8341 : 2126254 : if (mk & MK_template_mask
8342 : 1500046 : || mk == MK_partial
8343 : 1500046 : || mk == MK_friend_spec)
8344 : : {
8345 : 30346 : if (mk != MK_partial)
8346 : : {
8347 : : // FIXME: We should make use of the merge-key by
8348 : : // exposing it outside of key_mergeable. But this gets
8349 : : // the job done.
8350 : 626208 : auto *entry = reinterpret_cast <spec_entry *> (dep->deps[0]);
8351 : :
8352 : 626208 : if (streaming_p ())
8353 : 313104 : u (get_mergeable_specialization_flags (mk & MK_tmpl_decl_mask,
8354 : : entry->tmpl, decl));
8355 : 626208 : tree_node (entry->tmpl);
8356 : 626208 : tree_node (entry->args);
8357 : : }
8358 : : else
8359 : : {
8360 : 30346 : tree ti = get_template_info (inner);
8361 : 30346 : tree_node (TI_TEMPLATE (ti));
8362 : 30346 : tree_node (TI_ARGS (ti));
8363 : : }
8364 : : }
8365 : 2126254 : tree_node (get_constraints (decl));
8366 : : }
8367 : :
8368 : 2918491 : if (streaming_p ())
8369 : : {
8370 : : /* Do not stray outside this section. */
8371 : 1058398 : gcc_checking_assert (!dep || dep->section == dep_hash->section);
8372 : :
8373 : : /* Write the entity index, so we can insert it as soon as we
8374 : : know this is new. */
8375 : 1058398 : install_entity (decl, dep);
8376 : : }
8377 : :
8378 : 2918491 : if (DECL_LANG_SPECIFIC (inner)
8379 : 2637269 : && DECL_MODULE_KEYED_DECLS_P (inner)
8380 : 2918924 : && !is_key_order ())
8381 : : {
8382 : : /* Stream the keyed entities. */
8383 : 292 : auto *attach_vec = keyed_table->get (inner);
8384 : 292 : unsigned num = attach_vec->length ();
8385 : 292 : if (streaming_p ())
8386 : 141 : u (num);
8387 : 596 : for (unsigned ix = 0; ix != num; ix++)
8388 : : {
8389 : 304 : tree attached = (*attach_vec)[ix];
8390 : 304 : tree_node (attached);
8391 : 304 : if (streaming_p ())
8392 : 319 : dump (dumper::MERGE)
8393 : 15 : && dump ("Written %d[%u] attached decl %N", tag, ix, attached);
8394 : : }
8395 : : }
8396 : :
8397 : 2918491 : bool is_typedef = false;
8398 : 2918491 : if (!type && TREE_CODE (inner) == TYPE_DECL)
8399 : : {
8400 : 599905 : tree t = TREE_TYPE (inner);
8401 : 599905 : unsigned tdef_flags = 0;
8402 : 599905 : if (DECL_ORIGINAL_TYPE (inner)
8403 : 599905 : && TYPE_NAME (TREE_TYPE (inner)) == inner)
8404 : : {
8405 : 599905 : tdef_flags |= 1;
8406 : 599905 : if (TYPE_STRUCTURAL_EQUALITY_P (t)
8407 : 128492 : && TYPE_DEPENDENT_P_VALID (t)
8408 : 721662 : && TYPE_DEPENDENT_P (t))
8409 : : tdef_flags |= 2;
8410 : : }
8411 : 599905 : if (streaming_p ())
8412 : 203954 : u (tdef_flags);
8413 : :
8414 : 599905 : if (tdef_flags & 1)
8415 : : {
8416 : : /* A typedef type. */
8417 : 599905 : int type_tag = insert (t);
8418 : 599905 : if (streaming_p ())
8419 : 203954 : dump (dumper::TREE)
8420 : 206 : && dump ("Cloned:%d %s %C:%N", type_tag,
8421 : : tdef_flags & 2 ? "depalias" : "typedef",
8422 : 206 : TREE_CODE (t), t);
8423 : :
8424 : : is_typedef = true;
8425 : : }
8426 : : }
8427 : :
8428 : 2918491 : if (streaming_p () && DECL_MAYBE_IN_CHARGE_CDTOR_P (decl))
8429 : : {
8430 : 79787 : bool cloned_p
8431 : 79787 : = (DECL_CHAIN (decl) && DECL_CLONED_FUNCTION_P (DECL_CHAIN (decl)));
8432 : 55850 : bool needs_vtt_parm_p
8433 : 55850 : = (cloned_p && CLASSTYPE_VBASECLASSES (DECL_CONTEXT (decl)));
8434 : 55850 : bool omit_inherited_parms_p
8435 : 55850 : = (cloned_p && DECL_MAYBE_IN_CHARGE_CONSTRUCTOR_P (decl)
8436 : 44152 : && base_ctor_omit_inherited_parms (decl));
8437 : 79787 : unsigned flags = (int (cloned_p) << 0
8438 : 79787 : | int (needs_vtt_parm_p) << 1
8439 : 79787 : | int (omit_inherited_parms_p) << 2);
8440 : 79787 : u (flags);
8441 : 79866 : dump (dumper::TREE) && dump ("CDTOR %N is %scloned",
8442 : : decl, cloned_p ? "" : "not ");
8443 : : }
8444 : :
8445 : 2918491 : if (streaming_p () && VAR_P (decl) && CP_DECL_THREAD_LOCAL_P (decl))
8446 : 172 : u (decl_tls_model (decl));
8447 : :
8448 : 2918491 : if (streaming_p ())
8449 : 1058398 : dump (dumper::TREE) && dump ("Written decl:%d %C:%N", tag,
8450 : 683 : TREE_CODE (decl), decl);
8451 : :
8452 : 2918491 : if (NAMESPACE_SCOPE_P (inner))
8453 : 1746786 : gcc_checking_assert (!dep == (VAR_OR_FUNCTION_DECL_P (inner)
8454 : : && DECL_LOCAL_DECL_P (inner)));
8455 : 2044984 : else if ((TREE_CODE (inner) == TYPE_DECL
8456 : 590391 : && !is_typedef
8457 : 112685 : && TYPE_NAME (TREE_TYPE (inner)) == inner)
8458 : 2522690 : || TREE_CODE (inner) == FUNCTION_DECL)
8459 : : {
8460 : 945126 : bool write_defn = !dep && has_definition (decl);
8461 : 945126 : if (streaming_p ())
8462 : 315185 : u (write_defn);
8463 : 945126 : if (write_defn)
8464 : 0 : write_definition (decl);
8465 : : }
8466 : : }
8467 : :
8468 : : tree
8469 : 861824 : trees_in::decl_value ()
8470 : : {
8471 : 861824 : int tag = 0;
8472 : 861824 : bool is_attached = false;
8473 : 861824 : bool has_defn = false;
8474 : 861824 : unsigned mk_u = u ();
8475 : 861824 : if (mk_u >= MK_hwm || !merge_kind_name[mk_u])
8476 : : {
8477 : 0 : set_overrun ();
8478 : 0 : return NULL_TREE;
8479 : : }
8480 : :
8481 : 861824 : unsigned saved_unused = unused;
8482 : 861824 : unused = 0;
8483 : :
8484 : 861824 : merge_kind mk = merge_kind (mk_u);
8485 : :
8486 : 861824 : tree decl = start ();
8487 : 861824 : if (decl)
8488 : : {
8489 : 861824 : if (mk != MK_unique)
8490 : : {
8491 : 719612 : bits_in bits = stream_bits ();
8492 : 719612 : if (!(mk & MK_template_mask) && !state->is_header ())
8493 : 46302 : is_attached = bits.b ();
8494 : :
8495 : 719612 : has_defn = bits.b ();
8496 : 719612 : }
8497 : :
8498 : 861824 : if (!tree_node_bools (decl))
8499 : 0 : decl = NULL_TREE;
8500 : : }
8501 : :
8502 : : /* Insert into map. */
8503 : 861824 : tag = insert (decl);
8504 : 861824 : if (decl)
8505 : 861824 : dump (dumper::TREE)
8506 : 964 : && dump ("Reading:%d %C", tag, TREE_CODE (decl));
8507 : :
8508 : 861824 : tree inner = decl;
8509 : 861824 : int inner_tag = 0;
8510 : 861824 : if (decl && TREE_CODE (decl) == TEMPLATE_DECL)
8511 : : {
8512 : 229393 : int code = u ();
8513 : 229393 : inner = start (code);
8514 : 229393 : if (inner && tree_node_bools (inner))
8515 : 229393 : DECL_TEMPLATE_RESULT (decl) = inner;
8516 : : else
8517 : 0 : decl = NULL_TREE;
8518 : :
8519 : 229393 : inner_tag = insert (inner);
8520 : 229393 : if (decl)
8521 : 229393 : dump (dumper::TREE)
8522 : 204 : && dump ("Reading:%d %C", inner_tag, TREE_CODE (inner));
8523 : : }
8524 : :
8525 : 861824 : tree type = NULL_TREE;
8526 : 861824 : int type_tag = 0;
8527 : 861824 : tree stub_decl = NULL_TREE;
8528 : 861824 : int stub_tag = 0;
8529 : 861824 : if (decl && TREE_CODE (inner) == TYPE_DECL)
8530 : : {
8531 : 284239 : if (unsigned type_code = u ())
8532 : : {
8533 : 128308 : type = start (type_code);
8534 : 128308 : if (type && tree_node_bools (type))
8535 : : {
8536 : 128308 : TREE_TYPE (inner) = type;
8537 : 128308 : TYPE_NAME (type) = inner;
8538 : : }
8539 : : else
8540 : 0 : decl = NULL_TREE;
8541 : :
8542 : 128308 : type_tag = insert (type);
8543 : 128308 : if (decl)
8544 : 128308 : dump (dumper::TREE)
8545 : 212 : && dump ("Reading type:%d %C", type_tag, TREE_CODE (type));
8546 : :
8547 : 128308 : if (unsigned stub_code = u ())
8548 : : {
8549 : 413 : stub_decl = start (stub_code);
8550 : 413 : if (stub_decl && tree_node_bools (stub_decl))
8551 : : {
8552 : 413 : TREE_TYPE (stub_decl) = type;
8553 : 413 : TYPE_STUB_DECL (type) = stub_decl;
8554 : : }
8555 : : else
8556 : 0 : decl = NULL_TREE;
8557 : :
8558 : 413 : stub_tag = insert (stub_decl);
8559 : 413 : if (decl)
8560 : 413 : dump (dumper::TREE)
8561 : 0 : && dump ("Reading stub_decl:%d %C", stub_tag,
8562 : 0 : TREE_CODE (stub_decl));
8563 : : }
8564 : : }
8565 : : }
8566 : :
8567 : 861824 : if (!decl)
8568 : : {
8569 : 0 : bail:
8570 : 0 : if (inner_tag != 0)
8571 : 0 : back_refs[~inner_tag] = NULL_TREE;
8572 : 0 : if (type_tag != 0)
8573 : 0 : back_refs[~type_tag] = NULL_TREE;
8574 : 0 : if (stub_tag != 0)
8575 : 0 : back_refs[~stub_tag] = NULL_TREE;
8576 : 0 : if (tag != 0)
8577 : 0 : back_refs[~tag] = NULL_TREE;
8578 : 0 : set_overrun ();
8579 : : /* Bail. */
8580 : 0 : unused = saved_unused;
8581 : 0 : return NULL_TREE;
8582 : : }
8583 : :
8584 : : /* Read the container, to ensure it's already been streamed in. */
8585 : 861824 : tree container = decl_container ();
8586 : 861824 : unsigned tpl_levels = 0;
8587 : :
8588 : : /* If this is an imported temploid friend, get the owning decl its
8589 : : attachment is determined by (or NULL_TREE otherwise). */
8590 : 861824 : tree temploid_friend = NULL_TREE;
8591 : 861824 : if (TREE_CODE (inner) == FUNCTION_DECL || TREE_CODE (inner) == TYPE_DECL)
8592 : 607198 : temploid_friend = tree_node ();
8593 : :
8594 : : /* Figure out if this decl is already known about. */
8595 : 861824 : int parm_tag = 0;
8596 : :
8597 : 861824 : if (decl != inner)
8598 : 229393 : if (!tpl_header (decl, &tpl_levels))
8599 : 0 : goto bail;
8600 : 861824 : if (TREE_CODE (inner) == FUNCTION_DECL)
8601 : 322959 : parm_tag = fn_parms_init (inner);
8602 : :
8603 : 861824 : tree existing = key_mergeable (tag, mk, decl, inner, type, container,
8604 : : is_attached, temploid_friend);
8605 : 861824 : tree existing_inner = existing;
8606 : 861824 : if (existing)
8607 : : {
8608 : 416204 : if (existing == error_mark_node)
8609 : 0 : goto bail;
8610 : :
8611 : 416204 : if (TREE_CODE (STRIP_TEMPLATE (existing)) == TYPE_DECL)
8612 : : {
8613 : 156262 : tree etype = TREE_TYPE (existing);
8614 : 156262 : if (TYPE_LANG_SPECIFIC (etype)
8615 : 112891 : && COMPLETE_TYPE_P (etype)
8616 : 222626 : && !CLASSTYPE_MEMBER_VEC (etype))
8617 : : /* Give it a member vec, we're likely gonna be looking
8618 : : inside it. */
8619 : 14021 : set_class_bindings (etype, -1);
8620 : : }
8621 : :
8622 : : /* Install the existing decl into the back ref array. */
8623 : 416204 : register_duplicate (decl, existing);
8624 : 416204 : back_refs[~tag] = existing;
8625 : 416204 : if (inner_tag != 0)
8626 : : {
8627 : 139952 : existing_inner = DECL_TEMPLATE_RESULT (existing);
8628 : 139952 : back_refs[~inner_tag] = existing_inner;
8629 : : }
8630 : :
8631 : 416204 : if (type_tag != 0)
8632 : : {
8633 : 74341 : tree existing_type = TREE_TYPE (existing);
8634 : 74341 : back_refs[~type_tag] = existing_type;
8635 : 74341 : if (stub_tag != 0)
8636 : 254 : back_refs[~stub_tag] = TYPE_STUB_DECL (existing_type);
8637 : : }
8638 : : }
8639 : :
8640 : 861824 : if (parm_tag)
8641 : 322959 : fn_parms_fini (parm_tag, inner, existing_inner, has_defn);
8642 : :
8643 : 861824 : if (!tree_node_vals (decl))
8644 : 0 : goto bail;
8645 : :
8646 : 861824 : if (inner_tag)
8647 : : {
8648 : 229393 : gcc_checking_assert (DECL_TEMPLATE_RESULT (decl) == inner);
8649 : :
8650 : 229393 : if (!tree_node_vals (inner))
8651 : 0 : goto bail;
8652 : :
8653 : 229393 : if (!tpl_parms_fini (decl, tpl_levels))
8654 : 0 : goto bail;
8655 : : }
8656 : :
8657 : 861824 : if (type && (!tree_node_vals (type)
8658 : 128308 : || (stub_decl && !tree_node_vals (stub_decl))))
8659 : 0 : goto bail;
8660 : :
8661 : 861824 : spec_entry spec;
8662 : 861824 : unsigned spec_flags = 0;
8663 : 861824 : if (mk & MK_template_mask
8664 : 601376 : || mk == MK_partial
8665 : 601376 : || mk == MK_friend_spec)
8666 : : {
8667 : 9380 : if (mk == MK_partial)
8668 : : spec_flags = 2;
8669 : : else
8670 : 260448 : spec_flags = u ();
8671 : :
8672 : 269828 : spec.tmpl = tree_node ();
8673 : 269828 : spec.args = tree_node ();
8674 : : }
8675 : : /* Hold constraints on the spec field, for a short while. */
8676 : 861824 : spec.spec = tree_node ();
8677 : :
8678 : 862788 : dump (dumper::TREE) && dump ("Read:%d %C:%N", tag, TREE_CODE (decl), decl);
8679 : :
8680 : 861824 : existing = back_refs[~tag];
8681 : 861824 : bool installed = install_entity (existing);
8682 : 861824 : bool is_new = existing == decl;
8683 : :
8684 : 861824 : if (DECL_LANG_SPECIFIC (inner)
8685 : 1622477 : && DECL_MODULE_KEYED_DECLS_P (inner))
8686 : : {
8687 : : /* Read and maybe install the attached entities. */
8688 : 165 : bool existed;
8689 : 165 : auto &set = keyed_table->get_or_insert (STRIP_TEMPLATE (existing),
8690 : : &existed);
8691 : 165 : unsigned num = u ();
8692 : 165 : if (is_new == existed)
8693 : 0 : set_overrun ();
8694 : 165 : if (is_new)
8695 : 57 : set.reserve (num);
8696 : 342 : for (unsigned ix = 0; !get_overrun () && ix != num; ix++)
8697 : : {
8698 : 177 : tree attached = tree_node ();
8699 : 177 : dump (dumper::MERGE)
8700 : 60 : && dump ("Read %d[%u] %s attached decl %N", tag, ix,
8701 : : is_new ? "new" : "matched", attached);
8702 : 177 : if (is_new)
8703 : 63 : set.quick_push (attached);
8704 : 114 : else if (set[ix] != attached)
8705 : 0 : set_overrun ();
8706 : : }
8707 : : }
8708 : :
8709 : : /* Regular typedefs will have a NULL TREE_TYPE at this point. */
8710 : 861824 : unsigned tdef_flags = 0;
8711 : 861824 : bool is_typedef = false;
8712 : 861824 : if (!type && TREE_CODE (inner) == TYPE_DECL)
8713 : : {
8714 : 155931 : tdef_flags = u ();
8715 : 155931 : if (tdef_flags & 1)
8716 : 155931 : is_typedef = true;
8717 : : }
8718 : :
8719 : 861824 : if (is_new)
8720 : : {
8721 : : /* A newly discovered node. */
8722 : 445620 : if (TREE_CODE (decl) == FUNCTION_DECL && DECL_VIRTUAL_P (decl))
8723 : : /* Mark this identifier as naming a virtual function --
8724 : : lookup_overrides relies on this optimization. */
8725 : 4084 : IDENTIFIER_VIRTUAL_P (DECL_NAME (decl)) = true;
8726 : :
8727 : 445620 : if (installed)
8728 : : {
8729 : : /* Mark the entity as imported. */
8730 : 265293 : retrofit_lang_decl (inner);
8731 : 265293 : DECL_MODULE_IMPORT_P (inner) = true;
8732 : : }
8733 : :
8734 : 445620 : if (temploid_friend)
8735 : 22 : imported_temploid_friends->put (decl, temploid_friend);
8736 : :
8737 : 445620 : if (spec.spec)
8738 : 15813 : set_constraints (decl, spec.spec);
8739 : :
8740 : 445620 : if (TREE_CODE (decl) == INTEGER_CST && !TREE_OVERFLOW (decl))
8741 : : {
8742 : 0 : decl = cache_integer_cst (decl, true);
8743 : 0 : back_refs[~tag] = decl;
8744 : : }
8745 : :
8746 : 445620 : if (is_typedef)
8747 : : {
8748 : : /* Frob it to be ready for cloning. */
8749 : 74010 : TREE_TYPE (inner) = DECL_ORIGINAL_TYPE (inner);
8750 : 74010 : DECL_ORIGINAL_TYPE (inner) = NULL_TREE;
8751 : 74010 : if (TREE_CODE (TREE_TYPE (inner)) != TU_LOCAL_ENTITY)
8752 : : {
8753 : 74007 : set_underlying_type (inner);
8754 : 74007 : if (tdef_flags & 2)
8755 : : {
8756 : : /* Match instantiate_alias_template's handling. */
8757 : 17826 : tree type = TREE_TYPE (inner);
8758 : 17826 : TYPE_DEPENDENT_P (type) = true;
8759 : 17826 : TYPE_DEPENDENT_P_VALID (type) = true;
8760 : 17826 : SET_TYPE_STRUCTURAL_EQUALITY (type);
8761 : : }
8762 : : }
8763 : : }
8764 : :
8765 : 445620 : if (inner_tag)
8766 : : /* Set the TEMPLATE_DECL's type. */
8767 : 89441 : TREE_TYPE (decl) = TREE_TYPE (inner);
8768 : :
8769 : : /* Redetermine whether we need to import or export this declaration
8770 : : for this TU. But for extern templates we know we must import:
8771 : : they'll be defined in a different TU.
8772 : : FIXME: How do dllexport and dllimport interact across a module?
8773 : : See also https://github.com/itanium-cxx-abi/cxx-abi/issues/170.
8774 : : May have to revisit? */
8775 : 445620 : if (type
8776 : 53967 : && CLASS_TYPE_P (type)
8777 : 45534 : && TYPE_LANG_SPECIFIC (type)
8778 : 491154 : && !(CLASSTYPE_EXPLICIT_INSTANTIATION (type)
8779 : 523 : && CLASSTYPE_INTERFACE_KNOWN (type)
8780 : 523 : && CLASSTYPE_INTERFACE_ONLY (type)))
8781 : : {
8782 : 45060 : CLASSTYPE_INTERFACE_ONLY (type) = false;
8783 : 45060 : CLASSTYPE_INTERFACE_UNKNOWN (type) = true;
8784 : : }
8785 : :
8786 : : /* Add to specialization tables now that constraints etc are
8787 : : added. */
8788 : 445620 : if (mk == MK_partial)
8789 : : {
8790 : 3343 : bool is_type = TREE_CODE (inner) == TYPE_DECL;
8791 : 3343 : spec.spec = is_type ? type : inner;
8792 : 3343 : add_mergeable_specialization (!is_type, &spec, decl, spec_flags);
8793 : : }
8794 : 442277 : else if (mk & MK_template_mask)
8795 : : {
8796 : 117040 : bool is_type = !(mk & MK_tmpl_decl_mask);
8797 : 117040 : spec.spec = is_type ? type : mk & MK_tmpl_tmpl_mask ? inner : decl;
8798 : 117040 : add_mergeable_specialization (!is_type, &spec, decl, spec_flags);
8799 : : }
8800 : :
8801 : 445620 : if (NAMESPACE_SCOPE_P (decl)
8802 : 92512 : && (mk == MK_named || mk == MK_unique
8803 : 92512 : || mk == MK_enum || mk == MK_friend_spec)
8804 : 490254 : && !(VAR_OR_FUNCTION_DECL_P (decl) && DECL_LOCAL_DECL_P (decl)))
8805 : 44563 : add_module_namespace_decl (CP_DECL_CONTEXT (decl), decl);
8806 : :
8807 : 445620 : if (DECL_ARTIFICIAL (decl)
8808 : 126085 : && TREE_CODE (decl) == FUNCTION_DECL
8809 : 12330 : && !DECL_TEMPLATE_INFO (decl)
8810 : 12086 : && DECL_CONTEXT (decl) && TYPE_P (DECL_CONTEXT (decl))
8811 : 11949 : && TYPE_SIZE (DECL_CONTEXT (decl))
8812 : 446900 : && !DECL_THUNK_P (decl))
8813 : : /* A new implicit member function, when the class is
8814 : : complete. This means the importee declared it, and
8815 : : we must now add it to the class. Note that implicit
8816 : : member fns of template instantiations do not themselves
8817 : : look like templates. */
8818 : 790 : if (!install_implicit_member (inner))
8819 : 0 : set_overrun ();
8820 : :
8821 : : /* When importing a TLS wrapper from a header unit, we haven't
8822 : : actually emitted its definition yet. Remember it so we can
8823 : : do this later. */
8824 : 445620 : if (state->is_header ()
8825 : 445620 : && decl_tls_wrapper_p (decl))
8826 : 6 : note_vague_linkage_fn (decl);
8827 : :
8828 : : /* Setup aliases for the declaration. */
8829 : 445620 : if (tree alias = lookup_attribute ("alias", DECL_ATTRIBUTES (decl)))
8830 : : {
8831 : 3 : alias = TREE_VALUE (TREE_VALUE (alias));
8832 : 3 : alias = get_identifier (TREE_STRING_POINTER (alias));
8833 : 3 : assemble_alias (decl, alias);
8834 : : }
8835 : : }
8836 : : else
8837 : : {
8838 : : /* DECL is the to-be-discarded decl. Its internal pointers will
8839 : : be to the EXISTING's structure. Frob it to point to its
8840 : : own other structures, so loading its definition will alter
8841 : : it, and not the existing decl. */
8842 : 417639 : dump (dumper::MERGE) && dump ("Deduping %N", existing);
8843 : :
8844 : 416204 : if (inner_tag)
8845 : 139952 : DECL_TEMPLATE_RESULT (decl) = inner;
8846 : :
8847 : 416204 : if (type)
8848 : : {
8849 : : /* Point at the to-be-discarded type & decl. */
8850 : 74341 : TYPE_NAME (type) = inner;
8851 : 74341 : TREE_TYPE (inner) = type;
8852 : :
8853 : 148428 : TYPE_STUB_DECL (type) = stub_decl ? stub_decl : inner;
8854 : 74341 : if (stub_decl)
8855 : 254 : TREE_TYPE (stub_decl) = type;
8856 : :
8857 : 74341 : tree etype = TREE_TYPE (existing);
8858 : :
8859 : : /* Handle separate declarations with different attributes. */
8860 : 74341 : tree &dattr = TYPE_ATTRIBUTES (type);
8861 : 74341 : tree &eattr = TYPE_ATTRIBUTES (etype);
8862 : 74341 : check_abi_tags (existing, decl, eattr, dattr);
8863 : : // TODO: handle other conflicting type attributes
8864 : 74341 : eattr = merge_attributes (eattr, dattr);
8865 : :
8866 : : /* When merging a partial specialisation, the existing decl may have
8867 : : had its TYPE_CANONICAL adjusted. If so we should use structural
8868 : : equality to ensure is_matching_decl doesn't get confused. */
8869 : 74341 : if ((spec_flags & 2)
8870 : 74341 : && TYPE_CANONICAL (type) != TYPE_CANONICAL (etype))
8871 : 3 : SET_TYPE_STRUCTURAL_EQUALITY (type);
8872 : : }
8873 : :
8874 : 416204 : if (inner_tag)
8875 : : /* Set the TEMPLATE_DECL's type. */
8876 : 139952 : TREE_TYPE (decl) = TREE_TYPE (inner);
8877 : :
8878 : 416204 : if (!is_matching_decl (existing, decl, is_typedef))
8879 : 36 : unmatched_duplicate (existing);
8880 : :
8881 : 416204 : if (TREE_CODE (inner) == FUNCTION_DECL)
8882 : : {
8883 : 187861 : tree e_inner = STRIP_TEMPLATE (existing);
8884 : 187861 : for (auto parm = DECL_ARGUMENTS (inner);
8885 : 564355 : parm; parm = DECL_CHAIN (parm))
8886 : 376494 : DECL_CONTEXT (parm) = e_inner;
8887 : : }
8888 : :
8889 : : /* And our result is the existing node. */
8890 : 416204 : decl = existing;
8891 : : }
8892 : :
8893 : 861824 : if (mk == MK_friend_spec)
8894 : : {
8895 : 0 : tree e = match_mergeable_specialization (true, &spec);
8896 : 0 : if (!e)
8897 : : {
8898 : 0 : spec.spec = inner;
8899 : 0 : add_mergeable_specialization (true, &spec, decl, spec_flags);
8900 : : }
8901 : 0 : else if (e != existing)
8902 : 0 : set_overrun ();
8903 : : }
8904 : :
8905 : 861824 : if (is_typedef)
8906 : : {
8907 : : /* Insert the type into the array now. */
8908 : 155931 : tag = insert (TREE_TYPE (decl));
8909 : 155931 : dump (dumper::TREE)
8910 : 247 : && dump ("Cloned:%d typedef %C:%N",
8911 : 247 : tag, TREE_CODE (TREE_TYPE (decl)), TREE_TYPE (decl));
8912 : : }
8913 : :
8914 : 861824 : unused = saved_unused;
8915 : :
8916 : 861824 : if (DECL_MAYBE_IN_CHARGE_CDTOR_P (decl))
8917 : : {
8918 : 69823 : unsigned flags = u ();
8919 : :
8920 : 69823 : if (is_new)
8921 : : {
8922 : 30783 : bool cloned_p = flags & 1;
8923 : 30883 : dump (dumper::TREE) && dump ("CDTOR %N is %scloned",
8924 : : decl, cloned_p ? "" : "not ");
8925 : 30783 : if (cloned_p)
8926 : 45496 : build_cdtor_clones (decl, flags & 2, flags & 4,
8927 : : /* Update the member vec, if there is
8928 : : one (we're in a different cluster
8929 : : to the class defn). */
8930 : 22748 : CLASSTYPE_MEMBER_VEC (DECL_CONTEXT (decl)));
8931 : : }
8932 : : }
8933 : :
8934 : 861824 : if (VAR_P (decl) && CP_DECL_THREAD_LOCAL_P (decl))
8935 : : {
8936 : 162 : enum tls_model model = tls_model (u ());
8937 : 162 : if (is_new)
8938 : 142 : set_decl_tls_model (decl, model);
8939 : : }
8940 : :
8941 : 861824 : if (!NAMESPACE_SCOPE_P (inner)
8942 : 632523 : && ((TREE_CODE (inner) == TYPE_DECL
8943 : 152210 : && !is_typedef
8944 : 27249 : && TYPE_NAME (TREE_TYPE (inner)) == inner)
8945 : 605274 : || TREE_CODE (inner) == FUNCTION_DECL)
8946 : 1128351 : && u ())
8947 : 0 : read_definition (decl);
8948 : :
8949 : : return decl;
8950 : : }
8951 : :
8952 : : /* DECL is an unnameable member of CTX. Return a suitable identifying
8953 : : index. */
8954 : :
8955 : : static unsigned
8956 : 876 : get_field_ident (tree ctx, tree decl)
8957 : : {
8958 : 876 : gcc_checking_assert (TREE_CODE (decl) == USING_DECL
8959 : : || !DECL_NAME (decl)
8960 : : || IDENTIFIER_ANON_P (DECL_NAME (decl)));
8961 : :
8962 : 876 : unsigned ix = 0;
8963 : 876 : for (tree fields = TYPE_FIELDS (ctx);
8964 : 7649 : fields; fields = DECL_CHAIN (fields))
8965 : : {
8966 : 7649 : if (fields == decl)
8967 : 876 : return ix;
8968 : :
8969 : 6773 : if (DECL_CONTEXT (fields) == ctx
8970 : 6773 : && (TREE_CODE (fields) == USING_DECL
8971 : 6762 : || (TREE_CODE (fields) == FIELD_DECL
8972 : 19 : && (!DECL_NAME (fields)
8973 : 7 : || IDENTIFIER_ANON_P (DECL_NAME (fields))))))
8974 : : /* Count this field. */
8975 : 15 : ix++;
8976 : : }
8977 : 0 : gcc_unreachable ();
8978 : : }
8979 : :
8980 : : static tree
8981 : 743 : lookup_field_ident (tree ctx, unsigned ix)
8982 : : {
8983 : 743 : for (tree fields = TYPE_FIELDS (ctx);
8984 : 6259 : fields; fields = DECL_CHAIN (fields))
8985 : 6259 : if (DECL_CONTEXT (fields) == ctx
8986 : 6259 : && (TREE_CODE (fields) == USING_DECL
8987 : 6247 : || (TREE_CODE (fields) == FIELD_DECL
8988 : 755 : && (!DECL_NAME (fields)
8989 : 3 : || IDENTIFIER_ANON_P (DECL_NAME (fields))))))
8990 : 758 : if (!ix--)
8991 : : return fields;
8992 : :
8993 : : return NULL_TREE;
8994 : : }
8995 : :
8996 : : /* Reference DECL. REF indicates the walk kind we are performing.
8997 : : Return true if we should write this decl by value. */
8998 : :
8999 : : bool
9000 : 9533662 : trees_out::decl_node (tree decl, walk_kind ref)
9001 : : {
9002 : 9533662 : gcc_checking_assert (DECL_P (decl) && !DECL_TEMPLATE_PARM_P (decl)
9003 : : && DECL_CONTEXT (decl));
9004 : :
9005 : 9533662 : if (ref == WK_value)
9006 : : {
9007 : 1013606 : depset *dep = dep_hash->find_dependency (decl);
9008 : 1013606 : decl_value (decl, dep);
9009 : 1013606 : return false;
9010 : : }
9011 : :
9012 : 8520056 : switch (TREE_CODE (decl))
9013 : : {
9014 : : default:
9015 : : break;
9016 : :
9017 : 886410 : case FUNCTION_DECL:
9018 : 886410 : gcc_checking_assert (!DECL_LOCAL_DECL_P (decl));
9019 : : break;
9020 : :
9021 : : case RESULT_DECL:
9022 : : /* Unlike PARM_DECLs, RESULT_DECLs are only generated and
9023 : : referenced when we're inside the function itself. */
9024 : : return true;
9025 : :
9026 : 137753 : case PARM_DECL:
9027 : 137753 : {
9028 : 137753 : if (streaming_p ())
9029 : 61578 : i (tt_parm);
9030 : 137753 : tree_node (DECL_CONTEXT (decl));
9031 : :
9032 : : /* That must have put this in the map. */
9033 : 137753 : walk_kind ref = ref_node (decl);
9034 : 137753 : if (ref != WK_none)
9035 : : // FIXME:OPTIMIZATION We can wander into bits of the
9036 : : // template this was instantiated from, for instance
9037 : : // deferred noexcept and default parms, or references
9038 : : // to parms from earlier forward-decls (PR c++/119608).
9039 : : //
9040 : : // Currently we'll end up cloning those bits of tree.
9041 : : // It would be nice to reference those specific nodes.
9042 : : // I think putting those things in the map when we
9043 : : // reference their template by name.
9044 : : //
9045 : : // See the note in add_indirects.
9046 : : return true;
9047 : :
9048 : 0 : if (streaming_p ())
9049 : 0 : dump (dumper::TREE)
9050 : 0 : && dump ("Wrote %s reference %N",
9051 : 0 : TREE_CODE (decl) == PARM_DECL ? "parameter" : "result",
9052 : : decl);
9053 : : }
9054 : : return false;
9055 : :
9056 : : case IMPORTED_DECL:
9057 : : /* This describes a USING_DECL to the ME's debug machinery. It
9058 : : originates from the fortran FE, and has nothing to do with
9059 : : C++ modules. */
9060 : : return true;
9061 : :
9062 : : case LABEL_DECL:
9063 : : return true;
9064 : :
9065 : 46544 : case CONST_DECL:
9066 : 46544 : {
9067 : : /* If I end up cloning enum decls, implementing C++20 using
9068 : : E::v, this will need tweaking. */
9069 : 46544 : if (streaming_p ())
9070 : 10761 : i (tt_enum_decl);
9071 : 46544 : tree ctx = DECL_CONTEXT (decl);
9072 : 46544 : gcc_checking_assert (TREE_CODE (ctx) == ENUMERAL_TYPE);
9073 : 46544 : tree_node (ctx);
9074 : 46544 : tree_node (DECL_NAME (decl));
9075 : :
9076 : 46544 : int tag = insert (decl);
9077 : 46544 : if (streaming_p ())
9078 : 10761 : dump (dumper::TREE)
9079 : 21 : && dump ("Wrote enum decl:%d %C:%N", tag, TREE_CODE (decl), decl);
9080 : : return false;
9081 : : }
9082 : 14264 : break;
9083 : :
9084 : 14264 : case USING_DECL:
9085 : 14264 : if (TREE_CODE (DECL_CONTEXT (decl)) == FUNCTION_DECL)
9086 : : break;
9087 : : /* FALLTHROUGH */
9088 : :
9089 : 112525 : case FIELD_DECL:
9090 : 112525 : {
9091 : 112525 : if (streaming_p ())
9092 : 6908 : i (tt_data_member);
9093 : :
9094 : 112525 : tree ctx = DECL_CONTEXT (decl);
9095 : 112525 : tree_node (ctx);
9096 : :
9097 : 112525 : tree name = NULL_TREE;
9098 : :
9099 : 112525 : if (TREE_CODE (decl) == USING_DECL)
9100 : : ;
9101 : : else
9102 : : {
9103 : 111809 : name = DECL_NAME (decl);
9104 : 217540 : if (name && IDENTIFIER_ANON_P (name))
9105 : : name = NULL_TREE;
9106 : : }
9107 : :
9108 : 112525 : tree_node (name);
9109 : 112525 : if (!name && streaming_p ())
9110 : : {
9111 : 876 : unsigned ix = get_field_ident (ctx, decl);
9112 : 876 : u (ix);
9113 : : }
9114 : :
9115 : 112525 : int tag = insert (decl);
9116 : 112525 : if (streaming_p ())
9117 : 6908 : dump (dumper::TREE)
9118 : 26 : && dump ("Wrote member:%d %C:%N", tag, TREE_CODE (decl), decl);
9119 : : return false;
9120 : : }
9121 : 373453 : break;
9122 : :
9123 : 373453 : case VAR_DECL:
9124 : 373453 : gcc_checking_assert (!DECL_LOCAL_DECL_P (decl));
9125 : 373453 : if (DECL_VTABLE_OR_VTT_P (decl))
9126 : : {
9127 : : /* VTT or VTABLE, they are all on the vtables list. */
9128 : 1792 : tree ctx = CP_DECL_CONTEXT (decl);
9129 : 1792 : tree vtable = CLASSTYPE_VTABLES (ctx);
9130 : 1804 : for (unsigned ix = 0; ; vtable = DECL_CHAIN (vtable), ix++)
9131 : 1804 : if (vtable == decl)
9132 : : {
9133 : 1792 : gcc_checking_assert (DECL_VIRTUAL_P (decl));
9134 : 1792 : if (streaming_p ())
9135 : : {
9136 : 8 : u (tt_vtable);
9137 : 8 : u (ix);
9138 : 8 : dump (dumper::TREE)
9139 : 0 : && dump ("Writing vtable %N[%u]", ctx, ix);
9140 : : }
9141 : 1792 : tree_node (ctx);
9142 : 1792 : return false;
9143 : : }
9144 : : gcc_unreachable ();
9145 : : }
9146 : :
9147 : 371661 : if (DECL_TINFO_P (decl))
9148 : : {
9149 : 5312 : tinfo:
9150 : : /* A typeinfo, tt_tinfo_typedef or tt_tinfo_var. */
9151 : 9762 : bool is_var = VAR_P (decl);
9152 : 9762 : tree type = TREE_TYPE (decl);
9153 : 9762 : unsigned ix = get_pseudo_tinfo_index (type);
9154 : 9762 : if (streaming_p ())
9155 : : {
9156 : 6211 : i (is_var ? tt_tinfo_var : tt_tinfo_typedef);
9157 : 4405 : u (ix);
9158 : : }
9159 : :
9160 : 9762 : if (is_var)
9161 : : {
9162 : : /* We also need the type it is for and mangled name, so
9163 : : the reader doesn't need to complete the type (which
9164 : : would break section ordering). The type it is for is
9165 : : stashed on the name's TREE_TYPE. */
9166 : 5312 : tree name = DECL_NAME (decl);
9167 : 5312 : tree_node (name);
9168 : 5312 : type = TREE_TYPE (name);
9169 : 5312 : tree_node (type);
9170 : : }
9171 : :
9172 : 9762 : int tag = insert (decl);
9173 : 9762 : if (streaming_p ())
9174 : 4405 : dump (dumper::TREE)
9175 : 27 : && dump ("Wrote tinfo_%s:%d %u %N", is_var ? "var" : "type",
9176 : : tag, ix, type);
9177 : :
9178 : 9762 : if (!is_var)
9179 : : {
9180 : 4450 : tag = insert (type);
9181 : 4450 : if (streaming_p ())
9182 : 1806 : dump (dumper::TREE)
9183 : 9 : && dump ("Wrote tinfo_type:%d %u %N", tag, ix, type);
9184 : : }
9185 : 9762 : return false;
9186 : : }
9187 : :
9188 : 366349 : if (DECL_NTTP_OBJECT_P (decl))
9189 : : {
9190 : : /* A NTTP parm object. */
9191 : 33 : if (streaming_p ())
9192 : 7 : i (tt_nttp_var);
9193 : 33 : tree_node (tparm_object_argument (decl));
9194 : 33 : tree_node (DECL_NAME (decl));
9195 : 33 : int tag = insert (decl);
9196 : 33 : if (streaming_p ())
9197 : 7 : dump (dumper::TREE)
9198 : 0 : && dump ("Wrote nttp object:%d %N", tag, DECL_NAME (decl));
9199 : 33 : return false;
9200 : : }
9201 : :
9202 : : break;
9203 : :
9204 : 3031471 : case TYPE_DECL:
9205 : 3031471 : if (DECL_TINFO_P (decl))
9206 : 4450 : goto tinfo;
9207 : : break;
9208 : : }
9209 : :
9210 : 7739742 : if (DECL_THUNK_P (decl))
9211 : : {
9212 : : /* Thunks are similar to binfos -- write the thunked-to decl and
9213 : : then thunk-specific key info. */
9214 : 0 : if (streaming_p ())
9215 : : {
9216 : 0 : i (tt_thunk);
9217 : 0 : i (THUNK_FIXED_OFFSET (decl));
9218 : : }
9219 : :
9220 : : tree target = decl;
9221 : 0 : while (DECL_THUNK_P (target))
9222 : 0 : target = THUNK_TARGET (target);
9223 : 0 : tree_node (target);
9224 : 0 : tree_node (THUNK_VIRTUAL_OFFSET (decl));
9225 : 0 : int tag = insert (decl);
9226 : 0 : if (streaming_p ())
9227 : 0 : dump (dumper::TREE)
9228 : 0 : && dump ("Wrote:%d thunk %N to %N", tag, DECL_NAME (decl), target);
9229 : 0 : return false;
9230 : : }
9231 : :
9232 : 7739742 : if (DECL_CLONED_FUNCTION_P (decl))
9233 : : {
9234 : 256230 : tree target = get_clone_target (decl);
9235 : 256230 : if (streaming_p ())
9236 : 121245 : i (tt_clone_ref);
9237 : :
9238 : 256230 : tree_node (target);
9239 : 256230 : tree_node (DECL_NAME (decl));
9240 : 256230 : if (DECL_VIRTUAL_P (decl))
9241 : 19123 : tree_node (DECL_VINDEX (decl));
9242 : 256230 : int tag = insert (decl);
9243 : 256230 : if (streaming_p ())
9244 : 121245 : dump (dumper::TREE)
9245 : 164 : && dump ("Wrote:%d clone %N of %N", tag, DECL_NAME (decl), target);
9246 : 256230 : return false;
9247 : : }
9248 : :
9249 : : /* Everything left should be a thing that is in the entity table.
9250 : : Mostly things that can be defined outside of their (original
9251 : : declaration) context. */
9252 : 7483512 : gcc_checking_assert (TREE_CODE (decl) == TEMPLATE_DECL
9253 : : || VAR_P (decl)
9254 : : || TREE_CODE (decl) == FUNCTION_DECL
9255 : : || TREE_CODE (decl) == TYPE_DECL
9256 : : || TREE_CODE (decl) == USING_DECL
9257 : : || TREE_CODE (decl) == CONCEPT_DECL
9258 : : || TREE_CODE (decl) == NAMESPACE_DECL);
9259 : :
9260 : 7483512 : int use_tpl = -1;
9261 : 7483512 : tree ti = node_template_info (decl, use_tpl);
9262 : 7483512 : tree tpl = NULL_TREE;
9263 : :
9264 : : /* If this is the TEMPLATE_DECL_RESULT of a TEMPLATE_DECL, get the
9265 : : TEMPLATE_DECL. Note TI_TEMPLATE is not a TEMPLATE_DECL for
9266 : : (some) friends, so we need to check that. */
9267 : : // FIXME: Should local friend template specializations be by value?
9268 : : // They don't get idents so we'll never know they're imported, but I
9269 : : // think we can only reach them from the TU that defines the
9270 : : // befriending class?
9271 : 2829222 : if (ti && TREE_CODE (TI_TEMPLATE (ti)) == TEMPLATE_DECL
9272 : 10312703 : && DECL_TEMPLATE_RESULT (TI_TEMPLATE (ti)) == decl)
9273 : : {
9274 : : tpl = TI_TEMPLATE (ti);
9275 : 723133 : partial_template:
9276 : 723133 : if (streaming_p ())
9277 : : {
9278 : 2241 : i (tt_template);
9279 : 2241 : dump (dumper::TREE)
9280 : 9 : && dump ("Writing implicit template %C:%N%S",
9281 : 9 : TREE_CODE (tpl), tpl, tpl);
9282 : : }
9283 : 723133 : tree_node (tpl);
9284 : :
9285 : : /* Streaming TPL caused us to visit DECL and maybe its type,
9286 : : if it wasn't TU-local. */
9287 : 723133 : if (CHECKING_P && !has_tu_local_dep (tpl))
9288 : : {
9289 : 723106 : gcc_checking_assert (TREE_VISITED (decl));
9290 : 723106 : if (DECL_IMPLICIT_TYPEDEF_P (decl))
9291 : 377425 : gcc_checking_assert (TREE_VISITED (TREE_TYPE (decl)));
9292 : : }
9293 : : return false;
9294 : : }
9295 : :
9296 : 6845341 : tree ctx = CP_DECL_CONTEXT (decl);
9297 : 6845341 : depset *dep = NULL;
9298 : 6845341 : if (streaming_p ())
9299 : 1156010 : dep = dep_hash->find_dependency (decl);
9300 : 5689331 : else if (TREE_CODE (ctx) != FUNCTION_DECL
9301 : 207391 : || TREE_CODE (decl) == TEMPLATE_DECL
9302 : 189165 : || DECL_IMPLICIT_TYPEDEF_P (decl)
9303 : 5855586 : || (DECL_LANG_SPECIFIC (decl)
9304 : 118947 : && DECL_MODULE_IMPORT_P (decl)))
9305 : : {
9306 : 5523076 : auto kind = (TREE_CODE (decl) == NAMESPACE_DECL
9307 : 477376 : && !DECL_NAMESPACE_ALIAS (decl)
9308 : 5523076 : ? depset::EK_NAMESPACE : depset::EK_DECL);
9309 : 5523076 : dep = dep_hash->add_dependency (decl, kind);
9310 : : }
9311 : :
9312 : 13202109 : if (!dep || dep->is_tu_local ())
9313 : : {
9314 : : /* Some internal entity of context. Do by value. */
9315 : 322575 : decl_value (decl, dep);
9316 : 322575 : return false;
9317 : : }
9318 : :
9319 : 6522766 : if (dep->get_entity_kind () == depset::EK_REDIRECT)
9320 : : {
9321 : : /* The DECL_TEMPLATE_RESULT of a partial specialization.
9322 : : Write the partial specialization's template. */
9323 : 84962 : depset *redirect = dep->deps[0];
9324 : 84962 : gcc_checking_assert (redirect->get_entity_kind () == depset::EK_PARTIAL);
9325 : 84962 : tpl = redirect->get_entity ();
9326 : 84962 : goto partial_template;
9327 : : }
9328 : :
9329 : 6437804 : if (streaming_p ())
9330 : : {
9331 : : /* Locate the entity. */
9332 : 999947 : unsigned index = dep->cluster;
9333 : 999947 : unsigned import = 0;
9334 : :
9335 : 999947 : if (dep->is_import ())
9336 : 701 : import = dep->section;
9337 : 999246 : else if (CHECKING_P)
9338 : : /* It should be what we put there. */
9339 : 999246 : gcc_checking_assert (index == ~import_entity_index (decl));
9340 : :
9341 : : #if CHECKING_P
9342 : 701 : gcc_assert (!import || importedness >= 0);
9343 : : #endif
9344 : 999947 : i (tt_entity);
9345 : 999947 : u (import);
9346 : 999947 : u (index);
9347 : : }
9348 : :
9349 : 6437804 : int tag = insert (decl);
9350 : 6437804 : if (streaming_p () && dump (dumper::TREE))
9351 : : {
9352 : 529 : char const *kind = "import";
9353 : 529 : module_state *from = (*modules)[0];
9354 : 529 : if (dep->is_import ())
9355 : : /* Rediscover the unremapped index. */
9356 : 78 : from = import_entity_module (import_entity_index (decl));
9357 : : else
9358 : : {
9359 : 451 : tree o = get_originating_module_decl (decl);
9360 : 451 : o = STRIP_TEMPLATE (o);
9361 : 902 : kind = (DECL_LANG_SPECIFIC (o) && DECL_MODULE_PURVIEW_P (o)
9362 : 451 : ? "purview" : "GMF");
9363 : : }
9364 : 529 : dump ("Wrote %s:%d %C:%N@%M", kind,
9365 : 529 : tag, TREE_CODE (decl), decl, from);
9366 : : }
9367 : :
9368 : 6437804 : add_indirects (decl);
9369 : :
9370 : 6437804 : return false;
9371 : : }
9372 : :
9373 : : void
9374 : 7807165 : trees_out::type_node (tree type)
9375 : : {
9376 : 7807165 : gcc_assert (TYPE_P (type));
9377 : :
9378 : 7807165 : tree root = (TYPE_NAME (type)
9379 : 7807165 : ? TREE_TYPE (TYPE_NAME (type)) : TYPE_MAIN_VARIANT (type));
9380 : 7807165 : gcc_checking_assert (root);
9381 : :
9382 : 7807165 : if (type != root)
9383 : : {
9384 : 1769833 : if (streaming_p ())
9385 : 369509 : i (tt_variant_type);
9386 : 1769833 : tree_node (root);
9387 : :
9388 : 1769833 : int flags = -1;
9389 : :
9390 : 1769833 : if (TREE_CODE (type) == FUNCTION_TYPE
9391 : 1769833 : || TREE_CODE (type) == METHOD_TYPE)
9392 : : {
9393 : 421743 : int quals = type_memfn_quals (type);
9394 : 421743 : int rquals = type_memfn_rqual (type);
9395 : 421743 : tree raises = TYPE_RAISES_EXCEPTIONS (type);
9396 : 421743 : bool late = TYPE_HAS_LATE_RETURN_TYPE (type);
9397 : :
9398 : 421743 : if (raises != TYPE_RAISES_EXCEPTIONS (root)
9399 : 12215 : || rquals != type_memfn_rqual (root)
9400 : 8169 : || quals != type_memfn_quals (root)
9401 : 429786 : || late != TYPE_HAS_LATE_RETURN_TYPE (root))
9402 : 421743 : flags = rquals | (int (late) << 2) | (quals << 3);
9403 : : }
9404 : : else
9405 : : {
9406 : 1348090 : if (TYPE_USER_ALIGN (type))
9407 : 11945 : flags = TYPE_ALIGN_RAW (type);
9408 : : }
9409 : :
9410 : 1769833 : if (streaming_p ())
9411 : 369509 : i (flags);
9412 : :
9413 : 1769833 : if (flags < 0)
9414 : : ;
9415 : 433688 : else if (TREE_CODE (type) == FUNCTION_TYPE
9416 : 433688 : || TREE_CODE (type) == METHOD_TYPE)
9417 : : {
9418 : 421743 : tree raises = TYPE_RAISES_EXCEPTIONS (type);
9419 : 421743 : if (raises == TYPE_RAISES_EXCEPTIONS (root))
9420 : 12215 : raises = error_mark_node;
9421 : 421743 : tree_node (raises);
9422 : : }
9423 : :
9424 : : /* build_type_attribute_variant creates a new TYPE_MAIN_VARIANT, so
9425 : : variants should all have the same set of attributes. */
9426 : 1769833 : gcc_checking_assert (TYPE_ATTRIBUTES (type)
9427 : : == TYPE_ATTRIBUTES (TYPE_MAIN_VARIANT (type)));
9428 : :
9429 : 1769833 : if (streaming_p ())
9430 : : {
9431 : : /* Qualifiers. */
9432 : 369509 : int rquals = cp_type_quals (root);
9433 : 369509 : int quals = cp_type_quals (type);
9434 : 369509 : if (quals == rquals)
9435 : 170680 : quals = -1;
9436 : 369509 : i (quals);
9437 : : }
9438 : :
9439 : 1769833 : if (ref_node (type) != WK_none)
9440 : : {
9441 : 1769833 : int tag = insert (type);
9442 : 1769833 : if (streaming_p ())
9443 : : {
9444 : 369509 : i (0);
9445 : 369509 : dump (dumper::TREE)
9446 : 203 : && dump ("Wrote:%d variant type %C", tag, TREE_CODE (type));
9447 : : }
9448 : : }
9449 : 1769833 : return;
9450 : : }
9451 : :
9452 : 6037332 : if (tree name = TYPE_NAME (type))
9453 : 2372779 : if ((TREE_CODE (name) == TYPE_DECL && DECL_ORIGINAL_TYPE (name))
9454 : 1853942 : || DECL_TEMPLATE_PARM_P (name)
9455 : 1246251 : || TREE_CODE (type) == RECORD_TYPE
9456 : 221431 : || TREE_CODE (type) == UNION_TYPE
9457 : 2588679 : || TREE_CODE (type) == ENUMERAL_TYPE)
9458 : : {
9459 : 2214547 : gcc_checking_assert (DECL_P (name));
9460 : :
9461 : : /* We can meet template parms that we didn't meet in the
9462 : : tpl_parms walk, because we're referring to a derived type
9463 : : that was previously constructed from equivalent template
9464 : : parms. */
9465 : 2214547 : if (streaming_p ())
9466 : : {
9467 : 153857 : i (tt_typedef_type);
9468 : 153857 : dump (dumper::TREE)
9469 : 59 : && dump ("Writing %stypedef %C:%N",
9470 : 59 : DECL_IMPLICIT_TYPEDEF_P (name) ? "implicit " : "",
9471 : 59 : TREE_CODE (name), name);
9472 : : }
9473 : 2214547 : tree_node (name);
9474 : 2214547 : if (streaming_p ())
9475 : 153857 : dump (dumper::TREE) && dump ("Wrote typedef %C:%N%S",
9476 : 59 : TREE_CODE (name), name, name);
9477 : :
9478 : : /* We'll have either visited this type or have newly discovered
9479 : : that it's TU-local; either way we won't need to visit it again. */
9480 : 2214547 : gcc_checking_assert (TREE_VISITED (type) || has_tu_local_dep (name));
9481 : 2214547 : return;
9482 : : }
9483 : :
9484 : 3822785 : if (TYPE_PTRMEMFUNC_P (type))
9485 : : {
9486 : : /* This is a distinct type node, masquerading as a structure. */
9487 : 6639 : tree fn_type = TYPE_PTRMEMFUNC_FN_TYPE (type);
9488 : 6639 : if (streaming_p ())
9489 : 2017 : i (tt_ptrmem_type);
9490 : 6639 : tree_node (fn_type);
9491 : 6639 : int tag = insert (type);
9492 : 6639 : if (streaming_p ())
9493 : 2020 : dump (dumper::TREE) && dump ("Written:%d ptrmem type", tag);
9494 : 6639 : return;
9495 : : }
9496 : :
9497 : 3816146 : if (streaming_p ())
9498 : : {
9499 : 1217463 : u (tt_derived_type);
9500 : 1217463 : u (TREE_CODE (type));
9501 : : }
9502 : :
9503 : 3816146 : tree_node (TREE_TYPE (type));
9504 : 3816146 : switch (TREE_CODE (type))
9505 : : {
9506 : 0 : default:
9507 : : /* We should never meet a type here that is indescribable in
9508 : : terms of other types. */
9509 : 0 : gcc_unreachable ();
9510 : :
9511 : 62625 : case ARRAY_TYPE:
9512 : 62625 : tree_node (TYPE_DOMAIN (type));
9513 : 62625 : if (streaming_p ())
9514 : : /* Dependent arrays are constructed with TYPE_DEPENENT_P
9515 : : already set. */
9516 : 20185 : u (TYPE_DEPENDENT_P (type));
9517 : : break;
9518 : :
9519 : : case COMPLEX_TYPE:
9520 : : /* No additional data. */
9521 : : break;
9522 : :
9523 : 12 : case BOOLEAN_TYPE:
9524 : : /* A non-standard boolean type. */
9525 : 12 : if (streaming_p ())
9526 : 6 : u (TYPE_PRECISION (type));
9527 : : break;
9528 : :
9529 : 57591 : case INTEGER_TYPE:
9530 : 57591 : if (TREE_TYPE (type))
9531 : : {
9532 : : /* A range type (representing an array domain). */
9533 : 54652 : tree_node (TYPE_MIN_VALUE (type));
9534 : 54652 : tree_node (TYPE_MAX_VALUE (type));
9535 : : }
9536 : : else
9537 : : {
9538 : : /* A new integral type (representing a bitfield). */
9539 : 2939 : if (streaming_p ())
9540 : : {
9541 : 829 : unsigned prec = TYPE_PRECISION (type);
9542 : 829 : bool unsigned_p = TYPE_UNSIGNED (type);
9543 : :
9544 : 829 : u ((prec << 1) | unsigned_p);
9545 : : }
9546 : : }
9547 : : break;
9548 : :
9549 : 842261 : case METHOD_TYPE:
9550 : 842261 : case FUNCTION_TYPE:
9551 : 842261 : {
9552 : 842261 : gcc_checking_assert (type_memfn_rqual (type) == REF_QUAL_NONE);
9553 : :
9554 : 842261 : tree arg_types = TYPE_ARG_TYPES (type);
9555 : 842261 : if (TREE_CODE (type) == METHOD_TYPE)
9556 : : {
9557 : 516951 : tree_node (TREE_TYPE (TREE_VALUE (arg_types)));
9558 : 516951 : arg_types = TREE_CHAIN (arg_types);
9559 : : }
9560 : 842261 : tree_node (arg_types);
9561 : : }
9562 : 842261 : break;
9563 : :
9564 : 1282 : case OFFSET_TYPE:
9565 : 1282 : tree_node (TYPE_OFFSET_BASETYPE (type));
9566 : 1282 : break;
9567 : :
9568 : : case POINTER_TYPE:
9569 : : /* No additional data. */
9570 : : break;
9571 : :
9572 : 645355 : case REFERENCE_TYPE:
9573 : 645355 : if (streaming_p ())
9574 : 142809 : u (TYPE_REF_IS_RVALUE (type));
9575 : : break;
9576 : :
9577 : 736113 : case DECLTYPE_TYPE:
9578 : 736113 : case TYPEOF_TYPE:
9579 : 736113 : case DEPENDENT_OPERATOR_TYPE:
9580 : 736113 : tree_node (TYPE_VALUES_RAW (type));
9581 : 736113 : if (TREE_CODE (type) == DECLTYPE_TYPE)
9582 : : /* We stash a whole bunch of things into decltype's
9583 : : flags. */
9584 : 54107 : if (streaming_p ())
9585 : 18537 : tree_node_bools (type);
9586 : : break;
9587 : :
9588 : 7803 : case TRAIT_TYPE:
9589 : 7803 : tree_node (TRAIT_TYPE_KIND_RAW (type));
9590 : 7803 : tree_node (TRAIT_TYPE_TYPE1 (type));
9591 : 7803 : tree_node (TRAIT_TYPE_TYPE2 (type));
9592 : 7803 : break;
9593 : :
9594 : : case TYPE_ARGUMENT_PACK:
9595 : : /* No additional data. */
9596 : : break;
9597 : :
9598 : 136877 : case TYPE_PACK_EXPANSION:
9599 : 136877 : if (streaming_p ())
9600 : 55539 : u (PACK_EXPANSION_LOCAL_P (type));
9601 : 273754 : tree_node (PACK_EXPANSION_PARAMETER_PACKS (type));
9602 : 136877 : tree_node (PACK_EXPANSION_EXTRA_ARGS (type));
9603 : 136877 : break;
9604 : :
9605 : 22 : case PACK_INDEX_TYPE:
9606 : 22 : tree_node (PACK_INDEX_PACK (type));
9607 : 22 : tree_node (PACK_INDEX_INDEX (type));
9608 : 22 : break;
9609 : :
9610 : 160246 : case TYPENAME_TYPE:
9611 : 160246 : {
9612 : 160246 : tree_node (TYPE_CONTEXT (type));
9613 : 160246 : tree_node (DECL_NAME (TYPE_NAME (type)));
9614 : 160246 : tree_node (TYPENAME_TYPE_FULLNAME (type));
9615 : 160246 : if (streaming_p ())
9616 : : {
9617 : 56972 : enum tag_types tag_type = none_type;
9618 : 56972 : if (TYPENAME_IS_ENUM_P (type))
9619 : : tag_type = enum_type;
9620 : 56972 : else if (TYPENAME_IS_CLASS_P (type))
9621 : : tag_type = class_type;
9622 : 56972 : else if (TYPENAME_IS_UNION_P (type))
9623 : 0 : tag_type = union_type;
9624 : 56972 : u (int (tag_type));
9625 : : }
9626 : : }
9627 : : break;
9628 : :
9629 : 246 : case UNBOUND_CLASS_TEMPLATE:
9630 : 246 : {
9631 : 246 : tree decl = TYPE_NAME (type);
9632 : 246 : tree_node (DECL_CONTEXT (decl));
9633 : 246 : tree_node (DECL_NAME (decl));
9634 : 246 : tree_node (DECL_TEMPLATE_PARMS (decl));
9635 : : }
9636 : 246 : break;
9637 : :
9638 : 42 : case VECTOR_TYPE:
9639 : 42 : if (streaming_p ())
9640 : : {
9641 : 21 : poly_uint64 nunits = TYPE_VECTOR_SUBPARTS (type);
9642 : 42 : for (unsigned ix = 0; ix != NUM_POLY_INT_COEFFS; ix++)
9643 : 21 : wu (nunits.coeffs[ix]);
9644 : : }
9645 : : break;
9646 : : }
9647 : :
9648 : 3816146 : tree_node (TYPE_ATTRIBUTES (type));
9649 : :
9650 : : /* We may have met the type during emitting the above. */
9651 : 3816146 : if (ref_node (type) != WK_none)
9652 : : {
9653 : 3475143 : int tag = insert (type);
9654 : 3475143 : if (streaming_p ())
9655 : : {
9656 : 1057497 : i (0);
9657 : 1057497 : dump (dumper::TREE)
9658 : 558 : && dump ("Wrote:%d derived type %C", tag, TREE_CODE (type));
9659 : : }
9660 : : }
9661 : :
9662 : : return;
9663 : : }
9664 : :
9665 : : /* T is (mostly*) a non-mergeable node that must be written by value.
9666 : : The mergeable case is a BINFO, which are as-if DECLSs. */
9667 : :
9668 : : void
9669 : 23493759 : trees_out::tree_value (tree t)
9670 : : {
9671 : : /* We should never be writing a type by value. tree_type should
9672 : : have streamed it, or we're going via its TYPE_DECL. */
9673 : 23493759 : gcc_checking_assert (!TYPE_P (t));
9674 : :
9675 : 23493759 : if (DECL_P (t))
9676 : : /* No template, type, var or function, except anonymous
9677 : : non-context vars and types. */
9678 : 628964 : gcc_checking_assert ((TREE_CODE (t) != TEMPLATE_DECL
9679 : : && (TREE_CODE (t) != TYPE_DECL
9680 : : || (DECL_ARTIFICIAL (t) && !DECL_CONTEXT (t)))
9681 : : && (TREE_CODE (t) != VAR_DECL
9682 : : || ((!DECL_NAME (t)
9683 : : || IDENTIFIER_INTERNAL_P (DECL_NAME (t)))
9684 : : && !DECL_CONTEXT (t)))
9685 : : && TREE_CODE (t) != FUNCTION_DECL));
9686 : :
9687 : 23493759 : if (streaming_p ())
9688 : : {
9689 : : /* A new node -> tt_node. */
9690 : 9379842 : tree_val_count++;
9691 : 9379842 : i (tt_node);
9692 : 9379842 : start (t);
9693 : 9379842 : tree_node_bools (t);
9694 : : }
9695 : :
9696 : 23493759 : if (TREE_CODE (t) == TREE_BINFO)
9697 : : /* Binfos are decl-like and need merging information. */
9698 : 199448 : binfo_mergeable (t);
9699 : :
9700 : 23493759 : int tag = insert (t, WK_value);
9701 : 23493759 : if (streaming_p ())
9702 : 9379842 : dump (dumper::TREE)
9703 : 2831 : && dump ("Writing tree:%d %C:%N", tag, TREE_CODE (t), t);
9704 : :
9705 : 23493759 : int type_tag = 0;
9706 : 23493759 : tree type = NULL_TREE;
9707 : 23493759 : if (TREE_CODE (t) == TYPE_DECL)
9708 : : {
9709 : 28 : type = TREE_TYPE (t);
9710 : :
9711 : : /* We only support a limited set of features for uncontexted types;
9712 : : these are typically types created in the language-independent
9713 : : parts of the frontend (such as ubsan). */
9714 : 28 : gcc_checking_assert (RECORD_OR_UNION_TYPE_P (type)
9715 : : && TYPE_MAIN_VARIANT (type) == type
9716 : : && TYPE_NAME (type) == t
9717 : : && TYPE_STUB_DECL (type) == t
9718 : : && !TYPE_VFIELD (type)
9719 : : && !TYPE_BINFO (type)
9720 : : && !CLASS_TYPE_P (type));
9721 : :
9722 : 28 : if (streaming_p ())
9723 : : {
9724 : 14 : start (type);
9725 : 14 : tree_node_bools (type);
9726 : : }
9727 : :
9728 : 28 : type_tag = insert (type, WK_value);
9729 : 28 : if (streaming_p ())
9730 : 14 : dump (dumper::TREE)
9731 : 0 : && dump ("Writing type: %d %C:%N", type_tag,
9732 : 0 : TREE_CODE (type), type);
9733 : : }
9734 : :
9735 : 23493759 : tree_node_vals (t);
9736 : :
9737 : 23493759 : if (type)
9738 : : {
9739 : 28 : tree_node_vals (type);
9740 : 28 : tree_node (TYPE_SIZE (type));
9741 : 28 : tree_node (TYPE_SIZE_UNIT (type));
9742 : 28 : chained_decls (TYPE_FIELDS (type));
9743 : 28 : if (streaming_p ())
9744 : 14 : dump (dumper::TREE)
9745 : 0 : && dump ("Written type:%d %C:%N", type_tag, TREE_CODE (type), type);
9746 : : }
9747 : :
9748 : : /* For uncontexted VAR_DECLs we need to stream the definition so that
9749 : : importers can recreate their value. */
9750 : 23493759 : if (TREE_CODE (t) == VAR_DECL)
9751 : : {
9752 : 588 : gcc_checking_assert (!DECL_NONTRIVIALLY_INITIALIZED_P (t));
9753 : 588 : tree_node (DECL_INITIAL (t));
9754 : : }
9755 : :
9756 : 23493759 : if (streaming_p ())
9757 : 9382673 : dump (dumper::TREE) && dump ("Written tree:%d %C:%N", tag, TREE_CODE (t), t);
9758 : 23493759 : }
9759 : :
9760 : : tree
9761 : 8034866 : trees_in::tree_value ()
9762 : : {
9763 : 8034866 : tree t = start ();
9764 : 8034866 : if (!t || !tree_node_bools (t))
9765 : 0 : return NULL_TREE;
9766 : :
9767 : 8034866 : tree existing = t;
9768 : 8034866 : if (TREE_CODE (t) == TREE_BINFO)
9769 : : {
9770 : 71527 : tree type;
9771 : 71527 : unsigned ix = binfo_mergeable (&type);
9772 : 71527 : if (TYPE_BINFO (type))
9773 : : {
9774 : : /* We already have a definition, this must be a duplicate. */
9775 : 40735 : dump (dumper::MERGE)
9776 : 232 : && dump ("Deduping binfo %N[%u]", type, ix);
9777 : 40735 : existing = TYPE_BINFO (type);
9778 : 55941 : while (existing && ix--)
9779 : 15206 : existing = TREE_CHAIN (existing);
9780 : 40735 : if (existing)
9781 : 40735 : register_duplicate (t, existing);
9782 : : else
9783 : : /* Error, mismatch -- diagnose in read_class_def's
9784 : : checking. */
9785 : : existing = t;
9786 : : }
9787 : : }
9788 : :
9789 : : /* Insert into map. */
9790 : 8034866 : int tag = insert (existing);
9791 : 8034866 : dump (dumper::TREE)
9792 : 3667 : && dump ("Reading tree:%d %C", tag, TREE_CODE (t));
9793 : :
9794 : 8034866 : int type_tag = 0;
9795 : 8034866 : tree type = NULL_TREE;
9796 : 8034866 : if (TREE_CODE (t) == TYPE_DECL)
9797 : : {
9798 : 14 : type = start ();
9799 : 14 : if (!type || !tree_node_bools (type))
9800 : : t = NULL_TREE;
9801 : :
9802 : 14 : type_tag = insert (type);
9803 : 14 : if (t)
9804 : 14 : dump (dumper::TREE)
9805 : 0 : && dump ("Reading type:%d %C", type_tag, TREE_CODE (type));
9806 : : }
9807 : :
9808 : : if (!t)
9809 : : {
9810 : 0 : bail:
9811 : 0 : back_refs[~tag] = NULL_TREE;
9812 : 0 : if (type_tag)
9813 : 0 : back_refs[~type_tag] = NULL_TREE;
9814 : 0 : set_overrun ();
9815 : 0 : return NULL_TREE;
9816 : : }
9817 : :
9818 : 8034866 : if (!tree_node_vals (t))
9819 : 0 : goto bail;
9820 : :
9821 : 8034866 : if (type)
9822 : : {
9823 : 14 : if (!tree_node_vals (type))
9824 : 0 : goto bail;
9825 : :
9826 : 14 : TYPE_SIZE (type) = tree_node ();
9827 : 14 : TYPE_SIZE_UNIT (type) = tree_node ();
9828 : 14 : TYPE_FIELDS (type) = chained_decls ();
9829 : 14 : if (get_overrun ())
9830 : 0 : goto bail;
9831 : :
9832 : 14 : dump (dumper::TREE)
9833 : 0 : && dump ("Read type:%d %C:%N", type_tag, TREE_CODE (type), type);
9834 : : }
9835 : :
9836 : 8034866 : if (TREE_CODE (t) == VAR_DECL)
9837 : : {
9838 : 290 : DECL_INITIAL (t) = tree_node ();
9839 : 290 : if (TREE_STATIC (t))
9840 : 8 : varpool_node::finalize_decl (t);
9841 : : }
9842 : :
9843 : 8034866 : if (TREE_CODE (t) == LAMBDA_EXPR
9844 : 8034866 : && CLASSTYPE_LAMBDA_EXPR (TREE_TYPE (t)))
9845 : : {
9846 : 1733 : existing = CLASSTYPE_LAMBDA_EXPR (TREE_TYPE (t));
9847 : 1733 : back_refs[~tag] = existing;
9848 : : }
9849 : :
9850 : 8038533 : dump (dumper::TREE) && dump ("Read tree:%d %C:%N", tag, TREE_CODE (t), t);
9851 : :
9852 : 8034866 : if (TREE_CODE (existing) == INTEGER_CST && !TREE_OVERFLOW (existing))
9853 : : {
9854 : 463411 : existing = cache_integer_cst (t, true);
9855 : 463411 : back_refs[~tag] = existing;
9856 : : }
9857 : :
9858 : : return existing;
9859 : : }
9860 : :
9861 : : /* Whether DECL has a TU-local dependency in the hash. */
9862 : :
9863 : : bool
9864 : 723467 : trees_out::has_tu_local_dep (tree decl) const
9865 : : {
9866 : : /* Only the contexts of fields or enums remember that they're
9867 : : TU-local. */
9868 : 723467 : if (DECL_CONTEXT (decl)
9869 : 723467 : && (TREE_CODE (decl) == FIELD_DECL
9870 : 723464 : || TREE_CODE (decl) == CONST_DECL))
9871 : 3 : decl = TYPE_NAME (DECL_CONTEXT (decl));
9872 : :
9873 : 723467 : depset *dep = dep_hash->find_dependency (decl);
9874 : 723467 : if (!dep)
9875 : : {
9876 : : /* This might be the DECL_TEMPLATE_RESULT of a TEMPLATE_DECL
9877 : : which we found was TU-local and gave up early. */
9878 : 8477 : int use_tpl = -1;
9879 : 8477 : if (tree ti = node_template_info (decl, use_tpl))
9880 : 1729 : dep = dep_hash->find_dependency (TI_TEMPLATE (ti));
9881 : : }
9882 : :
9883 : 723482 : return dep && dep->is_tu_local ();
9884 : : }
9885 : :
9886 : : /* If T depends on a TU-local entity, return that decl. */
9887 : :
9888 : : tree
9889 : 366 : trees_out::find_tu_local_decl (tree t)
9890 : : {
9891 : : /* We need to have walked all deps first before we can check. */
9892 : 366 : gcc_checking_assert (!is_initial_scan ());
9893 : :
9894 : 894 : auto walker = [](tree *tp, int *walk_subtrees, void *data) -> tree
9895 : : {
9896 : 528 : auto self = (trees_out *)data;
9897 : :
9898 : 528 : tree decl = NULL_TREE;
9899 : 528 : if (TYPE_P (*tp))
9900 : : {
9901 : : /* A PMF type is a record type, which we otherwise wouldn't walk;
9902 : : return whether the function type is TU-local. */
9903 : 354 : if (TYPE_PTRMEMFUNC_P (*tp))
9904 : : {
9905 : 3 : *walk_subtrees = 0;
9906 : 3 : return self->find_tu_local_decl (TYPE_PTRMEMFUNC_FN_TYPE (*tp));
9907 : : }
9908 : : else
9909 : 351 : decl = TYPE_MAIN_DECL (*tp);
9910 : : }
9911 : 174 : else if (DECL_P (*tp))
9912 : : decl = *tp;
9913 : :
9914 : 357 : if (decl)
9915 : : {
9916 : : /* We found a DECL, this will tell us whether we're TU-local. */
9917 : 57 : *walk_subtrees = 0;
9918 : 57 : return self->has_tu_local_dep (decl) ? decl : NULL_TREE;
9919 : : }
9920 : : return NULL_TREE;
9921 : : };
9922 : :
9923 : : /* We need to walk without duplicates so that we step into the pointed-to
9924 : : types of array types. */
9925 : 366 : return cp_walk_tree_without_duplicates (&t, walker, this);
9926 : : }
9927 : :
9928 : : /* Stream out tree node T. We automatically create local back
9929 : : references, which is essentially a single pass lisp
9930 : : self-referential structure pretty-printer. */
9931 : :
9932 : : void
9933 : 208233890 : trees_out::tree_node (tree t)
9934 : : {
9935 : 208233890 : dump.indent ();
9936 : 208233890 : walk_kind ref = ref_node (t);
9937 : 208233890 : if (ref == WK_none)
9938 : 160506560 : goto done;
9939 : :
9940 : : /* Find TU-local entities and intercept streaming to instead write a
9941 : : placeholder value; this way we don't need to emit such decls.
9942 : : We only need to do this when writing a definition of an entity
9943 : : that we know names a TU-local entity. */
9944 : 55933643 : if (!is_initial_scan () && writing_local_entities)
9945 : : {
9946 : 846 : tree local_decl = NULL_TREE;
9947 : 846 : if (DECL_P (t) && has_tu_local_dep (t))
9948 : : local_decl = t;
9949 : : /* Consider a type to be TU-local if it refers to any TU-local decl,
9950 : : no matter how deep.
9951 : :
9952 : : This worsens diagnostics slightly, as we often no longer point
9953 : : directly to the at-fault entity when instantiating. However, this
9954 : : reduces the module size slightly and means that much less of pt.cc
9955 : : needs to know about us. */
9956 : 750 : else if (TYPE_P (t))
9957 : 141 : local_decl = find_tu_local_decl (t);
9958 : 609 : else if (EXPR_P (t))
9959 : 222 : local_decl = find_tu_local_decl (TREE_TYPE (t));
9960 : :
9961 : 459 : if (local_decl)
9962 : : {
9963 : 150 : int tag = insert (t, WK_value);
9964 : 150 : if (streaming_p ())
9965 : : {
9966 : 150 : tu_local_count++;
9967 : 150 : i (tt_tu_local);
9968 : 150 : dump (dumper::TREE)
9969 : 0 : && dump ("Writing TU-local entity:%d %C:%N",
9970 : 0 : tag, TREE_CODE (t), t);
9971 : : }
9972 : : /* TODO: Get a more descriptive name? */
9973 : 150 : tree_node (DECL_NAME (local_decl));
9974 : 150 : if (state)
9975 : 150 : state->write_location (*this, DECL_SOURCE_LOCATION (local_decl));
9976 : 150 : goto done;
9977 : : }
9978 : : }
9979 : :
9980 : 47727180 : if (ref != WK_normal)
9981 : 1240179 : goto skip_normal;
9982 : :
9983 : 46487001 : if (TREE_CODE (t) == IDENTIFIER_NODE)
9984 : : {
9985 : : /* An identifier node -> tt_id, tt_conv_id, tt_anon_id, tt_lambda_id,
9986 : : tt_internal_id. */
9987 : 5806771 : int code = tt_id;
9988 : 5806771 : if (IDENTIFIER_ANON_P (t))
9989 : 20581 : code = IDENTIFIER_LAMBDA_P (t) ? tt_lambda_id : tt_anon_id;
9990 : 5786190 : else if (IDENTIFIER_INTERNAL_P (t))
9991 : : code = tt_internal_id;
9992 : 5786176 : else if (IDENTIFIER_CONV_OP_P (t))
9993 : 8419 : code = tt_conv_id;
9994 : :
9995 : 5806771 : if (streaming_p ())
9996 : 1196084 : i (code);
9997 : :
9998 : 5806771 : if (code == tt_conv_id)
9999 : : {
10000 : 8419 : tree type = TREE_TYPE (t);
10001 : 8419 : gcc_checking_assert (type || t == conv_op_identifier);
10002 : 8419 : tree_node (type);
10003 : : }
10004 : 5798352 : else if (code == tt_id && streaming_p ())
10005 : 1186838 : str (IDENTIFIER_POINTER (t), IDENTIFIER_LENGTH (t));
10006 : 4611514 : else if (code == tt_internal_id && streaming_p ())
10007 : 7 : str (prefix_for_internal_label (t));
10008 : :
10009 : 5806771 : int tag = insert (t);
10010 : 5806771 : if (streaming_p ())
10011 : : {
10012 : : /* We know the ordering of the 5 id tags. */
10013 : 1196084 : static const char *const kinds[] =
10014 : : {"", "conv_op ", "anon ", "lambda ", "internal "};
10015 : 1196084 : dump (dumper::TREE)
10016 : 1070 : && dump ("Written:%d %sidentifier:%N", tag,
10017 : 1067 : kinds[code - tt_id],
10018 : 3 : code == tt_conv_id ? TREE_TYPE (t) : t);
10019 : : }
10020 : 5806771 : goto done;
10021 : : }
10022 : :
10023 : 40680230 : if (TREE_CODE (t) == TREE_BINFO)
10024 : : {
10025 : : /* A BINFO -> tt_binfo.
10026 : : We must do this by reference. We stream the binfo tree
10027 : : itself when streaming its owning RECORD_TYPE. That we got
10028 : : here means the dominating type is not in this SCC. */
10029 : 46704 : if (streaming_p ())
10030 : 1178 : i (tt_binfo);
10031 : 46704 : binfo_mergeable (t);
10032 : 46704 : gcc_checking_assert (!TREE_VISITED (t));
10033 : 46704 : int tag = insert (t);
10034 : 46704 : if (streaming_p ())
10035 : 1178 : dump (dumper::TREE) && dump ("Inserting binfo:%d %N", tag, t);
10036 : 46704 : goto done;
10037 : : }
10038 : :
10039 : 40633526 : if (TREE_CODE (t) == INTEGER_CST
10040 : 3027967 : && !TREE_OVERFLOW (t)
10041 : 43661493 : && TREE_CODE (TREE_TYPE (t)) == ENUMERAL_TYPE)
10042 : : {
10043 : : /* An integral constant of enumeral type. See if it matches one
10044 : : of the enumeration values. */
10045 : 25656 : for (tree values = TYPE_VALUES (TREE_TYPE (t));
10046 : 698850 : values; values = TREE_CHAIN (values))
10047 : : {
10048 : 697058 : tree decl = TREE_VALUE (values);
10049 : 697058 : if (tree_int_cst_equal (DECL_INITIAL (decl), t))
10050 : : {
10051 : 23864 : if (streaming_p ())
10052 : 6905 : u (tt_enum_value);
10053 : 23864 : tree_node (decl);
10054 : 23906 : dump (dumper::TREE) && dump ("Written enum value %N", decl);
10055 : 23864 : goto done;
10056 : : }
10057 : : }
10058 : : /* It didn't match. We'll write it a an explicit INTEGER_CST
10059 : : node. */
10060 : : }
10061 : :
10062 : 40609662 : if (TYPE_P (t))
10063 : : {
10064 : 7807165 : type_node (t);
10065 : 7807165 : goto done;
10066 : : }
10067 : :
10068 : 32802497 : if (DECL_P (t))
10069 : : {
10070 : 10164275 : if (DECL_TEMPLATE_PARM_P (t))
10071 : : {
10072 : 1624913 : tpl_parm_value (t);
10073 : 1624913 : goto done;
10074 : : }
10075 : :
10076 : 8539362 : if (!DECL_CONTEXT (t))
10077 : : {
10078 : : /* There are a few cases of decls with no context. We'll write
10079 : : these by value, but first assert they are cases we expect. */
10080 : 19306 : gcc_checking_assert (ref == WK_normal);
10081 : 19306 : switch (TREE_CODE (t))
10082 : : {
10083 : 0 : default: gcc_unreachable ();
10084 : :
10085 : 6306 : case LABEL_DECL:
10086 : : /* CASE_LABEL_EXPRs contain uncontexted LABEL_DECLs. */
10087 : 6306 : gcc_checking_assert (!DECL_NAME (t));
10088 : : break;
10089 : :
10090 : 588 : case VAR_DECL:
10091 : : /* AGGR_INIT_EXPRs cons up anonymous uncontexted VAR_DECLs,
10092 : : and internal vars are created by sanitizers and
10093 : : __builtin_source_location. */
10094 : 588 : gcc_checking_assert ((!DECL_NAME (t)
10095 : : || IDENTIFIER_INTERNAL_P (DECL_NAME (t)))
10096 : : && DECL_ARTIFICIAL (t));
10097 : : break;
10098 : :
10099 : : case PARM_DECL:
10100 : : /* REQUIRES_EXPRs have a tree list of uncontexted
10101 : : PARM_DECLS. It'd be nice if they had a
10102 : : distinguishing flag to double check. */
10103 : : break;
10104 : :
10105 : 28 : case TYPE_DECL:
10106 : : /* Some parts of the compiler need internal struct types;
10107 : : these types may not have an appropriate context to use.
10108 : : Walk the whole type (including its definition) by value. */
10109 : 28 : gcc_checking_assert (DECL_ARTIFICIAL (t)
10110 : : && TYPE_ARTIFICIAL (TREE_TYPE (t))
10111 : : && RECORD_OR_UNION_TYPE_P (TREE_TYPE (t))
10112 : : && !CLASS_TYPE_P (TREE_TYPE (t)));
10113 : : break;
10114 : : }
10115 : 19306 : mark_declaration (t, has_definition (t));
10116 : 19306 : goto by_value;
10117 : : }
10118 : : }
10119 : :
10120 : 22638222 : skip_normal:
10121 : 32398457 : if (DECL_P (t) && !decl_node (t, ref))
10122 : 8924004 : goto done;
10123 : :
10124 : : /* Otherwise by value */
10125 : 23493759 : by_value:
10126 : 23493759 : tree_value (t);
10127 : :
10128 : 208233890 : done:
10129 : : /* And, breath out. */
10130 : 208233890 : dump.outdent ();
10131 : 208233890 : }
10132 : :
10133 : : /* Stream in a tree node. */
10134 : :
10135 : : tree
10136 : 67618938 : trees_in::tree_node (bool is_use)
10137 : : {
10138 : 67618938 : if (get_overrun ())
10139 : : return NULL_TREE;
10140 : :
10141 : 67618938 : dump.indent ();
10142 : 67618938 : int tag = i ();
10143 : 67618938 : tree res = NULL_TREE;
10144 : 67618938 : switch (tag)
10145 : : {
10146 : 22016722 : default:
10147 : : /* backref, pull it out of the map. */
10148 : 22016722 : res = back_ref (tag);
10149 : 22016722 : break;
10150 : :
10151 : : case tt_null:
10152 : : /* NULL_TREE. */
10153 : : break;
10154 : :
10155 : 150 : case tt_tu_local:
10156 : 150 : {
10157 : : /* A translation-unit-local entity. */
10158 : 150 : res = make_node (TU_LOCAL_ENTITY);
10159 : 150 : int tag = insert (res);
10160 : :
10161 : 150 : TU_LOCAL_ENTITY_NAME (res) = tree_node ();
10162 : 150 : TU_LOCAL_ENTITY_LOCATION (res) = state->read_location (*this);
10163 : 150 : dump (dumper::TREE) && dump ("Read TU-local entity:%d %N", tag, res);
10164 : : }
10165 : : break;
10166 : :
10167 : 5157066 : case tt_fixed:
10168 : : /* A fixed ref, find it in the fixed_ref array. */
10169 : 5157066 : {
10170 : 5157066 : unsigned fix = u ();
10171 : 5157066 : if (fix < (*fixed_trees).length ())
10172 : : {
10173 : 5157066 : res = (*fixed_trees)[fix];
10174 : 5157066 : dump (dumper::TREE) && dump ("Read fixed:%u %C:%N%S", fix,
10175 : 5054 : TREE_CODE (res), res, res);
10176 : : }
10177 : :
10178 : 5152012 : if (!res)
10179 : 0 : set_overrun ();
10180 : : }
10181 : : break;
10182 : :
10183 : 53348 : case tt_parm:
10184 : 53348 : {
10185 : 53348 : tree fn = tree_node ();
10186 : 53348 : if (fn && TREE_CODE (fn) == FUNCTION_DECL)
10187 : 53348 : res = tree_node ();
10188 : 53348 : if (res)
10189 : 53348 : dump (dumper::TREE)
10190 : 21 : && dump ("Read %s reference %N",
10191 : 21 : TREE_CODE (res) == PARM_DECL ? "parameter" : "result",
10192 : : res);
10193 : : }
10194 : : break;
10195 : :
10196 : 8034866 : case tt_node:
10197 : : /* A new node. Stream it in. */
10198 : 8034866 : res = tree_value ();
10199 : 8034866 : break;
10200 : :
10201 : 861824 : case tt_decl:
10202 : : /* A new decl. Stream it in. */
10203 : 861824 : res = decl_value ();
10204 : 861824 : break;
10205 : :
10206 : 295010 : case tt_tpl_parm:
10207 : : /* A template parameter. Stream it in. */
10208 : 295010 : res = tpl_parm_value ();
10209 : 295010 : break;
10210 : :
10211 : 919979 : case tt_id:
10212 : : /* An identifier node. */
10213 : 919979 : {
10214 : 919979 : size_t l;
10215 : 919979 : const char *chars = str (&l);
10216 : 919979 : res = get_identifier_with_length (chars, l);
10217 : 919979 : int tag = insert (res);
10218 : 919979 : dump (dumper::TREE)
10219 : 1466 : && dump ("Read identifier:%d %N", tag, res);
10220 : : }
10221 : 919979 : break;
10222 : :
10223 : 2306 : case tt_conv_id:
10224 : : /* A conversion operator. Get the type and recreate the
10225 : : identifier. */
10226 : 2306 : {
10227 : 2306 : tree type = tree_node ();
10228 : 2306 : if (!get_overrun ())
10229 : : {
10230 : 2306 : res = type ? make_conv_op_name (type) : conv_op_identifier;
10231 : 2306 : int tag = insert (res);
10232 : 2306 : dump (dumper::TREE)
10233 : 27 : && dump ("Created conv_op:%d %S for %N", tag, res, type);
10234 : : }
10235 : : }
10236 : : break;
10237 : :
10238 : 4891 : case tt_anon_id:
10239 : 4891 : case tt_lambda_id:
10240 : : /* An anonymous or lambda id. */
10241 : 4891 : {
10242 : 4891 : res = make_anon_name ();
10243 : 4891 : if (tag == tt_lambda_id)
10244 : 2601 : IDENTIFIER_LAMBDA_P (res) = true;
10245 : 4891 : int tag = insert (res);
10246 : 4894 : dump (dumper::TREE)
10247 : 3 : && dump ("Read %s identifier:%d %N",
10248 : 3 : IDENTIFIER_LAMBDA_P (res) ? "lambda" : "anon", tag, res);
10249 : : }
10250 : : break;
10251 : :
10252 : 8 : case tt_internal_id:
10253 : : /* An internal label. */
10254 : 8 : {
10255 : 8 : const char *prefix = str ();
10256 : 8 : res = generate_internal_label (prefix);
10257 : 8 : int tag = insert (res);
10258 : 8 : dump (dumper::TREE)
10259 : 1 : && dump ("Read internal identifier:%d %N", tag, res);
10260 : : }
10261 : : break;
10262 : :
10263 : 129847 : case tt_typedef_type:
10264 : 129847 : res = tree_node ();
10265 : 129847 : if (res)
10266 : : {
10267 : 129847 : dump (dumper::TREE)
10268 : 74 : && dump ("Read %stypedef %C:%N",
10269 : 74 : DECL_IMPLICIT_TYPEDEF_P (res) ? "implicit " : "",
10270 : 74 : TREE_CODE (res), res);
10271 : 129847 : res = TREE_TYPE (res);
10272 : : }
10273 : : break;
10274 : :
10275 : 1021601 : case tt_derived_type:
10276 : : /* A type derived from some other type. */
10277 : 1021601 : {
10278 : 1021601 : enum tree_code code = tree_code (u ());
10279 : 1021601 : res = tree_node ();
10280 : :
10281 : 1021601 : switch (code)
10282 : : {
10283 : 0 : default:
10284 : 0 : set_overrun ();
10285 : 0 : break;
10286 : :
10287 : 16424 : case ARRAY_TYPE:
10288 : 16424 : {
10289 : 16424 : tree domain = tree_node ();
10290 : 16424 : int dep = u ();
10291 : 16424 : if (!get_overrun ())
10292 : 16424 : res = build_cplus_array_type (res, domain, dep);
10293 : : }
10294 : : break;
10295 : :
10296 : 87 : case COMPLEX_TYPE:
10297 : 87 : if (!get_overrun ())
10298 : 87 : res = build_complex_type (res);
10299 : : break;
10300 : :
10301 : 9 : case BOOLEAN_TYPE:
10302 : 9 : {
10303 : 9 : unsigned precision = u ();
10304 : 9 : if (!get_overrun ())
10305 : 9 : res = build_nonstandard_boolean_type (precision);
10306 : : }
10307 : : break;
10308 : :
10309 : 14968 : case INTEGER_TYPE:
10310 : 14968 : if (res)
10311 : : {
10312 : : /* A range type (representing an array domain). */
10313 : 14329 : tree min = tree_node ();
10314 : 14329 : tree max = tree_node ();
10315 : :
10316 : 14329 : if (!get_overrun ())
10317 : 14329 : res = build_range_type (res, min, max);
10318 : : }
10319 : : else
10320 : : {
10321 : : /* A new integral type (representing a bitfield). */
10322 : 639 : unsigned enc = u ();
10323 : 639 : if (!get_overrun ())
10324 : 639 : res = build_nonstandard_integer_type (enc >> 1, enc & 1);
10325 : : }
10326 : : break;
10327 : :
10328 : 292311 : case FUNCTION_TYPE:
10329 : 292311 : case METHOD_TYPE:
10330 : 292311 : {
10331 : 292311 : tree klass = code == METHOD_TYPE ? tree_node () : NULL_TREE;
10332 : 292311 : tree args = tree_node ();
10333 : 292311 : if (!get_overrun ())
10334 : : {
10335 : 292311 : if (klass)
10336 : 179428 : res = build_method_type_directly (klass, res, args);
10337 : : else
10338 : 112883 : res = build_function_type (res, args);
10339 : : }
10340 : : }
10341 : : break;
10342 : :
10343 : 231 : case OFFSET_TYPE:
10344 : 231 : {
10345 : 231 : tree base = tree_node ();
10346 : 231 : if (!get_overrun ())
10347 : 231 : res = build_offset_type (base, res);
10348 : : }
10349 : : break;
10350 : :
10351 : 142374 : case POINTER_TYPE:
10352 : 142374 : if (!get_overrun ())
10353 : 142374 : res = build_pointer_type (res);
10354 : : break;
10355 : :
10356 : 117431 : case REFERENCE_TYPE:
10357 : 117431 : {
10358 : 117431 : bool rval = bool (u ());
10359 : 117431 : if (!get_overrun ())
10360 : 117431 : res = cp_build_reference_type (res, rval);
10361 : : }
10362 : : break;
10363 : :
10364 : 304614 : case DECLTYPE_TYPE:
10365 : 304614 : case TYPEOF_TYPE:
10366 : 304614 : case DEPENDENT_OPERATOR_TYPE:
10367 : 304614 : {
10368 : 304614 : tree expr = tree_node ();
10369 : 304614 : if (!get_overrun ())
10370 : : {
10371 : 304614 : res = cxx_make_type (code);
10372 : 304614 : TYPE_VALUES_RAW (res) = expr;
10373 : 304614 : if (code == DECLTYPE_TYPE)
10374 : 14747 : tree_node_bools (res);
10375 : 304614 : SET_TYPE_STRUCTURAL_EQUALITY (res);
10376 : : }
10377 : : }
10378 : : break;
10379 : :
10380 : 2040 : case TRAIT_TYPE:
10381 : 2040 : {
10382 : 2040 : tree kind = tree_node ();
10383 : 2040 : tree type1 = tree_node ();
10384 : 2040 : tree type2 = tree_node ();
10385 : 2040 : if (!get_overrun ())
10386 : : {
10387 : 2040 : res = cxx_make_type (TRAIT_TYPE);
10388 : 2040 : TRAIT_TYPE_KIND_RAW (res) = kind;
10389 : 2040 : TRAIT_TYPE_TYPE1 (res) = type1;
10390 : 2040 : TRAIT_TYPE_TYPE2 (res) = type2;
10391 : 2040 : SET_TYPE_STRUCTURAL_EQUALITY (res);
10392 : : }
10393 : : }
10394 : : break;
10395 : :
10396 : 42580 : case TYPE_ARGUMENT_PACK:
10397 : 42580 : if (!get_overrun ())
10398 : : {
10399 : 42580 : tree pack = cxx_make_type (TYPE_ARGUMENT_PACK);
10400 : 42580 : ARGUMENT_PACK_ARGS (pack) = res;
10401 : : res = pack;
10402 : : }
10403 : : break;
10404 : :
10405 : 42469 : case TYPE_PACK_EXPANSION:
10406 : 42469 : {
10407 : 42469 : bool local = u ();
10408 : 42469 : tree param_packs = tree_node ();
10409 : 42469 : tree extra_args = tree_node ();
10410 : 42469 : if (!get_overrun ())
10411 : : {
10412 : 42469 : tree expn = cxx_make_type (TYPE_PACK_EXPANSION);
10413 : 42469 : SET_TYPE_STRUCTURAL_EQUALITY (expn);
10414 : 42469 : PACK_EXPANSION_PATTERN (expn) = res;
10415 : 84938 : PACK_EXPANSION_PARAMETER_PACKS (expn) = param_packs;
10416 : 42469 : PACK_EXPANSION_EXTRA_ARGS (expn) = extra_args;
10417 : 42469 : PACK_EXPANSION_LOCAL_P (expn) = local;
10418 : 42469 : res = expn;
10419 : : }
10420 : : }
10421 : : break;
10422 : :
10423 : 13 : case PACK_INDEX_TYPE:
10424 : 13 : {
10425 : 13 : tree pack = tree_node ();
10426 : 13 : tree index = tree_node ();
10427 : 13 : if (!get_overrun ())
10428 : 13 : res = make_pack_index (pack, index);
10429 : : }
10430 : : break;
10431 : :
10432 : 45976 : case TYPENAME_TYPE:
10433 : 45976 : {
10434 : 45976 : tree ctx = tree_node ();
10435 : 45976 : tree name = tree_node ();
10436 : 45976 : tree fullname = tree_node ();
10437 : 45976 : enum tag_types tag_type = tag_types (u ());
10438 : :
10439 : 45976 : if (!get_overrun ())
10440 : 45976 : res = build_typename_type (ctx, name, fullname, tag_type);
10441 : : }
10442 : : break;
10443 : :
10444 : 44 : case UNBOUND_CLASS_TEMPLATE:
10445 : 44 : {
10446 : 44 : tree ctx = tree_node ();
10447 : 44 : tree name = tree_node ();
10448 : 44 : tree parms = tree_node ();
10449 : :
10450 : 44 : if (!get_overrun ())
10451 : 44 : res = make_unbound_class_template_raw (ctx, name, parms);
10452 : : }
10453 : : break;
10454 : :
10455 : : case VECTOR_TYPE:
10456 : : {
10457 : : poly_uint64 nunits;
10458 : 60 : for (unsigned ix = 0; ix != NUM_POLY_INT_COEFFS; ix++)
10459 : 30 : nunits.coeffs[ix] = wu ();
10460 : 30 : if (!get_overrun ())
10461 : 30 : res = build_vector_type (res, nunits);
10462 : : }
10463 : : break;
10464 : : }
10465 : :
10466 : : /* In the exporting TU, a derived type with attributes was built by
10467 : : build_type_attribute_variant as a distinct copy, with itself as
10468 : : TYPE_MAIN_VARIANT. We repeat that on import to get the version
10469 : : without attributes as TYPE_CANONICAL. */
10470 : 1021601 : if (tree attribs = tree_node ())
10471 : 13225 : res = cp_build_type_attribute_variant (res, attribs);
10472 : :
10473 : 1021601 : int tag = i ();
10474 : 1021601 : if (!tag)
10475 : : {
10476 : 876669 : tag = insert (res);
10477 : 876669 : if (res)
10478 : 876669 : dump (dumper::TREE)
10479 : 678 : && dump ("Created:%d derived type %C", tag, code);
10480 : : }
10481 : : else
10482 : 144932 : res = back_ref (tag);
10483 : : }
10484 : : break;
10485 : :
10486 : 301798 : case tt_variant_type:
10487 : : /* Variant of some type. */
10488 : 301798 : {
10489 : 301798 : res = tree_node ();
10490 : 301798 : int flags = i ();
10491 : 301798 : if (get_overrun ())
10492 : : ;
10493 : 301798 : else if (flags < 0)
10494 : : /* No change. */;
10495 : 145606 : else if (TREE_CODE (res) == FUNCTION_TYPE
10496 : 145606 : || TREE_CODE (res) == METHOD_TYPE)
10497 : : {
10498 : 144306 : cp_ref_qualifier rqual = cp_ref_qualifier (flags & 3);
10499 : 144306 : bool late = (flags >> 2) & 1;
10500 : 144306 : cp_cv_quals quals = cp_cv_quals (flags >> 3);
10501 : :
10502 : 144306 : tree raises = tree_node ();
10503 : 144306 : if (raises == error_mark_node)
10504 : 5066 : raises = TYPE_RAISES_EXCEPTIONS (res);
10505 : :
10506 : 144306 : res = build_cp_fntype_variant (res, rqual, raises, late);
10507 : 144306 : if (TREE_CODE (res) == FUNCTION_TYPE)
10508 : 54266 : res = apply_memfn_quals (res, quals, rqual);
10509 : : }
10510 : : else
10511 : : {
10512 : 1300 : res = build_aligned_type (res, (1u << flags) >> 1);
10513 : 1300 : TYPE_USER_ALIGN (res) = true;
10514 : : }
10515 : :
10516 : 301798 : int quals = i ();
10517 : 301798 : if (quals >= 0 && !get_overrun ())
10518 : 157174 : res = cp_build_qualified_type (res, quals);
10519 : :
10520 : 301798 : int tag = i ();
10521 : 301798 : if (!tag)
10522 : : {
10523 : 301798 : tag = insert (res);
10524 : 301798 : if (res)
10525 : 301798 : dump (dumper::TREE)
10526 : 292 : && dump ("Created:%d variant type %C", tag, TREE_CODE (res));
10527 : : }
10528 : : else
10529 : 0 : res = back_ref (tag);
10530 : : }
10531 : : break;
10532 : :
10533 : 3816 : case tt_tinfo_var:
10534 : 3816 : case tt_tinfo_typedef:
10535 : : /* A tinfo var or typedef. */
10536 : 3816 : {
10537 : 3816 : bool is_var = tag == tt_tinfo_var;
10538 : 3816 : unsigned ix = u ();
10539 : 3816 : tree type = NULL_TREE;
10540 : :
10541 : 3816 : if (is_var)
10542 : : {
10543 : 2278 : tree name = tree_node ();
10544 : 2278 : type = tree_node ();
10545 : :
10546 : 2278 : if (!get_overrun ())
10547 : 2278 : res = get_tinfo_decl_direct (type, name, int (ix));
10548 : : }
10549 : : else
10550 : : {
10551 : 1538 : if (!get_overrun ())
10552 : : {
10553 : 1538 : type = get_pseudo_tinfo_type (ix);
10554 : 1538 : res = TYPE_NAME (type);
10555 : : }
10556 : : }
10557 : 3816 : if (res)
10558 : : {
10559 : 3816 : int tag = insert (res);
10560 : 3816 : dump (dumper::TREE)
10561 : 36 : && dump ("Created tinfo_%s:%d %S:%u for %N",
10562 : : is_var ? "var" : "decl", tag, res, ix, type);
10563 : 3816 : if (!is_var)
10564 : : {
10565 : 1538 : tag = insert (type);
10566 : 1538 : dump (dumper::TREE)
10567 : 12 : && dump ("Created tinfo_type:%d %u %N", tag, ix, type);
10568 : : }
10569 : : }
10570 : : }
10571 : : break;
10572 : :
10573 : 1113 : case tt_ptrmem_type:
10574 : : /* A pointer to member function. */
10575 : 1113 : {
10576 : 1113 : tree type = tree_node ();
10577 : 1113 : if (type && TREE_CODE (type) == POINTER_TYPE
10578 : 2226 : && TREE_CODE (TREE_TYPE (type)) == METHOD_TYPE)
10579 : : {
10580 : 1113 : res = build_ptrmemfunc_type (type);
10581 : 1113 : int tag = insert (res);
10582 : 1116 : dump (dumper::TREE) && dump ("Created:%d ptrmem type", tag);
10583 : : }
10584 : : else
10585 : 0 : set_overrun ();
10586 : : }
10587 : : break;
10588 : :
10589 : 6 : case tt_nttp_var:
10590 : : /* An NTTP object. */
10591 : 6 : {
10592 : 6 : tree init = tree_node ();
10593 : 6 : tree name = tree_node ();
10594 : 6 : if (!get_overrun ())
10595 : : {
10596 : : /* We don't want to check the initializer as that may require
10597 : : name lookup, which could recursively start lazy loading.
10598 : : Instead we know that INIT is already valid so we can just
10599 : : apply that directly. */
10600 : 6 : res = get_template_parm_object (init, name, /*check_init=*/false);
10601 : 6 : int tag = insert (res);
10602 : 6 : dump (dumper::TREE)
10603 : 0 : && dump ("Created nttp object:%d %N", tag, name);
10604 : : }
10605 : : }
10606 : : break;
10607 : :
10608 : 5239 : case tt_enum_value:
10609 : : /* An enum const value. */
10610 : 5239 : {
10611 : 5239 : if (tree decl = tree_node ())
10612 : : {
10613 : 5257 : dump (dumper::TREE) && dump ("Read enum value %N", decl);
10614 : 5239 : res = DECL_INITIAL (decl);
10615 : : }
10616 : :
10617 : 5239 : if (!res)
10618 : 0 : set_overrun ();
10619 : : }
10620 : : break;
10621 : :
10622 : 8938 : case tt_enum_decl:
10623 : : /* An enum decl. */
10624 : 8938 : {
10625 : 8938 : tree ctx = tree_node ();
10626 : 8938 : tree name = tree_node ();
10627 : :
10628 : 8938 : if (!get_overrun ()
10629 : 8938 : && TREE_CODE (ctx) == ENUMERAL_TYPE)
10630 : 8938 : res = find_enum_member (ctx, name);
10631 : :
10632 : 8938 : if (!res)
10633 : 0 : set_overrun ();
10634 : : else
10635 : : {
10636 : 8938 : int tag = insert (res);
10637 : 8938 : dump (dumper::TREE)
10638 : 18 : && dump ("Read enum decl:%d %C:%N", tag, TREE_CODE (res), res);
10639 : : }
10640 : : }
10641 : : break;
10642 : :
10643 : 5730 : case tt_data_member:
10644 : : /* A data member. */
10645 : 5730 : {
10646 : 5730 : tree ctx = tree_node ();
10647 : 5730 : tree name = tree_node ();
10648 : :
10649 : 5730 : if (!get_overrun ()
10650 : 5730 : && RECORD_OR_UNION_TYPE_P (ctx))
10651 : : {
10652 : 5730 : if (name)
10653 : 4987 : res = lookup_class_binding (ctx, name);
10654 : : else
10655 : 743 : res = lookup_field_ident (ctx, u ());
10656 : :
10657 : 5730 : if (!res
10658 : 5730 : || (TREE_CODE (res) != FIELD_DECL
10659 : 5730 : && TREE_CODE (res) != USING_DECL)
10660 : 11460 : || DECL_CONTEXT (res) != ctx)
10661 : : res = NULL_TREE;
10662 : : }
10663 : :
10664 : 5730 : if (!res)
10665 : 0 : set_overrun ();
10666 : : else
10667 : : {
10668 : 5730 : int tag = insert (res);
10669 : 5730 : dump (dumper::TREE)
10670 : 26 : && dump ("Read member:%d %C:%N", tag, TREE_CODE (res), res);
10671 : : }
10672 : : }
10673 : : break;
10674 : :
10675 : 1051 : case tt_binfo:
10676 : : /* A BINFO. Walk the tree of the dominating type. */
10677 : 1051 : {
10678 : 1051 : tree type;
10679 : 1051 : unsigned ix = binfo_mergeable (&type);
10680 : 1051 : if (type)
10681 : : {
10682 : 1051 : res = TYPE_BINFO (type);
10683 : 1114 : for (; ix && res; res = TREE_CHAIN (res))
10684 : 63 : ix--;
10685 : 1051 : if (!res)
10686 : 0 : set_overrun ();
10687 : : }
10688 : :
10689 : 1051 : if (get_overrun ())
10690 : : break;
10691 : :
10692 : : /* Insert binfo into backreferences. */
10693 : 1051 : tag = insert (res);
10694 : 1051 : dump (dumper::TREE) && dump ("Read binfo:%d %N", tag, res);
10695 : : }
10696 : 1051 : break;
10697 : :
10698 : 9 : case tt_vtable:
10699 : 9 : {
10700 : 9 : unsigned ix = u ();
10701 : 9 : tree ctx = tree_node ();
10702 : 9 : dump (dumper::TREE) && dump ("Reading vtable %N[%u]", ctx, ix);
10703 : 9 : if (TREE_CODE (ctx) == RECORD_TYPE && TYPE_LANG_SPECIFIC (ctx))
10704 : 9 : for (res = CLASSTYPE_VTABLES (ctx); res; res = DECL_CHAIN (res))
10705 : 9 : if (!ix--)
10706 : : break;
10707 : 9 : if (!res)
10708 : 0 : set_overrun ();
10709 : : }
10710 : : break;
10711 : :
10712 : 0 : case tt_thunk:
10713 : 0 : {
10714 : 0 : int fixed = i ();
10715 : 0 : tree target = tree_node ();
10716 : 0 : tree virt = tree_node ();
10717 : :
10718 : 0 : for (tree thunk = DECL_THUNKS (target);
10719 : 0 : thunk; thunk = DECL_CHAIN (thunk))
10720 : 0 : if (THUNK_FIXED_OFFSET (thunk) == fixed
10721 : 0 : && !THUNK_VIRTUAL_OFFSET (thunk) == !virt
10722 : 0 : && (!virt
10723 : 0 : || tree_int_cst_equal (virt, THUNK_VIRTUAL_OFFSET (thunk))))
10724 : : {
10725 : : res = thunk;
10726 : : break;
10727 : : }
10728 : :
10729 : 0 : int tag = insert (res);
10730 : 0 : if (res)
10731 : 0 : dump (dumper::TREE)
10732 : 0 : && dump ("Read:%d thunk %N to %N", tag, DECL_NAME (res), target);
10733 : : else
10734 : 0 : set_overrun ();
10735 : : }
10736 : : break;
10737 : :
10738 : 104454 : case tt_clone_ref:
10739 : 104454 : {
10740 : 104454 : tree target = tree_node ();
10741 : 104454 : tree name = tree_node ();
10742 : :
10743 : 104454 : if (DECL_P (target) && DECL_MAYBE_IN_CHARGE_CDTOR_P (target))
10744 : : {
10745 : 104454 : tree clone;
10746 : 163233 : FOR_EVERY_CLONE (clone, target)
10747 : 163233 : if (DECL_NAME (clone) == name)
10748 : : {
10749 : : res = clone;
10750 : : break;
10751 : : }
10752 : : }
10753 : :
10754 : : /* A clone might have a different vtable entry. */
10755 : 104454 : if (res && DECL_VIRTUAL_P (res))
10756 : 6684 : DECL_VINDEX (res) = tree_node ();
10757 : :
10758 : 104454 : if (!res)
10759 : 0 : set_overrun ();
10760 : 104454 : int tag = insert (res);
10761 : 104454 : if (res)
10762 : 104454 : dump (dumper::TREE)
10763 : 230 : && dump ("Read:%d clone %N of %N", tag, DECL_NAME (res), target);
10764 : : else
10765 : 0 : set_overrun ();
10766 : : }
10767 : : break;
10768 : :
10769 : 786519 : case tt_entity:
10770 : : /* Index into the entity table. Perhaps not loaded yet! */
10771 : 786519 : {
10772 : 786519 : unsigned origin = state->slurp->remap_module (u ());
10773 : 786519 : unsigned ident = u ();
10774 : 786519 : module_state *from = (*modules)[origin];
10775 : :
10776 : 786519 : if (!origin || ident >= from->entity_num)
10777 : 0 : set_overrun ();
10778 : 786519 : if (!get_overrun ())
10779 : : {
10780 : 786519 : binding_slot *slot = &(*entity_ary)[from->entity_lwm + ident];
10781 : 786519 : if (slot->is_lazy ())
10782 : 24377 : if (!from->lazy_load (ident, slot))
10783 : 0 : set_overrun ();
10784 : 786519 : res = *slot;
10785 : : }
10786 : :
10787 : 786519 : if (res)
10788 : : {
10789 : 786519 : const char *kind = (origin != state->mod ? "Imported" : "Named");
10790 : 786519 : int tag = insert (res);
10791 : 786519 : dump (dumper::TREE)
10792 : 605 : && dump ("%s:%d %C:%N@%M", kind, tag, TREE_CODE (res),
10793 : 605 : res, (*modules)[origin]);
10794 : :
10795 : 786519 : if (!add_indirects (res))
10796 : : {
10797 : 0 : set_overrun ();
10798 : 0 : res = NULL_TREE;
10799 : : }
10800 : : }
10801 : : }
10802 : : break;
10803 : :
10804 : 2031 : case tt_template:
10805 : : /* A template. */
10806 : 2031 : if (tree tpl = tree_node ())
10807 : : {
10808 : 2031 : res = (TREE_CODE (tpl) == TU_LOCAL_ENTITY ?
10809 : 2031 : tpl : DECL_TEMPLATE_RESULT (tpl));
10810 : 2031 : dump (dumper::TREE)
10811 : 9 : && dump ("Read template %C:%N", TREE_CODE (res), res);
10812 : : }
10813 : : break;
10814 : : }
10815 : :
10816 : 67618938 : if (is_use && !unused && res && DECL_P (res) && !TREE_USED (res))
10817 : : {
10818 : : /* Mark decl used as mark_used does -- we cannot call
10819 : : mark_used in the middle of streaming, we only need a subset
10820 : : of its functionality. */
10821 : 466715 : TREE_USED (res) = true;
10822 : :
10823 : : /* And for structured bindings also the underlying decl. */
10824 : 466715 : if (DECL_DECOMPOSITION_P (res) && !DECL_DECOMP_IS_BASE (res))
10825 : 860 : TREE_USED (DECL_DECOMP_BASE (res)) = true;
10826 : :
10827 : 466715 : if (DECL_CLONED_FUNCTION_P (res))
10828 : 4187 : TREE_USED (DECL_CLONED_FUNCTION (res)) = true;
10829 : : }
10830 : :
10831 : 67618938 : dump.outdent ();
10832 : 67618938 : return res;
10833 : : }
10834 : :
10835 : : void
10836 : 1382789 : trees_out::tpl_parms (tree parms, unsigned &tpl_levels)
10837 : : {
10838 : 1382789 : if (!parms)
10839 : : return;
10840 : :
10841 : 930939 : if (TREE_VISITED (parms))
10842 : : {
10843 : 392985 : ref_node (parms);
10844 : 392985 : return;
10845 : : }
10846 : :
10847 : 537954 : tpl_parms (TREE_CHAIN (parms), tpl_levels);
10848 : :
10849 : 537954 : tree vec = TREE_VALUE (parms);
10850 : 537954 : unsigned len = TREE_VEC_LENGTH (vec);
10851 : : /* Depth. */
10852 : 537954 : int tag = insert (parms);
10853 : 537954 : if (streaming_p ())
10854 : : {
10855 : 145759 : i (len + 1);
10856 : 145825 : dump (dumper::TREE)
10857 : 66 : && dump ("Writing template parms:%d level:%N length:%d",
10858 : 66 : tag, TREE_PURPOSE (parms), len);
10859 : : }
10860 : 537954 : tree_node (TREE_PURPOSE (parms));
10861 : :
10862 : 1475074 : for (unsigned ix = 0; ix != len; ix++)
10863 : : {
10864 : 937120 : tree parm = TREE_VEC_ELT (vec, ix);
10865 : 937120 : tree decl = TREE_VALUE (parm);
10866 : :
10867 : 937120 : gcc_checking_assert (DECL_TEMPLATE_PARM_P (decl));
10868 : 937120 : if (CHECKING_P)
10869 : 937120 : switch (TREE_CODE (decl))
10870 : : {
10871 : 0 : default: gcc_unreachable ();
10872 : :
10873 : 2757 : case TEMPLATE_DECL:
10874 : 2757 : gcc_assert ((TREE_CODE (TREE_TYPE (decl)) == TEMPLATE_TEMPLATE_PARM)
10875 : : && (TREE_CODE (DECL_TEMPLATE_RESULT (decl)) == TYPE_DECL)
10876 : : && (TYPE_NAME (TREE_TYPE (decl)) == decl));
10877 : : break;
10878 : :
10879 : 876880 : case TYPE_DECL:
10880 : 876880 : gcc_assert ((TREE_CODE (TREE_TYPE (decl)) == TEMPLATE_TYPE_PARM)
10881 : : && (TYPE_NAME (TREE_TYPE (decl)) == decl));
10882 : : break;
10883 : :
10884 : 57483 : case PARM_DECL:
10885 : 57483 : gcc_assert ((TREE_CODE (DECL_INITIAL (decl)) == TEMPLATE_PARM_INDEX)
10886 : : && (TREE_CODE (TEMPLATE_PARM_DECL (DECL_INITIAL (decl)))
10887 : : == CONST_DECL)
10888 : : && (DECL_TEMPLATE_PARM_P
10889 : : (TEMPLATE_PARM_DECL (DECL_INITIAL (decl)))));
10890 : : break;
10891 : : }
10892 : :
10893 : 937120 : tree_node (decl);
10894 : 937120 : tree_node (TEMPLATE_PARM_CONSTRAINTS (parm));
10895 : : }
10896 : :
10897 : 537954 : tpl_levels++;
10898 : : }
10899 : :
10900 : : tree
10901 : 230217 : trees_in::tpl_parms (unsigned &tpl_levels)
10902 : : {
10903 : 230217 : tree parms = NULL_TREE;
10904 : :
10905 : 483370 : while (int len = i ())
10906 : : {
10907 : 253153 : if (len < 0)
10908 : : {
10909 : 136965 : parms = back_ref (len);
10910 : 136965 : continue;
10911 : : }
10912 : :
10913 : 116188 : len -= 1;
10914 : 116188 : parms = tree_cons (NULL_TREE, NULL_TREE, parms);
10915 : 116188 : int tag = insert (parms);
10916 : 116188 : TREE_PURPOSE (parms) = tree_node ();
10917 : :
10918 : 116188 : dump (dumper::TREE)
10919 : 105 : && dump ("Reading template parms:%d level:%N length:%d",
10920 : 105 : tag, TREE_PURPOSE (parms), len);
10921 : :
10922 : 116188 : tree vec = make_tree_vec (len);
10923 : 308426 : for (int ix = 0; ix != len; ix++)
10924 : : {
10925 : 192238 : tree decl = tree_node ();
10926 : 192238 : if (!decl)
10927 : : return NULL_TREE;
10928 : :
10929 : 192238 : tree parm = build_tree_list (NULL, decl);
10930 : 192238 : TEMPLATE_PARM_CONSTRAINTS (parm) = tree_node ();
10931 : :
10932 : 192238 : TREE_VEC_ELT (vec, ix) = parm;
10933 : : }
10934 : :
10935 : 116188 : TREE_VALUE (parms) = vec;
10936 : 116188 : tpl_levels++;
10937 : : }
10938 : :
10939 : : return parms;
10940 : : }
10941 : :
10942 : : void
10943 : 844835 : trees_out::tpl_parms_fini (tree tmpl, unsigned tpl_levels)
10944 : : {
10945 : 844835 : for (tree parms = DECL_TEMPLATE_PARMS (tmpl);
10946 : 1382789 : tpl_levels--; parms = TREE_CHAIN (parms))
10947 : : {
10948 : 537954 : tree vec = TREE_VALUE (parms);
10949 : :
10950 : 537954 : tree_node (TREE_TYPE (vec));
10951 : 1475074 : for (unsigned ix = TREE_VEC_LENGTH (vec); ix--;)
10952 : : {
10953 : 937120 : tree parm = TREE_VEC_ELT (vec, ix);
10954 : 937120 : tree dflt = TREE_PURPOSE (parm);
10955 : 937120 : tree_node (dflt);
10956 : :
10957 : : /* Template template parameters need a context of their owning
10958 : : template. This is quite tricky to infer correctly on stream-in
10959 : : (see PR c++/98881) so we'll just provide it directly. */
10960 : 937120 : tree decl = TREE_VALUE (parm);
10961 : 937120 : if (TREE_CODE (decl) == TEMPLATE_DECL)
10962 : 2757 : tree_node (DECL_CONTEXT (decl));
10963 : : }
10964 : : }
10965 : 844835 : }
10966 : :
10967 : : bool
10968 : 230217 : trees_in::tpl_parms_fini (tree tmpl, unsigned tpl_levels)
10969 : : {
10970 : 230217 : for (tree parms = DECL_TEMPLATE_PARMS (tmpl);
10971 : 346405 : tpl_levels--; parms = TREE_CHAIN (parms))
10972 : : {
10973 : 116188 : tree vec = TREE_VALUE (parms);
10974 : :
10975 : 116188 : TREE_TYPE (vec) = tree_node ();
10976 : 308426 : for (unsigned ix = TREE_VEC_LENGTH (vec); ix--;)
10977 : : {
10978 : 192238 : tree parm = TREE_VEC_ELT (vec, ix);
10979 : 192238 : tree dflt = tree_node ();
10980 : 192238 : TREE_PURPOSE (parm) = dflt;
10981 : :
10982 : 192238 : tree decl = TREE_VALUE (parm);
10983 : 192238 : if (TREE_CODE (decl) == TEMPLATE_DECL)
10984 : 612 : DECL_CONTEXT (decl) = tree_node ();
10985 : :
10986 : 192238 : if (get_overrun ())
10987 : : return false;
10988 : : }
10989 : : }
10990 : : return true;
10991 : : }
10992 : :
10993 : : /* PARMS is a LIST, one node per level.
10994 : : TREE_VALUE is a TREE_VEC of parm info for that level.
10995 : : each ELT is a TREE_LIST
10996 : : TREE_VALUE is PARM_DECL, TYPE_DECL or TEMPLATE_DECL
10997 : : TREE_PURPOSE is the default value. */
10998 : :
10999 : : void
11000 : 844835 : trees_out::tpl_header (tree tpl, unsigned *tpl_levels)
11001 : : {
11002 : 844835 : tree parms = DECL_TEMPLATE_PARMS (tpl);
11003 : 844835 : tpl_parms (parms, *tpl_levels);
11004 : :
11005 : : /* Mark end. */
11006 : 844835 : if (streaming_p ())
11007 : 281234 : u (0);
11008 : :
11009 : 844835 : if (*tpl_levels)
11010 : 507459 : tree_node (TEMPLATE_PARMS_CONSTRAINTS (parms));
11011 : 844835 : }
11012 : :
11013 : : bool
11014 : 230217 : trees_in::tpl_header (tree tpl, unsigned *tpl_levels)
11015 : : {
11016 : 230217 : tree parms = tpl_parms (*tpl_levels);
11017 : 230217 : if (!parms)
11018 : : return false;
11019 : :
11020 : 230217 : DECL_TEMPLATE_PARMS (tpl) = parms;
11021 : :
11022 : 230217 : if (*tpl_levels)
11023 : 114767 : TEMPLATE_PARMS_CONSTRAINTS (parms) = tree_node ();
11024 : :
11025 : : return true;
11026 : : }
11027 : :
11028 : : /* Stream skeleton parm nodes, with their flags, type & parm indices.
11029 : : All the parms will have consecutive tags. */
11030 : :
11031 : : void
11032 : 1133198 : trees_out::fn_parms_init (tree fn)
11033 : : {
11034 : : /* First init them. */
11035 : 1133198 : int base_tag = ref_num - 1;
11036 : 1133198 : int ix = 0;
11037 : 1133198 : for (tree parm = DECL_ARGUMENTS (fn);
11038 : 3403800 : parm; parm = DECL_CHAIN (parm), ix++)
11039 : : {
11040 : 2270602 : if (streaming_p ())
11041 : : {
11042 : 756833 : start (parm);
11043 : 756833 : tree_node_bools (parm);
11044 : : }
11045 : 2270602 : int tag = insert (parm);
11046 : 2270602 : gcc_checking_assert (base_tag - ix == tag);
11047 : : }
11048 : : /* Mark the end. */
11049 : 1133198 : if (streaming_p ())
11050 : 377900 : u (0);
11051 : :
11052 : : /* Now stream their contents. */
11053 : 1133198 : ix = 0;
11054 : 1133198 : for (tree parm = DECL_ARGUMENTS (fn);
11055 : 3403800 : parm; parm = DECL_CHAIN (parm), ix++)
11056 : : {
11057 : 2270602 : if (streaming_p ())
11058 : 756833 : dump (dumper::TREE)
11059 : 222 : && dump ("Writing parm:%d %u (%N) of %N",
11060 : : base_tag - ix, ix, parm, fn);
11061 : 2270602 : tree_node_vals (parm);
11062 : : }
11063 : :
11064 : 1133198 : if (!streaming_p ())
11065 : : {
11066 : : /* We must walk contract attrs so the dependency graph is complete. */
11067 : 755298 : for (tree contract = DECL_CONTRACTS (fn);
11068 : 755406 : contract;
11069 : 108 : contract = CONTRACT_CHAIN (contract))
11070 : 108 : tree_node (contract);
11071 : : }
11072 : :
11073 : : /* Write a reference to contracts pre/post functions, if any, to avoid
11074 : : regenerating them in importers. */
11075 : 1133198 : tree_node (DECL_PRE_FN (fn));
11076 : 1133198 : tree_node (DECL_POST_FN (fn));
11077 : 1133198 : }
11078 : :
11079 : : /* Build skeleton parm nodes, read their flags, type & parm indices. */
11080 : :
11081 : : int
11082 : 322959 : trees_in::fn_parms_init (tree fn)
11083 : : {
11084 : 322959 : int base_tag = ~(int)back_refs.length ();
11085 : :
11086 : 322959 : tree *parm_ptr = &DECL_ARGUMENTS (fn);
11087 : 322959 : int ix = 0;
11088 : 972815 : for (; int code = u (); ix++)
11089 : : {
11090 : 649856 : tree parm = start (code);
11091 : 649856 : if (!tree_node_bools (parm))
11092 : : return 0;
11093 : :
11094 : 649856 : int tag = insert (parm);
11095 : 649856 : gcc_checking_assert (base_tag - ix == tag);
11096 : 649856 : *parm_ptr = parm;
11097 : 649856 : parm_ptr = &DECL_CHAIN (parm);
11098 : 649856 : }
11099 : :
11100 : 322959 : ix = 0;
11101 : 322959 : for (tree parm = DECL_ARGUMENTS (fn);
11102 : 972815 : parm; parm = DECL_CHAIN (parm), ix++)
11103 : : {
11104 : 649856 : dump (dumper::TREE)
11105 : 362 : && dump ("Reading parm:%d %u (%N) of %N",
11106 : : base_tag - ix, ix, parm, fn);
11107 : 649856 : if (!tree_node_vals (parm))
11108 : : return 0;
11109 : : }
11110 : :
11111 : : /* Reload references to contract functions, if any. */
11112 : 322959 : tree pre_fn = tree_node ();
11113 : 322959 : tree post_fn = tree_node ();
11114 : 322959 : set_contract_functions (fn, pre_fn, post_fn);
11115 : :
11116 : 322959 : return base_tag;
11117 : : }
11118 : :
11119 : : /* Read the remaining parm node data. Replace with existing (if
11120 : : non-null) in the map. */
11121 : :
11122 : : void
11123 : 322959 : trees_in::fn_parms_fini (int tag, tree fn, tree existing, bool is_defn)
11124 : : {
11125 : 510820 : tree existing_parm = existing ? DECL_ARGUMENTS (existing) : NULL_TREE;
11126 : 322959 : tree parms = DECL_ARGUMENTS (fn);
11127 : 322959 : unsigned ix = 0;
11128 : 972815 : for (tree parm = parms; parm; parm = DECL_CHAIN (parm), ix++)
11129 : : {
11130 : 649856 : if (existing_parm)
11131 : : {
11132 : 553540 : if (is_defn && !DECL_SAVED_TREE (existing))
11133 : : {
11134 : : /* If we're about to become the definition, set the
11135 : : names of the parms from us. */
11136 : 13655 : DECL_NAME (existing_parm) = DECL_NAME (parm);
11137 : 13655 : DECL_SOURCE_LOCATION (existing_parm) = DECL_SOURCE_LOCATION (parm);
11138 : : }
11139 : :
11140 : 375183 : back_refs[~tag] = existing_parm;
11141 : 375183 : existing_parm = DECL_CHAIN (existing_parm);
11142 : : }
11143 : 649856 : tag--;
11144 : : }
11145 : 322959 : }
11146 : :
11147 : : /* Encode into KEY the position of the local type (class or enum)
11148 : : declaration DECL within FN. The position is encoded as the
11149 : : index of the innermost BLOCK (numbered in BFS order) along with
11150 : : the index within its BLOCK_VARS list. */
11151 : :
11152 : : void
11153 : 9846 : trees_out::key_local_type (merge_key& key, tree decl, tree fn)
11154 : : {
11155 : 9846 : auto_vec<tree, 4> blocks;
11156 : 9846 : blocks.quick_push (DECL_INITIAL (fn));
11157 : 9846 : unsigned block_ix = 0;
11158 : 45276 : while (block_ix != blocks.length ())
11159 : : {
11160 : 17715 : tree block = blocks[block_ix];
11161 : 17715 : unsigned decl_ix = 0;
11162 : 51258 : for (tree var = BLOCK_VARS (block); var; var = DECL_CHAIN (var))
11163 : : {
11164 : 43389 : if (TREE_CODE (var) != TYPE_DECL)
11165 : 26253 : continue;
11166 : 17136 : if (var == decl)
11167 : : {
11168 : 9846 : key.index = (block_ix << 10) | decl_ix;
11169 : 9846 : return;
11170 : : }
11171 : 7290 : ++decl_ix;
11172 : : }
11173 : 16431 : for (tree sub = BLOCK_SUBBLOCKS (block); sub; sub = BLOCK_CHAIN (sub))
11174 : 8562 : blocks.safe_push (sub);
11175 : 7869 : ++block_ix;
11176 : : }
11177 : :
11178 : : /* Not-found value. */
11179 : 0 : key.index = 1023;
11180 : 9846 : }
11181 : :
11182 : : /* Look up the local type corresponding at the position encoded by
11183 : : KEY within FN and named NAME. */
11184 : :
11185 : : tree
11186 : 2906 : trees_in::key_local_type (const merge_key& key, tree fn, tree name)
11187 : : {
11188 : 2906 : if (!DECL_INITIAL (fn))
11189 : : return NULL_TREE;
11190 : :
11191 : 1773 : const unsigned block_pos = key.index >> 10;
11192 : 1773 : const unsigned decl_pos = key.index & 1023;
11193 : :
11194 : 1773 : if (decl_pos == 1023)
11195 : : return NULL_TREE;
11196 : :
11197 : 1773 : auto_vec<tree, 4> blocks;
11198 : 1773 : blocks.quick_push (DECL_INITIAL (fn));
11199 : 1773 : unsigned block_ix = 0;
11200 : 8207 : while (block_ix != blocks.length ())
11201 : : {
11202 : 3217 : tree block = blocks[block_ix];
11203 : 3217 : if (block_ix == block_pos)
11204 : : {
11205 : 1773 : unsigned decl_ix = 0;
11206 : 4745 : for (tree var = BLOCK_VARS (block); var; var = DECL_CHAIN (var))
11207 : : {
11208 : 4745 : if (TREE_CODE (var) != TYPE_DECL)
11209 : 2052 : continue;
11210 : : /* Prefer using the identifier as the key for more robustness
11211 : : to ODR violations, except for anonymous types since their
11212 : : compiler-generated identifiers aren't stable. */
11213 : 5386 : if (IDENTIFIER_ANON_P (name)
11214 : 2693 : ? decl_ix == decl_pos
11215 : 302 : : DECL_NAME (var) == name)
11216 : : return var;
11217 : 920 : ++decl_ix;
11218 : : }
11219 : : return NULL_TREE;
11220 : : }
11221 : 2998 : for (tree sub = BLOCK_SUBBLOCKS (block); sub; sub = BLOCK_CHAIN (sub))
11222 : 1554 : blocks.safe_push (sub);
11223 : 1444 : ++block_ix;
11224 : : }
11225 : :
11226 : : return NULL_TREE;
11227 : 1773 : }
11228 : :
11229 : : /* DEP is the depset of some decl we're streaming by value. Determine
11230 : : the merging behaviour. */
11231 : :
11232 : : merge_kind
11233 : 2918491 : trees_out::get_merge_kind (tree decl, depset *dep)
11234 : : {
11235 : 2918491 : if (!dep)
11236 : : {
11237 : 544835 : if (VAR_OR_FUNCTION_DECL_P (decl))
11238 : : {
11239 : : /* Any var or function with template info should have DEP. */
11240 : 286386 : gcc_checking_assert (!DECL_LANG_SPECIFIC (decl)
11241 : : || !DECL_TEMPLATE_INFO (decl));
11242 : 286386 : if (DECL_LOCAL_DECL_P (decl))
11243 : : return MK_unique;
11244 : : }
11245 : :
11246 : : /* Either unique, or some member of a class that cannot have an
11247 : : out-of-class definition. For instance a FIELD_DECL. */
11248 : 544607 : tree ctx = CP_DECL_CONTEXT (decl);
11249 : 544607 : if (TREE_CODE (ctx) == FUNCTION_DECL)
11250 : : {
11251 : : /* USING_DECLs and NAMESPACE_DECLs cannot have DECL_TEMPLATE_INFO --
11252 : : this isn't permitting them to have one. */
11253 : 322318 : gcc_checking_assert (TREE_CODE (decl) == USING_DECL
11254 : : || TREE_CODE (decl) == NAMESPACE_DECL
11255 : : || !DECL_LANG_SPECIFIC (decl)
11256 : : || !DECL_TEMPLATE_INFO (decl));
11257 : :
11258 : : return MK_unique;
11259 : : }
11260 : :
11261 : 222289 : if (TREE_CODE (decl) == TEMPLATE_DECL
11262 : 222289 : && DECL_UNINSTANTIATED_TEMPLATE_FRIEND_P (decl))
11263 : : return MK_local_friend;
11264 : :
11265 : 222289 : gcc_checking_assert (TYPE_P (ctx));
11266 : :
11267 : : /* Internal-only types will not need to dedup their members. */
11268 : 222289 : if (!DECL_CONTEXT (TYPE_NAME (ctx)))
11269 : : return MK_unique;
11270 : :
11271 : 222233 : if (TREE_CODE (decl) == USING_DECL)
11272 : : return MK_field;
11273 : :
11274 : 140920 : if (TREE_CODE (decl) == FIELD_DECL)
11275 : : {
11276 : 98577 : if (DECL_NAME (decl))
11277 : : {
11278 : : /* Anonymous FIELD_DECLs have a NULL name. */
11279 : 76556 : gcc_checking_assert (!IDENTIFIER_ANON_P (DECL_NAME (decl)));
11280 : : return MK_named;
11281 : : }
11282 : :
11283 : 22021 : if (walking_bit_field_unit)
11284 : : {
11285 : : /* The underlying storage unit for a bitfield. We do not
11286 : : need to dedup it, because it's only reachable through
11287 : : the bitfields it represents. And those are deduped. */
11288 : : // FIXME: Is that assertion correct -- do we ever fish it
11289 : : // out and put it in an expr?
11290 : 354 : gcc_checking_assert (!DECL_NAME (decl)
11291 : : && !RECORD_OR_UNION_TYPE_P (TREE_TYPE (decl))
11292 : : && !DECL_BIT_FIELD_REPRESENTATIVE (decl));
11293 : 354 : gcc_checking_assert ((TREE_CODE (TREE_TYPE (decl)) == ARRAY_TYPE
11294 : : ? TREE_CODE (TREE_TYPE (TREE_TYPE (decl)))
11295 : : : TREE_CODE (TREE_TYPE (decl)))
11296 : : == INTEGER_TYPE);
11297 : : return MK_unique;
11298 : : }
11299 : :
11300 : : return MK_field;
11301 : : }
11302 : :
11303 : 42343 : if (TREE_CODE (decl) == CONST_DECL)
11304 : : return MK_named;
11305 : :
11306 : 6764 : if (TREE_CODE (decl) == VAR_DECL
11307 : 6764 : && DECL_VTABLE_OR_VTT_P (decl))
11308 : : return MK_vtable;
11309 : :
11310 : 1128 : if (DECL_THUNK_P (decl))
11311 : : /* Thunks are unique-enough, because they're only referenced
11312 : : from the vtable. And that's either new (so we want the
11313 : : thunks), or it's a duplicate (so it will be dropped). */
11314 : : return MK_unique;
11315 : :
11316 : : /* There should be no other cases. */
11317 : 0 : gcc_unreachable ();
11318 : : }
11319 : :
11320 : 2373656 : gcc_checking_assert (TREE_CODE (decl) != FIELD_DECL
11321 : : && TREE_CODE (decl) != USING_DECL
11322 : : && TREE_CODE (decl) != CONST_DECL);
11323 : :
11324 : 2373656 : if (is_key_order ())
11325 : : {
11326 : : /* When doing the mergeablilty graph, there's an indirection to
11327 : : the actual depset. */
11328 : 791089 : gcc_assert (dep->is_special ());
11329 : 791089 : dep = dep->deps[0];
11330 : : }
11331 : :
11332 : 2373656 : gcc_checking_assert (decl == dep->get_entity ());
11333 : :
11334 : 2373656 : merge_kind mk = MK_named;
11335 : 2373656 : switch (dep->get_entity_kind ())
11336 : : {
11337 : 0 : default:
11338 : 0 : gcc_unreachable ();
11339 : :
11340 : : case depset::EK_PARTIAL:
11341 : : mk = MK_partial;
11342 : : break;
11343 : :
11344 : 1388825 : case depset::EK_DECL:
11345 : 1388825 : {
11346 : 1388825 : tree ctx = CP_DECL_CONTEXT (decl);
11347 : :
11348 : 1388825 : switch (TREE_CODE (ctx))
11349 : : {
11350 : 0 : default:
11351 : 0 : gcc_unreachable ();
11352 : :
11353 : 9846 : case FUNCTION_DECL:
11354 : 9846 : gcc_checking_assert
11355 : : (DECL_IMPLICIT_TYPEDEF_P (STRIP_TEMPLATE (decl)));
11356 : :
11357 : : mk = MK_local_type;
11358 : : break;
11359 : :
11360 : 1378979 : case RECORD_TYPE:
11361 : 1378979 : case UNION_TYPE:
11362 : 1378979 : case NAMESPACE_DECL:
11363 : 1378979 : if (DECL_NAME (decl) == as_base_identifier)
11364 : : {
11365 : : mk = MK_as_base;
11366 : : break;
11367 : : }
11368 : :
11369 : : /* A lambda may have a class as its context, even though it
11370 : : isn't a member in the traditional sense; see the test
11371 : : g++.dg/modules/lambda-6_a.C. */
11372 : 1749772 : if (DECL_IMPLICIT_TYPEDEF_P (STRIP_TEMPLATE (decl))
11373 : 1511506 : && LAMBDA_TYPE_P (TREE_TYPE (decl)))
11374 : : {
11375 : 974 : if (tree scope = LAMBDA_TYPE_EXTRA_SCOPE (TREE_TYPE (decl)))
11376 : : {
11377 : : /* Lambdas attached to fields are keyed to its class. */
11378 : 469 : if (TREE_CODE (scope) == FIELD_DECL)
11379 : 9 : scope = TYPE_NAME (DECL_CONTEXT (scope));
11380 : 469 : if (DECL_LANG_SPECIFIC (scope)
11381 : 938 : && DECL_MODULE_KEYED_DECLS_P (scope))
11382 : : {
11383 : : mk = MK_keyed;
11384 : : break;
11385 : : }
11386 : : }
11387 : : /* Lambdas not attached to any mangling scope are TU-local. */
11388 : : mk = MK_unique;
11389 : : break;
11390 : : }
11391 : :
11392 : 1314514 : if (TREE_CODE (decl) == TEMPLATE_DECL
11393 : 1314514 : && DECL_UNINSTANTIATED_TEMPLATE_FRIEND_P (decl))
11394 : : {
11395 : : mk = MK_local_friend;
11396 : : break;
11397 : : }
11398 : :
11399 : 1299592 : if (DECL_DECOMPOSITION_P (decl))
11400 : : {
11401 : : mk = MK_unique;
11402 : : break;
11403 : : }
11404 : :
11405 : 1299115 : if (IDENTIFIER_ANON_P (DECL_NAME (decl)))
11406 : : {
11407 : 20419 : if (RECORD_OR_UNION_TYPE_P (ctx))
11408 : : mk = MK_field;
11409 : 1008 : else if (DECL_IMPLICIT_TYPEDEF_P (decl)
11410 : 1008 : && UNSCOPED_ENUM_P (TREE_TYPE (decl))
11411 : 2016 : && TYPE_VALUES (TREE_TYPE (decl)))
11412 : : /* Keyed by first enum value, and underlying type. */
11413 : : mk = MK_enum;
11414 : : else
11415 : : /* No way to merge it, it is an ODR land-mine. */
11416 : : mk = MK_unique;
11417 : : }
11418 : : }
11419 : : }
11420 : : break;
11421 : :
11422 : 939312 : case depset::EK_SPECIALIZATION:
11423 : 939312 : {
11424 : 939312 : gcc_checking_assert (dep->is_special ());
11425 : :
11426 : 939312 : if (TREE_CODE (DECL_CONTEXT (decl)) == FUNCTION_DECL)
11427 : : /* An block-scope classes of templates are themselves
11428 : : templates. */
11429 : 4140 : gcc_checking_assert (DECL_IMPLICIT_TYPEDEF_P (decl));
11430 : :
11431 : 939312 : if (dep->is_friend_spec ())
11432 : : mk = MK_friend_spec;
11433 : 939312 : else if (dep->is_type_spec ())
11434 : : mk = MK_type_spec;
11435 : : else
11436 : 650967 : mk = MK_decl_spec;
11437 : :
11438 : 939312 : if (TREE_CODE (decl) == TEMPLATE_DECL)
11439 : : {
11440 : 65433 : spec_entry *entry = reinterpret_cast <spec_entry *> (dep->deps[0]);
11441 : 65433 : if (TREE_CODE (entry->spec) != TEMPLATE_DECL)
11442 : 7023 : mk = merge_kind (mk | MK_tmpl_tmpl_mask);
11443 : : }
11444 : : }
11445 : : break;
11446 : : }
11447 : :
11448 : : return mk;
11449 : : }
11450 : :
11451 : :
11452 : : /* The container of DECL -- not necessarily its context! */
11453 : :
11454 : : tree
11455 : 2918491 : trees_out::decl_container (tree decl)
11456 : : {
11457 : 2918491 : int use_tpl;
11458 : 2918491 : tree tpl = NULL_TREE;
11459 : 2918491 : if (tree template_info = node_template_info (decl, use_tpl))
11460 : 990721 : tpl = TI_TEMPLATE (template_info);
11461 : 2918491 : if (tpl == decl)
11462 : 0 : tpl = nullptr;
11463 : :
11464 : : /* Stream the template we're instantiated from. */
11465 : 2918491 : tree_node (tpl);
11466 : :
11467 : 2918491 : tree container = NULL_TREE;
11468 : 2918491 : if (TREE_CODE (decl) == TEMPLATE_DECL
11469 : 2918491 : && DECL_UNINSTANTIATED_TEMPLATE_FRIEND_P (decl))
11470 : 14922 : container = DECL_CHAIN (decl);
11471 : : else
11472 : 2903569 : container = CP_DECL_CONTEXT (decl);
11473 : :
11474 : 2918491 : if (TYPE_P (container))
11475 : 1723548 : container = TYPE_NAME (container);
11476 : :
11477 : 2918491 : tree_node (container);
11478 : :
11479 : 2918491 : return container;
11480 : : }
11481 : :
11482 : : tree
11483 : 861824 : trees_in::decl_container ()
11484 : : {
11485 : : /* The maybe-template. */
11486 : 861824 : (void)tree_node ();
11487 : :
11488 : 861824 : tree container = tree_node ();
11489 : :
11490 : 861824 : return container;
11491 : : }
11492 : :
11493 : : /* Write out key information about a mergeable DEP. Does not write
11494 : : the contents of DEP itself. The context has already been
11495 : : written. The container has already been streamed. */
11496 : :
11497 : : void
11498 : 2918491 : trees_out::key_mergeable (int tag, merge_kind mk, tree decl, tree inner,
11499 : : tree container, depset *dep)
11500 : : {
11501 : 2918491 : if (dep && is_key_order ())
11502 : : {
11503 : 791089 : gcc_checking_assert (dep->is_special ());
11504 : 791089 : dep = dep->deps[0];
11505 : : }
11506 : :
11507 : 2918491 : if (streaming_p ())
11508 : 1058398 : dump (dumper::MERGE)
11509 : 798 : && dump ("Writing:%d's %s merge key (%s) %C:%N", tag, merge_kind_name[mk],
11510 : 723 : dep ? dep->entity_kind_name () : "contained",
11511 : 798 : TREE_CODE (decl), decl);
11512 : :
11513 : : /* Now write the locating information. */
11514 : 2918491 : if (mk & MK_template_mask)
11515 : : {
11516 : : /* Specializations are located via their originating template,
11517 : : and the set of template args they specialize. */
11518 : 939312 : gcc_checking_assert (dep && dep->is_special ());
11519 : 939312 : spec_entry *entry = reinterpret_cast <spec_entry *> (dep->deps[0]);
11520 : :
11521 : 939312 : tree_node (entry->tmpl);
11522 : 939312 : tree_node (entry->args);
11523 : 939312 : if (mk & MK_tmpl_decl_mask)
11524 : 650967 : if (flag_concepts && TREE_CODE (inner) == VAR_DECL)
11525 : : {
11526 : : /* Variable template partial specializations might need
11527 : : constraints (see spec_hasher::equal). It's simpler to
11528 : : write NULL when we don't need them. */
11529 : 13065 : tree constraints = NULL_TREE;
11530 : :
11531 : 13065 : if (uses_template_parms (entry->args))
11532 : 396 : constraints = get_constraints (inner);
11533 : 13065 : tree_node (constraints);
11534 : : }
11535 : :
11536 : 939312 : if (CHECKING_P)
11537 : : {
11538 : : /* Make sure we can locate the decl. */
11539 : 939312 : tree existing = match_mergeable_specialization
11540 : 939312 : (bool (mk & MK_tmpl_decl_mask), entry);
11541 : :
11542 : 939312 : gcc_assert (existing);
11543 : 939312 : if (mk & MK_tmpl_decl_mask)
11544 : : {
11545 : 650967 : if (mk & MK_tmpl_tmpl_mask)
11546 : 5571 : existing = DECL_TI_TEMPLATE (existing);
11547 : : }
11548 : : else
11549 : : {
11550 : 288345 : if (mk & MK_tmpl_tmpl_mask)
11551 : 1452 : existing = CLASSTYPE_TI_TEMPLATE (existing);
11552 : : else
11553 : 286893 : existing = TYPE_NAME (existing);
11554 : : }
11555 : :
11556 : : /* The walkabout should have found ourselves. */
11557 : 939312 : gcc_checking_assert (TREE_CODE (decl) == TYPE_DECL
11558 : : ? same_type_p (TREE_TYPE (decl),
11559 : : TREE_TYPE (existing))
11560 : : : existing == decl);
11561 : : }
11562 : : }
11563 : 1979179 : else if (mk != MK_unique)
11564 : : {
11565 : 1654600 : merge_key key;
11566 : 1654600 : tree name = DECL_NAME (decl);
11567 : :
11568 : 1654600 : switch (mk)
11569 : : {
11570 : 0 : default:
11571 : 0 : gcc_unreachable ();
11572 : :
11573 : 1390831 : case MK_named:
11574 : 1390831 : case MK_friend_spec:
11575 : 1390831 : if (IDENTIFIER_CONV_OP_P (name))
11576 : 4522 : name = conv_op_identifier;
11577 : :
11578 : 1390831 : if (TREE_CODE (inner) == FUNCTION_DECL)
11579 : : {
11580 : : /* Functions are distinguished by parameter types. */
11581 : 724727 : tree fn_type = TREE_TYPE (inner);
11582 : :
11583 : 724727 : key.ref_q = type_memfn_rqual (fn_type);
11584 : 724727 : key.args = TYPE_ARG_TYPES (fn_type);
11585 : :
11586 : 724727 : if (tree reqs = get_constraints (inner))
11587 : : {
11588 : 46839 : if (cxx_dialect < cxx20)
11589 : 42 : reqs = CI_ASSOCIATED_CONSTRAINTS (reqs);
11590 : : else
11591 : 93636 : reqs = CI_DECLARATOR_REQS (reqs);
11592 : 46839 : key.constraints = reqs;
11593 : : }
11594 : :
11595 : 724727 : if (IDENTIFIER_CONV_OP_P (name)
11596 : 724727 : || (decl != inner
11597 : 377610 : && !(name == fun_identifier
11598 : : /* In case the user names something _FUN */
11599 : 66 : && LAMBDA_TYPE_P (DECL_CONTEXT (inner)))))
11600 : : /* And a function template, or conversion operator needs
11601 : : the return type. Except for the _FUN thunk of a
11602 : : generic lambda, which has a recursive decl_type'd
11603 : : return type. */
11604 : : // FIXME: What if the return type is a voldemort?
11605 : 382099 : key.ret = fndecl_declared_return_type (inner);
11606 : : }
11607 : : break;
11608 : :
11609 : 122391 : case MK_field:
11610 : 122391 : {
11611 : 122391 : unsigned ix = 0;
11612 : 122391 : if (TREE_CODE (inner) != FIELD_DECL)
11613 : : name = NULL_TREE;
11614 : : else
11615 : 21667 : gcc_checking_assert (!name || !IDENTIFIER_ANON_P (name));
11616 : :
11617 : 122391 : for (tree field = TYPE_FIELDS (TREE_TYPE (container));
11618 : 2899149 : ; field = DECL_CHAIN (field))
11619 : : {
11620 : 3021540 : tree finner = STRIP_TEMPLATE (field);
11621 : 3021540 : if (TREE_CODE (finner) == TREE_CODE (inner))
11622 : : {
11623 : 1022618 : if (finner == inner)
11624 : : break;
11625 : 900227 : ix++;
11626 : : }
11627 : 2899149 : }
11628 : 122391 : key.index = ix;
11629 : : }
11630 : 122391 : break;
11631 : :
11632 : 5636 : case MK_vtable:
11633 : 5636 : {
11634 : 5636 : tree vtable = CLASSTYPE_VTABLES (TREE_TYPE (container));
11635 : 7196 : for (unsigned ix = 0; ; vtable = DECL_CHAIN (vtable), ix++)
11636 : 7196 : if (vtable == decl)
11637 : : {
11638 : 5636 : key.index = ix;
11639 : 5636 : break;
11640 : : }
11641 : 5636 : name = NULL_TREE;
11642 : : }
11643 : 5636 : break;
11644 : :
11645 : 63978 : case MK_as_base:
11646 : 63978 : gcc_checking_assert
11647 : : (decl == TYPE_NAME (CLASSTYPE_AS_BASE (TREE_TYPE (container))));
11648 : : break;
11649 : :
11650 : 14922 : case MK_local_friend:
11651 : 14922 : {
11652 : : /* Find by index on the class's DECL_LIST */
11653 : 14922 : unsigned ix = 0;
11654 : 14922 : for (tree decls = CLASSTYPE_DECL_LIST (TREE_CHAIN (decl));
11655 : 411036 : decls; decls = TREE_CHAIN (decls))
11656 : 411036 : if (!TREE_PURPOSE (decls))
11657 : : {
11658 : 59301 : tree frnd = friend_from_decl_list (TREE_VALUE (decls));
11659 : 59301 : if (frnd == decl)
11660 : : break;
11661 : 44379 : ix++;
11662 : : }
11663 : 14922 : key.index = ix;
11664 : 14922 : name = NULL_TREE;
11665 : : }
11666 : 14922 : break;
11667 : :
11668 : 9846 : case MK_local_type:
11669 : 9846 : key_local_type (key, STRIP_TEMPLATE (decl), container);
11670 : 9846 : break;
11671 : :
11672 : 1008 : case MK_enum:
11673 : 1008 : {
11674 : : /* Anonymous enums are located by their first identifier,
11675 : : and underlying type. */
11676 : 1008 : tree type = TREE_TYPE (decl);
11677 : :
11678 : 1008 : gcc_checking_assert (UNSCOPED_ENUM_P (type));
11679 : : /* Using the type name drops the bit precision we might
11680 : : have been using on the enum. */
11681 : 1008 : key.ret = TYPE_NAME (ENUM_UNDERLYING_TYPE (type));
11682 : 1008 : if (tree values = TYPE_VALUES (type))
11683 : 1008 : name = DECL_NAME (TREE_VALUE (values));
11684 : : }
11685 : : break;
11686 : :
11687 : 469 : case MK_keyed:
11688 : 469 : {
11689 : 938 : gcc_checking_assert (LAMBDA_TYPE_P (TREE_TYPE (inner)));
11690 : 469 : tree scope = LAMBDA_TYPE_EXTRA_SCOPE (TREE_TYPE (inner));
11691 : 469 : gcc_checking_assert (TREE_CODE (scope) == VAR_DECL
11692 : : || TREE_CODE (scope) == FIELD_DECL
11693 : : || TREE_CODE (scope) == PARM_DECL
11694 : : || TREE_CODE (scope) == TYPE_DECL
11695 : : || TREE_CODE (scope) == CONCEPT_DECL);
11696 : : /* Lambdas attached to fields are keyed to the class. */
11697 : 469 : if (TREE_CODE (scope) == FIELD_DECL)
11698 : 9 : scope = TYPE_NAME (DECL_CONTEXT (scope));
11699 : 469 : auto *root = keyed_table->get (scope);
11700 : 469 : unsigned ix = root->length ();
11701 : : /* If we don't find it, we'll write a really big number
11702 : : that the reader will ignore. */
11703 : 487 : while (ix--)
11704 : 487 : if ((*root)[ix] == inner)
11705 : : break;
11706 : :
11707 : : /* Use the keyed-to decl as the 'name'. */
11708 : 469 : name = scope;
11709 : 469 : key.index = ix;
11710 : : }
11711 : 469 : break;
11712 : :
11713 : 45519 : case MK_partial:
11714 : 45519 : {
11715 : 45519 : tree ti = get_template_info (inner);
11716 : 45519 : key.constraints = get_constraints (inner);
11717 : 45519 : key.ret = TI_TEMPLATE (ti);
11718 : 45519 : key.args = TI_ARGS (ti);
11719 : : }
11720 : 45519 : break;
11721 : : }
11722 : :
11723 : 1654600 : tree_node (name);
11724 : 1654600 : if (streaming_p ())
11725 : : {
11726 : 588183 : unsigned code = (key.ref_q << 0) | (key.index << 2);
11727 : 588183 : u (code);
11728 : : }
11729 : :
11730 : 1654600 : if (mk == MK_enum)
11731 : 1008 : tree_node (key.ret);
11732 : 1653592 : else if (mk == MK_partial
11733 : 1608073 : || (mk == MK_named && inner
11734 : 1390831 : && TREE_CODE (inner) == FUNCTION_DECL))
11735 : : {
11736 : 770246 : tree_node (key.ret);
11737 : 770246 : tree arg = key.args;
11738 : 770246 : if (mk == MK_named)
11739 : 2080597 : while (arg && arg != void_list_node)
11740 : : {
11741 : 1355870 : tree_node (TREE_VALUE (arg));
11742 : 1355870 : arg = TREE_CHAIN (arg);
11743 : : }
11744 : 770246 : tree_node (arg);
11745 : 770246 : tree_node (key.constraints);
11746 : : }
11747 : : }
11748 : 2918491 : }
11749 : :
11750 : : /* DECL is a new declaration that may be duplicated in OVL. Use RET &
11751 : : ARGS to find its clone, or NULL. If DECL's DECL_NAME is NULL, this
11752 : : has been found by a proxy. It will be an enum type located by its
11753 : : first member.
11754 : :
11755 : : We're conservative with matches, so ambiguous decls will be
11756 : : registered as different, then lead to a lookup error if the two
11757 : : modules are both visible. Perhaps we want to do something similar
11758 : : to duplicate decls to get ODR errors on loading? We already have
11759 : : some special casing for namespaces. */
11760 : :
11761 : : static tree
11762 : 263414 : check_mergeable_decl (merge_kind mk, tree decl, tree ovl, merge_key const &key)
11763 : : {
11764 : 263414 : tree found = NULL_TREE;
11765 : 1078094 : for (ovl_iterator iter (ovl); !found && iter; ++iter)
11766 : : {
11767 : 522634 : tree match = *iter;
11768 : :
11769 : 522634 : tree d_inner = decl;
11770 : 522634 : tree m_inner = match;
11771 : :
11772 : 606224 : again:
11773 : 606224 : if (TREE_CODE (d_inner) != TREE_CODE (m_inner))
11774 : : {
11775 : 79576 : if (TREE_CODE (match) == NAMESPACE_DECL
11776 : 79576 : && !DECL_NAMESPACE_ALIAS (match))
11777 : : /* Namespaces are never overloaded. */
11778 : : found = match;
11779 : :
11780 : 79576 : continue;
11781 : : }
11782 : :
11783 : 526648 : switch (TREE_CODE (d_inner))
11784 : : {
11785 : 170916 : case TEMPLATE_DECL:
11786 : 170916 : if (template_heads_equivalent_p (d_inner, m_inner))
11787 : : {
11788 : 83590 : d_inner = DECL_TEMPLATE_RESULT (d_inner);
11789 : 83590 : m_inner = DECL_TEMPLATE_RESULT (m_inner);
11790 : 83590 : if (d_inner == error_mark_node
11791 : 83590 : && TYPE_DECL_ALIAS_P (m_inner))
11792 : : {
11793 : : found = match;
11794 : : break;
11795 : : }
11796 : 83590 : goto again;
11797 : : }
11798 : : break;
11799 : :
11800 : 258627 : case FUNCTION_DECL:
11801 : 258627 : if (tree m_type = TREE_TYPE (m_inner))
11802 : 258627 : if ((!key.ret
11803 : 133092 : || same_type_p (key.ret, fndecl_declared_return_type (m_inner)))
11804 : 241492 : && type_memfn_rqual (m_type) == key.ref_q
11805 : 241232 : && compparms (key.args, TYPE_ARG_TYPES (m_type))
11806 : : /* Reject if old is a "C" builtin and new is not "C".
11807 : : Matches decls_match behaviour. */
11808 : 123371 : && (!DECL_IS_UNDECLARED_BUILTIN (m_inner)
11809 : 5233 : || !DECL_EXTERN_C_P (m_inner)
11810 : 5105 : || DECL_EXTERN_C_P (d_inner))
11811 : : /* Reject if one is a different member of a
11812 : : guarded/pre/post fn set. */
11813 : 123366 : && (!flag_contracts
11814 : 346 : || (DECL_IS_PRE_FN_P (d_inner)
11815 : 173 : == DECL_IS_PRE_FN_P (m_inner)))
11816 : 381993 : && (!flag_contracts
11817 : 346 : || (DECL_IS_POST_FN_P (d_inner)
11818 : 173 : == DECL_IS_POST_FN_P (m_inner))))
11819 : : {
11820 : 123366 : tree m_reqs = get_constraints (m_inner);
11821 : 123366 : if (m_reqs)
11822 : : {
11823 : 8377 : if (cxx_dialect < cxx20)
11824 : 8 : m_reqs = CI_ASSOCIATED_CONSTRAINTS (m_reqs);
11825 : : else
11826 : 16746 : m_reqs = CI_DECLARATOR_REQS (m_reqs);
11827 : : }
11828 : :
11829 : 123366 : if (cp_tree_equal (key.constraints, m_reqs))
11830 : 161290 : found = match;
11831 : : }
11832 : : break;
11833 : :
11834 : 59123 : case TYPE_DECL:
11835 : 118246 : if (DECL_IMPLICIT_TYPEDEF_P (d_inner)
11836 : 59123 : == DECL_IMPLICIT_TYPEDEF_P (m_inner))
11837 : : {
11838 : 59097 : if (!IDENTIFIER_ANON_P (DECL_NAME (m_inner)))
11839 : 58970 : return match;
11840 : 127 : else if (mk == MK_enum
11841 : 127 : && (TYPE_NAME (ENUM_UNDERLYING_TYPE (TREE_TYPE (m_inner)))
11842 : 127 : == key.ret))
11843 : : found = match;
11844 : : }
11845 : : break;
11846 : :
11847 : : default:
11848 : : found = match;
11849 : : break;
11850 : : }
11851 : : }
11852 : :
11853 : 204444 : return found;
11854 : : }
11855 : :
11856 : : /* DECL, INNER & TYPE are a skeleton set of nodes for a decl. Only
11857 : : the bools have been filled in. Read its merging key and merge it.
11858 : : Returns the existing decl if there is one. */
11859 : :
11860 : : tree
11861 : 861824 : trees_in::key_mergeable (int tag, merge_kind mk, tree decl, tree inner,
11862 : : tree type, tree container, bool is_attached,
11863 : : bool is_imported_temploid_friend)
11864 : : {
11865 : 861824 : const char *kind = "new";
11866 : 861824 : tree existing = NULL_TREE;
11867 : :
11868 : 861824 : if (mk & MK_template_mask)
11869 : : {
11870 : : // FIXME: We could stream the specialization hash?
11871 : 260448 : spec_entry spec;
11872 : 260448 : spec.tmpl = tree_node ();
11873 : 260448 : spec.args = tree_node ();
11874 : :
11875 : 260448 : if (get_overrun ())
11876 : 0 : return error_mark_node;
11877 : :
11878 : 260448 : DECL_NAME (decl) = DECL_NAME (spec.tmpl);
11879 : 260448 : DECL_CONTEXT (decl) = DECL_CONTEXT (spec.tmpl);
11880 : 260448 : DECL_NAME (inner) = DECL_NAME (decl);
11881 : 260448 : DECL_CONTEXT (inner) = DECL_CONTEXT (decl);
11882 : :
11883 : 260448 : tree constr = NULL_TREE;
11884 : 260448 : bool is_decl = mk & MK_tmpl_decl_mask;
11885 : 260448 : if (is_decl)
11886 : : {
11887 : 184824 : if (flag_concepts && TREE_CODE (inner) == VAR_DECL)
11888 : : {
11889 : 3827 : constr = tree_node ();
11890 : 3827 : if (constr)
11891 : 0 : set_constraints (inner, constr);
11892 : : }
11893 : 367942 : spec.spec = (mk & MK_tmpl_tmpl_mask) ? inner : decl;
11894 : : }
11895 : : else
11896 : 75624 : spec.spec = type;
11897 : 260448 : existing = match_mergeable_specialization (is_decl, &spec);
11898 : 260448 : if (constr)
11899 : : /* We'll add these back later, if this is the new decl. */
11900 : 0 : remove_constraints (inner);
11901 : :
11902 : 260448 : if (!existing)
11903 : : ; /* We'll add to the table once read. */
11904 : 143408 : else if (mk & MK_tmpl_decl_mask)
11905 : : {
11906 : : /* A declaration specialization. */
11907 : 99116 : if (mk & MK_tmpl_tmpl_mask)
11908 : 1029 : existing = DECL_TI_TEMPLATE (existing);
11909 : : }
11910 : : else
11911 : : {
11912 : : /* A type specialization. */
11913 : 44292 : if (mk & MK_tmpl_tmpl_mask)
11914 : 234 : existing = CLASSTYPE_TI_TEMPLATE (existing);
11915 : : else
11916 : 44058 : existing = TYPE_NAME (existing);
11917 : : }
11918 : : }
11919 : 601376 : else if (mk == MK_unique)
11920 : : kind = "unique";
11921 : : else
11922 : : {
11923 : 459164 : tree name = tree_node ();
11924 : :
11925 : 459164 : merge_key key;
11926 : 459164 : unsigned code = u ();
11927 : 459164 : key.ref_q = cp_ref_qualifier ((code >> 0) & 3);
11928 : 459164 : key.index = code >> 2;
11929 : :
11930 : 459164 : if (mk == MK_enum)
11931 : 219 : key.ret = tree_node ();
11932 : 458945 : else if (mk == MK_partial
11933 : 449565 : || ((mk == MK_named || mk == MK_friend_spec)
11934 : 380304 : && TREE_CODE (inner) == FUNCTION_DECL))
11935 : : {
11936 : 213142 : key.ret = tree_node ();
11937 : 213142 : tree arg, *arg_ptr = &key.args;
11938 : 213142 : while ((arg = tree_node ())
11939 : 597534 : && arg != void_list_node
11940 : 993898 : && mk != MK_partial)
11941 : : {
11942 : 385688 : *arg_ptr = tree_cons (NULL_TREE, arg, NULL_TREE);
11943 : 385688 : arg_ptr = &TREE_CHAIN (*arg_ptr);
11944 : : }
11945 : 213142 : *arg_ptr = arg;
11946 : 213142 : key.constraints = tree_node ();
11947 : : }
11948 : :
11949 : 459164 : if (get_overrun ())
11950 : 0 : return error_mark_node;
11951 : :
11952 : 459164 : if (mk < MK_indirect_lwm)
11953 : : {
11954 : 451193 : DECL_NAME (decl) = name;
11955 : 451193 : DECL_CONTEXT (decl) = FROB_CONTEXT (container);
11956 : : }
11957 : 459164 : DECL_NAME (inner) = DECL_NAME (decl);
11958 : 459164 : DECL_CONTEXT (inner) = DECL_CONTEXT (decl);
11959 : :
11960 : 459164 : if (mk == MK_partial)
11961 : : {
11962 : 9380 : for (tree spec = DECL_TEMPLATE_SPECIALIZATIONS (key.ret);
11963 : 48769 : spec; spec = TREE_CHAIN (spec))
11964 : : {
11965 : 45426 : tree tmpl = TREE_VALUE (spec);
11966 : 45426 : tree ti = get_template_info (tmpl);
11967 : 45426 : if (template_args_equal (key.args, TI_ARGS (ti))
11968 : 52057 : && cp_tree_equal (key.constraints,
11969 : : get_constraints
11970 : 6631 : (DECL_TEMPLATE_RESULT (tmpl))))
11971 : : {
11972 : : existing = tmpl;
11973 : : break;
11974 : : }
11975 : : }
11976 : : }
11977 : 449784 : else if (mk == MK_keyed
11978 : 189 : && DECL_LANG_SPECIFIC (name)
11979 : 449973 : && DECL_MODULE_KEYED_DECLS_P (name))
11980 : : {
11981 : 189 : gcc_checking_assert (TREE_CODE (container) == NAMESPACE_DECL
11982 : : || TREE_CODE (container) == TYPE_DECL);
11983 : 189 : if (auto *set = keyed_table->get (name))
11984 : 459284 : if (key.index < set->length ())
11985 : : {
11986 : 120 : existing = (*set)[key.index];
11987 : 120 : if (existing)
11988 : : {
11989 : 120 : gcc_checking_assert
11990 : : (DECL_IMPLICIT_TYPEDEF_P (existing));
11991 : 120 : if (inner != decl)
11992 : 85 : existing
11993 : 85 : = CLASSTYPE_TI_TEMPLATE (TREE_TYPE (existing));
11994 : : }
11995 : : }
11996 : : }
11997 : : else
11998 : 449595 : switch (TREE_CODE (container))
11999 : : {
12000 : 0 : default:
12001 : 0 : gcc_unreachable ();
12002 : :
12003 : 115692 : case NAMESPACE_DECL:
12004 : 115692 : if (is_attached
12005 : 115692 : && !is_imported_temploid_friend
12006 : 115692 : && !(state->is_module () || state->is_partition ()))
12007 : : kind = "unique";
12008 : : else
12009 : : {
12010 : 114014 : gcc_checking_assert (mk == MK_named || mk == MK_enum);
12011 : 114014 : tree mvec;
12012 : 114014 : tree *vslot = mergeable_namespace_slots (container, name,
12013 : : is_attached, &mvec);
12014 : 114014 : existing = check_mergeable_decl (mk, decl, *vslot, key);
12015 : 114014 : if (!existing)
12016 : 42720 : add_mergeable_namespace_entity (vslot, decl);
12017 : : else
12018 : : {
12019 : : /* Note that we now have duplicates to deal with in
12020 : : name lookup. */
12021 : 71294 : if (is_attached)
12022 : 33 : BINDING_VECTOR_PARTITION_DUPS_P (mvec) = true;
12023 : : else
12024 : 71261 : BINDING_VECTOR_GLOBAL_DUPS_P (mvec) = true;
12025 : : }
12026 : : }
12027 : : break;
12028 : :
12029 : 2906 : case FUNCTION_DECL:
12030 : 2906 : gcc_checking_assert (mk == MK_local_type);
12031 : 2906 : existing = key_local_type (key, container, name);
12032 : 2906 : if (existing && inner != decl)
12033 : 3224 : existing = TYPE_TI_TEMPLATE (TREE_TYPE (existing));
12034 : : break;
12035 : :
12036 : 330997 : case TYPE_DECL:
12037 : 330997 : gcc_checking_assert (!is_imported_temploid_friend);
12038 : 3993 : if (is_attached && !(state->is_module () || state->is_partition ())
12039 : : /* Implicit member functions can come from
12040 : : anywhere. */
12041 : 334073 : && !(DECL_ARTIFICIAL (decl)
12042 : 1803 : && TREE_CODE (decl) == FUNCTION_DECL
12043 : 567 : && !DECL_THUNK_P (decl)))
12044 : : kind = "unique";
12045 : : else
12046 : : {
12047 : 328488 : tree ctx = TREE_TYPE (container);
12048 : :
12049 : : /* For some reason templated enumeral types are not marked
12050 : : as COMPLETE_TYPE_P, even though they have members.
12051 : : This may well be a bug elsewhere. */
12052 : 328488 : if (TREE_CODE (ctx) == ENUMERAL_TYPE)
12053 : 11752 : existing = find_enum_member (ctx, name);
12054 : 316736 : else if (COMPLETE_TYPE_P (ctx))
12055 : : {
12056 : 187845 : switch (mk)
12057 : : {
12058 : 0 : default:
12059 : 0 : gcc_unreachable ();
12060 : :
12061 : 149756 : case MK_named:
12062 : 149756 : existing = lookup_class_binding (ctx, name);
12063 : 149756 : if (existing)
12064 : : {
12065 : 149400 : tree inner = decl;
12066 : 149400 : if (TREE_CODE (inner) == TEMPLATE_DECL
12067 : 149400 : && !DECL_MEMBER_TEMPLATE_P (inner))
12068 : 67117 : inner = DECL_TEMPLATE_RESULT (inner);
12069 : :
12070 : 149400 : existing = check_mergeable_decl
12071 : 149400 : (mk, inner, existing, key);
12072 : :
12073 : 149400 : if (!existing && DECL_ALIAS_TEMPLATE_P (decl))
12074 : : {} // FIXME: Insert into specialization
12075 : : // tables, we'll need the arguments for that!
12076 : : }
12077 : : break;
12078 : :
12079 : 26270 : case MK_field:
12080 : 26270 : {
12081 : 26270 : unsigned ix = key.index;
12082 : 26270 : for (tree field = TYPE_FIELDS (ctx);
12083 : 889534 : field; field = DECL_CHAIN (field))
12084 : : {
12085 : 889534 : tree finner = STRIP_TEMPLATE (field);
12086 : 889534 : if (TREE_CODE (finner) == TREE_CODE (inner))
12087 : 298581 : if (!ix--)
12088 : : {
12089 : : existing = field;
12090 : : break;
12091 : : }
12092 : : }
12093 : : }
12094 : : break;
12095 : :
12096 : 1384 : case MK_vtable:
12097 : 1384 : {
12098 : 1384 : unsigned ix = key.index;
12099 : 1384 : for (tree vtable = CLASSTYPE_VTABLES (ctx);
12100 : 1761 : vtable; vtable = DECL_CHAIN (vtable))
12101 : 1596 : if (!ix--)
12102 : : {
12103 : : existing = vtable;
12104 : : break;
12105 : : }
12106 : : }
12107 : : break;
12108 : :
12109 : 7503 : case MK_as_base:
12110 : 7503 : {
12111 : 7503 : tree as_base = CLASSTYPE_AS_BASE (ctx);
12112 : 7503 : if (as_base && as_base != ctx)
12113 : 7503 : existing = TYPE_NAME (as_base);
12114 : : }
12115 : : break;
12116 : :
12117 : 2932 : case MK_local_friend:
12118 : 2932 : {
12119 : 2932 : unsigned ix = key.index;
12120 : 2932 : for (tree decls = CLASSTYPE_DECL_LIST (ctx);
12121 : 81828 : decls; decls = TREE_CHAIN (decls))
12122 : 81828 : if (!TREE_PURPOSE (decls) && !ix--)
12123 : : {
12124 : 2932 : existing
12125 : 2932 : = friend_from_decl_list (TREE_VALUE (decls));
12126 : 2932 : break;
12127 : : }
12128 : : }
12129 : : break;
12130 : : }
12131 : :
12132 : 187845 : if (existing && mk < MK_indirect_lwm && mk != MK_partial
12133 : 183958 : && TREE_CODE (decl) == TEMPLATE_DECL
12134 : 272830 : && !DECL_MEMBER_TEMPLATE_P (decl))
12135 : : {
12136 : 68970 : tree ti;
12137 : 68970 : if (DECL_IMPLICIT_TYPEDEF_P (existing))
12138 : 863 : ti = TYPE_TEMPLATE_INFO (TREE_TYPE (existing));
12139 : : else
12140 : 68107 : ti = DECL_TEMPLATE_INFO (existing);
12141 : 68970 : existing = TI_TEMPLATE (ti);
12142 : : }
12143 : : }
12144 : : }
12145 : : }
12146 : : }
12147 : :
12148 : 861824 : dump (dumper::MERGE)
12149 : 2401 : && dump ("Read:%d's %s merge key (%s) %C:%N", tag, merge_kind_name[mk],
12150 : 2401 : existing ? "matched" : kind, TREE_CODE (decl), decl);
12151 : :
12152 : : return existing;
12153 : : }
12154 : :
12155 : : void
12156 : 246152 : trees_out::binfo_mergeable (tree binfo)
12157 : : {
12158 : 246152 : tree dom = binfo;
12159 : 314074 : while (tree parent = BINFO_INHERITANCE_CHAIN (dom))
12160 : : dom = parent;
12161 : 246152 : tree type = BINFO_TYPE (dom);
12162 : 246152 : gcc_checking_assert (TYPE_BINFO (type) == dom);
12163 : 246152 : tree_node (type);
12164 : 246152 : if (streaming_p ())
12165 : : {
12166 : : unsigned ix = 0;
12167 : 136091 : for (; dom != binfo; dom = TREE_CHAIN (dom))
12168 : 35209 : ix++;
12169 : 100882 : u (ix);
12170 : : }
12171 : 246152 : }
12172 : :
12173 : : unsigned
12174 : 72578 : trees_in::binfo_mergeable (tree *type)
12175 : : {
12176 : 72578 : *type = tree_node ();
12177 : 72578 : return u ();
12178 : : }
12179 : :
12180 : : /* DECL is a just streamed declaration with attributes DATTR that should
12181 : : have matching ABI tags as EXISTING's attributes EATTR. Check that the
12182 : : ABI tags match, and report an error if not. */
12183 : :
12184 : : void
12185 : 281359 : trees_in::check_abi_tags (tree existing, tree decl, tree &eattr, tree &dattr)
12186 : : {
12187 : 281359 : tree etags = lookup_attribute ("abi_tag", eattr);
12188 : 281359 : tree dtags = lookup_attribute ("abi_tag", dattr);
12189 : 281359 : if ((etags == nullptr) != (dtags == nullptr)
12190 : 281359 : || (etags && !attribute_value_equal (etags, dtags)))
12191 : : {
12192 : 30 : if (etags)
12193 : 21 : etags = TREE_VALUE (etags);
12194 : 30 : if (dtags)
12195 : 24 : dtags = TREE_VALUE (dtags);
12196 : :
12197 : : /* We only error if mangling wouldn't consider the tags equivalent. */
12198 : 30 : if (!equal_abi_tags (etags, dtags))
12199 : : {
12200 : 21 : auto_diagnostic_group d;
12201 : 21 : if (dtags)
12202 : 15 : error_at (DECL_SOURCE_LOCATION (decl),
12203 : : "mismatching abi tags for %qD with tags %qE",
12204 : : decl, dtags);
12205 : : else
12206 : 6 : error_at (DECL_SOURCE_LOCATION (decl),
12207 : : "mismatching abi tags for %qD with no tags", decl);
12208 : 21 : if (etags)
12209 : 15 : inform (DECL_SOURCE_LOCATION (existing),
12210 : : "existing declaration here with tags %qE", etags);
12211 : : else
12212 : 6 : inform (DECL_SOURCE_LOCATION (existing),
12213 : : "existing declaration here with no tags");
12214 : 21 : }
12215 : :
12216 : : /* Always use the existing abi_tags as the canonical set so that
12217 : : later processing doesn't get confused. */
12218 : 30 : if (dtags)
12219 : 24 : dattr = remove_attribute ("abi_tag", dattr);
12220 : 30 : if (etags)
12221 : 21 : duplicate_one_attribute (&dattr, eattr, "abi_tag");
12222 : : }
12223 : 281359 : }
12224 : :
12225 : : /* DECL is a just streamed mergeable decl that should match EXISTING. Check
12226 : : it does and issue an appropriate diagnostic if not. Merge any
12227 : : bits from DECL to EXISTING. This is stricter matching than
12228 : : decls_match, because we can rely on ODR-sameness, and we cannot use
12229 : : decls_match because it can cause instantiations of constraints. */
12230 : :
12231 : : bool
12232 : 416204 : trees_in::is_matching_decl (tree existing, tree decl, bool is_typedef)
12233 : : {
12234 : : // FIXME: We should probably do some duplicate decl-like stuff here
12235 : : // (beware, default parms should be the same?) Can we just call
12236 : : // duplicate_decls and teach it how to handle the module-specific
12237 : : // permitted/required duplications?
12238 : :
12239 : : // We know at this point that the decls have matched by key, so we
12240 : : // can elide some of the checking
12241 : 416204 : gcc_checking_assert (TREE_CODE (existing) == TREE_CODE (decl));
12242 : :
12243 : 416204 : tree d_inner = decl;
12244 : 416204 : tree e_inner = existing;
12245 : 416204 : if (TREE_CODE (decl) == TEMPLATE_DECL)
12246 : : {
12247 : 139952 : d_inner = DECL_TEMPLATE_RESULT (d_inner);
12248 : 139952 : e_inner = DECL_TEMPLATE_RESULT (e_inner);
12249 : 139952 : gcc_checking_assert (TREE_CODE (e_inner) == TREE_CODE (d_inner));
12250 : : }
12251 : :
12252 : : // FIXME: do more precise errors at point of mismatch
12253 : 416204 : const char *mismatch_msg = nullptr;
12254 : 416204 : if (TREE_CODE (d_inner) == FUNCTION_DECL)
12255 : : {
12256 : 187861 : tree e_ret = fndecl_declared_return_type (existing);
12257 : 187861 : tree d_ret = fndecl_declared_return_type (decl);
12258 : :
12259 : 81813 : if (decl != d_inner && DECL_NAME (d_inner) == fun_identifier
12260 : 187879 : && LAMBDA_TYPE_P (DECL_CONTEXT (d_inner)))
12261 : : /* This has a recursive type that will compare different. */;
12262 : 187852 : else if (!same_type_p (d_ret, e_ret))
12263 : : {
12264 : 6 : mismatch_msg = G_("conflicting type for imported declaration %#qD");
12265 : 6 : goto mismatch;
12266 : : }
12267 : :
12268 : 187855 : tree e_type = TREE_TYPE (e_inner);
12269 : 187855 : tree d_type = TREE_TYPE (d_inner);
12270 : :
12271 : 187855 : if (DECL_EXTERN_C_P (d_inner) != DECL_EXTERN_C_P (e_inner))
12272 : : {
12273 : 0 : mismatch_msg = G_("conflicting language linkage for imported "
12274 : : "declaration %#qD");
12275 : 0 : goto mismatch;
12276 : : }
12277 : :
12278 : 187855 : for (tree e_args = TYPE_ARG_TYPES (e_type),
12279 : 187855 : d_args = TYPE_ARG_TYPES (d_type);
12280 : 321423 : e_args != d_args && (e_args || d_args);
12281 : 133568 : e_args = TREE_CHAIN (e_args), d_args = TREE_CHAIN (d_args))
12282 : : {
12283 : 133568 : if (!(e_args && d_args))
12284 : : {
12285 : 0 : mismatch_msg = G_("conflicting argument list for imported "
12286 : : "declaration %#qD");
12287 : 0 : goto mismatch;
12288 : : }
12289 : :
12290 : 133568 : if (!same_type_p (TREE_VALUE (d_args), TREE_VALUE (e_args)))
12291 : : {
12292 : 0 : mismatch_msg = G_("conflicting argument types for imported "
12293 : : "declaration %#qD");
12294 : 0 : goto mismatch;
12295 : : }
12296 : : }
12297 : :
12298 : : /* If EXISTING has an undeduced or uninstantiated exception
12299 : : specification, but DECL does not, propagate the exception
12300 : : specification. Otherwise we end up asserting or trying to
12301 : : instantiate it in the middle of loading. */
12302 : 187855 : tree e_spec = TYPE_RAISES_EXCEPTIONS (e_type);
12303 : 187855 : tree d_spec = TYPE_RAISES_EXCEPTIONS (d_type);
12304 : 275647 : if (DECL_MAYBE_DELETED (e_inner) || DEFERRED_NOEXCEPT_SPEC_P (e_spec))
12305 : : {
12306 : 15305 : if (!DEFERRED_NOEXCEPT_SPEC_P (d_spec)
12307 : 45550 : || (UNEVALUATED_NOEXCEPT_SPEC_P (e_spec)
12308 : 12347 : && !UNEVALUATED_NOEXCEPT_SPEC_P (d_spec)))
12309 : : {
12310 : 253 : dump (dumper::MERGE)
12311 : 3 : && dump ("Propagating instantiated noexcept to %N", existing);
12312 : 253 : TREE_TYPE (existing) = d_type;
12313 : :
12314 : : /* Propagate to existing clones. */
12315 : 253 : tree clone;
12316 : 517 : FOR_EACH_CLONE (clone, existing)
12317 : : {
12318 : 264 : if (TREE_TYPE (clone) == e_type)
12319 : 264 : TREE_TYPE (clone) = d_type;
12320 : : else
12321 : 0 : TREE_TYPE (clone)
12322 : 0 : = build_exception_variant (TREE_TYPE (clone), d_spec);
12323 : : }
12324 : : }
12325 : : }
12326 : 172503 : else if (!DECL_MAYBE_DELETED (d_inner)
12327 : 245769 : && !DEFERRED_NOEXCEPT_SPEC_P (d_spec)
12328 : 345005 : && !comp_except_specs (d_spec, e_spec, ce_type))
12329 : : {
12330 : 761 : mismatch_msg = G_("conflicting %<noexcept%> specifier for "
12331 : : "imported declaration %#qD");
12332 : 761 : goto mismatch;
12333 : : }
12334 : :
12335 : : /* Similarly if EXISTING has an undeduced return type, but DECL's
12336 : : is already deduced. */
12337 : 187094 : if (undeduced_auto_decl (existing) && !undeduced_auto_decl (decl))
12338 : : {
12339 : 13 : dump (dumper::MERGE)
12340 : 0 : && dump ("Propagating deduced return type to %N", existing);
12341 : 13 : gcc_checking_assert (existing == e_inner);
12342 : 13 : FNDECL_USED_AUTO (existing) = true;
12343 : 13 : DECL_SAVED_AUTO_RETURN_TYPE (existing) = TREE_TYPE (e_type);
12344 : 13 : TREE_TYPE (existing) = change_return_type (TREE_TYPE (d_type), e_type);
12345 : : }
12346 : 187081 : else if (type_uses_auto (d_ret)
12347 : 187081 : && !same_type_p (TREE_TYPE (d_type), TREE_TYPE (e_type)))
12348 : : {
12349 : 3 : mismatch_msg = G_("conflicting deduced return type for "
12350 : : "imported declaration %#qD");
12351 : 3 : goto mismatch;
12352 : : }
12353 : :
12354 : : /* Similarly if EXISTING has undeduced constexpr, but DECL's
12355 : : is already deduced. */
12356 : 187091 : if (DECL_DECLARED_CONSTEXPR_P (e_inner)
12357 : 187091 : == DECL_DECLARED_CONSTEXPR_P (d_inner))
12358 : : /* Already matches. */;
12359 : 2 : else if (DECL_DECLARED_CONSTEXPR_P (d_inner)
12360 : 2 : && (DECL_MAYBE_DELETED (e_inner)
12361 : 0 : || decl_implicit_constexpr_p (d_inner)))
12362 : : /* DECL was deduced, copy to EXISTING. */
12363 : : {
12364 : 1 : DECL_DECLARED_CONSTEXPR_P (e_inner) = true;
12365 : 1 : if (decl_implicit_constexpr_p (d_inner))
12366 : 0 : DECL_LANG_SPECIFIC (e_inner)->u.fn.implicit_constexpr = true;
12367 : : }
12368 : 1 : else if (DECL_DECLARED_CONSTEXPR_P (e_inner)
12369 : 1 : && (DECL_MAYBE_DELETED (d_inner)
12370 : 0 : || decl_implicit_constexpr_p (e_inner)))
12371 : : /* EXISTING was deduced, leave it alone. */;
12372 : : else
12373 : : {
12374 : 0 : mismatch_msg = G_("conflicting %<constexpr%> for imported "
12375 : : "declaration %#qD");
12376 : 0 : goto mismatch;
12377 : : }
12378 : :
12379 : : /* Don't synthesize a defaulted function if we're importing one
12380 : : we've already determined. */
12381 : 187091 : if (!DECL_MAYBE_DELETED (d_inner))
12382 : 186984 : DECL_MAYBE_DELETED (e_inner) = false;
12383 : : }
12384 : 228343 : else if (is_typedef)
12385 : : {
12386 : 81921 : if (!DECL_ORIGINAL_TYPE (e_inner)
12387 : 81921 : || !same_type_p (DECL_ORIGINAL_TYPE (d_inner),
12388 : : DECL_ORIGINAL_TYPE (e_inner)))
12389 : : {
12390 : 3 : mismatch_msg = G_("conflicting imported declaration %q#D");
12391 : 3 : goto mismatch;
12392 : : }
12393 : : }
12394 : : /* Using cp_tree_equal because we can meet TYPE_ARGUMENT_PACKs
12395 : : here. I suspect the entities that directly do that are things
12396 : : that shouldn't go to duplicate_decls (FIELD_DECLs etc). */
12397 : 146422 : else if (!cp_tree_equal (TREE_TYPE (decl), TREE_TYPE (existing)))
12398 : : {
12399 : : mismatch_msg = G_("conflicting type for imported declaration %#qD");
12400 : 1444 : mismatch:
12401 : 1444 : if (DECL_IS_UNDECLARED_BUILTIN (existing))
12402 : : /* Just like duplicate_decls, presum the user knows what
12403 : : they're doing in overriding a builtin. */
12404 : 764 : TREE_TYPE (existing) = TREE_TYPE (decl);
12405 : 680 : else if (decl_function_context (decl))
12406 : : /* The type of a mergeable local entity (such as a function scope
12407 : : capturing lambda's closure type fields) can depend on an
12408 : : unmergeable local entity (such as a local variable), so type
12409 : : equality isn't feasible in general for local entities. */;
12410 : : else
12411 : : {
12412 : 15 : gcc_checking_assert (mismatch_msg);
12413 : 15 : auto_diagnostic_group d;
12414 : 15 : error_at (DECL_SOURCE_LOCATION (decl), mismatch_msg, decl);
12415 : 15 : inform (DECL_SOURCE_LOCATION (existing),
12416 : : "existing declaration %#qD", existing);
12417 : 15 : return false;
12418 : 15 : }
12419 : : }
12420 : :
12421 : 416189 : if (DECL_IS_UNDECLARED_BUILTIN (existing)
12422 : 416189 : && !DECL_IS_UNDECLARED_BUILTIN (decl))
12423 : : {
12424 : : /* We're matching a builtin that the user has yet to declare.
12425 : : We are the one! This is very much duplicate-decl
12426 : : shenanigans. */
12427 : 946 : DECL_SOURCE_LOCATION (existing) = DECL_SOURCE_LOCATION (decl);
12428 : 946 : if (TREE_CODE (decl) != TYPE_DECL)
12429 : : {
12430 : : /* Propagate exceptions etc. */
12431 : 935 : TREE_TYPE (existing) = TREE_TYPE (decl);
12432 : 935 : TREE_NOTHROW (existing) = TREE_NOTHROW (decl);
12433 : : }
12434 : : /* This is actually an import! */
12435 : 946 : DECL_MODULE_IMPORT_P (existing) = true;
12436 : :
12437 : : /* Yay, sliced! */
12438 : 946 : existing->base = decl->base;
12439 : :
12440 : 946 : if (TREE_CODE (decl) == FUNCTION_DECL)
12441 : : {
12442 : : /* Ew :( */
12443 : 935 : memcpy (&existing->decl_common.size,
12444 : : &decl->decl_common.size,
12445 : : (offsetof (tree_decl_common, pt_uid)
12446 : : - offsetof (tree_decl_common, size)));
12447 : 935 : auto bltin_class = DECL_BUILT_IN_CLASS (decl);
12448 : 935 : existing->function_decl.built_in_class = bltin_class;
12449 : 935 : auto fncode = DECL_UNCHECKED_FUNCTION_CODE (decl);
12450 : 935 : DECL_UNCHECKED_FUNCTION_CODE (existing) = fncode;
12451 : 935 : if (existing->function_decl.built_in_class == BUILT_IN_NORMAL)
12452 : : {
12453 : 815 : if (builtin_decl_explicit_p (built_in_function (fncode)))
12454 : 815 : switch (fncode)
12455 : : {
12456 : 0 : case BUILT_IN_STPCPY:
12457 : 0 : set_builtin_decl_implicit_p
12458 : 0 : (built_in_function (fncode), true);
12459 : 0 : break;
12460 : 815 : default:
12461 : 815 : set_builtin_decl_declared_p
12462 : 815 : (built_in_function (fncode), true);
12463 : 815 : break;
12464 : : }
12465 : 815 : copy_attributes_to_builtin (decl);
12466 : : }
12467 : : }
12468 : : }
12469 : :
12470 : 416189 : if (VAR_OR_FUNCTION_DECL_P (decl)
12471 : 416189 : && DECL_TEMPLATE_INSTANTIATED (decl))
12472 : : /* Don't instantiate again! */
12473 : 8119 : DECL_TEMPLATE_INSTANTIATED (existing) = true;
12474 : :
12475 : 416189 : if (TREE_CODE (d_inner) == FUNCTION_DECL
12476 : 416189 : && DECL_DECLARED_INLINE_P (d_inner))
12477 : : {
12478 : 153038 : DECL_DECLARED_INLINE_P (e_inner) = true;
12479 : 153038 : if (!DECL_SAVED_TREE (e_inner)
12480 : 75361 : && lookup_attribute ("gnu_inline", DECL_ATTRIBUTES (d_inner))
12481 : 153044 : && !lookup_attribute ("gnu_inline", DECL_ATTRIBUTES (e_inner)))
12482 : : {
12483 : 18 : DECL_INTERFACE_KNOWN (e_inner)
12484 : 6 : |= DECL_INTERFACE_KNOWN (d_inner);
12485 : 6 : DECL_DISREGARD_INLINE_LIMITS (e_inner)
12486 : 6 : |= DECL_DISREGARD_INLINE_LIMITS (d_inner);
12487 : : // TODO: we will eventually want to merge all decl attributes
12488 : 6 : duplicate_one_attribute (&DECL_ATTRIBUTES (e_inner),
12489 : 6 : DECL_ATTRIBUTES (d_inner), "gnu_inline");
12490 : : }
12491 : : }
12492 : 416189 : if (!DECL_EXTERNAL (d_inner))
12493 : 196882 : DECL_EXTERNAL (e_inner) = false;
12494 : :
12495 : 416189 : if (VAR_OR_FUNCTION_DECL_P (d_inner))
12496 : 414036 : check_abi_tags (existing, decl,
12497 : 207018 : DECL_ATTRIBUTES (e_inner), DECL_ATTRIBUTES (d_inner));
12498 : :
12499 : 416189 : if (TREE_CODE (decl) == TEMPLATE_DECL)
12500 : : {
12501 : : /* Merge default template arguments. */
12502 : 139950 : tree d_parms = DECL_INNERMOST_TEMPLATE_PARMS (decl);
12503 : 139950 : tree e_parms = DECL_INNERMOST_TEMPLATE_PARMS (existing);
12504 : 139950 : gcc_checking_assert (TREE_VEC_LENGTH (d_parms)
12505 : : == TREE_VEC_LENGTH (e_parms));
12506 : 402827 : for (int i = 0; i < TREE_VEC_LENGTH (d_parms); ++i)
12507 : : {
12508 : 262886 : tree d_default = TREE_PURPOSE (TREE_VEC_ELT (d_parms, i));
12509 : 262886 : tree& e_default = TREE_PURPOSE (TREE_VEC_ELT (e_parms, i));
12510 : 262886 : if (e_default == NULL_TREE)
12511 : 222909 : e_default = d_default;
12512 : 39977 : else if (d_default != NULL_TREE
12513 : 39977 : && !cp_tree_equal (d_default, e_default))
12514 : : {
12515 : 9 : auto_diagnostic_group d;
12516 : 9 : tree d_parm = TREE_VALUE (TREE_VEC_ELT (d_parms, i));
12517 : 9 : tree e_parm = TREE_VALUE (TREE_VEC_ELT (e_parms, i));
12518 : 9 : error_at (DECL_SOURCE_LOCATION (d_parm),
12519 : : "conflicting default argument for %#qD", d_parm);
12520 : 9 : inform (DECL_SOURCE_LOCATION (e_parm),
12521 : : "existing default declared here");
12522 : 9 : return false;
12523 : 9 : }
12524 : : }
12525 : : }
12526 : :
12527 : 416180 : if (TREE_CODE (d_inner) == FUNCTION_DECL)
12528 : : {
12529 : : /* Merge default function arguments. */
12530 : 187852 : tree d_parm = FUNCTION_FIRST_USER_PARMTYPE (d_inner);
12531 : 187852 : tree e_parm = FUNCTION_FIRST_USER_PARMTYPE (e_inner);
12532 : 187852 : int i = 0;
12533 : 446390 : for (; d_parm && d_parm != void_list_node;
12534 : 258538 : d_parm = TREE_CHAIN (d_parm), e_parm = TREE_CHAIN (e_parm), ++i)
12535 : : {
12536 : 258550 : tree d_default = TREE_PURPOSE (d_parm);
12537 : 258550 : tree& e_default = TREE_PURPOSE (e_parm);
12538 : 258550 : if (e_default == NULL_TREE)
12539 : 243665 : e_default = d_default;
12540 : 14885 : else if (d_default != NULL_TREE
12541 : 14885 : && !cp_tree_equal (d_default, e_default))
12542 : : {
12543 : 12 : auto_diagnostic_group d;
12544 : 12 : error_at (get_fndecl_argument_location (d_inner, i),
12545 : : "conflicting default argument for parameter %P of %#qD",
12546 : : i, decl);
12547 : 12 : inform (get_fndecl_argument_location (e_inner, i),
12548 : : "existing default declared here");
12549 : 12 : return false;
12550 : 12 : }
12551 : : }
12552 : : }
12553 : :
12554 : : return true;
12555 : : }
12556 : :
12557 : : /* FN is an implicit member function that we've discovered is new to
12558 : : the class. Add it to the TYPE_FIELDS chain and the method vector.
12559 : : Reset the appropriate classtype lazy flag. */
12560 : :
12561 : : bool
12562 : 790 : trees_in::install_implicit_member (tree fn)
12563 : : {
12564 : 790 : tree ctx = DECL_CONTEXT (fn);
12565 : 790 : tree name = DECL_NAME (fn);
12566 : : /* We know these are synthesized, so the set of expected prototypes
12567 : : is quite restricted. We're not validating correctness, just
12568 : : distinguishing beteeen the small set of possibilities. */
12569 : 790 : tree parm_type = TREE_VALUE (FUNCTION_FIRST_USER_PARMTYPE (fn));
12570 : 790 : if (IDENTIFIER_CTOR_P (name))
12571 : : {
12572 : 526 : if (CLASSTYPE_LAZY_DEFAULT_CTOR (ctx)
12573 : 526 : && VOID_TYPE_P (parm_type))
12574 : 124 : CLASSTYPE_LAZY_DEFAULT_CTOR (ctx) = false;
12575 : 402 : else if (!TYPE_REF_P (parm_type))
12576 : : return false;
12577 : 402 : else if (CLASSTYPE_LAZY_COPY_CTOR (ctx)
12578 : 402 : && !TYPE_REF_IS_RVALUE (parm_type))
12579 : 204 : CLASSTYPE_LAZY_COPY_CTOR (ctx) = false;
12580 : 198 : else if (CLASSTYPE_LAZY_MOVE_CTOR (ctx))
12581 : 198 : CLASSTYPE_LAZY_MOVE_CTOR (ctx) = false;
12582 : : else
12583 : : return false;
12584 : : }
12585 : 264 : else if (IDENTIFIER_DTOR_P (name))
12586 : : {
12587 : 200 : if (CLASSTYPE_LAZY_DESTRUCTOR (ctx))
12588 : 200 : CLASSTYPE_LAZY_DESTRUCTOR (ctx) = false;
12589 : : else
12590 : : return false;
12591 : 200 : if (DECL_VIRTUAL_P (fn))
12592 : : /* A virtual dtor should have been created when the class
12593 : : became complete. */
12594 : : return false;
12595 : : }
12596 : 64 : else if (name == assign_op_identifier)
12597 : : {
12598 : 64 : if (!TYPE_REF_P (parm_type))
12599 : : return false;
12600 : 64 : else if (CLASSTYPE_LAZY_COPY_ASSIGN (ctx)
12601 : 64 : && !TYPE_REF_IS_RVALUE (parm_type))
12602 : 32 : CLASSTYPE_LAZY_COPY_ASSIGN (ctx) = false;
12603 : 32 : else if (CLASSTYPE_LAZY_MOVE_ASSIGN (ctx))
12604 : 32 : CLASSTYPE_LAZY_MOVE_ASSIGN (ctx) = false;
12605 : : else
12606 : : return false;
12607 : : }
12608 : : else
12609 : : return false;
12610 : :
12611 : 820 : dump (dumper::MERGE) && dump ("Adding implicit member %N", fn);
12612 : :
12613 : 790 : DECL_CHAIN (fn) = TYPE_FIELDS (ctx);
12614 : 790 : TYPE_FIELDS (ctx) = fn;
12615 : :
12616 : 790 : add_method (ctx, fn, false);
12617 : :
12618 : : /* Propagate TYPE_FIELDS. */
12619 : 790 : fixup_type_variants (ctx);
12620 : :
12621 : 790 : return true;
12622 : : }
12623 : :
12624 : : /* Return non-zero if DECL has a definition that would be interesting to
12625 : : write out. */
12626 : :
12627 : : static bool
12628 : 1241975 : has_definition (tree decl)
12629 : : {
12630 : 1241975 : bool is_tmpl = TREE_CODE (decl) == TEMPLATE_DECL;
12631 : 1241975 : if (is_tmpl)
12632 : 290745 : decl = DECL_TEMPLATE_RESULT (decl);
12633 : :
12634 : 1241975 : switch (TREE_CODE (decl))
12635 : : {
12636 : : default:
12637 : : break;
12638 : :
12639 : 437757 : case FUNCTION_DECL:
12640 : 437757 : if (!DECL_SAVED_TREE (decl))
12641 : : /* Not defined. */
12642 : : break;
12643 : :
12644 : 192606 : if (DECL_DECLARED_INLINE_P (decl))
12645 : : return true;
12646 : :
12647 : 11110 : if (header_module_p ())
12648 : : /* We always need to write definitions in header modules,
12649 : : since there's no TU to emit them in otherwise. */
12650 : : return true;
12651 : :
12652 : 3210 : if (DECL_TEMPLATE_INFO (decl))
12653 : : {
12654 : 2603 : int use_tpl = DECL_USE_TEMPLATE (decl);
12655 : :
12656 : : // FIXME: Partial specializations have definitions too.
12657 : 2603 : if (use_tpl < 2)
12658 : : return true;
12659 : : }
12660 : : break;
12661 : :
12662 : 521028 : case TYPE_DECL:
12663 : 521028 : {
12664 : 521028 : tree type = TREE_TYPE (decl);
12665 : 521028 : if (type == TYPE_MAIN_VARIANT (type)
12666 : 244711 : && decl == TYPE_NAME (type)
12667 : 765739 : && (TREE_CODE (type) == ENUMERAL_TYPE
12668 : 244711 : ? TYPE_VALUES (type) : TYPE_FIELDS (type)))
12669 : : return true;
12670 : : }
12671 : : break;
12672 : :
12673 : 68303 : case VAR_DECL:
12674 : : /* DECL_INITIALIZED_P might not be set on a dependent VAR_DECL. */
12675 : 68303 : if (DECL_LANG_SPECIFIC (decl)
12676 : 67634 : && DECL_TEMPLATE_INFO (decl)
12677 : 102820 : && DECL_INITIAL (decl))
12678 : : return true;
12679 : : else
12680 : : {
12681 : 37141 : if (!DECL_INITIALIZED_P (decl))
12682 : : /* Not defined. */
12683 : : return false;
12684 : :
12685 : 32013 : if (header_module_p ())
12686 : : /* We always need to write definitions in header modules,
12687 : : since there's no TU to emit them in otherwise. */
12688 : : return true;
12689 : :
12690 : 9982 : if (decl_maybe_constant_var_p (decl))
12691 : : /* We might need its constant value. */
12692 : : return true;
12693 : :
12694 : 336 : if (vague_linkage_p (decl))
12695 : : /* These are emitted as needed. */
12696 : : return true;
12697 : :
12698 : : return false;
12699 : : }
12700 : 5729 : break;
12701 : :
12702 : 5729 : case CONCEPT_DECL:
12703 : 5729 : if (DECL_INITIAL (decl))
12704 : : return true;
12705 : :
12706 : : break;
12707 : : }
12708 : :
12709 : : return false;
12710 : : }
12711 : :
12712 : : uintptr_t *
12713 : 485843 : trees_in::find_duplicate (tree existing)
12714 : : {
12715 : 242471 : if (!duplicates)
12716 : : return NULL;
12717 : :
12718 : 344774 : return duplicates->get (existing);
12719 : : }
12720 : :
12721 : : /* We're starting to read a duplicate DECL. EXISTING is the already
12722 : : known node. */
12723 : :
12724 : : void
12725 : 596891 : trees_in::register_duplicate (tree decl, tree existing)
12726 : : {
12727 : 596891 : if (!duplicates)
12728 : 94899 : duplicates = new duplicate_hash_map (40);
12729 : :
12730 : 596891 : bool existed;
12731 : 596891 : uintptr_t &slot = duplicates->get_or_insert (existing, &existed);
12732 : 596891 : gcc_checking_assert (!existed);
12733 : 596891 : slot = reinterpret_cast<uintptr_t> (decl);
12734 : :
12735 : 596891 : if (TREE_CODE (decl) == TEMPLATE_DECL)
12736 : : /* Also register the DECL_TEMPLATE_RESULT as a duplicate so
12737 : : that passing decl's _RESULT to maybe_duplicate naturally
12738 : : gives us existing's _RESULT back. */
12739 : 279904 : register_duplicate (DECL_TEMPLATE_RESULT (decl),
12740 : 139952 : DECL_TEMPLATE_RESULT (existing));
12741 : 596891 : }
12742 : :
12743 : : /* We've read a definition of MAYBE_EXISTING. If not a duplicate,
12744 : : return MAYBE_EXISTING (into which the definition should be
12745 : : installed). Otherwise return NULL if already known bad, or the
12746 : : duplicate we read (for ODR checking, or extracting additional merge
12747 : : information). */
12748 : :
12749 : : tree
12750 : 243372 : trees_in::odr_duplicate (tree maybe_existing, bool has_defn)
12751 : : {
12752 : 243372 : tree res = NULL_TREE;
12753 : :
12754 : 390809 : if (uintptr_t *dup = find_duplicate (maybe_existing))
12755 : : {
12756 : 140211 : if (!(*dup & 1))
12757 : 140208 : res = reinterpret_cast<tree> (*dup);
12758 : : }
12759 : : else
12760 : : res = maybe_existing;
12761 : :
12762 : 243372 : assert_definition (maybe_existing, res && !has_defn);
12763 : :
12764 : : // FIXME: We probably need to return the template, so that the
12765 : : // template header can be checked?
12766 : 243372 : return res ? STRIP_TEMPLATE (res) : NULL_TREE;
12767 : : }
12768 : :
12769 : : /* The following writer functions rely on the current behaviour of
12770 : : depset::hash::add_dependency making the decl and defn depset nodes
12771 : : depend on eachother. That way we don't have to worry about seeding
12772 : : the tree map with named decls that cannot be looked up by name (I.e
12773 : : template and function parms). We know the decl and definition will
12774 : : be in the same cluster, which is what we want. */
12775 : :
12776 : : void
12777 : 361327 : trees_out::write_function_def (tree decl)
12778 : : {
12779 : 361327 : tree_node (DECL_RESULT (decl));
12780 : :
12781 : 361327 : {
12782 : : /* The function body for a non-inline function or function template
12783 : : is ignored for determining exposures. This should only matter
12784 : : for templates (we don't emit the bodies of non-inline functions
12785 : : to begin with). */
12786 : 361327 : auto ovr = make_temp_override (dep_hash->ignore_tu_local,
12787 : 361327 : !DECL_DECLARED_INLINE_P (decl));
12788 : 361327 : tree_node (DECL_INITIAL (decl));
12789 : 361327 : tree_node (DECL_SAVED_TREE (decl));
12790 : 361327 : }
12791 : :
12792 : 769582 : tree_node (DECL_FRIEND_CONTEXT (decl));
12793 : :
12794 : 361327 : constexpr_fundef *cexpr = retrieve_constexpr_fundef (decl);
12795 : :
12796 : 361327 : if (streaming_p ())
12797 : 180632 : u (cexpr != nullptr);
12798 : 361327 : if (cexpr)
12799 : : {
12800 : 88618 : chained_decls (cexpr->parms);
12801 : 88618 : tree_node (cexpr->result);
12802 : 88618 : tree_node (cexpr->body);
12803 : : }
12804 : :
12805 : 361327 : function* f = DECL_STRUCT_FUNCTION (decl);
12806 : :
12807 : 361327 : if (streaming_p ())
12808 : : {
12809 : 180632 : unsigned flags = 0;
12810 : :
12811 : : /* Whether the importer should emit this definition, if used. */
12812 : 180632 : flags |= 1 * (DECL_NOT_REALLY_EXTERN (decl)
12813 : 180632 : && (get_importer_interface (decl)
12814 : : != importer_interface::external));
12815 : :
12816 : 180632 : if (f)
12817 : : {
12818 : 180006 : flags |= 2;
12819 : : /* These flags are needed in tsubst_lambda_expr. */
12820 : 180006 : flags |= 4 * f->language->returns_value;
12821 : 180006 : flags |= 8 * f->language->returns_null;
12822 : 180006 : flags |= 16 * f->language->returns_abnormally;
12823 : 180006 : flags |= 32 * f->language->infinite_loop;
12824 : : }
12825 : :
12826 : 180632 : u (flags);
12827 : : }
12828 : :
12829 : 361327 : if (state && f)
12830 : : {
12831 : 360075 : state->write_location (*this, f->function_start_locus);
12832 : 360075 : state->write_location (*this, f->function_end_locus);
12833 : : }
12834 : 361327 : }
12835 : :
12836 : : void
12837 : 0 : trees_out::mark_function_def (tree)
12838 : : {
12839 : 0 : }
12840 : :
12841 : : bool
12842 : 153882 : trees_in::read_function_def (tree decl, tree maybe_template)
12843 : : {
12844 : 154318 : dump () && dump ("Reading function definition %N", decl);
12845 : 153882 : tree result = tree_node ();
12846 : 153882 : tree initial = tree_node ();
12847 : 153882 : tree saved = tree_node ();
12848 : 153882 : tree context = tree_node ();
12849 : 153882 : constexpr_fundef cexpr;
12850 : 153882 : post_process_data pdata {};
12851 : 153882 : pdata.decl = maybe_template;
12852 : :
12853 : 153882 : tree maybe_dup = odr_duplicate (maybe_template, DECL_SAVED_TREE (decl));
12854 : 307761 : bool installing = maybe_dup && !DECL_SAVED_TREE (decl);
12855 : :
12856 : 153882 : if (u ())
12857 : : {
12858 : 35079 : cexpr.parms = chained_decls ();
12859 : 35079 : cexpr.result = tree_node ();
12860 : 35079 : cexpr.body = tree_node ();
12861 : 35079 : cexpr.decl = decl;
12862 : : }
12863 : : else
12864 : 118803 : cexpr.decl = NULL_TREE;
12865 : :
12866 : 153882 : unsigned flags = u ();
12867 : :
12868 : 153882 : if (flags & 2)
12869 : : {
12870 : 153323 : pdata.start_locus = state->read_location (*this);
12871 : 153323 : pdata.end_locus = state->read_location (*this);
12872 : 153323 : pdata.returns_value = flags & 4;
12873 : 153323 : pdata.returns_null = flags & 8;
12874 : 153323 : pdata.returns_abnormally = flags & 16;
12875 : 153323 : pdata.infinite_loop = flags & 32;
12876 : : }
12877 : :
12878 : 153882 : if (get_overrun ())
12879 : : return NULL_TREE;
12880 : :
12881 : 153882 : if (installing)
12882 : : {
12883 : 71530 : DECL_NOT_REALLY_EXTERN (decl) = flags & 1;
12884 : 71530 : DECL_RESULT (decl) = result;
12885 : 71530 : DECL_INITIAL (decl) = initial;
12886 : 71530 : DECL_SAVED_TREE (decl) = saved;
12887 : :
12888 : 71530 : if (context)
12889 : 2994 : SET_DECL_FRIEND_CONTEXT (decl, context);
12890 : 71530 : if (cexpr.decl)
12891 : 21924 : register_constexpr_fundef (cexpr);
12892 : 71530 : post_process (pdata);
12893 : : }
12894 : : else if (maybe_dup)
12895 : : {
12896 : : // FIXME:QOI Check matching defn
12897 : : }
12898 : :
12899 : : return true;
12900 : : }
12901 : :
12902 : : /* Also for CONCEPT_DECLs. */
12903 : :
12904 : : void
12905 : 106618 : trees_out::write_var_def (tree decl)
12906 : : {
12907 : : /* The initializer of a non-inline variable or variable template is
12908 : : ignored for determining exposures. */
12909 : 106618 : auto ovr = make_temp_override (dep_hash->ignore_tu_local,
12910 : 106618 : VAR_P (decl) && !DECL_INLINE_VAR_P (decl));
12911 : :
12912 : 106618 : tree init = DECL_INITIAL (decl);
12913 : 106618 : tree_node (init);
12914 : 106618 : if (!init)
12915 : : {
12916 : 1210 : tree dyn_init = NULL_TREE;
12917 : :
12918 : : /* We only need to write initializers in header modules. */
12919 : 2248 : if (header_module_p () && DECL_NONTRIVIALLY_INITIALIZED_P (decl))
12920 : : {
12921 : 450 : dyn_init = value_member (decl,
12922 : 450 : CP_DECL_THREAD_LOCAL_P (decl)
12923 : : ? tls_aggregates : static_aggregates);
12924 : 450 : gcc_checking_assert (dyn_init);
12925 : : /* Mark it so write_inits knows this is needed. */
12926 : 450 : TREE_LANG_FLAG_0 (dyn_init) = true;
12927 : 450 : dyn_init = TREE_PURPOSE (dyn_init);
12928 : : }
12929 : 1210 : tree_node (dyn_init);
12930 : : }
12931 : 106618 : }
12932 : :
12933 : : void
12934 : 0 : trees_out::mark_var_def (tree)
12935 : : {
12936 : 0 : }
12937 : :
12938 : : bool
12939 : 34941 : trees_in::read_var_def (tree decl, tree maybe_template)
12940 : : {
12941 : : /* Do not mark the virtual table entries as used. */
12942 : 34941 : bool vtable = VAR_P (decl) && DECL_VTABLE_OR_VTT_P (decl);
12943 : 34941 : unused += vtable;
12944 : 34941 : tree init = tree_node ();
12945 : 34941 : tree dyn_init = init ? NULL_TREE : tree_node ();
12946 : 34941 : unused -= vtable;
12947 : :
12948 : 34941 : if (get_overrun ())
12949 : : return false;
12950 : :
12951 : 34941 : bool initialized = (VAR_P (decl) ? bool (DECL_INITIALIZED_P (decl))
12952 : 34941 : : bool (DECL_INITIAL (decl)));
12953 : 34941 : tree maybe_dup = odr_duplicate (maybe_template, initialized);
12954 : 34941 : bool installing = maybe_dup && !initialized;
12955 : 34941 : if (installing)
12956 : : {
12957 : 19115 : DECL_INITIAL (decl) = init;
12958 : 19115 : if (DECL_EXTERNAL (decl))
12959 : 2363 : DECL_NOT_REALLY_EXTERN (decl) = true;
12960 : 19115 : if (VAR_P (decl))
12961 : : {
12962 : 17025 : DECL_INITIALIZED_P (decl) = true;
12963 : 17025 : if (maybe_dup && DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (maybe_dup))
12964 : 16683 : DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl) = true;
12965 : 17025 : tentative_decl_linkage (decl);
12966 : 17025 : if (DECL_EXPLICIT_INSTANTIATION (decl)
12967 : 17025 : && !DECL_EXTERNAL (decl))
12968 : 9 : setup_explicit_instantiation_definition_linkage (decl);
12969 : 17025 : if (DECL_IMPLICIT_INSTANTIATION (decl)
12970 : 16262 : || (DECL_EXPLICIT_INSTANTIATION (decl)
12971 : 18 : && !DECL_EXTERNAL (decl))
12972 : 33278 : || (DECL_CLASS_SCOPE_P (decl)
12973 : 10083 : && !DECL_VTABLE_OR_VTT_P (decl)
12974 : 8843 : && !DECL_TEMPLATE_INFO (decl)))
12975 : 6754 : note_vague_linkage_variable (decl);
12976 : : }
12977 : 19115 : if (!dyn_init)
12978 : : ;
12979 : 216 : else if (CP_DECL_THREAD_LOCAL_P (decl))
12980 : 96 : tls_aggregates = tree_cons (dyn_init, decl, tls_aggregates);
12981 : : else
12982 : 120 : static_aggregates = tree_cons (dyn_init, decl, static_aggregates);
12983 : : }
12984 : : else if (maybe_dup)
12985 : : {
12986 : : // FIXME:QOI Check matching defn
12987 : : }
12988 : :
12989 : : return true;
12990 : : }
12991 : :
12992 : : /* If MEMBER doesn't have an independent life outside the class,
12993 : : return it (or its TEMPLATE_DECL). Otherwise NULL. */
12994 : :
12995 : : static tree
12996 : 186312 : member_owned_by_class (tree member)
12997 : : {
12998 : 186312 : gcc_assert (DECL_P (member));
12999 : :
13000 : : /* Clones are owned by their origin. */
13001 : 186312 : if (DECL_CLONED_FUNCTION_P (member))
13002 : : return NULL;
13003 : :
13004 : 186312 : if (TREE_CODE (member) == FIELD_DECL)
13005 : : /* FIELD_DECLS can have template info in some cases. We always
13006 : : want the FIELD_DECL though, as there's never a TEMPLATE_DECL
13007 : : wrapping them. */
13008 : : return member;
13009 : :
13010 : 88033 : int use_tpl = -1;
13011 : 88033 : if (tree ti = node_template_info (member, use_tpl))
13012 : : {
13013 : : // FIXME: Don't bail on things that CANNOT have their own
13014 : : // template header. No, make sure they're in the same cluster.
13015 : 0 : if (use_tpl > 0)
13016 : : return NULL_TREE;
13017 : :
13018 : 0 : if (DECL_TEMPLATE_RESULT (TI_TEMPLATE (ti)) == member)
13019 : 186312 : member = TI_TEMPLATE (ti);
13020 : : }
13021 : : return member;
13022 : : }
13023 : :
13024 : : void
13025 : 144240 : trees_out::write_class_def (tree defn)
13026 : : {
13027 : 144240 : gcc_assert (DECL_P (defn));
13028 : 144240 : if (streaming_p ())
13029 : 72372 : dump () && dump ("Writing class definition %N", defn);
13030 : :
13031 : 144240 : tree type = TREE_TYPE (defn);
13032 : 144240 : tree_node (TYPE_SIZE (type));
13033 : 144240 : tree_node (TYPE_SIZE_UNIT (type));
13034 : 144240 : tree_node (TYPE_VFIELD (type));
13035 : 144240 : tree_node (TYPE_BINFO (type));
13036 : :
13037 : 144240 : vec_chained_decls (TYPE_FIELDS (type));
13038 : :
13039 : : /* Every class but __as_base has a type-specific. */
13040 : 286288 : gcc_checking_assert (!TYPE_LANG_SPECIFIC (type) == IS_FAKE_BASE_TYPE (type));
13041 : :
13042 : 144240 : if (TYPE_LANG_SPECIFIC (type))
13043 : : {
13044 : 142048 : {
13045 : 142048 : vec<tree, va_gc> *v = CLASSTYPE_MEMBER_VEC (type);
13046 : 142048 : if (!v)
13047 : : {
13048 : 38949 : gcc_checking_assert (!streaming_p ());
13049 : : /* Force a class vector. */
13050 : 38949 : v = set_class_bindings (type, -1);
13051 : 38949 : gcc_checking_assert (v);
13052 : : }
13053 : :
13054 : 142048 : unsigned len = v->length ();
13055 : 142048 : if (streaming_p ())
13056 : 71004 : u (len);
13057 : 1105980 : for (unsigned ix = 0; ix != len; ix++)
13058 : : {
13059 : 963932 : tree m = (*v)[ix];
13060 : 963932 : if (TREE_CODE (m) == TYPE_DECL
13061 : 295927 : && DECL_ARTIFICIAL (m)
13062 : 1116709 : && TYPE_STUB_DECL (TREE_TYPE (m)) == m)
13063 : : /* This is a using-decl for a type, or an anonymous
13064 : : struct (maybe with a typedef name). Write the type. */
13065 : 9972 : m = TREE_TYPE (m);
13066 : 963932 : tree_node (m);
13067 : : }
13068 : : }
13069 : 142048 : tree_node (CLASSTYPE_LAMBDA_EXPR (type));
13070 : :
13071 : : /* TYPE_CONTAINS_VPTR_P looks at the vbase vector, which the
13072 : : reader won't know at this point. */
13073 : 142048 : int has_vptr = TYPE_CONTAINS_VPTR_P (type);
13074 : :
13075 : 142048 : if (streaming_p ())
13076 : : {
13077 : 71004 : unsigned nvbases = vec_safe_length (CLASSTYPE_VBASECLASSES (type));
13078 : 71004 : u (nvbases);
13079 : 71004 : i (has_vptr);
13080 : : }
13081 : :
13082 : 142048 : if (has_vptr)
13083 : : {
13084 : 4820 : tree_vec (CLASSTYPE_PURE_VIRTUALS (type));
13085 : 4820 : tree_pair_vec (CLASSTYPE_VCALL_INDICES (type));
13086 : 4820 : tree_node (CLASSTYPE_KEY_METHOD (type));
13087 : : }
13088 : : }
13089 : :
13090 : 144240 : if (TYPE_LANG_SPECIFIC (type))
13091 : : {
13092 : 142048 : tree_node (CLASSTYPE_PRIMARY_BINFO (type));
13093 : :
13094 : 142048 : tree as_base = CLASSTYPE_AS_BASE (type);
13095 : 142048 : if (as_base)
13096 : 71995 : as_base = TYPE_NAME (as_base);
13097 : 142048 : tree_node (as_base);
13098 : :
13099 : : /* Write the vtables. */
13100 : 142048 : tree vtables = CLASSTYPE_VTABLES (type);
13101 : 142048 : vec_chained_decls (vtables);
13102 : 289732 : for (; vtables; vtables = TREE_CHAIN (vtables))
13103 : 5636 : write_definition (vtables);
13104 : :
13105 : 142048 : {
13106 : : /* Friend declarations in class definitions are ignored when
13107 : : determining exposures. */
13108 : 142048 : auto ovr = make_temp_override (dep_hash->ignore_tu_local, true);
13109 : :
13110 : : /* Write the friend classes. */
13111 : 142048 : tree_list (CLASSTYPE_FRIEND_CLASSES (type), false);
13112 : :
13113 : : /* Write the friend functions. */
13114 : 142048 : for (tree friends = DECL_FRIENDLIST (defn);
13115 : 159226 : friends; friends = TREE_CHAIN (friends))
13116 : : {
13117 : 17178 : tree_node (FRIEND_NAME (friends));
13118 : 17178 : tree_list (FRIEND_DECLS (friends), false);
13119 : : }
13120 : : /* End of friend fns. */
13121 : 142048 : tree_node (NULL_TREE);
13122 : 142048 : }
13123 : :
13124 : : /* Write the decl list. We don't need to ignore exposures of friend
13125 : : decls here as any such decls should already have been added and
13126 : : ignored above. */
13127 : 142048 : tree_list (CLASSTYPE_DECL_LIST (type), true);
13128 : :
13129 : 142048 : if (TYPE_CONTAINS_VPTR_P (type))
13130 : : {
13131 : : /* Write the thunks. */
13132 : 4820 : for (tree decls = TYPE_FIELDS (type);
13133 : 136674 : decls; decls = DECL_CHAIN (decls))
13134 : 131854 : if (TREE_CODE (decls) == FUNCTION_DECL
13135 : 95480 : && DECL_VIRTUAL_P (decls)
13136 : 156608 : && DECL_THUNKS (decls))
13137 : : {
13138 : 852 : tree_node (decls);
13139 : : /* Thunks are always unique, so chaining is ok. */
13140 : 852 : chained_decls (DECL_THUNKS (decls));
13141 : : }
13142 : 4820 : tree_node (NULL_TREE);
13143 : : }
13144 : : }
13145 : 144240 : }
13146 : :
13147 : : void
13148 : 186312 : trees_out::mark_class_member (tree member, bool do_defn)
13149 : : {
13150 : 186312 : gcc_assert (DECL_P (member));
13151 : :
13152 : 186312 : member = member_owned_by_class (member);
13153 : 186312 : if (member)
13154 : 372624 : mark_declaration (member, do_defn && has_definition (member));
13155 : 186312 : }
13156 : :
13157 : : void
13158 : 144268 : trees_out::mark_class_def (tree defn)
13159 : : {
13160 : 144268 : gcc_assert (DECL_P (defn));
13161 : 144268 : tree type = TREE_TYPE (defn);
13162 : : /* Mark the class members that are not type-decls and cannot have
13163 : : independent definitions. */
13164 : 1487616 : for (tree member = TYPE_FIELDS (type); member; member = DECL_CHAIN (member))
13165 : 1343348 : if (TREE_CODE (member) == FIELD_DECL
13166 : 1343348 : || TREE_CODE (member) == USING_DECL
13167 : : /* A cloned enum-decl from 'using enum unrelated;' */
13168 : 1343348 : || (TREE_CODE (member) == CONST_DECL
13169 : 13504 : && DECL_CONTEXT (member) == type))
13170 : : {
13171 : 186312 : mark_class_member (member);
13172 : 186312 : if (TREE_CODE (member) == FIELD_DECL)
13173 : 98279 : if (tree repr = DECL_BIT_FIELD_REPRESENTATIVE (member))
13174 : : /* If we're marking a class template definition, then
13175 : : this'll contain the width (as set by grokbitfield)
13176 : : instead of a decl. */
13177 : 1984 : if (DECL_P (repr))
13178 : 1652 : mark_declaration (repr, false);
13179 : : }
13180 : :
13181 : : /* Mark the binfo hierarchy. */
13182 : 343716 : for (tree child = TYPE_BINFO (type); child; child = TREE_CHAIN (child))
13183 : 199448 : mark_by_value (child);
13184 : :
13185 : 144268 : if (TYPE_LANG_SPECIFIC (type))
13186 : : {
13187 : 142052 : for (tree vtable = CLASSTYPE_VTABLES (type);
13188 : 147688 : vtable; vtable = TREE_CHAIN (vtable))
13189 : 5636 : mark_declaration (vtable, true);
13190 : :
13191 : 142052 : if (TYPE_CONTAINS_VPTR_P (type))
13192 : : /* Mark the thunks, they belong to the class definition,
13193 : : /not/ the thunked-to function. */
13194 : 4820 : for (tree decls = TYPE_FIELDS (type);
13195 : 136674 : decls; decls = DECL_CHAIN (decls))
13196 : 131854 : if (TREE_CODE (decls) == FUNCTION_DECL)
13197 : 95480 : for (tree thunks = DECL_THUNKS (decls);
13198 : 96608 : thunks; thunks = DECL_CHAIN (thunks))
13199 : 1128 : mark_declaration (thunks, false);
13200 : : }
13201 : 144268 : }
13202 : :
13203 : : /* Nop sorting, needed for resorting the member vec. */
13204 : :
13205 : : static void
13206 : 5750050 : nop (void *, void *, void *)
13207 : : {
13208 : 5750050 : }
13209 : :
13210 : : bool
13211 : 52107 : trees_in::read_class_def (tree defn, tree maybe_template)
13212 : : {
13213 : 52107 : gcc_assert (DECL_P (defn));
13214 : 52611 : dump () && dump ("Reading class definition %N", defn);
13215 : 52107 : tree type = TREE_TYPE (defn);
13216 : 52107 : tree size = tree_node ();
13217 : 52107 : tree size_unit = tree_node ();
13218 : 52107 : tree vfield = tree_node ();
13219 : 52107 : tree binfo = tree_node ();
13220 : 52107 : vec<tree, va_gc> *vbase_vec = NULL;
13221 : 52107 : vec<tree, va_gc> *member_vec = NULL;
13222 : 52107 : vec<tree, va_gc> *pure_virts = NULL;
13223 : 52107 : vec<tree_pair_s, va_gc> *vcall_indices = NULL;
13224 : 52107 : tree key_method = NULL_TREE;
13225 : 52107 : tree lambda = NULL_TREE;
13226 : :
13227 : : /* Read the fields. */
13228 : 52107 : vec<tree, va_heap> *fields = vec_chained_decls ();
13229 : :
13230 : 52107 : if (TYPE_LANG_SPECIFIC (type))
13231 : : {
13232 : 51081 : if (unsigned len = u ())
13233 : : {
13234 : 51081 : vec_alloc (member_vec, len);
13235 : 432618 : for (unsigned ix = 0; ix != len; ix++)
13236 : : {
13237 : 381537 : tree m = tree_node ();
13238 : 381537 : if (get_overrun ())
13239 : : break;
13240 : 381537 : if (TYPE_P (m))
13241 : 3662 : m = TYPE_STUB_DECL (m);
13242 : 381537 : member_vec->quick_push (m);
13243 : : }
13244 : : }
13245 : 51081 : lambda = tree_node ();
13246 : :
13247 : 51081 : if (!get_overrun ())
13248 : : {
13249 : 51081 : unsigned nvbases = u ();
13250 : 51081 : if (nvbases)
13251 : : {
13252 : 246 : vec_alloc (vbase_vec, nvbases);
13253 : 1117 : for (tree child = binfo; child; child = TREE_CHAIN (child))
13254 : 871 : if (BINFO_VIRTUAL_P (child))
13255 : 246 : vbase_vec->quick_push (child);
13256 : : }
13257 : : }
13258 : :
13259 : 51081 : if (!get_overrun ())
13260 : : {
13261 : 51081 : int has_vptr = i ();
13262 : 51081 : if (has_vptr)
13263 : : {
13264 : 2078 : pure_virts = tree_vec ();
13265 : 2078 : vcall_indices = tree_pair_vec ();
13266 : 2078 : key_method = tree_node ();
13267 : : }
13268 : : }
13269 : : }
13270 : :
13271 : 52107 : tree maybe_dup = odr_duplicate (maybe_template, TYPE_SIZE (type));
13272 : 52107 : bool installing = maybe_dup && !TYPE_SIZE (type);
13273 : 22947 : if (installing)
13274 : : {
13275 : 22947 : if (maybe_dup != defn)
13276 : : {
13277 : : // FIXME: This is needed on other defns too, almost
13278 : : // duplicate-decl like? See is_matching_decl too.
13279 : : /* Copy flags from the duplicate. */
13280 : 234 : tree type_dup = TREE_TYPE (maybe_dup);
13281 : :
13282 : : /* Core pieces. */
13283 : 234 : TYPE_MODE_RAW (type) = TYPE_MODE_RAW (type_dup);
13284 : 234 : TYPE_ALIGN_RAW (type) = TYPE_ALIGN_RAW (type_dup);
13285 : 468 : TYPE_WARN_IF_NOT_ALIGN_RAW (type)
13286 : 234 : = TYPE_WARN_IF_NOT_ALIGN_RAW (type_dup);
13287 : 234 : TYPE_USER_ALIGN (type) = TYPE_USER_ALIGN (type_dup);
13288 : :
13289 : 234 : SET_DECL_MODE (defn, DECL_MODE (maybe_dup));
13290 : 234 : DECL_SIZE (defn) = DECL_SIZE (maybe_dup);
13291 : 234 : DECL_SIZE_UNIT (defn) = DECL_SIZE_UNIT (maybe_dup);
13292 : 234 : DECL_ALIGN_RAW (defn) = DECL_ALIGN_RAW (maybe_dup);
13293 : 468 : DECL_WARN_IF_NOT_ALIGN_RAW (defn)
13294 : 234 : = DECL_WARN_IF_NOT_ALIGN_RAW (maybe_dup);
13295 : 234 : DECL_USER_ALIGN (defn) = DECL_USER_ALIGN (maybe_dup);
13296 : :
13297 : 234 : TYPE_TYPELESS_STORAGE (type) = TYPE_TYPELESS_STORAGE (type_dup);
13298 : 234 : TYPE_CXX_ODR_P (type) = TYPE_CXX_ODR_P (type_dup);
13299 : 234 : TYPE_NO_FORCE_BLK (type) = TYPE_NO_FORCE_BLK (type_dup);
13300 : 234 : TYPE_TRANSPARENT_AGGR (type) = TYPE_TRANSPARENT_AGGR (type_dup);
13301 : 468 : TYPE_CONTAINS_PLACEHOLDER_INTERNAL (type)
13302 : 234 : = TYPE_CONTAINS_PLACEHOLDER_INTERNAL (type_dup);
13303 : :
13304 : 234 : TYPE_EMPTY_P (type) = TYPE_EMPTY_P (type_dup);
13305 : 234 : TREE_ADDRESSABLE (type) = TREE_ADDRESSABLE (type_dup);
13306 : :
13307 : : /* C++ pieces. */
13308 : 234 : TYPE_POLYMORPHIC_P (type) = TYPE_POLYMORPHIC_P (type_dup);
13309 : 234 : CLASSTYPE_FINAL (type) = CLASSTYPE_FINAL (type_dup);
13310 : :
13311 : 468 : TYPE_HAS_USER_CONSTRUCTOR (type)
13312 : 234 : = TYPE_HAS_USER_CONSTRUCTOR (type_dup);
13313 : 468 : TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type)
13314 : 234 : = TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type_dup);
13315 : 468 : TYPE_NEEDS_CONSTRUCTING (type)
13316 : 234 : = TYPE_NEEDS_CONSTRUCTING (type_dup);
13317 : :
13318 : 234 : if (auto ls = TYPE_LANG_SPECIFIC (type_dup))
13319 : : {
13320 : 234 : if (TYPE_LANG_SPECIFIC (type))
13321 : : {
13322 : 702 : CLASSTYPE_BEFRIENDING_CLASSES (type_dup)
13323 : 234 : = CLASSTYPE_BEFRIENDING_CLASSES (type);
13324 : 234 : if (!ANON_AGGR_TYPE_P (type))
13325 : 702 : CLASSTYPE_TYPEINFO_VAR (type_dup)
13326 : 234 : = CLASSTYPE_TYPEINFO_VAR (type);
13327 : : }
13328 : 1087 : for (tree v = type; v; v = TYPE_NEXT_VARIANT (v))
13329 : 853 : TYPE_LANG_SPECIFIC (v) = ls;
13330 : : }
13331 : : }
13332 : :
13333 : 22947 : TYPE_SIZE (type) = size;
13334 : 22947 : TYPE_SIZE_UNIT (type) = size_unit;
13335 : :
13336 : 22947 : if (fields)
13337 : : {
13338 : 22947 : tree *chain = &TYPE_FIELDS (type);
13339 : 22947 : unsigned len = fields->length ();
13340 : 257980 : for (unsigned ix = 0; ix != len; ix++)
13341 : : {
13342 : 235033 : tree decl = (*fields)[ix];
13343 : :
13344 : 235033 : if (!decl)
13345 : : {
13346 : : /* An anonymous struct with typedef name. */
13347 : 3 : tree tdef = (*fields)[ix+1];
13348 : 3 : decl = TYPE_STUB_DECL (TREE_TYPE (tdef));
13349 : 3 : gcc_checking_assert (IDENTIFIER_ANON_P (DECL_NAME (decl))
13350 : : && decl != tdef);
13351 : : }
13352 : :
13353 : 426287 : gcc_checking_assert (!*chain == !DECL_CLONED_FUNCTION_P (decl));
13354 : 235033 : *chain = decl;
13355 : 235033 : chain = &DECL_CHAIN (decl);
13356 : :
13357 : 235033 : if (TREE_CODE (decl) == FIELD_DECL
13358 : 235033 : && ANON_AGGR_TYPE_P (TREE_TYPE (decl)))
13359 : : {
13360 : 157 : tree anon_type = TYPE_MAIN_VARIANT (TREE_TYPE (decl));
13361 : 157 : if (DECL_NAME (defn) == as_base_identifier)
13362 : : /* ANON_AGGR_TYPE_FIELD should already point to the
13363 : : original FIELD_DECL; don't overwrite it to point
13364 : : to the as-base FIELD_DECL copy. */
13365 : 10 : gcc_checking_assert (ANON_AGGR_TYPE_FIELD (anon_type));
13366 : : else
13367 : 147 : ANON_AGGR_TYPE_FIELD (anon_type) = decl;
13368 : : }
13369 : :
13370 : 235033 : if (TREE_CODE (decl) == USING_DECL
13371 : 235033 : && TREE_CODE (USING_DECL_SCOPE (decl)) == RECORD_TYPE)
13372 : : {
13373 : : /* Reconstruct DECL_ACCESS. */
13374 : 10540 : tree decls = USING_DECL_DECLS (decl);
13375 : 10540 : tree access = declared_access (decl);
13376 : :
13377 : 12060 : for (ovl_iterator iter (decls); iter; ++iter)
13378 : : {
13379 : 970 : tree d = *iter;
13380 : :
13381 : 970 : retrofit_lang_decl (d);
13382 : 970 : tree list = DECL_ACCESS (d);
13383 : :
13384 : 970 : if (!purpose_member (type, list))
13385 : 1144 : DECL_ACCESS (d) = tree_cons (type, access, list);
13386 : : }
13387 : : }
13388 : : }
13389 : : }
13390 : :
13391 : 22947 : TYPE_VFIELD (type) = vfield;
13392 : 22947 : TYPE_BINFO (type) = binfo;
13393 : :
13394 : 22947 : if (TYPE_LANG_SPECIFIC (type))
13395 : : {
13396 : 22431 : if (!TYPE_POLYMORPHIC_P (type))
13397 : 21413 : SET_CLASSTYPE_LAMBDA_EXPR (type, lambda);
13398 : : else
13399 : 1018 : gcc_checking_assert (lambda == NULL_TREE);
13400 : :
13401 : 22431 : CLASSTYPE_MEMBER_VEC (type) = member_vec;
13402 : 22431 : CLASSTYPE_PURE_VIRTUALS (type) = pure_virts;
13403 : 22431 : CLASSTYPE_VCALL_INDICES (type) = vcall_indices;
13404 : :
13405 : 22431 : if (TYPE_POLYMORPHIC_P (type))
13406 : 1018 : SET_CLASSTYPE_KEY_METHOD (type, key_method);
13407 : : else
13408 : 21413 : gcc_checking_assert (key_method == NULL_TREE);
13409 : :
13410 : 22431 : CLASSTYPE_VBASECLASSES (type) = vbase_vec;
13411 : :
13412 : : /* Resort the member vector. */
13413 : 22431 : resort_type_member_vec (member_vec, NULL, nop, NULL);
13414 : : }
13415 : : }
13416 : : else if (maybe_dup)
13417 : : {
13418 : : // FIXME:QOI Check matching defn
13419 : : }
13420 : :
13421 : 52107 : if (TYPE_LANG_SPECIFIC (type))
13422 : : {
13423 : 51081 : tree primary = tree_node ();
13424 : 51081 : tree as_base = tree_node ();
13425 : :
13426 : 51081 : if (as_base)
13427 : 25851 : as_base = TREE_TYPE (as_base);
13428 : :
13429 : : /* Read the vtables. */
13430 : 51081 : vec<tree, va_heap> *vtables = vec_chained_decls ();
13431 : 51081 : if (vtables)
13432 : : {
13433 : 2044 : unsigned len = vtables->length ();
13434 : 4503 : for (unsigned ix = 0; ix != len; ix++)
13435 : : {
13436 : 2459 : tree vtable = (*vtables)[ix];
13437 : 2459 : read_var_def (vtable, vtable);
13438 : : }
13439 : : }
13440 : :
13441 : 51081 : tree friend_classes = tree_list (false);
13442 : 51081 : tree friend_functions = NULL_TREE;
13443 : 51081 : for (tree *chain = &friend_functions;
13444 : 58974 : tree name = tree_node (); chain = &TREE_CHAIN (*chain))
13445 : : {
13446 : 7893 : tree val = tree_list (false);
13447 : 7893 : *chain = build_tree_list (name, val);
13448 : 7893 : }
13449 : 51081 : tree decl_list = tree_list (true);
13450 : :
13451 : 51081 : if (installing)
13452 : : {
13453 : 22431 : CLASSTYPE_PRIMARY_BINFO (type) = primary;
13454 : 22431 : CLASSTYPE_AS_BASE (type) = as_base;
13455 : :
13456 : 22431 : if (vtables)
13457 : : {
13458 : 1030 : if ((!CLASSTYPE_KEY_METHOD (type)
13459 : : /* Sneaky user may have defined it inline
13460 : : out-of-class. */
13461 : 598 : || DECL_DECLARED_INLINE_P (CLASSTYPE_KEY_METHOD (type)))
13462 : : /* An imported non-template class attached to a module
13463 : : doesn't need to have its vtables emitted here. */
13464 : 1036 : && (CLASSTYPE_USE_TEMPLATE (type)
13465 : 138 : || !DECL_MODULE_ATTACH_P (defn)))
13466 : 689 : vec_safe_push (keyed_classes, type);
13467 : 1030 : unsigned len = vtables->length ();
13468 : 1030 : tree *chain = &CLASSTYPE_VTABLES (type);
13469 : 2270 : for (unsigned ix = 0; ix != len; ix++)
13470 : : {
13471 : 1240 : tree vtable = (*vtables)[ix];
13472 : 1240 : gcc_checking_assert (!*chain);
13473 : 1240 : *chain = vtable;
13474 : 1240 : chain = &DECL_CHAIN (vtable);
13475 : : }
13476 : : }
13477 : 22431 : CLASSTYPE_FRIEND_CLASSES (type) = friend_classes;
13478 : 22431 : DECL_FRIENDLIST (defn) = friend_functions;
13479 : 22431 : CLASSTYPE_DECL_LIST (type) = decl_list;
13480 : :
13481 : 23969 : for (; friend_classes; friend_classes = TREE_CHAIN (friend_classes))
13482 : : {
13483 : 1538 : tree f = TREE_VALUE (friend_classes);
13484 : 1538 : if (TREE_CODE (f) == TEMPLATE_DECL)
13485 : 594 : f = TREE_TYPE (f);
13486 : :
13487 : 1538 : if (CLASS_TYPE_P (f))
13488 : : {
13489 : 1506 : CLASSTYPE_BEFRIENDING_CLASSES (f)
13490 : 3012 : = tree_cons (NULL_TREE, type,
13491 : 1506 : CLASSTYPE_BEFRIENDING_CLASSES (f));
13492 : 1544 : dump () && dump ("Class %N befriending %C:%N",
13493 : 6 : type, TREE_CODE (f), f);
13494 : : }
13495 : : }
13496 : :
13497 : 25937 : for (; friend_functions;
13498 : 3506 : friend_functions = TREE_CHAIN (friend_functions))
13499 : 3506 : for (tree friend_decls = TREE_VALUE (friend_functions);
13500 : 7945 : friend_decls; friend_decls = TREE_CHAIN (friend_decls))
13501 : : {
13502 : 4439 : tree f = TREE_VALUE (friend_decls);
13503 : 4439 : if (TREE_CODE (f) == TU_LOCAL_ENTITY)
13504 : 36 : continue;
13505 : :
13506 : 4403 : DECL_BEFRIENDING_CLASSES (f)
13507 : 4403 : = tree_cons (NULL_TREE, type, DECL_BEFRIENDING_CLASSES (f));
13508 : 4466 : dump () && dump ("Class %N befriending %C:%N",
13509 : 27 : type, TREE_CODE (f), f);
13510 : : }
13511 : : }
13512 : :
13513 : 51081 : if (TYPE_CONTAINS_VPTR_P (type))
13514 : : /* Read and install the thunks. */
13515 : 2454 : while (tree vfunc = tree_node ())
13516 : : {
13517 : 376 : tree thunks = chained_decls ();
13518 : 376 : if (installing)
13519 : 186 : SET_DECL_THUNKS (vfunc, thunks);
13520 : : }
13521 : :
13522 : 51081 : vec_free (vtables);
13523 : : }
13524 : :
13525 : : /* Propagate to all variants. */
13526 : 52107 : if (installing)
13527 : 22947 : fixup_type_variants (type);
13528 : :
13529 : : /* IS_FAKE_BASE_TYPE is inaccurate at this point, because if this is
13530 : : the fake base, we've not hooked it into the containing class's
13531 : : data structure yet. Fortunately it has a unique name. */
13532 : 22947 : if (installing
13533 : 22947 : && DECL_NAME (defn) != as_base_identifier
13534 : 22431 : && (!CLASSTYPE_TEMPLATE_INFO (type)
13535 : 19045 : || !uses_template_parms (TI_ARGS (CLASSTYPE_TEMPLATE_INFO (type)))))
13536 : : /* Emit debug info. It'd be nice to know if the interface TU
13537 : : already emitted this. */
13538 : 12650 : rest_of_type_compilation (type, !LOCAL_CLASS_P (type));
13539 : :
13540 : 52107 : vec_free (fields);
13541 : :
13542 : 52107 : return !get_overrun ();
13543 : : }
13544 : :
13545 : : void
13546 : 7433 : trees_out::write_enum_def (tree decl)
13547 : : {
13548 : 7433 : tree type = TREE_TYPE (decl);
13549 : :
13550 : 7433 : tree_node (TYPE_VALUES (type));
13551 : : /* Note that we stream TYPE_MIN/MAX_VALUE directly as part of the
13552 : : ENUMERAL_TYPE. */
13553 : 7433 : }
13554 : :
13555 : : void
13556 : 7433 : trees_out::mark_enum_def (tree decl)
13557 : : {
13558 : 7433 : tree type = TREE_TYPE (decl);
13559 : :
13560 : 36292 : for (tree values = TYPE_VALUES (type); values; values = TREE_CHAIN (values))
13561 : : {
13562 : 28859 : tree cst = TREE_VALUE (values);
13563 : 28859 : mark_by_value (cst);
13564 : : /* We must mark the init to avoid circularity in tt_enum_int. */
13565 : 28859 : if (tree init = DECL_INITIAL (cst))
13566 : 28651 : if (TREE_CODE (init) == INTEGER_CST)
13567 : 28091 : mark_by_value (init);
13568 : : }
13569 : 7433 : }
13570 : :
13571 : : bool
13572 : 2442 : trees_in::read_enum_def (tree defn, tree maybe_template)
13573 : : {
13574 : 2442 : tree type = TREE_TYPE (defn);
13575 : 2442 : tree values = tree_node ();
13576 : :
13577 : 2442 : if (get_overrun ())
13578 : : return false;
13579 : :
13580 : 2442 : tree maybe_dup = odr_duplicate (maybe_template, TYPE_VALUES (type));
13581 : 4884 : bool installing = maybe_dup && !TYPE_VALUES (type);
13582 : :
13583 : 2442 : if (installing)
13584 : : {
13585 : 1022 : TYPE_VALUES (type) = values;
13586 : : /* Note that we stream TYPE_MIN/MAX_VALUE directly as part of the
13587 : : ENUMERAL_TYPE. */
13588 : :
13589 : 1655 : rest_of_type_compilation (type, DECL_NAMESPACE_SCOPE_P (defn));
13590 : : }
13591 : 1420 : else if (maybe_dup)
13592 : : {
13593 : 1420 : tree known = TYPE_VALUES (type);
13594 : 8099 : for (; known && values;
13595 : 6679 : known = TREE_CHAIN (known), values = TREE_CHAIN (values))
13596 : : {
13597 : 6688 : tree known_decl = TREE_VALUE (known);
13598 : 6688 : tree new_decl = TREE_VALUE (values);
13599 : :
13600 : 6688 : if (DECL_NAME (known_decl) != DECL_NAME (new_decl))
13601 : : break;
13602 : :
13603 : 6682 : new_decl = maybe_duplicate (new_decl);
13604 : :
13605 : 6682 : if (!cp_tree_equal (DECL_INITIAL (known_decl),
13606 : 6682 : DECL_INITIAL (new_decl)))
13607 : : break;
13608 : : }
13609 : :
13610 : 1420 : if (known || values)
13611 : : {
13612 : 12 : auto_diagnostic_group d;
13613 : 12 : error_at (DECL_SOURCE_LOCATION (maybe_dup),
13614 : : "definition of %qD does not match", maybe_dup);
13615 : 12 : inform (DECL_SOURCE_LOCATION (defn),
13616 : : "existing definition %qD", defn);
13617 : :
13618 : 12 : tree known_decl = NULL_TREE, new_decl = NULL_TREE;
13619 : :
13620 : 12 : if (known)
13621 : 9 : known_decl = TREE_VALUE (known);
13622 : 12 : if (values)
13623 : 12 : new_decl = maybe_duplicate (TREE_VALUE (values));
13624 : :
13625 : 12 : if (known_decl && new_decl)
13626 : : {
13627 : 9 : inform (DECL_SOURCE_LOCATION (new_decl),
13628 : : "enumerator %qD does not match ...", new_decl);
13629 : 9 : inform (DECL_SOURCE_LOCATION (known_decl),
13630 : : "... this enumerator %qD", known_decl);
13631 : : }
13632 : 3 : else if (known_decl || new_decl)
13633 : : {
13634 : 3 : tree extra = known_decl ? known_decl : new_decl;
13635 : 3 : inform (DECL_SOURCE_LOCATION (extra),
13636 : : "additional enumerators beginning with %qD", extra);
13637 : : }
13638 : : else
13639 : 0 : inform (DECL_SOURCE_LOCATION (maybe_dup),
13640 : : "enumeration range differs");
13641 : :
13642 : : /* Mark it bad. */
13643 : 12 : unmatched_duplicate (maybe_template);
13644 : 12 : }
13645 : : }
13646 : :
13647 : : return true;
13648 : : }
13649 : :
13650 : : /* Write out the body of DECL. See above circularity note. */
13651 : :
13652 : : void
13653 : 619618 : trees_out::write_definition (tree decl, bool refs_tu_local)
13654 : : {
13655 : 619618 : auto ovr = make_temp_override (writing_local_entities,
13656 : 619618 : writing_local_entities || refs_tu_local);
13657 : :
13658 : 619618 : if (streaming_p ())
13659 : : {
13660 : 309732 : assert_definition (decl);
13661 : 309732 : dump ()
13662 : 584 : && dump ("Writing definition %C:%N", TREE_CODE (decl), decl);
13663 : : }
13664 : : else
13665 : 309886 : dump (dumper::DEPEND)
13666 : 75 : && dump ("Depending definition %C:%N", TREE_CODE (decl), decl);
13667 : :
13668 : 954788 : again:
13669 : 954788 : switch (TREE_CODE (decl))
13670 : : {
13671 : 0 : default:
13672 : 0 : gcc_unreachable ();
13673 : :
13674 : 335170 : case TEMPLATE_DECL:
13675 : 335170 : decl = DECL_TEMPLATE_RESULT (decl);
13676 : 335170 : goto again;
13677 : :
13678 : 361327 : case FUNCTION_DECL:
13679 : 361327 : write_function_def (decl);
13680 : 361327 : break;
13681 : :
13682 : 151673 : case TYPE_DECL:
13683 : 151673 : {
13684 : 151673 : tree type = TREE_TYPE (decl);
13685 : 151673 : gcc_assert (TYPE_MAIN_VARIANT (type) == type
13686 : : && TYPE_NAME (type) == decl);
13687 : 151673 : if (TREE_CODE (type) == ENUMERAL_TYPE)
13688 : 7433 : write_enum_def (decl);
13689 : : else
13690 : 144240 : write_class_def (decl);
13691 : : }
13692 : : break;
13693 : :
13694 : 106618 : case VAR_DECL:
13695 : 106618 : case CONCEPT_DECL:
13696 : 106618 : write_var_def (decl);
13697 : 106618 : break;
13698 : : }
13699 : 619618 : }
13700 : :
13701 : : /* Mark a declaration for by-value walking. If DO_DEFN is true, mark
13702 : : its body too. */
13703 : :
13704 : : void
13705 : 2587870 : trees_out::mark_declaration (tree decl, bool do_defn)
13706 : : {
13707 : 2587870 : mark_by_value (decl);
13708 : :
13709 : 2587870 : if (TREE_CODE (decl) == TEMPLATE_DECL)
13710 : 840078 : decl = DECL_TEMPLATE_RESULT (decl);
13711 : :
13712 : 2587870 : if (!do_defn)
13713 : : return;
13714 : :
13715 : 619646 : switch (TREE_CODE (decl))
13716 : : {
13717 : 0 : default:
13718 : 0 : gcc_unreachable ();
13719 : :
13720 : : case FUNCTION_DECL:
13721 : : mark_function_def (decl);
13722 : : break;
13723 : :
13724 : 151701 : case TYPE_DECL:
13725 : 151701 : {
13726 : 151701 : tree type = TREE_TYPE (decl);
13727 : 151701 : gcc_assert (TYPE_MAIN_VARIANT (type) == type
13728 : : && TYPE_NAME (type) == decl);
13729 : 151701 : if (TREE_CODE (type) == ENUMERAL_TYPE)
13730 : 7433 : mark_enum_def (decl);
13731 : : else
13732 : 144268 : mark_class_def (decl);
13733 : : }
13734 : : break;
13735 : :
13736 : : case VAR_DECL:
13737 : : case CONCEPT_DECL:
13738 : : mark_var_def (decl);
13739 : : break;
13740 : : }
13741 : : }
13742 : :
13743 : : /* Read in the body of DECL. See above circularity note. */
13744 : :
13745 : : bool
13746 : 240913 : trees_in::read_definition (tree decl)
13747 : : {
13748 : 241970 : dump () && dump ("Reading definition %C %N", TREE_CODE (decl), decl);
13749 : :
13750 : : tree maybe_template = decl;
13751 : :
13752 : 240913 : again:
13753 : 381026 : switch (TREE_CODE (decl))
13754 : : {
13755 : : default:
13756 : : break;
13757 : :
13758 : 140113 : case TEMPLATE_DECL:
13759 : 140113 : decl = DECL_TEMPLATE_RESULT (decl);
13760 : 140113 : goto again;
13761 : :
13762 : 153882 : case FUNCTION_DECL:
13763 : 153882 : return read_function_def (decl, maybe_template);
13764 : :
13765 : 54549 : case TYPE_DECL:
13766 : 54549 : {
13767 : 54549 : tree type = TREE_TYPE (decl);
13768 : 54549 : gcc_assert (TYPE_MAIN_VARIANT (type) == type
13769 : : && TYPE_NAME (type) == decl);
13770 : 54549 : if (TREE_CODE (type) == ENUMERAL_TYPE)
13771 : 2442 : return read_enum_def (decl, maybe_template);
13772 : : else
13773 : 52107 : return read_class_def (decl, maybe_template);
13774 : : }
13775 : 32482 : break;
13776 : :
13777 : 32482 : case VAR_DECL:
13778 : 32482 : case CONCEPT_DECL:
13779 : 32482 : return read_var_def (decl, maybe_template);
13780 : : }
13781 : :
13782 : : return false;
13783 : : }
13784 : :
13785 : : /* Lookup an maybe insert a slot for depset for KEY. */
13786 : :
13787 : : depset **
13788 : 11870408 : depset::hash::entity_slot (tree entity, bool insert)
13789 : : {
13790 : 11870408 : traits::compare_type key (entity, NULL);
13791 : 18008017 : depset **slot = find_slot_with_hash (key, traits::hash (key),
13792 : : insert ? INSERT : NO_INSERT);
13793 : :
13794 : 11870408 : return slot;
13795 : : }
13796 : :
13797 : : depset **
13798 : 149802 : depset::hash::binding_slot (tree ctx, tree name, bool insert)
13799 : : {
13800 : 149802 : traits::compare_type key (ctx, name);
13801 : 183053 : depset **slot = find_slot_with_hash (key, traits::hash (key),
13802 : : insert ? INSERT : NO_INSERT);
13803 : :
13804 : 149802 : return slot;
13805 : : }
13806 : :
13807 : : depset *
13808 : 5846864 : depset::hash::find_dependency (tree decl)
13809 : : {
13810 : 5846864 : depset **slot = entity_slot (decl, false);
13811 : :
13812 : 5846864 : return slot ? *slot : NULL;
13813 : : }
13814 : :
13815 : : depset *
13816 : 33251 : depset::hash::find_binding (tree ctx, tree name)
13817 : : {
13818 : 33251 : depset **slot = binding_slot (ctx, name, false);
13819 : :
13820 : 33251 : return slot ? *slot : NULL;
13821 : : }
13822 : :
13823 : : /* Returns true if DECL is a TU-local entity, as defined by [basic.link].
13824 : : If EXPLAIN is true, emit an informative note about why DECL is TU-local. */
13825 : :
13826 : : bool
13827 : 1078955 : depset::hash::is_tu_local_entity (tree decl, bool explain/*=false*/)
13828 : : {
13829 : 1078955 : gcc_checking_assert (DECL_P (decl));
13830 : 1078955 : location_t loc = DECL_SOURCE_LOCATION (decl);
13831 : 1078955 : tree type = TREE_TYPE (decl);
13832 : :
13833 : : /* Only types, functions, variables, and template (specialisations)
13834 : : can be TU-local. */
13835 : 1078955 : if (TREE_CODE (decl) != TYPE_DECL
13836 : 1078955 : && TREE_CODE (decl) != FUNCTION_DECL
13837 : 405995 : && TREE_CODE (decl) != VAR_DECL
13838 : 384913 : && TREE_CODE (decl) != TEMPLATE_DECL)
13839 : : return false;
13840 : :
13841 : : /* An explicit type alias is not an entity; we don't want to stream
13842 : : such aliases if they refer to TU-local entities, so propagate this
13843 : : from the original type. The built-in declarations of 'int' and such
13844 : : are never TU-local. */
13845 : 1076399 : if (TREE_CODE (decl) == TYPE_DECL
13846 : 576196 : && !DECL_SELF_REFERENCE_P (decl)
13847 : 1628788 : && !DECL_IMPLICIT_TYPEDEF_P (decl))
13848 : : {
13849 : 314195 : tree orig = DECL_ORIGINAL_TYPE (decl);
13850 : 314195 : if (orig && TYPE_NAME (orig))
13851 : : {
13852 : 62660 : if (explain)
13853 : 11 : inform (loc, "%qD is an alias of TU-local type %qT", decl, orig);
13854 : 62660 : return is_tu_local_entity (TYPE_NAME (orig), explain);
13855 : : }
13856 : : else
13857 : : return false;
13858 : : }
13859 : :
13860 : : /* Check specializations first for slightly better explanations. */
13861 : 762204 : int use_tpl = -1;
13862 : 762204 : tree ti = node_template_info (decl, use_tpl);
13863 : 1073706 : if (use_tpl > 0 && TREE_CODE (TI_TEMPLATE (ti)) == TEMPLATE_DECL)
13864 : : {
13865 : : /* A specialization of a TU-local template. */
13866 : 311489 : tree tmpl = TI_TEMPLATE (ti);
13867 : 311489 : if (is_tu_local_entity (tmpl))
13868 : : {
13869 : 42 : if (explain)
13870 : : {
13871 : 9 : inform (loc, "%qD is a specialization of TU-local template %qD",
13872 : : decl, tmpl);
13873 : 9 : is_tu_local_entity (tmpl, /*explain=*/true);
13874 : : }
13875 : 42 : return true;
13876 : : }
13877 : :
13878 : : /* A specialization of a template with any TU-local template argument. */
13879 : 311447 : if (has_tu_local_tmpl_arg (decl, TI_ARGS (ti), explain))
13880 : : return true;
13881 : :
13882 : : /* FIXME A specialization of a template whose (possibly instantiated)
13883 : : declaration is an exposure. This should always be covered by the
13884 : : above cases?? */
13885 : : }
13886 : :
13887 : : /* A type, function, variable, or template with internal linkage. */
13888 : 762135 : linkage_kind kind = decl_linkage (decl);
13889 : 762135 : if (kind == lk_internal
13890 : : /* But although weakrefs are marked static, don't consider them
13891 : : to be TU-local. */
13892 : 762135 : && !lookup_attribute ("weakref", DECL_ATTRIBUTES (decl)))
13893 : : {
13894 : 567 : if (explain)
13895 : 126 : inform (loc, "%qD declared with internal linkage", decl);
13896 : 567 : return true;
13897 : : }
13898 : :
13899 : : /* Does not have a name with linkage and is declared, or introduced by a
13900 : : lambda-expression, within the definition of a TU-local entity. */
13901 : 761568 : if (kind == lk_none)
13902 : : {
13903 : 16734 : tree ctx = CP_DECL_CONTEXT (decl);
13904 : 28897 : if (LAMBDA_TYPE_P (type))
13905 : 7712 : if (tree extra = LAMBDA_TYPE_EXTRA_SCOPE (type))
13906 : 16734 : ctx = extra;
13907 : :
13908 : 16734 : if (TREE_CODE (ctx) == NAMESPACE_DECL)
13909 : : {
13910 : 62 : if (!TREE_PUBLIC (ctx))
13911 : : {
13912 : 0 : if (explain)
13913 : 0 : inform (loc, "%qD has no linkage and is declared in an "
13914 : : "anonymous namespace", decl);
13915 : 0 : return true;
13916 : : }
13917 : : }
13918 : 16672 : else if (TYPE_P (ctx))
13919 : : {
13920 : 5784 : tree ctx_decl = TYPE_MAIN_DECL (ctx);
13921 : 5784 : if (is_tu_local_entity (ctx_decl))
13922 : : {
13923 : 0 : if (explain)
13924 : : {
13925 : 0 : inform (loc, "%qD has no linkage and is declared within "
13926 : : "TU-local entity %qT", decl, ctx);
13927 : 0 : is_tu_local_entity (ctx_decl, /*explain=*/true);
13928 : : }
13929 : 0 : return true;
13930 : : }
13931 : : }
13932 : 10888 : else if (is_tu_local_entity (ctx))
13933 : : {
13934 : 27 : if (explain)
13935 : : {
13936 : 9 : inform (loc, "%qD has no linkage and is declared within "
13937 : : "TU-local entity %qD", decl, ctx);
13938 : 9 : is_tu_local_entity (ctx, /*explain=*/true);
13939 : : }
13940 : 27 : return true;
13941 : : }
13942 : : }
13943 : :
13944 : : /* A type with no name that is defined outside a class-specifier, function
13945 : : body, or initializer; or is introduced by a defining-type-specifier that
13946 : : is used to declare only TU-local entities.
13947 : :
13948 : : We consider types with names for linkage purposes as having names, since
13949 : : these aren't really TU-local. */
13950 : 761541 : tree inner = STRIP_TEMPLATE (decl);
13951 : 382258 : if (inner
13952 : 761541 : && TREE_CODE (inner) == TYPE_DECL
13953 : 1036990 : && TYPE_ANON_P (type)
13954 : 5673 : && !DECL_SELF_REFERENCE_P (inner)
13955 : : /* An enum with an enumerator name for linkage. */
13956 : 387134 : && !(UNSCOPED_ENUM_P (type) && TYPE_VALUES (type)))
13957 : : {
13958 : 3555 : tree main_decl = TYPE_MAIN_DECL (type);
13959 : 6894 : if (LAMBDA_TYPE_P (type))
13960 : : {
13961 : : /* A lambda expression is, in practice, TU-local iff it has no
13962 : : mangling scope. This currently doesn't line up exactly with
13963 : : the standard's definition due to some ABI issues, but it's
13964 : : pretty close, and avoids other issues down the line. */
13965 : 6616 : if (!LAMBDA_TYPE_EXTRA_SCOPE (type))
13966 : : {
13967 : 14 : if (explain)
13968 : 6 : inform (loc, "%qT has no name and cannot be differentiated "
13969 : : "from similar lambdas in other TUs", type);
13970 : 14 : return true;
13971 : : }
13972 : : }
13973 : 494 : else if (!DECL_CLASS_SCOPE_P (main_decl)
13974 : 277 : && !decl_function_context (main_decl))
13975 : : {
13976 : 30 : if (explain)
13977 : 12 : inform (loc, "%qT has no name and is not defined within a class, "
13978 : : "function, or initializer", type);
13979 : 30 : return true;
13980 : : }
13981 : :
13982 : : // FIXME introduced by a defining-type-specifier only declaring TU-local
13983 : : // entities; does this refer to e.g. 'static struct {} a;"? I can't
13984 : : // think of any cases where this isn't covered by earlier cases. */
13985 : : }
13986 : :
13987 : : return false;
13988 : : }
13989 : :
13990 : : /* Helper for is_tu_local_entity. Returns true if one of the ARGS of
13991 : : DECL is TU-local. Emits an explanation if EXPLAIN is true. */
13992 : :
13993 : : bool
13994 : 349295 : depset::hash::has_tu_local_tmpl_arg (tree decl, tree args, bool explain)
13995 : : {
13996 : 349295 : if (!args || TREE_CODE (args) != TREE_VEC)
13997 : : return false;
13998 : :
13999 : 932706 : for (tree a : tree_vec_range (args))
14000 : : {
14001 : 583438 : if (TREE_CODE (a) == TREE_VEC)
14002 : : {
14003 : 37848 : if (has_tu_local_tmpl_arg (decl, a, explain))
14004 : 27 : return true;
14005 : : }
14006 : 545590 : else if (!WILDCARD_TYPE_P (a))
14007 : : {
14008 : 493114 : if (DECL_P (a) && is_tu_local_entity (a))
14009 : : {
14010 : 0 : if (explain)
14011 : : {
14012 : 0 : inform (DECL_SOURCE_LOCATION (decl),
14013 : : "%qD has TU-local template argument %qD",
14014 : : decl, a);
14015 : 0 : is_tu_local_entity (a, /*explain=*/true);
14016 : : }
14017 : 0 : return true;
14018 : : }
14019 : :
14020 : 493114 : if (TYPE_P (a) && TYPE_NAME (a) && is_tu_local_entity (TYPE_NAME (a)))
14021 : : {
14022 : 12 : if (explain)
14023 : : {
14024 : 0 : inform (DECL_SOURCE_LOCATION (decl),
14025 : : "%qD has TU-local template argument %qT",
14026 : : decl, a);
14027 : 0 : is_tu_local_entity (TYPE_NAME (a), /*explain=*/true);
14028 : : }
14029 : 12 : return true;
14030 : : }
14031 : :
14032 : 493102 : if (EXPR_P (a) && is_tu_local_value (decl, a, explain))
14033 : : return true;
14034 : : }
14035 : : }
14036 : :
14037 : 349268 : return false;
14038 : : }
14039 : :
14040 : : /* Returns true if EXPR (part of the initializer for DECL) is a TU-local value
14041 : : or object. Emits an explanation if EXPLAIN is true. */
14042 : :
14043 : : bool
14044 : 40833 : depset::hash::is_tu_local_value (tree decl, tree expr, bool explain)
14045 : : {
14046 : 40833 : if (!expr)
14047 : : return false;
14048 : :
14049 : 39827 : tree e = expr;
14050 : 39827 : STRIP_ANY_LOCATION_WRAPPER (e);
14051 : 39827 : STRIP_NOPS (e);
14052 : 39827 : if (TREE_CODE (e) == TARGET_EXPR)
14053 : 0 : e = TARGET_EXPR_INITIAL (e);
14054 : 0 : if (!e)
14055 : : return false;
14056 : :
14057 : : /* It is, or is a pointer to, a TU-local function or the object associated
14058 : : with a TU-local variable. */
14059 : 39827 : tree object = NULL_TREE;
14060 : 39827 : if (TREE_CODE (e) == ADDR_EXPR)
14061 : 1876 : object = TREE_OPERAND (e, 0);
14062 : 37951 : else if (TREE_CODE (e) == PTRMEM_CST)
14063 : 0 : object = PTRMEM_CST_MEMBER (e);
14064 : 37951 : else if (VAR_OR_FUNCTION_DECL_P (e))
14065 : : object = e;
14066 : :
14067 : 1876 : if (object
14068 : 2152 : && VAR_OR_FUNCTION_DECL_P (object)
14069 : 2236 : && is_tu_local_entity (object))
14070 : : {
14071 : 54 : if (explain)
14072 : : {
14073 : : /* We've lost a lot of location information by the time we get here,
14074 : : so let's just do our best effort. */
14075 : 18 : auto loc = cp_expr_loc_or_loc (expr, DECL_SOURCE_LOCATION (decl));
14076 : 18 : if (VAR_P (object))
14077 : 9 : inform (loc, "%qD refers to TU-local object %qD", decl, object);
14078 : : else
14079 : 9 : inform (loc, "%qD refers to TU-local function %qD", decl, object);
14080 : 18 : is_tu_local_entity (object, true);
14081 : : }
14082 : 54 : return true;
14083 : : }
14084 : :
14085 : : /* It is an object of class or array type and any of its subobjects or
14086 : : any of the objects or functions to which its non-static data members
14087 : : of reference type refer is TU-local and is usable in constant
14088 : : expressions. */
14089 : 39773 : if (TREE_CODE (e) == CONSTRUCTOR && AGGREGATE_TYPE_P (TREE_TYPE (e)))
14090 : 20220 : for (auto &f : CONSTRUCTOR_ELTS (e))
14091 : 13868 : if (is_tu_local_value (decl, f.value, explain))
14092 : : return true;
14093 : :
14094 : : return false;
14095 : : }
14096 : :
14097 : : /* DECL is a newly discovered dependency. Create the depset, if it
14098 : : doesn't already exist. Add it to the worklist if so.
14099 : :
14100 : : DECL will be an OVL_USING_P OVERLOAD, if it's from a binding that's
14101 : : a using decl.
14102 : :
14103 : : We do not have to worry about adding the same dependency more than
14104 : : once. First it's harmless, but secondly the TREE_VISITED marking
14105 : : prevents us wanting to do it anyway. */
14106 : :
14107 : : depset *
14108 : 4941710 : depset::hash::make_dependency (tree decl, entity_kind ek)
14109 : : {
14110 : : /* Make sure we're being told consistent information. */
14111 : 9168221 : gcc_checking_assert ((ek == EK_NAMESPACE)
14112 : : == (TREE_CODE (decl) == NAMESPACE_DECL
14113 : : && !DECL_NAMESPACE_ALIAS (decl)));
14114 : 4941710 : gcc_checking_assert (ek != EK_BINDING && ek != EK_REDIRECT);
14115 : 4941710 : gcc_checking_assert (TREE_CODE (decl) != FIELD_DECL
14116 : : && (TREE_CODE (decl) != USING_DECL
14117 : : || TREE_CODE (DECL_CONTEXT (decl)) == FUNCTION_DECL));
14118 : 4941710 : gcc_checking_assert (!is_key_order ());
14119 : 4941710 : if (ek == EK_USING)
14120 : 13528 : gcc_checking_assert (TREE_CODE (decl) == OVERLOAD);
14121 : :
14122 : 4941710 : if (TREE_CODE (decl) == TEMPLATE_DECL)
14123 : : /* The template should have copied these from its result decl. */
14124 : 1770089 : gcc_checking_assert (DECL_MODULE_EXPORT_P (decl)
14125 : : == DECL_MODULE_EXPORT_P (DECL_TEMPLATE_RESULT (decl)));
14126 : :
14127 : 4941710 : depset **slot = entity_slot (decl, true);
14128 : 4941710 : depset *dep = *slot;
14129 : 4941710 : bool for_binding = ek == EK_FOR_BINDING;
14130 : :
14131 : 4941710 : if (!dep)
14132 : : {
14133 : 419981 : if ((DECL_IMPLICIT_TYPEDEF_P (decl)
14134 : : /* ... not an enum, for instance. */
14135 : 217361 : && RECORD_OR_UNION_TYPE_P (TREE_TYPE (decl))
14136 : 213501 : && TYPE_LANG_SPECIFIC (TREE_TYPE (decl))
14137 : 192145 : && CLASSTYPE_USE_TEMPLATE (TREE_TYPE (decl)) == 2)
14138 : 1457167 : || (VAR_P (decl)
14139 : 58853 : && DECL_LANG_SPECIFIC (decl)
14140 : 58772 : && DECL_USE_TEMPLATE (decl) == 2))
14141 : : {
14142 : : /* A partial or explicit specialization. Partial
14143 : : specializations might not be in the hash table, because
14144 : : there can be multiple differently-constrained variants.
14145 : :
14146 : : template<typename T> class silly;
14147 : : template<typename T> requires true class silly {};
14148 : :
14149 : : We need to find them, insert their TEMPLATE_DECL in the
14150 : : dep_hash, and then convert the dep we just found into a
14151 : : redirect. */
14152 : :
14153 : 34998 : tree ti = get_template_info (decl);
14154 : 34998 : tree tmpl = TI_TEMPLATE (ti);
14155 : 34998 : tree partial = NULL_TREE;
14156 : 34998 : for (tree spec = DECL_TEMPLATE_SPECIALIZATIONS (tmpl);
14157 : 132906 : spec; spec = TREE_CHAIN (spec))
14158 : 117175 : if (DECL_TEMPLATE_RESULT (TREE_VALUE (spec)) == decl)
14159 : : {
14160 : : partial = TREE_VALUE (spec);
14161 : : break;
14162 : : }
14163 : :
14164 : 34998 : if (partial)
14165 : : {
14166 : : /* Eagerly create an empty redirect. The following
14167 : : make_dependency call could cause hash reallocation,
14168 : : and invalidate slot's value. */
14169 : 19267 : depset *redirect = make_entity (decl, EK_REDIRECT);
14170 : :
14171 : : /* Redirects are never reached -- always snap to their target. */
14172 : 19267 : redirect->set_flag_bit<DB_UNREACHED_BIT> ();
14173 : :
14174 : 19267 : *slot = redirect;
14175 : :
14176 : 19267 : depset *tmpl_dep = make_dependency (partial, EK_PARTIAL);
14177 : 19267 : gcc_checking_assert (tmpl_dep->get_entity_kind () == EK_PARTIAL);
14178 : :
14179 : 19267 : redirect->deps.safe_push (tmpl_dep);
14180 : :
14181 : 19267 : return redirect;
14182 : : }
14183 : : }
14184 : :
14185 : 1048757 : bool has_def = ek != EK_USING && has_definition (decl);
14186 : 1035229 : if (ek > EK_BINDING)
14187 : 132323 : ek = EK_DECL;
14188 : :
14189 : : /* The only OVERLOADS we should see are USING decls from
14190 : : bindings. */
14191 : 1048757 : *slot = dep = make_entity (decl, ek, has_def);
14192 : :
14193 : 1048757 : if (CHECKING_P && TREE_CODE (decl) == TEMPLATE_DECL)
14194 : : /* The template_result should otherwise not be in the
14195 : : table, or be an empty redirect (created above). */
14196 : 290745 : if (auto *eslot = entity_slot (DECL_TEMPLATE_RESULT (decl), false))
14197 : 19267 : gcc_checking_assert ((*eslot)->get_entity_kind () == EK_REDIRECT
14198 : : && !(*eslot)->deps.length ());
14199 : :
14200 : 1048757 : if (ek != EK_USING)
14201 : : {
14202 : 1035229 : tree not_tmpl = STRIP_TEMPLATE (decl);
14203 : 1035229 : bool imported_from_module_p = false;
14204 : :
14205 : 1035229 : if (DECL_LANG_SPECIFIC (not_tmpl)
14206 : 2009062 : && DECL_MODULE_IMPORT_P (not_tmpl))
14207 : : {
14208 : : /* Store the module number and index in cluster/section,
14209 : : so we don't have to look them up again. */
14210 : 2213 : unsigned index = import_entity_index (decl);
14211 : 2213 : module_state *from = import_entity_module (index);
14212 : : /* Remap will be zero for imports from partitions, which
14213 : : we want to treat as-if declared in this TU. */
14214 : 2213 : if (from->remap)
14215 : : {
14216 : 1764 : dep->cluster = index - from->entity_lwm;
14217 : 1764 : dep->section = from->remap;
14218 : 1764 : dep->set_flag_bit<DB_IMPORTED_BIT> ();
14219 : :
14220 : 1764 : if (!from->is_header ())
14221 : 1035229 : imported_from_module_p = true;
14222 : : }
14223 : : }
14224 : :
14225 : : /* Check for TU-local entities. This is unnecessary in header
14226 : : units because we can export internal-linkage decls, and
14227 : : no declarations are exposures. Similarly, if the decl was
14228 : : imported from a non-header module we know it cannot have
14229 : : been TU-local. */
14230 : 1035229 : if (!header_module_p () && !imported_from_module_p)
14231 : : {
14232 : 301455 : if (is_tu_local_entity (decl))
14233 : 198 : dep->set_flag_bit<DB_TU_LOCAL_BIT> ();
14234 : :
14235 : 301455 : if (VAR_P (decl)
14236 : 19844 : && decl_maybe_constant_var_p (decl)
14237 : 320656 : && is_tu_local_value (decl, DECL_INITIAL (decl)))
14238 : : {
14239 : : /* A potentially-constant variable initialized to a TU-local
14240 : : value is not usable in constant expressions within other
14241 : : translation units. We can achieve this by simply not
14242 : : streaming the definition in such cases. */
14243 : 24 : dep->clear_flag_bit<DB_DEFN_BIT> ();
14244 : :
14245 : 24 : if (DECL_DECLARED_CONSTEXPR_P (decl)
14246 : 39 : || DECL_INLINE_VAR_P (decl))
14247 : : /* A constexpr variable initialized to a TU-local value,
14248 : : or an inline value (PR c++/119996), is an exposure. */
14249 : 15 : dep->set_flag_bit<DB_EXPOSURE_BIT> ();
14250 : : }
14251 : : }
14252 : :
14253 : : /* A namespace-scope type may be declared in one module unit
14254 : : and defined in another; make sure that we're found when
14255 : : completing the class. */
14256 : 1035229 : if (ek == EK_DECL
14257 : 463825 : && !dep->is_import ()
14258 : 463336 : && dep->has_defn ()
14259 : 237925 : && DECL_NAMESPACE_SCOPE_P (not_tmpl)
14260 : 87497 : && DECL_IMPLICIT_TYPEDEF_P (not_tmpl)
14261 : : /* Anonymous types can't be forward-declared. */
14262 : 1060722 : && !IDENTIFIER_ANON_P (DECL_NAME (not_tmpl)))
14263 : 25087 : dep->set_flag_bit<DB_IS_PENDING_BIT> ();
14264 : : }
14265 : :
14266 : 1048757 : if (!dep->is_import ())
14267 : 1046993 : worklist.safe_push (dep);
14268 : : }
14269 : :
14270 : 4922443 : dump (dumper::DEPEND)
14271 : 33880 : && dump ("%s on %s %C:%N found",
14272 : : ek == EK_REDIRECT ? "Redirect"
14273 : 33880 : : for_binding ? "Binding" : "Dependency",
14274 : 33880 : dep->entity_kind_name (), TREE_CODE (decl), decl);
14275 : :
14276 : 4922443 : return dep;
14277 : : }
14278 : :
14279 : : /* Whether REF is an exposure of a member type of SOURCE.
14280 : :
14281 : : This comes up with exposures of class-scope lambdas, that we currently
14282 : : treat as TU-local due to ABI reasons. In such a case the type of the
14283 : : lambda will be exposed in two places, first by the class type it is in
14284 : : the TYPE_FIELDS list of, and second by the actual member declaring that
14285 : : lambda. We only want the second case to warn. */
14286 : :
14287 : : static bool
14288 : 167 : is_exposure_of_member_type (depset *source, depset *ref)
14289 : : {
14290 : 334 : gcc_checking_assert (source->refs_tu_local () && ref->is_tu_local ());
14291 : 167 : tree source_entity = STRIP_TEMPLATE (source->get_entity ());
14292 : 167 : tree ref_entity = STRIP_TEMPLATE (ref->get_entity ());
14293 : :
14294 : 167 : if (source_entity
14295 : 167 : && ref_entity
14296 : 167 : && DECL_IMPLICIT_TYPEDEF_P (source_entity)
14297 : 2 : && DECL_IMPLICIT_TYPEDEF_P (ref_entity)
14298 : 2 : && DECL_CLASS_SCOPE_P (ref_entity)
14299 : 169 : && DECL_CONTEXT (ref_entity) == TREE_TYPE (source_entity))
14300 : : {
14301 : 4 : gcc_checking_assert (LAMBDA_TYPE_P (TREE_TYPE (ref_entity)));
14302 : : return true;
14303 : : }
14304 : : else
14305 : : return false;
14306 : : }
14307 : :
14308 : : /* DEP is a newly discovered dependency. Append it to current's
14309 : : depset. */
14310 : :
14311 : : void
14312 : 3674839 : depset::hash::add_dependency (depset *dep)
14313 : : {
14314 : 3674839 : gcc_checking_assert (current && !is_key_order ());
14315 : 3674839 : current->deps.safe_push (dep);
14316 : :
14317 : 3674839 : if (dep->is_tu_local ())
14318 : : {
14319 : 257 : current->set_flag_bit<DB_REFS_TU_LOCAL_BIT> ();
14320 : 257 : if (!ignore_tu_local && !is_exposure_of_member_type (current, dep))
14321 : 84 : current->set_flag_bit<DB_EXPOSURE_BIT> ();
14322 : : }
14323 : :
14324 : 3674839 : if (current->get_entity_kind () == EK_USING
14325 : 13528 : && DECL_IMPLICIT_TYPEDEF_P (dep->get_entity ())
14326 : 3678723 : && TREE_CODE (TREE_TYPE (dep->get_entity ())) == ENUMERAL_TYPE)
14327 : : {
14328 : : /* CURRENT is an unwrapped using-decl and DECL is an enum's
14329 : : implicit typedef. Is CURRENT a member of the enum? */
14330 : 3761 : tree c_decl = OVL_FUNCTION (current->get_entity ());
14331 : :
14332 : 3761 : if (TREE_CODE (c_decl) == CONST_DECL
14333 : 7511 : && (current->deps[0]->get_entity ()
14334 : 3750 : == CP_DECL_CONTEXT (dep->get_entity ())))
14335 : : /* Make DECL depend on CURRENT. */
14336 : 3699 : dep->deps.safe_push (current);
14337 : : }
14338 : :
14339 : : /* If two dependencies recursively depend on each other existing within
14340 : : their own merge keys, we must ensure that the first dep we saw while
14341 : : walking is written first in this cluster. See sort_cluster for more
14342 : : details. */
14343 : 3674839 : if (writing_merge_key)
14344 : : {
14345 : 643435 : dep->set_flag_bit<DB_MAYBE_RECURSIVE_BIT> ();
14346 : 643435 : if (!current->is_maybe_recursive ())
14347 : 607471 : current->set_flag_bit<DB_ENTRY_BIT> ();
14348 : : }
14349 : :
14350 : 3674839 : if (dep->is_unreached ())
14351 : : {
14352 : : /* The dependency is reachable now. */
14353 : 281268 : reached_unreached = true;
14354 : 281268 : dep->clear_flag_bit<DB_UNREACHED_BIT> ();
14355 : 281268 : dump (dumper::DEPEND)
14356 : 24 : && dump ("Reaching unreached %s %C:%N", dep->entity_kind_name (),
14357 : 24 : TREE_CODE (dep->get_entity ()), dep->get_entity ());
14358 : : }
14359 : 3674839 : }
14360 : :
14361 : : depset *
14362 : 5523076 : depset::hash::add_dependency (tree decl, entity_kind ek)
14363 : : {
14364 : 5523076 : depset *dep;
14365 : :
14366 : 5523076 : if (is_key_order ())
14367 : : {
14368 : 1804801 : dep = find_dependency (decl);
14369 : 1804801 : if (dep)
14370 : : {
14371 : 889163 : current->deps.safe_push (dep);
14372 : 889163 : dump (dumper::MERGE)
14373 : 471 : && dump ("Key dependency on %s %C:%N found",
14374 : 471 : dep->entity_kind_name (), TREE_CODE (decl), decl);
14375 : : }
14376 : : else
14377 : : {
14378 : : /* It's not a mergeable decl, look for it in the original
14379 : : table. */
14380 : 915638 : dep = chain->find_dependency (decl);
14381 : 915638 : gcc_checking_assert (dep);
14382 : : }
14383 : : }
14384 : : else
14385 : : {
14386 : 3718275 : dep = make_dependency (decl, ek);
14387 : 3718275 : if (dep->get_entity_kind () != EK_REDIRECT)
14388 : 3674839 : add_dependency (dep);
14389 : : }
14390 : :
14391 : 5523076 : return dep;
14392 : : }
14393 : :
14394 : : void
14395 : 471917 : depset::hash::add_namespace_context (depset *dep, tree ns)
14396 : : {
14397 : 471917 : depset *ns_dep = make_dependency (ns, depset::EK_NAMESPACE);
14398 : 471917 : dep->deps.safe_push (ns_dep);
14399 : :
14400 : : /* Mark it as special if imported so we don't walk connect when
14401 : : SCCing. */
14402 : 471917 : if (!dep->is_binding () && ns_dep->is_import ())
14403 : 0 : dep->set_special ();
14404 : 471917 : }
14405 : :
14406 : : struct add_binding_data
14407 : : {
14408 : : tree ns;
14409 : : bitmap partitions;
14410 : : depset *binding;
14411 : : depset::hash *hash;
14412 : : bool met_namespace;
14413 : : };
14414 : :
14415 : : /* Return true if we are, or contain something that is exported. */
14416 : :
14417 : : bool
14418 : 4999167 : depset::hash::add_binding_entity (tree decl, WMB_Flags flags, void *data_)
14419 : : {
14420 : 4999167 : auto data = static_cast <add_binding_data *> (data_);
14421 : 4999167 : decl = strip_using_decl (decl);
14422 : :
14423 : 4999167 : if (!(TREE_CODE (decl) == NAMESPACE_DECL && !DECL_NAMESPACE_ALIAS (decl)))
14424 : : {
14425 : 4992168 : tree inner = decl;
14426 : :
14427 : 4992168 : if (TREE_CODE (inner) == CONST_DECL
14428 : 4974 : && TREE_CODE (DECL_CONTEXT (inner)) == ENUMERAL_TYPE
14429 : : /* A using-decl could make a CONST_DECL purview for a non-purview
14430 : : enumeration. */
14431 : 4997142 : && (!DECL_LANG_SPECIFIC (inner) || !DECL_MODULE_PURVIEW_P (inner)))
14432 : 4938 : inner = TYPE_NAME (DECL_CONTEXT (inner));
14433 : 4987230 : else if (TREE_CODE (inner) == TEMPLATE_DECL)
14434 : 93263 : inner = DECL_TEMPLATE_RESULT (inner);
14435 : :
14436 : 9830737 : if ((!DECL_LANG_SPECIFIC (inner) || !DECL_MODULE_PURVIEW_P (inner))
14437 : 9684880 : && !((flags & WMB_Using) && (flags & WMB_Purview)))
14438 : : /* Ignore entities not within the module purview. */
14439 : : return false;
14440 : :
14441 : 145953 : if (!header_module_p () && data->hash->is_tu_local_entity (decl))
14442 : : /* Ignore TU-local entitites. */
14443 : : return false;
14444 : :
14445 : 145744 : if ((TREE_CODE (decl) == VAR_DECL
14446 : 145744 : || TREE_CODE (decl) == TYPE_DECL)
14447 : 145744 : && DECL_TINFO_P (decl))
14448 : : /* Ignore TINFO things. */
14449 : : return false;
14450 : :
14451 : 145744 : if (TREE_CODE (decl) == VAR_DECL && DECL_NTTP_OBJECT_P (decl))
14452 : : /* Ignore NTTP objects. */
14453 : : return false;
14454 : :
14455 : 145744 : if (deduction_guide_p (decl))
14456 : : {
14457 : : /* Ignore deduction guides, bindings for them will be created within
14458 : : find_dependencies for their class template. But still build a dep
14459 : : for them so that we don't discard them. */
14460 : 1336 : data->hash->make_dependency (decl, EK_FOR_BINDING);
14461 : 1336 : return false;
14462 : : }
14463 : :
14464 : 144408 : if (!(flags & WMB_Using) && CP_DECL_CONTEXT (decl) != data->ns)
14465 : : {
14466 : : /* An unscoped enum constant implicitly brought into the containing
14467 : : namespace. We treat this like a using-decl. */
14468 : 3112 : gcc_checking_assert (TREE_CODE (decl) == CONST_DECL);
14469 : :
14470 : 3112 : flags = WMB_Flags (flags | WMB_Using);
14471 : 3112 : if (DECL_MODULE_EXPORT_P (TYPE_NAME (TREE_TYPE (decl)))
14472 : : /* A using-decl can make an enum constant exported for a
14473 : : non-exported enumeration. */
14474 : 3112 : || (DECL_LANG_SPECIFIC (decl) && DECL_MODULE_EXPORT_P (decl)))
14475 : 2893 : flags = WMB_Flags (flags | WMB_Export);
14476 : : }
14477 : :
14478 : 144408 : if (!data->binding)
14479 : : /* No binding to check. */;
14480 : 28455 : else if (flags & WMB_Using)
14481 : : {
14482 : : /* Look in the binding to see if we already have this
14483 : : using. */
14484 : 19127 : for (unsigned ix = data->binding->deps.length (); --ix;)
14485 : : {
14486 : 15065 : depset *d = data->binding->deps[ix];
14487 : 30130 : if (d->get_entity_kind () == EK_USING
14488 : 15065 : && OVL_FUNCTION (d->get_entity ()) == decl)
14489 : : {
14490 : 0 : if (!(flags & WMB_Hidden))
14491 : 0 : d->clear_hidden_binding ();
14492 : 0 : OVL_PURVIEW_P (d->get_entity ()) = true;
14493 : 0 : if (flags & WMB_Export)
14494 : 0 : OVL_EXPORT_P (d->get_entity ()) = true;
14495 : 0 : return bool (flags & WMB_Export);
14496 : : }
14497 : : }
14498 : : }
14499 : 26424 : else if (flags & WMB_Dups)
14500 : : {
14501 : : /* Look in the binding to see if we already have this decl. */
14502 : 78 : for (unsigned ix = data->binding->deps.length (); --ix;)
14503 : : {
14504 : 39 : depset *d = data->binding->deps[ix];
14505 : 39 : if (d->get_entity () == decl)
14506 : : {
14507 : 33 : if (!(flags & WMB_Hidden))
14508 : 30 : d->clear_hidden_binding ();
14509 : 33 : return false;
14510 : : }
14511 : : }
14512 : : }
14513 : :
14514 : : /* We're adding something. */
14515 : 144375 : if (!data->binding)
14516 : : {
14517 : 115953 : data->binding = make_binding (data->ns, DECL_NAME (decl));
14518 : 115953 : data->hash->add_namespace_context (data->binding, data->ns);
14519 : :
14520 : 115953 : depset **slot = data->hash->binding_slot (data->ns,
14521 : 115953 : DECL_NAME (decl), true);
14522 : 115953 : gcc_checking_assert (!*slot);
14523 : 115953 : *slot = data->binding;
14524 : : }
14525 : :
14526 : : /* Make sure nobody left a tree visited lying about. */
14527 : 144375 : gcc_checking_assert (!TREE_VISITED (decl));
14528 : :
14529 : 144375 : if (flags & WMB_Using)
14530 : : {
14531 : 13528 : decl = ovl_make (decl, NULL_TREE);
14532 : 13528 : OVL_USING_P (decl) = true;
14533 : 13528 : OVL_PURVIEW_P (decl) = true;
14534 : 13528 : if (flags & WMB_Export)
14535 : 12674 : OVL_EXPORT_P (decl) = true;
14536 : : }
14537 : :
14538 : 144375 : depset *dep = data->hash->make_dependency
14539 : 275222 : (decl, flags & WMB_Using ? EK_USING : EK_FOR_BINDING);
14540 : 144375 : if (flags & WMB_Hidden)
14541 : 4238 : dep->set_hidden_binding ();
14542 : 144375 : data->binding->deps.safe_push (dep);
14543 : : /* Binding and contents are mutually dependent. */
14544 : 144375 : dep->deps.safe_push (data->binding);
14545 : :
14546 : 144375 : return (flags & WMB_Using
14547 : 144375 : ? flags & WMB_Export : DECL_MODULE_EXPORT_P (decl));
14548 : : }
14549 : 6999 : else if (!data->met_namespace)
14550 : : {
14551 : : /* Namespace, walk exactly once. */
14552 : 6990 : data->met_namespace = true;
14553 : 6990 : if (data->hash->add_namespace_entities (decl, data->partitions))
14554 : : {
14555 : : /* It contains an exported thing, so it is exported. */
14556 : 1325 : gcc_checking_assert (DECL_MODULE_PURVIEW_P (decl));
14557 : 1325 : gcc_checking_assert (TREE_PUBLIC (decl) || header_module_p ());
14558 : 1325 : DECL_MODULE_EXPORT_P (decl) = true;
14559 : : }
14560 : :
14561 : 6990 : if (DECL_MODULE_PURVIEW_P (decl))
14562 : : {
14563 : 1615 : data->hash->make_dependency (decl, depset::EK_NAMESPACE);
14564 : :
14565 : 1615 : return DECL_MODULE_EXPORT_P (decl);
14566 : : }
14567 : : }
14568 : :
14569 : : return false;
14570 : : }
14571 : :
14572 : : /* Recursively find all the namespace bindings of NS. Add a depset
14573 : : for every binding that contains an export or module-linkage entity.
14574 : : Add a defining depset for every such decl that we need to write a
14575 : : definition. Such defining depsets depend on the binding depset.
14576 : : Returns true if we contain something exported. */
14577 : :
14578 : : bool
14579 : 9416 : depset::hash::add_namespace_entities (tree ns, bitmap partitions)
14580 : : {
14581 : 10376 : dump () && dump ("Looking for writables in %N", ns);
14582 : 9416 : dump.indent ();
14583 : :
14584 : 9416 : unsigned count = 0;
14585 : 9416 : add_binding_data data;
14586 : 9416 : data.ns = ns;
14587 : 9416 : data.partitions = partitions;
14588 : 9416 : data.hash = this;
14589 : :
14590 : 9416 : hash_table<named_decl_hash>::iterator end
14591 : 9416 : (DECL_NAMESPACE_BINDINGS (ns)->end ());
14592 : 6550367 : for (hash_table<named_decl_hash>::iterator iter
14593 : 13100734 : (DECL_NAMESPACE_BINDINGS (ns)->begin ()); iter != end; ++iter)
14594 : : {
14595 : 6540951 : data.binding = nullptr;
14596 : 6540951 : data.met_namespace = false;
14597 : 6540951 : if (walk_module_binding (*iter, partitions, add_binding_entity, &data))
14598 : 108612 : count++;
14599 : : }
14600 : :
14601 : 9416 : if (count)
14602 : 3345 : dump () && dump ("Found %u entries", count);
14603 : 9416 : dump.outdent ();
14604 : :
14605 : 9416 : return count != 0;
14606 : : }
14607 : :
14608 : : void
14609 : 194 : depset::hash::add_partial_entities (vec<tree, va_gc> *partial_classes)
14610 : : {
14611 : 17064 : for (unsigned ix = 0; ix != partial_classes->length (); ix++)
14612 : : {
14613 : 16870 : tree inner = (*partial_classes)[ix];
14614 : :
14615 : 16870 : depset *dep = make_dependency (inner, depset::EK_DECL);
14616 : :
14617 : 16870 : if (dep->get_entity_kind () == depset::EK_REDIRECT)
14618 : : {
14619 : 16870 : dep = dep->deps[0];
14620 : : /* We should have recorded the template as a partial
14621 : : specialization. */
14622 : 16870 : gcc_checking_assert (dep->get_entity_kind ()
14623 : : == depset::EK_PARTIAL);
14624 : :
14625 : : /* Only emit GM entities if reached. */
14626 : 16870 : if (!DECL_LANG_SPECIFIC (inner)
14627 : 28537 : || !DECL_MODULE_PURVIEW_P (inner))
14628 : 5245 : dep->set_flag_bit<DB_UNREACHED_BIT> ();
14629 : : }
14630 : : else
14631 : : {
14632 : : /* It was an explicit specialization, not a partial one.
14633 : : We should have already added this. */
14634 : 0 : gcc_checking_assert (dep->get_entity_kind ()
14635 : : == depset::EK_SPECIALIZATION);
14636 : 0 : gcc_checking_assert (dep->is_special ());
14637 : : }
14638 : : }
14639 : 194 : }
14640 : :
14641 : : /* Add the members of imported classes that we defined in this TU.
14642 : : This will also include lazily created implicit member function
14643 : : declarations. (All others will be definitions.) */
14644 : :
14645 : : void
14646 : 12 : depset::hash::add_class_entities (vec<tree, va_gc> *class_members)
14647 : : {
14648 : 24 : for (unsigned ix = 0; ix != class_members->length (); ix++)
14649 : : {
14650 : 12 : tree defn = (*class_members)[ix];
14651 : 12 : depset *dep = make_dependency (defn, EK_INNER_DECL);
14652 : :
14653 : 12 : if (dep->get_entity_kind () == EK_REDIRECT)
14654 : 0 : dep = dep->deps[0];
14655 : :
14656 : : /* Only non-instantiations need marking as pendings. */
14657 : 24 : if (dep->get_entity_kind () == EK_DECL)
14658 : 12 : dep->set_flag_bit <DB_IS_PENDING_BIT> ();
14659 : : }
14660 : 12 : }
14661 : :
14662 : : /* We add the partial & explicit specializations, and the explicit
14663 : : instantiations. */
14664 : :
14665 : : static void
14666 : 569503 : specialization_add (bool decl_p, spec_entry *entry, void *data_)
14667 : : {
14668 : 569503 : vec<spec_entry *> *data = reinterpret_cast <vec<spec_entry *> *> (data_);
14669 : :
14670 : 569503 : if (!decl_p)
14671 : : {
14672 : : /* We exclusively use decls to locate things. Make sure there's
14673 : : no mismatch between the two specialization tables we keep.
14674 : : pt.cc optimizes instantiation lookup using a complicated
14675 : : heuristic. We don't attempt to replicate that algorithm, but
14676 : : observe its behaviour and reproduce it upon read back. */
14677 : :
14678 : 183654 : gcc_checking_assert (TREE_CODE (entry->spec) == ENUMERAL_TYPE
14679 : : || DECL_CLASS_TEMPLATE_P (entry->tmpl));
14680 : :
14681 : 183654 : gcc_checking_assert (!match_mergeable_specialization (true, entry));
14682 : : }
14683 : 385849 : else if (VAR_OR_FUNCTION_DECL_P (entry->spec))
14684 : 192808 : gcc_checking_assert (!DECL_LOCAL_DECL_P (entry->spec));
14685 : :
14686 : 569503 : data->safe_push (entry);
14687 : 569503 : }
14688 : :
14689 : : /* Arbitrary stable comparison. */
14690 : :
14691 : : static int
14692 : 33519757 : specialization_cmp (const void *a_, const void *b_)
14693 : : {
14694 : 33519757 : const spec_entry *ea = *reinterpret_cast<const spec_entry *const *> (a_);
14695 : 33519757 : const spec_entry *eb = *reinterpret_cast<const spec_entry *const *> (b_);
14696 : :
14697 : 33519757 : if (ea == eb)
14698 : : return 0;
14699 : :
14700 : 33519757 : tree a = ea->spec;
14701 : 33519757 : tree b = eb->spec;
14702 : 33519757 : if (TYPE_P (a))
14703 : : {
14704 : 10277319 : a = TYPE_NAME (a);
14705 : 10277319 : b = TYPE_NAME (b);
14706 : : }
14707 : :
14708 : 33519757 : if (a == b)
14709 : : /* This can happen with friend specializations. Just order by
14710 : : entry address. See note in depset_cmp. */
14711 : 160 : return ea < eb ? -1 : +1;
14712 : :
14713 : 33519639 : return DECL_UID (a) < DECL_UID (b) ? -1 : +1;
14714 : : }
14715 : :
14716 : : /* We add all kinds of specialializations. Implicit specializations
14717 : : should only streamed and walked if they are reachable from
14718 : : elsewhere. Hence the UNREACHED flag. This is making the
14719 : : assumption that it is cheaper to reinstantiate them on demand
14720 : : elsewhere, rather than stream them in when we instantiate their
14721 : : general template. Also, if we do stream them, we can only do that
14722 : : if they are not internal (which they can become if they themselves
14723 : : touch an internal entity?). */
14724 : :
14725 : : void
14726 : 4852 : depset::hash::add_specializations (bool decl_p)
14727 : : {
14728 : 4852 : vec<spec_entry *> data;
14729 : 4852 : data.create (100);
14730 : 4852 : walk_specializations (decl_p, specialization_add, &data);
14731 : 4852 : data.qsort (specialization_cmp);
14732 : 574355 : while (data.length ())
14733 : : {
14734 : 569503 : spec_entry *entry = data.pop ();
14735 : 569503 : tree spec = entry->spec;
14736 : 569503 : int use_tpl = 0;
14737 : 569503 : bool is_friend = false;
14738 : :
14739 : 569503 : if (decl_p && DECL_UNINSTANTIATED_TEMPLATE_FRIEND_P (entry->tmpl))
14740 : : /* A friend of a template. This is keyed to the
14741 : : instantiation. */
14742 : : is_friend = true;
14743 : :
14744 : 569503 : if (decl_p)
14745 : : {
14746 : 385849 : if (tree ti = DECL_TEMPLATE_INFO (spec))
14747 : : {
14748 : 385849 : tree tmpl = TI_TEMPLATE (ti);
14749 : :
14750 : 385849 : use_tpl = DECL_USE_TEMPLATE (spec);
14751 : 385849 : if (spec == DECL_TEMPLATE_RESULT (tmpl))
14752 : : {
14753 : 2792 : spec = tmpl;
14754 : 2792 : gcc_checking_assert (DECL_USE_TEMPLATE (spec) == use_tpl);
14755 : : }
14756 : 383057 : else if (is_friend)
14757 : : {
14758 : 2924 : if (TI_TEMPLATE (ti) != entry->tmpl
14759 : 2924 : || !template_args_equal (TI_ARGS (ti), entry->tmpl))
14760 : 2924 : goto template_friend;
14761 : : }
14762 : : }
14763 : : else
14764 : : {
14765 : 0 : template_friend:;
14766 : 2924 : gcc_checking_assert (is_friend);
14767 : : /* This is a friend of a template class, but not the one
14768 : : that generated entry->spec itself (i.e. it's an
14769 : : equivalent clone). We do not need to record
14770 : : this. */
14771 : 2924 : continue;
14772 : : }
14773 : : }
14774 : : else
14775 : : {
14776 : 183654 : if (TREE_CODE (spec) == ENUMERAL_TYPE)
14777 : : {
14778 : 1051 : tree ctx = DECL_CONTEXT (TYPE_NAME (spec));
14779 : :
14780 : 1051 : if (TYPE_P (ctx))
14781 : 1045 : use_tpl = CLASSTYPE_USE_TEMPLATE (ctx);
14782 : : else
14783 : 6 : use_tpl = DECL_USE_TEMPLATE (ctx);
14784 : : }
14785 : : else
14786 : 182603 : use_tpl = CLASSTYPE_USE_TEMPLATE (spec);
14787 : :
14788 : 183654 : tree ti = TYPE_TEMPLATE_INFO (spec);
14789 : 183654 : tree tmpl = TI_TEMPLATE (ti);
14790 : :
14791 : 183654 : spec = TYPE_NAME (spec);
14792 : 183654 : if (spec == DECL_TEMPLATE_RESULT (tmpl))
14793 : : {
14794 : 775 : spec = tmpl;
14795 : 775 : use_tpl = DECL_USE_TEMPLATE (spec);
14796 : : }
14797 : : }
14798 : :
14799 : 566579 : bool needs_reaching = false;
14800 : 566579 : if (use_tpl == 1)
14801 : : /* Implicit instantiations only walked if we reach them. */
14802 : : needs_reaching = true;
14803 : 59252 : else if (!DECL_LANG_SPECIFIC (STRIP_TEMPLATE (spec))
14804 : 107887 : || !DECL_MODULE_PURVIEW_P (STRIP_TEMPLATE (spec)))
14805 : : /* Likewise, GMF explicit or partial specializations. */
14806 : : needs_reaching = true;
14807 : :
14808 : : #if false && CHECKING_P
14809 : : /* The instantiation isn't always on
14810 : : DECL_TEMPLATE_INSTANTIATIONS, */
14811 : : // FIXME: we probably need to remember this information?
14812 : : /* Verify the specialization is on the
14813 : : DECL_TEMPLATE_INSTANTIATIONS of the template. */
14814 : : for (tree cons = DECL_TEMPLATE_INSTANTIATIONS (entry->tmpl);
14815 : : cons; cons = TREE_CHAIN (cons))
14816 : : if (TREE_VALUE (cons) == entry->spec)
14817 : : {
14818 : : gcc_assert (entry->args == TREE_PURPOSE (cons));
14819 : : goto have_spec;
14820 : : }
14821 : : gcc_unreachable ();
14822 : : have_spec:;
14823 : : #endif
14824 : :
14825 : : /* Make sure nobody left a tree visited lying about. */
14826 : 566579 : gcc_checking_assert (!TREE_VISITED (spec));
14827 : 566579 : depset *dep = make_dependency (spec, depset::EK_SPECIALIZATION);
14828 : 566579 : if (dep->is_special ())
14829 : 0 : gcc_unreachable ();
14830 : : else
14831 : : {
14832 : 566579 : if (dep->get_entity_kind () == depset::EK_REDIRECT)
14833 : 18574 : dep = dep->deps[0];
14834 : 548005 : else if (dep->get_entity_kind () == depset::EK_SPECIALIZATION)
14835 : : {
14836 : 548005 : dep->set_special ();
14837 : 548005 : dep->deps.safe_push (reinterpret_cast<depset *> (entry));
14838 : 548005 : if (!decl_p)
14839 : 167477 : dep->set_flag_bit<DB_TYPE_SPEC_BIT> ();
14840 : : }
14841 : :
14842 : 566579 : if (needs_reaching)
14843 : 525260 : dep->set_flag_bit<DB_UNREACHED_BIT> ();
14844 : 566579 : if (is_friend)
14845 : 0 : dep->set_flag_bit<DB_FRIEND_SPEC_BIT> ();
14846 : : }
14847 : : }
14848 : 4852 : data.release ();
14849 : 4852 : }
14850 : :
14851 : : /* Add a depset into the mergeable hash. */
14852 : :
14853 : : void
14854 : 791089 : depset::hash::add_mergeable (depset *mergeable)
14855 : : {
14856 : 791089 : gcc_checking_assert (is_key_order ());
14857 : 791089 : entity_kind ek = mergeable->get_entity_kind ();
14858 : 791089 : tree decl = mergeable->get_entity ();
14859 : 791089 : gcc_checking_assert (ek < EK_DIRECT_HWM);
14860 : :
14861 : 791089 : depset **slot = entity_slot (decl, true);
14862 : 791089 : gcc_checking_assert (!*slot);
14863 : 791089 : depset *dep = make_entity (decl, ek);
14864 : 791089 : *slot = dep;
14865 : :
14866 : 791089 : worklist.safe_push (dep);
14867 : :
14868 : : /* So we can locate the mergeable depset this depset refers to,
14869 : : mark the first dep. */
14870 : 791089 : dep->set_special ();
14871 : 791089 : dep->deps.safe_push (mergeable);
14872 : 791089 : }
14873 : :
14874 : : /* Find the innermost-namespace scope of DECL, and that
14875 : : namespace-scope decl. */
14876 : :
14877 : : tree
14878 : 25034111 : find_pending_key (tree decl, tree *decl_p = nullptr)
14879 : : {
14880 : 25034111 : tree ns = decl;
14881 : 29900666 : do
14882 : : {
14883 : 29900666 : decl = ns;
14884 : 29900666 : ns = CP_DECL_CONTEXT (ns);
14885 : 29900666 : if (TYPE_P (ns))
14886 : 3074794 : ns = TYPE_NAME (ns);
14887 : : }
14888 : 29900666 : while (TREE_CODE (ns) != NAMESPACE_DECL);
14889 : :
14890 : 25034111 : if (decl_p)
14891 : 24680690 : *decl_p = decl;
14892 : :
14893 : 25034111 : return ns;
14894 : : }
14895 : :
14896 : : /* Creates bindings and dependencies for all deduction guides of
14897 : : the given class template DECL as needed. */
14898 : :
14899 : : void
14900 : 40959 : depset::hash::add_deduction_guides (tree decl)
14901 : : {
14902 : : /* Alias templates never have deduction guides. */
14903 : 40959 : if (DECL_ALIAS_TEMPLATE_P (decl))
14904 : 40355 : return;
14905 : :
14906 : : /* We don't need to do anything for class-scope deduction guides,
14907 : : as they will be added as members anyway. */
14908 : 40959 : if (!DECL_NAMESPACE_SCOPE_P (decl))
14909 : : return;
14910 : :
14911 : 33251 : tree ns = CP_DECL_CONTEXT (decl);
14912 : 33251 : tree name = dguide_name (decl);
14913 : :
14914 : : /* We always add all deduction guides with a given name at once,
14915 : : so if there's already a binding there's nothing to do. */
14916 : 33251 : if (find_binding (ns, name))
14917 : : return;
14918 : :
14919 : 31676 : tree guides = lookup_qualified_name (ns, name, LOOK_want::NORMAL,
14920 : : /*complain=*/false);
14921 : 31676 : if (guides == error_mark_node)
14922 : : return;
14923 : :
14924 : 604 : depset *binding = nullptr;
14925 : 2672 : for (tree t : lkp_range (guides))
14926 : : {
14927 : 1464 : gcc_checking_assert (!TREE_VISITED (t));
14928 : 1464 : depset *dep = make_dependency (t, EK_FOR_BINDING);
14929 : :
14930 : : /* We don't want to create bindings for imported deduction guides, as
14931 : : this would potentially cause name lookup to return duplicates. */
14932 : 1464 : if (dep->is_import ())
14933 : 6 : continue;
14934 : :
14935 : 1458 : if (!binding)
14936 : : {
14937 : : /* We have bindings to add. */
14938 : 598 : binding = make_binding (ns, name);
14939 : 598 : add_namespace_context (binding, ns);
14940 : :
14941 : 598 : depset **slot = binding_slot (ns, name, /*insert=*/true);
14942 : 598 : *slot = binding;
14943 : : }
14944 : :
14945 : 1458 : binding->deps.safe_push (dep);
14946 : 1458 : dep->deps.safe_push (binding);
14947 : : }
14948 : : }
14949 : :
14950 : : /* Iteratively find dependencies. During the walk we may find more
14951 : : entries on the same binding that need walking. */
14952 : :
14953 : : void
14954 : 226424 : depset::hash::find_dependencies (module_state *module)
14955 : : {
14956 : 226424 : trees_out walker (NULL, module, *this);
14957 : 226424 : vec<depset *> unreached;
14958 : 452848 : unreached.create (worklist.length ());
14959 : :
14960 : 999 : for (;;)
14961 : : {
14962 : 227423 : reached_unreached = false;
14963 : 3175049 : while (worklist.length ())
14964 : : {
14965 : 2947626 : depset *item = worklist.pop ();
14966 : :
14967 : 2947626 : gcc_checking_assert (!item->is_binding ());
14968 : 2947626 : if (item->is_unreached ())
14969 : 1347219 : unreached.quick_push (item);
14970 : : else
14971 : : {
14972 : 1600407 : current = item;
14973 : 1600407 : tree decl = current->get_entity ();
14974 : 1600407 : dump (is_key_order () ? dumper::MERGE : dumper::DEPEND)
14975 : 1601361 : && dump ("Dependencies of %s %C:%N",
14976 : 954 : is_key_order () ? "key-order"
14977 : 954 : : current->entity_kind_name (), TREE_CODE (decl), decl);
14978 : 1600407 : dump.indent ();
14979 : 1600407 : walker.begin ();
14980 : 1600407 : if (current->get_entity_kind () == EK_USING)
14981 : 13528 : walker.tree_node (OVL_FUNCTION (decl));
14982 : 1586879 : else if (TREE_VISITED (decl))
14983 : : /* A global tree. */;
14984 : 1584692 : else if (item->get_entity_kind () == EK_NAMESPACE)
14985 : : {
14986 : 1945 : module->note_location (DECL_SOURCE_LOCATION (decl));
14987 : 1945 : add_namespace_context (current, CP_DECL_CONTEXT (decl));
14988 : : }
14989 : : else
14990 : : {
14991 : 1582747 : walker.mark_declaration (decl, current->has_defn ());
14992 : :
14993 : 1582747 : if (!is_key_order ()
14994 : 1582747 : && item->is_pending_entity ())
14995 : : {
14996 : 353421 : tree ns = find_pending_key (decl, nullptr);
14997 : 353421 : add_namespace_context (item, ns);
14998 : : }
14999 : :
15000 : 1582747 : walker.decl_value (decl, current);
15001 : 1582747 : if (current->has_defn ())
15002 : 307068 : walker.write_definition (decl, current->refs_tu_local ());
15003 : : }
15004 : 1600407 : walker.end ();
15005 : :
15006 : : /* If we see either a class template or a deduction guide, make
15007 : : sure to add all visible deduction guides. We need to check
15008 : : both in case they have been added in separate modules, or
15009 : : one is in the GMF and would have otherwise been discarded. */
15010 : 1600407 : if (!is_key_order ()
15011 : 1600407 : && DECL_CLASS_TEMPLATE_P (decl))
15012 : 39495 : add_deduction_guides (decl);
15013 : 1600407 : if (!is_key_order ()
15014 : 1600407 : && deduction_guide_p (decl))
15015 : 1464 : add_deduction_guides (TYPE_NAME (TREE_TYPE (TREE_TYPE (decl))));
15016 : :
15017 : 1600407 : if (!is_key_order ()
15018 : 809318 : && TREE_CODE (decl) == TEMPLATE_DECL
15019 : 1880485 : && !DECL_UNINSTANTIATED_TEMPLATE_FRIEND_P (decl))
15020 : : {
15021 : : /* Mark all the explicit & partial specializations as
15022 : : reachable. We search both specialization lists as some
15023 : : constrained partial specializations for class types are
15024 : : only found in DECL_TEMPLATE_SPECIALIZATIONS. */
15025 : 756605 : auto mark_reached = [this](tree spec)
15026 : : {
15027 : 481519 : if (TYPE_P (spec))
15028 : 148665 : spec = TYPE_NAME (spec);
15029 : 481519 : int use_tpl;
15030 : 481519 : node_template_info (spec, use_tpl);
15031 : 481519 : if (use_tpl & 2)
15032 : : {
15033 : 62326 : depset *spec_dep = find_dependency (spec);
15034 : 62326 : if (spec_dep->get_entity_kind () == EK_REDIRECT)
15035 : 14556 : spec_dep = spec_dep->deps[0];
15036 : 62326 : if (spec_dep->is_unreached ())
15037 : : {
15038 : 5368 : reached_unreached = true;
15039 : 5368 : spec_dep->clear_flag_bit<DB_UNREACHED_BIT> ();
15040 : 5368 : dump (dumper::DEPEND)
15041 : 0 : && dump ("Reaching unreached specialization"
15042 : 0 : " %C:%N", TREE_CODE (spec), spec);
15043 : : }
15044 : : }
15045 : 756605 : };
15046 : :
15047 : 275086 : for (tree cons = DECL_TEMPLATE_INSTANTIATIONS (decl);
15048 : 741427 : cons; cons = TREE_CHAIN (cons))
15049 : 466341 : mark_reached (TREE_VALUE (cons));
15050 : 275086 : for (tree cons = DECL_TEMPLATE_SPECIALIZATIONS (decl);
15051 : 290264 : cons; cons = TREE_CHAIN (cons))
15052 : 15178 : mark_reached (TREE_VALUE (cons));
15053 : : }
15054 : :
15055 : 1600407 : dump.outdent ();
15056 : 1600407 : current = NULL;
15057 : : }
15058 : : }
15059 : :
15060 : 227423 : if (!reached_unreached)
15061 : : break;
15062 : :
15063 : : /* It's possible the we reached the unreached before we
15064 : : processed it in the above loop, so we'll be doing this an
15065 : : extra time. However, to avoid that we have to do some
15066 : : bit shuffling that also involves a scan of the list.
15067 : : Swings & roundabouts I guess. */
15068 : 999 : std::swap (worklist, unreached);
15069 : : }
15070 : :
15071 : 226424 : unreached.release ();
15072 : 226424 : }
15073 : :
15074 : : /* Compare two entries of a single binding. TYPE_DECL before
15075 : : non-exported before exported. */
15076 : :
15077 : : static int
15078 : 412866 : binding_cmp (const void *a_, const void *b_)
15079 : : {
15080 : 412866 : depset *a = *(depset *const *)a_;
15081 : 412866 : depset *b = *(depset *const *)b_;
15082 : :
15083 : 412866 : tree a_ent = a->get_entity ();
15084 : 412866 : tree b_ent = b->get_entity ();
15085 : 412866 : gcc_checking_assert (a_ent != b_ent
15086 : : && !a->is_binding ()
15087 : : && !b->is_binding ());
15088 : :
15089 : : /* Implicit typedefs come first. */
15090 : 412866 : bool a_implicit = DECL_IMPLICIT_TYPEDEF_P (a_ent);
15091 : 412866 : bool b_implicit = DECL_IMPLICIT_TYPEDEF_P (b_ent);
15092 : 412764 : if (a_implicit || b_implicit)
15093 : : {
15094 : : /* A binding with two implicit type decls? That's unpossible! */
15095 : 204 : gcc_checking_assert (!(a_implicit && b_implicit));
15096 : 306 : return a_implicit ? -1 : +1; /* Implicit first. */
15097 : : }
15098 : :
15099 : : /* Hidden before non-hidden. */
15100 : 412662 : bool a_hidden = a->is_hidden ();
15101 : 412662 : bool b_hidden = b->is_hidden ();
15102 : 412662 : if (a_hidden != b_hidden)
15103 : 41433 : return a_hidden ? -1 : +1;
15104 : :
15105 : 385482 : bool a_using = a->get_entity_kind () == depset::EK_USING;
15106 : 385482 : bool a_export;
15107 : 385482 : if (a_using)
15108 : : {
15109 : 27661 : a_export = OVL_EXPORT_P (a_ent);
15110 : 27661 : a_ent = OVL_FUNCTION (a_ent);
15111 : : }
15112 : 357821 : else if (TREE_CODE (a_ent) == CONST_DECL
15113 : 0 : && DECL_LANG_SPECIFIC (a_ent)
15114 : 357821 : && DECL_MODULE_EXPORT_P (a_ent))
15115 : : a_export = true;
15116 : : else
15117 : 357821 : a_export = DECL_MODULE_EXPORT_P (TREE_CODE (a_ent) == CONST_DECL
15118 : : ? TYPE_NAME (TREE_TYPE (a_ent))
15119 : : : STRIP_TEMPLATE (a_ent));
15120 : :
15121 : 385482 : bool b_using = b->get_entity_kind () == depset::EK_USING;
15122 : 385482 : bool b_export;
15123 : 385482 : if (b_using)
15124 : : {
15125 : 24079 : b_export = OVL_EXPORT_P (b_ent);
15126 : 24079 : b_ent = OVL_FUNCTION (b_ent);
15127 : : }
15128 : 361403 : else if (TREE_CODE (b_ent) == CONST_DECL
15129 : 0 : && DECL_LANG_SPECIFIC (b_ent)
15130 : 361403 : && DECL_MODULE_EXPORT_P (b_ent))
15131 : : b_export = true;
15132 : : else
15133 : 361403 : b_export = DECL_MODULE_EXPORT_P (TREE_CODE (b_ent) == CONST_DECL
15134 : : ? TYPE_NAME (TREE_TYPE (b_ent))
15135 : : : STRIP_TEMPLATE (b_ent));
15136 : :
15137 : : /* Non-exports before exports. */
15138 : 385482 : if (a_export != b_export)
15139 : 72 : return a_export ? +1 : -1;
15140 : :
15141 : : /* At this point we don't care, but want a stable sort. */
15142 : :
15143 : 385437 : if (a_using != b_using)
15144 : : /* using first. */
15145 : 19920 : return a_using? -1 : +1;
15146 : :
15147 : 370963 : return DECL_UID (a_ent) < DECL_UID (b_ent) ? -1 : +1;
15148 : : }
15149 : :
15150 : : /* True iff TMPL has an explicit instantiation definition.
15151 : :
15152 : : This is local to module.cc because register_specialization skips adding most
15153 : : instantiations unless module_maybe_has_cmi_p. */
15154 : :
15155 : : static bool
15156 : 54 : template_has_explicit_inst (tree tmpl)
15157 : : {
15158 : 60 : for (tree t = DECL_TEMPLATE_INSTANTIATIONS (tmpl); t; t = TREE_CHAIN (t))
15159 : : {
15160 : 12 : tree spec = TREE_VALUE (t);
15161 : 12 : if (DECL_EXPLICIT_INSTANTIATION (spec)
15162 : 12 : && !DECL_REALLY_EXTERN (spec))
15163 : : return true;
15164 : : }
15165 : : return false;
15166 : : }
15167 : :
15168 : : /* Sort the bindings, issue errors about bad internal refs. */
15169 : :
15170 : : bool
15171 : 2426 : depset::hash::finalize_dependencies ()
15172 : : {
15173 : 2426 : bool ok = true;
15174 : 2371576 : for (depset *dep : *this)
15175 : : {
15176 : 1184575 : if (dep->is_binding ())
15177 : : {
15178 : : /* Keep the containing namespace dep first. */
15179 : 116551 : gcc_checking_assert (dep->deps.length () > 1
15180 : : && (dep->deps[0]->get_entity_kind ()
15181 : : == EK_NAMESPACE)
15182 : : && (dep->deps[0]->get_entity ()
15183 : : == dep->get_entity ()));
15184 : 116551 : if (dep->deps.length () > 2)
15185 : 8416 : gcc_qsort (&dep->deps[1], dep->deps.length () - 1,
15186 : : sizeof (dep->deps[1]), binding_cmp);
15187 : :
15188 : : /* Bindings shouldn't refer to imported entities. */
15189 : 116551 : if (CHECKING_P)
15190 : 612037 : for (depset *entity : dep->deps)
15191 : 262384 : gcc_checking_assert (!entity->is_import ());
15192 : : }
15193 : 1068120 : else if (dep->is_exposure () && !dep->is_tu_local ())
15194 : : {
15195 : 96 : ok = false;
15196 : 96 : bool explained = false;
15197 : 96 : tree decl = dep->get_entity ();
15198 : :
15199 : 390 : for (depset *rdep : dep->deps)
15200 : 183 : if (!rdep->is_binding ()
15201 : 95 : && rdep->is_tu_local ()
15202 : 264 : && !is_exposure_of_member_type (dep, rdep))
15203 : : {
15204 : : // FIXME:QOI Better location information? We're
15205 : : // losing, so it doesn't matter about efficiency
15206 : 81 : tree exposed = rdep->get_entity ();
15207 : 81 : auto_diagnostic_group d;
15208 : 81 : error_at (DECL_SOURCE_LOCATION (decl),
15209 : : "%qD exposes TU-local entity %qD", decl, exposed);
15210 : 81 : bool informed = is_tu_local_entity (exposed, /*explain=*/true);
15211 : 81 : gcc_checking_assert (informed);
15212 : 81 : explained = true;
15213 : 81 : break;
15214 : 81 : }
15215 : :
15216 : 81 : if (!explained
15217 : 15 : && VAR_P (decl)
15218 : 15 : && (DECL_DECLARED_CONSTEXPR_P (decl)
15219 : 6 : || DECL_INLINE_VAR_P (decl)))
15220 : : {
15221 : 15 : auto_diagnostic_group d;
15222 : 15 : if (DECL_DECLARED_CONSTEXPR_P (decl))
15223 : 9 : error_at (DECL_SOURCE_LOCATION (decl),
15224 : : "%qD is declared %<constexpr%> and is initialized to "
15225 : : "a TU-local value", decl);
15226 : : else
15227 : : {
15228 : : /* This can only occur with references. */
15229 : 6 : gcc_checking_assert (TYPE_REF_P (TREE_TYPE (decl)));
15230 : 6 : error_at (DECL_SOURCE_LOCATION (decl),
15231 : : "%qD is a reference declared %<inline%> and is "
15232 : : "constant-initialized to a TU-local value", decl);
15233 : : }
15234 : 15 : bool informed = is_tu_local_value (decl, DECL_INITIAL (decl),
15235 : : /*explain=*/true);
15236 : 15 : gcc_checking_assert (informed);
15237 : 15 : explained = true;
15238 : 15 : }
15239 : :
15240 : : /* We should have emitted an error above. */
15241 : 96 : gcc_checking_assert (explained);
15242 : : }
15243 : 1067928 : else if (warn_template_names_tu_local
15244 : 1149045 : && dep->refs_tu_local () && !dep->is_tu_local ())
15245 : : {
15246 : 63 : tree decl = dep->get_entity ();
15247 : :
15248 : : /* Friend decls in a class body are ignored, but this is harmless:
15249 : : it should not impact any consumers. */
15250 : 63 : if (RECORD_OR_UNION_TYPE_P (TREE_TYPE (decl)))
15251 : 9 : continue;
15252 : :
15253 : : /* We should now only be warning about templates. */
15254 : 54 : gcc_checking_assert
15255 : : (TREE_CODE (decl) == TEMPLATE_DECL
15256 : : && VAR_OR_FUNCTION_DECL_P (DECL_TEMPLATE_RESULT (decl)));
15257 : :
15258 : : /* Don't warn if we've seen any explicit instantiation definitions,
15259 : : the intent might be for importers to only use those. */
15260 : 54 : if (template_has_explicit_inst (decl))
15261 : 6 : continue;
15262 : :
15263 : 195 : for (depset *rdep : dep->deps)
15264 : 153 : if (!rdep->is_binding () && rdep->is_tu_local ())
15265 : : {
15266 : 48 : tree ref = rdep->get_entity ();
15267 : 48 : auto_diagnostic_group d;
15268 : 48 : if (warning_at (DECL_SOURCE_LOCATION (decl),
15269 : 48 : OPT_Wtemplate_names_tu_local,
15270 : : "%qD refers to TU-local entity %qD and cannot "
15271 : : "be instantiated in other TUs", decl, ref))
15272 : 48 : is_tu_local_entity (ref, /*explain=*/true);
15273 : 48 : break;
15274 : 48 : }
15275 : : }
15276 : : }
15277 : :
15278 : 2426 : return ok;
15279 : : }
15280 : :
15281 : : /* Core of TARJAN's algorithm to find Strongly Connected Components
15282 : : within a graph. See https://en.wikipedia.org/wiki/
15283 : : Tarjan%27s_strongly_connected_components_algorithm for details.
15284 : :
15285 : : We use depset::section as lowlink. Completed nodes have
15286 : : depset::cluster containing the cluster number, with the top
15287 : : bit set.
15288 : :
15289 : : A useful property is that the output vector is a reverse
15290 : : topological sort of the resulting DAG. In our case that means
15291 : : dependent SCCs are found before their dependers. We make use of
15292 : : that property. */
15293 : :
15294 : : void
15295 : 1716142 : depset::tarjan::connect (depset *v)
15296 : : {
15297 : 1716142 : gcc_checking_assert (v->is_binding ()
15298 : : || !(v->is_tu_local ()
15299 : : || v->is_unreached ()
15300 : : || v->is_import ()));
15301 : :
15302 : 1716142 : v->cluster = v->section = ++index;
15303 : 1716142 : stack.safe_push (v);
15304 : :
15305 : : /* Walk all our dependencies, ignore a first marked slot */
15306 : 14085048 : for (unsigned ix = v->is_special (); ix != v->deps.length (); ix++)
15307 : : {
15308 : 5330046 : depset *dep = v->deps[ix];
15309 : :
15310 : 5330046 : if (dep->is_binding ()
15311 : 15698129 : || !(dep->is_import () || dep->is_tu_local ()))
15312 : : {
15313 : 5329154 : unsigned lwm = dep->cluster;
15314 : :
15315 : 5329154 : if (!dep->cluster)
15316 : : {
15317 : : /* A new node. Connect it. */
15318 : 945111 : connect (dep);
15319 : 945111 : lwm = dep->section;
15320 : : }
15321 : :
15322 : 5329154 : if (dep->section && v->section > lwm)
15323 : 845316 : v->section = lwm;
15324 : : }
15325 : : }
15326 : :
15327 : 1716142 : if (v->section == v->cluster)
15328 : : {
15329 : : /* Root of a new SCC. Push all the members onto the result list. */
15330 : : unsigned num = v->cluster;
15331 : 1716142 : depset *p;
15332 : 1716142 : do
15333 : : {
15334 : 1716142 : p = stack.pop ();
15335 : 1716142 : p->cluster = num;
15336 : 1716142 : p->section = 0;
15337 : 1716142 : result.quick_push (p);
15338 : : }
15339 : 1716142 : while (p != v);
15340 : : }
15341 : 1716142 : }
15342 : :
15343 : : /* Compare two depsets. The specific ordering is unimportant, we're
15344 : : just trying to get consistency. */
15345 : :
15346 : : static int
15347 : 79602331 : depset_cmp (const void *a_, const void *b_)
15348 : : {
15349 : 79602331 : depset *a = *(depset *const *)a_;
15350 : 79602331 : depset *b = *(depset *const *)b_;
15351 : :
15352 : 79602331 : depset::entity_kind a_kind = a->get_entity_kind ();
15353 : 79602331 : depset::entity_kind b_kind = b->get_entity_kind ();
15354 : :
15355 : 79602331 : if (a_kind != b_kind)
15356 : : /* Different entity kinds, order by that. */
15357 : 4085844 : return a_kind < b_kind ? -1 : +1;
15358 : :
15359 : 76698896 : tree a_decl = a->get_entity ();
15360 : 76698896 : tree b_decl = b->get_entity ();
15361 : 76698896 : if (a_kind == depset::EK_USING)
15362 : : {
15363 : : /* If one is a using, the other must be too. */
15364 : 740996 : a_decl = OVL_FUNCTION (a_decl);
15365 : 740996 : b_decl = OVL_FUNCTION (b_decl);
15366 : : }
15367 : :
15368 : 76698896 : if (a_decl != b_decl)
15369 : : /* Different entities, order by their UID. */
15370 : 70977421 : return DECL_UID (a_decl) < DECL_UID (b_decl) ? -1 : +1;
15371 : :
15372 : 5721475 : if (a_kind == depset::EK_BINDING)
15373 : : {
15374 : : /* Both are bindings. Order by identifier hash. */
15375 : 5718551 : gcc_checking_assert (a->get_name () != b->get_name ());
15376 : 5718551 : hashval_t ah = IDENTIFIER_HASH_VALUE (a->get_name ());
15377 : 5718551 : hashval_t bh = IDENTIFIER_HASH_VALUE (b->get_name ());
15378 : 8518861 : return (ah == bh ? 0 : ah < bh ? -1 : +1);
15379 : : }
15380 : :
15381 : : /* They are the same decl. This can happen with two using decls
15382 : : pointing to the same target. The best we can aim for is
15383 : : consistently telling qsort how to order them. Hopefully we'll
15384 : : never have to debug a case that depends on this. Oh, who am I
15385 : : kidding? Good luck. */
15386 : 2924 : gcc_checking_assert (a_kind == depset::EK_USING);
15387 : :
15388 : : /* Order by depset address. Not the best, but it is something. */
15389 : 2924 : return a < b ? -1 : +1;
15390 : : }
15391 : :
15392 : : /* Sort the clusters in SCC such that those that depend on one another
15393 : : are placed later. */
15394 : :
15395 : : // FIXME: I am not convinced this is needed and, if needed,
15396 : : // sufficient. We emit the decls in this order but that emission
15397 : : // could walk into later decls (from the body of the decl, or default
15398 : : // arg-like things). Why doesn't that walk do the right thing? And
15399 : : // if it DTRT why do we need to sort here -- won't things naturally
15400 : : // work? I think part of the issue is that when we're going to refer
15401 : : // to an entity by name, and that entity is in the same cluster as us,
15402 : : // we need to actually walk that entity, if we've not already walked
15403 : : // it.
15404 : : static void
15405 : 223998 : sort_cluster (depset::hash *original, depset *scc[], unsigned size)
15406 : : {
15407 : 223998 : depset::hash table (size, original);
15408 : :
15409 : 223998 : dump.indent ();
15410 : :
15411 : : /* Place bindings last, usings before that. It's not strictly
15412 : : necessary, but it does make things neater. Says Mr OCD. */
15413 : : unsigned bind_lwm = size;
15414 : : unsigned use_lwm = size;
15415 : 1144965 : for (unsigned ix = 0; ix != use_lwm;)
15416 : : {
15417 : 920967 : depset *dep = scc[ix];
15418 : 920967 : switch (dep->get_entity_kind ())
15419 : : {
15420 : 116353 : case depset::EK_BINDING:
15421 : : /* Move to end. No increment. Notice this could be moving
15422 : : a using decl, which we'll then move again. */
15423 : 116353 : if (--bind_lwm != ix)
15424 : : {
15425 : 61016 : scc[ix] = scc[bind_lwm];
15426 : 61016 : scc[bind_lwm] = dep;
15427 : : }
15428 : 116353 : if (use_lwm > bind_lwm)
15429 : : {
15430 : 98317 : use_lwm--;
15431 : 98317 : break;
15432 : : }
15433 : : /* We must have copied a using, so move it too. */
15434 : 18036 : dep = scc[ix];
15435 : 18036 : gcc_checking_assert (dep->get_entity_kind () == depset::EK_USING);
15436 : : /* FALLTHROUGH */
15437 : :
15438 : 31561 : case depset::EK_USING:
15439 : 31561 : if (--use_lwm != ix)
15440 : : {
15441 : 22387 : scc[ix] = scc[use_lwm];
15442 : 22387 : scc[use_lwm] = dep;
15443 : : }
15444 : : break;
15445 : :
15446 : 791089 : case depset::EK_DECL:
15447 : 791089 : case depset::EK_SPECIALIZATION:
15448 : 791089 : case depset::EK_PARTIAL:
15449 : 791089 : table.add_mergeable (dep);
15450 : 791089 : ix++;
15451 : 791089 : break;
15452 : :
15453 : 0 : default:
15454 : 0 : gcc_unreachable ();
15455 : : }
15456 : : }
15457 : :
15458 : 223998 : gcc_checking_assert (use_lwm <= bind_lwm);
15459 : 224262 : dump (dumper::MERGE) && dump ("Ordering %u/%u depsets", use_lwm, size);
15460 : :
15461 : 223998 : table.find_dependencies (nullptr);
15462 : :
15463 : 447996 : auto_vec<depset *> order = table.connect ();
15464 : 447996 : gcc_checking_assert (order.length () == use_lwm);
15465 : :
15466 : : /* Now rewrite entries [0,lwm), in the dependency order we
15467 : : discovered. Usually each entity is in its own cluster. Rarely,
15468 : : we can get multi-entity clusters, in which case all but one must
15469 : : only be reached from within the cluster. This happens for
15470 : : something like:
15471 : :
15472 : : template<typename T>
15473 : : auto Foo (const T &arg) -> TPL<decltype (arg)>;
15474 : :
15475 : : The instantiation of TPL will be in the specialization table, and
15476 : : refer to Foo via arg. But we can only get to that specialization
15477 : : from Foo's declaration, so we only need to treat Foo as mergable
15478 : : (We'll do structural comparison of TPL<decltype (arg)>).
15479 : :
15480 : : We approximate finding the single cluster entry dep by checking for
15481 : : entities recursively depending on a dep first seen when streaming
15482 : : its own merge key; the first dep we see in such a cluster should be
15483 : : the first one streamed. */
15484 : : unsigned entry_pos = ~0u;
15485 : : unsigned cluster = ~0u;
15486 : 2030174 : for (unsigned ix = 0; ix != order.length (); ix++)
15487 : : {
15488 : 791089 : gcc_checking_assert (order[ix]->is_special ());
15489 : 791089 : bool tight = order[ix]->cluster == cluster;
15490 : 791089 : depset *dep = order[ix]->deps[0];
15491 : 791812 : dump (dumper::MERGE)
15492 : 1443 : && dump ("Mergeable %u is %N%s%s", ix, dep->get_entity (),
15493 : 723 : tight ? " (tight)" : "", dep->is_entry () ? " (entry)" : "");
15494 : 791089 : scc[ix] = dep;
15495 : 791089 : if (tight)
15496 : : {
15497 : 93 : gcc_checking_assert (dep->is_maybe_recursive ());
15498 : 93 : if (dep->is_entry ())
15499 : : {
15500 : : /* There should only be one entry dep in a cluster. */
15501 : 6 : gcc_checking_assert (!scc[entry_pos]->is_entry ());
15502 : 6 : gcc_checking_assert (scc[entry_pos]->is_maybe_recursive ());
15503 : 6 : scc[ix] = scc[entry_pos];
15504 : 6 : scc[entry_pos] = dep;
15505 : : }
15506 : : }
15507 : : else
15508 : : entry_pos = ix;
15509 : 791089 : cluster = order[ix]->cluster;
15510 : : }
15511 : :
15512 : 224262 : dump (dumper::MERGE) && dump ("Ordered %u keys", order.length ());
15513 : 223998 : dump.outdent ();
15514 : 223998 : }
15515 : :
15516 : : /* Reduce graph to SCCS clusters. SCCS will be populated with the
15517 : : depsets in dependency order. Each depset's CLUSTER field contains
15518 : : its cluster number. Each SCC has a unique cluster number, and are
15519 : : contiguous in SCCS. Cluster numbers are otherwise arbitrary. */
15520 : :
15521 : : vec<depset *>
15522 : 226396 : depset::hash::connect ()
15523 : : {
15524 : 226396 : tarjan connector (size ());
15525 : 226396 : vec<depset *> deps;
15526 : 226396 : deps.create (size ());
15527 : 4176242 : for (depset *item : *this)
15528 : : {
15529 : 1974923 : entity_kind kind = item->get_entity_kind ();
15530 : 1858570 : if (kind == EK_BINDING
15531 : 1858570 : || !(kind == EK_REDIRECT
15532 : 1839303 : || item->is_tu_local ()
15533 : 1839210 : || item->is_unreached ()
15534 : 1600337 : || item->is_import ()))
15535 : 1716142 : deps.quick_push (item);
15536 : : }
15537 : :
15538 : : /* Iteration over the hash table is an unspecified ordering. While
15539 : : that has advantages, it causes 2 problems. Firstly repeatable
15540 : : builds are tricky. Secondly creating testcases that check
15541 : : dependencies are correct by making sure a bad ordering would
15542 : : happen if that was wrong. */
15543 : 1223823 : deps.qsort (depset_cmp);
15544 : :
15545 : 1942538 : while (deps.length ())
15546 : : {
15547 : 1716142 : depset *v = deps.pop ();
15548 : 1716142 : dump (dumper::CLUSTER) &&
15549 : 1599 : (v->is_binding ()
15550 : 195 : ? dump ("Connecting binding %P", v->get_entity (), v->get_name ())
15551 : 1404 : : dump ("Connecting %s %s %C:%N",
15552 : 1404 : is_key_order () ? "key-order"
15553 : 774 : : !v->has_defn () ? "declaration" : "definition",
15554 : 1404 : v->entity_kind_name (), TREE_CODE (v->get_entity ()),
15555 : : v->get_entity ()));
15556 : 1716142 : if (!v->cluster)
15557 : 771031 : connector.connect (v);
15558 : : }
15559 : :
15560 : 226396 : deps.release ();
15561 : 452792 : return connector.result;
15562 : 226396 : }
15563 : :
15564 : : /* Initialize location spans. */
15565 : :
15566 : : void
15567 : 4318 : loc_spans::init (const line_maps *lmaps, const line_map_ordinary *map)
15568 : : {
15569 : 4318 : gcc_checking_assert (!init_p ());
15570 : 4318 : spans = new vec<span> ();
15571 : 4318 : spans->reserve (20);
15572 : :
15573 : 4318 : span interval;
15574 : 4318 : interval.ordinary.first = 0;
15575 : 4318 : interval.macro.second = MAX_LOCATION_T + 1;
15576 : 4318 : interval.ordinary_delta = interval.macro_delta = 0;
15577 : :
15578 : : /* A span for reserved fixed locs. */
15579 : 4318 : interval.ordinary.second
15580 : 4318 : = MAP_START_LOCATION (LINEMAPS_ORDINARY_MAP_AT (line_table, 0));
15581 : 4318 : interval.macro.first = interval.macro.second;
15582 : 4318 : dump (dumper::LOCATION)
15583 : 42 : && dump ("Fixed span %u ordinary:[%K,%K) macro:[%K,%K)", spans->length (),
15584 : : interval.ordinary.first, interval.ordinary.second,
15585 : : interval.macro.first, interval.macro.second);
15586 : 4318 : spans->quick_push (interval);
15587 : :
15588 : : /* A span for command line & forced headers. */
15589 : 4318 : interval.ordinary.first = interval.ordinary.second;
15590 : 4318 : interval.macro.second = interval.macro.first;
15591 : 4318 : if (map)
15592 : : {
15593 : 4312 : interval.ordinary.second = map->start_location;
15594 : 4312 : interval.macro.first = LINEMAPS_MACRO_LOWEST_LOCATION (lmaps);
15595 : : }
15596 : 4318 : dump (dumper::LOCATION)
15597 : 21 : && dump ("Pre span %u ordinary:[%K,%K) macro:[%K,%K)", spans->length (),
15598 : : interval.ordinary.first, interval.ordinary.second,
15599 : : interval.macro.first, interval.macro.second);
15600 : 4318 : spans->quick_push (interval);
15601 : :
15602 : : /* Start an interval for the main file. */
15603 : 4318 : interval.ordinary.first = interval.ordinary.second;
15604 : 4318 : interval.macro.second = interval.macro.first;
15605 : 4318 : dump (dumper::LOCATION)
15606 : 21 : && dump ("Main span %u ordinary:[%K,*) macro:[*,%K)", spans->length (),
15607 : : interval.ordinary.first, interval.macro.second);
15608 : 4318 : spans->quick_push (interval);
15609 : 4318 : }
15610 : :
15611 : : /* Reopen the span, if we want the about-to-be-inserted set of maps to
15612 : : be propagated in our own location table. I.e. we are the primary
15613 : : interface and we're importing a partition. */
15614 : :
15615 : : bool
15616 : 2674 : loc_spans::maybe_propagate (module_state *import, location_t hwm)
15617 : : {
15618 : 2674 : bool opened = (module_interface_p () && !module_partition_p ()
15619 : 3028 : && import->is_partition ());
15620 : 147 : if (opened)
15621 : 147 : open (hwm);
15622 : 2674 : return opened;
15623 : : }
15624 : :
15625 : : /* Open a new linemap interval. The just-created ordinary map is the
15626 : : first map of the interval. */
15627 : :
15628 : : void
15629 : 1003 : loc_spans::open (location_t hwm)
15630 : : {
15631 : 1003 : span interval;
15632 : 1003 : interval.ordinary.first = interval.ordinary.second = hwm;
15633 : 2006 : interval.macro.first = interval.macro.second
15634 : 1003 : = LINEMAPS_MACRO_LOWEST_LOCATION (line_table);
15635 : 1003 : interval.ordinary_delta = interval.macro_delta = 0;
15636 : 1003 : dump (dumper::LOCATION)
15637 : 0 : && dump ("Opening span %u ordinary:[%K,... macro:...,%K)",
15638 : 0 : spans->length (), interval.ordinary.first,
15639 : : interval.macro.second);
15640 : 1003 : if (spans->length ())
15641 : : {
15642 : : /* No overlapping! */
15643 : 1003 : auto &last = spans->last ();
15644 : 1003 : gcc_checking_assert (interval.ordinary.first >= last.ordinary.second);
15645 : 1003 : gcc_checking_assert (interval.macro.second <= last.macro.first);
15646 : : }
15647 : 1003 : spans->safe_push (interval);
15648 : 1003 : }
15649 : :
15650 : : /* Close out the current linemap interval. The last maps are within
15651 : : the interval. */
15652 : :
15653 : : void
15654 : 5318 : loc_spans::close ()
15655 : : {
15656 : 5318 : span &interval = spans->last ();
15657 : :
15658 : 5318 : interval.ordinary.second
15659 : 5318 : = ((line_table->highest_location
15660 : 5318 : + (loc_one << line_table->default_range_bits))
15661 : 5318 : & ~((loc_one << line_table->default_range_bits) - 1));
15662 : 5318 : interval.macro.first = LINEMAPS_MACRO_LOWEST_LOCATION (line_table);
15663 : 5318 : dump (dumper::LOCATION)
15664 : 21 : && dump ("Closing span %u ordinary:[%K,%K) macro:[%K,%K)",
15665 : 21 : spans->length () - 1,
15666 : : interval.ordinary.first,interval.ordinary.second,
15667 : : interval.macro.first, interval.macro.second);
15668 : 5318 : }
15669 : :
15670 : : /* Given an ordinary location LOC, return the lmap_interval it resides
15671 : : in. NULL if it is not in an interval. */
15672 : :
15673 : : const loc_spans::span *
15674 : 24021647 : loc_spans::ordinary (location_t loc)
15675 : : {
15676 : 24021647 : unsigned len = spans->length ();
15677 : 47903972 : unsigned pos = 0;
15678 : 47911050 : while (len)
15679 : : {
15680 : 47903579 : unsigned half = len / 2;
15681 : 47903579 : const span &probe = (*spans)[pos + half];
15682 : 47903579 : if (loc < probe.ordinary.first)
15683 : : len = half;
15684 : 47896501 : else if (loc < probe.ordinary.second)
15685 : : return &probe;
15686 : : else
15687 : : {
15688 : 23882325 : pos += half + 1;
15689 : 23882325 : len = len - (half + 1);
15690 : : }
15691 : : }
15692 : : return NULL;
15693 : : }
15694 : :
15695 : : /* Likewise, given a macro location LOC, return the lmap interval it
15696 : : resides in. */
15697 : :
15698 : : const loc_spans::span *
15699 : 2041092 : loc_spans::macro (location_t loc)
15700 : : {
15701 : 2041092 : unsigned len = spans->length ();
15702 : 4077050 : unsigned pos = 0;
15703 : 4077050 : while (len)
15704 : : {
15705 : 4077038 : unsigned half = len / 2;
15706 : 4077038 : const span &probe = (*spans)[pos + half];
15707 : 4077038 : if (loc >= probe.macro.second)
15708 : : len = half;
15709 : 4077038 : else if (loc >= probe.macro.first)
15710 : : return &probe;
15711 : : else
15712 : : {
15713 : 2035958 : pos += half + 1;
15714 : 2035958 : len = len - (half + 1);
15715 : : }
15716 : : }
15717 : : return NULL;
15718 : : }
15719 : :
15720 : : /* Return the ordinary location closest to FROM. */
15721 : :
15722 : : static location_t
15723 : 6110 : ordinary_loc_of (line_maps *lmaps, location_t from)
15724 : : {
15725 : 12223 : while (!IS_ORDINARY_LOC (from))
15726 : : {
15727 : 3 : if (IS_ADHOC_LOC (from))
15728 : 3 : from = get_location_from_adhoc_loc (lmaps, from);
15729 : 3 : if (from >= LINEMAPS_MACRO_LOWEST_LOCATION (lmaps))
15730 : : {
15731 : : /* Find the ordinary location nearest FROM. */
15732 : 0 : const line_map *map = linemap_lookup (lmaps, from);
15733 : 0 : const line_map_macro *mac_map = linemap_check_macro (map);
15734 : 0 : from = mac_map->get_expansion_point_location ();
15735 : : }
15736 : : }
15737 : 6110 : return from;
15738 : : }
15739 : :
15740 : : static module_state **
15741 : 10744 : get_module_slot (tree name, module_state *parent, bool partition, bool insert)
15742 : : {
15743 : 10744 : module_state_hash::compare_type ct (name, uintptr_t (parent) | partition);
15744 : 10744 : hashval_t hv = module_state_hash::hash (ct);
15745 : :
15746 : 10744 : return modules_hash->find_slot_with_hash (ct, hv, insert ? INSERT : NO_INSERT);
15747 : : }
15748 : :
15749 : : static module_state *
15750 : 393 : get_primary (module_state *parent)
15751 : : {
15752 : 3768 : while (parent->is_partition ())
15753 : 345 : parent = parent->parent;
15754 : :
15755 : 3423 : if (!parent->name)
15756 : : // Implementation unit has null name
15757 : 126 : parent = parent->parent;
15758 : :
15759 : 357 : return parent;
15760 : : }
15761 : :
15762 : : /* Find or create module NAME & PARENT in the hash table. */
15763 : :
15764 : : module_state *
15765 : 10744 : get_module (tree name, module_state *parent, bool partition)
15766 : : {
15767 : : /* We might be given an empty NAME if preprocessing fails to handle
15768 : : a header-name token. */
15769 : 10744 : if (name && TREE_CODE (name) == STRING_CST
15770 : 13479 : && TREE_STRING_LENGTH (name) == 0)
15771 : : return nullptr;
15772 : :
15773 : 10744 : if (partition)
15774 : : {
15775 : 879 : if (!parent)
15776 : 186 : parent = get_primary ((*modules)[0]);
15777 : :
15778 : 879 : if (!parent->is_partition () && !parent->flatname)
15779 : 207 : parent->set_flatname ();
15780 : : }
15781 : :
15782 : 10744 : module_state **slot = get_module_slot (name, parent, partition, true);
15783 : 10744 : module_state *state = *slot;
15784 : 10744 : if (!state)
15785 : : {
15786 : 5868 : state = (new (ggc_alloc<module_state> ())
15787 : 5868 : module_state (name, parent, partition));
15788 : 5868 : *slot = state;
15789 : : }
15790 : : return state;
15791 : : }
15792 : :
15793 : : /* Process string name PTR into a module_state. */
15794 : :
15795 : : static module_state *
15796 : 366 : get_module (const char *ptr)
15797 : : {
15798 : : /* On DOS based file systems, there is an ambiguity with A:B which can be
15799 : : interpreted as a module Module:Partition or Drive:PATH. Interpret strings
15800 : : which clearly starts as pathnames as header-names and everything else is
15801 : : treated as a (possibly malformed) named moduled. */
15802 : 366 : if (IS_DIR_SEPARATOR (ptr[ptr[0] == '.']) // ./FOO or /FOO
15803 : : #if HAVE_DOS_BASED_FILE_SYSTEM
15804 : : || (HAS_DRIVE_SPEC (ptr) && IS_DIR_SEPARATOR (ptr[2])) // A:/FOO
15805 : : #endif
15806 : : || false)
15807 : : /* A header name. */
15808 : 107 : return get_module (build_string (strlen (ptr), ptr));
15809 : :
15810 : : bool partition = false;
15811 : : module_state *mod = NULL;
15812 : :
15813 : 1062 : for (const char *probe = ptr;; probe++)
15814 : 1321 : if (!*probe || *probe == '.' || *probe == ':')
15815 : : {
15816 : 331 : if (probe == ptr)
15817 : : return NULL;
15818 : :
15819 : 331 : mod = get_module (get_identifier_with_length (ptr, probe - ptr),
15820 : : mod, partition);
15821 : 331 : ptr = probe;
15822 : 331 : if (*ptr == ':')
15823 : : {
15824 : 69 : if (partition)
15825 : : return NULL;
15826 : : partition = true;
15827 : : }
15828 : :
15829 : 331 : if (!*ptr++)
15830 : : break;
15831 : : }
15832 : 990 : else if (!(ISALPHA (*probe) || *probe == '_'
15833 : 18 : || (probe != ptr && ISDIGIT (*probe))))
15834 : : return NULL;
15835 : :
15836 : : return mod;
15837 : : }
15838 : :
15839 : : /* Create a new mapper connecting to OPTION. */
15840 : :
15841 : : module_client *
15842 : 4318 : make_mapper (location_t loc, class mkdeps *deps)
15843 : : {
15844 : 4318 : timevar_start (TV_MODULE_MAPPER);
15845 : 4318 : const char *option = module_mapper_name;
15846 : 4318 : if (!option)
15847 : 4276 : option = getenv ("CXX_MODULE_MAPPER");
15848 : :
15849 : 8636 : mapper = module_client::open_module_client
15850 : 4318 : (loc, option, deps, &set_cmi_repo,
15851 : 4318 : (save_decoded_options[0].opt_index == OPT_SPECIAL_program_name)
15852 : 4318 : && save_decoded_options[0].arg != progname
15853 : : ? save_decoded_options[0].arg : nullptr);
15854 : :
15855 : 4318 : timevar_stop (TV_MODULE_MAPPER);
15856 : :
15857 : 4318 : return mapper;
15858 : : }
15859 : :
15860 : : static unsigned lazy_snum;
15861 : :
15862 : : static bool
15863 : 7141 : recursive_lazy (unsigned snum = ~0u)
15864 : : {
15865 : 7141 : if (lazy_snum)
15866 : : {
15867 : 0 : error_at (input_location, "recursive lazy load");
15868 : 0 : return true;
15869 : : }
15870 : :
15871 : 7141 : lazy_snum = snum;
15872 : 7141 : return false;
15873 : : }
15874 : :
15875 : : /* If THIS is the current purview, issue an import error and return false. */
15876 : :
15877 : : bool
15878 : 2537 : module_state::check_not_purview (location_t from)
15879 : : {
15880 : 2537 : module_state *imp = (*modules)[0];
15881 : 2537 : if (imp && !imp->name)
15882 : 1925 : imp = imp->parent;
15883 : 2537 : if (imp == this)
15884 : : {
15885 : : /* Cannot import the current module. */
15886 : 12 : auto_diagnostic_group d;
15887 : 12 : error_at (from, "cannot import module in its own purview");
15888 : 12 : inform (loc, "module %qs declared here", get_flatname ());
15889 : 12 : return false;
15890 : 12 : }
15891 : : return true;
15892 : : }
15893 : :
15894 : : /* Module name substitutions. */
15895 : : static vec<module_state *,va_heap> substs;
15896 : :
15897 : : void
15898 : 7219 : module_state::mangle (bool include_partition)
15899 : : {
15900 : 7219 : if (subst)
15901 : 170 : mangle_module_substitution (subst);
15902 : : else
15903 : : {
15904 : 7049 : if (parent)
15905 : 700 : parent->mangle (include_partition);
15906 : 7049 : if (include_partition || !is_partition ())
15907 : : {
15908 : : // Partitions are significant for global initializer
15909 : : // functions
15910 : 6967 : bool partition = is_partition () && !parent->is_partition ();
15911 : 6967 : subst = mangle_module_component (name, partition);
15912 : 6967 : substs.safe_push (this);
15913 : : }
15914 : : }
15915 : 7219 : }
15916 : :
15917 : : void
15918 : 6519 : mangle_module (int mod, bool include_partition)
15919 : : {
15920 : 6519 : module_state *imp = (*modules)[mod];
15921 : :
15922 : 6519 : gcc_checking_assert (!imp->is_header ());
15923 : :
15924 : 6519 : if (!imp->name)
15925 : : /* Set when importing the primary module interface. */
15926 : 222 : imp = imp->parent;
15927 : :
15928 : 6519 : imp->mangle (include_partition);
15929 : 6519 : }
15930 : :
15931 : : /* Clean up substitutions. */
15932 : : void
15933 : 6313 : mangle_module_fini ()
15934 : : {
15935 : 13280 : while (substs.length ())
15936 : 6967 : substs.pop ()->subst = 0;
15937 : 6313 : }
15938 : :
15939 : : /* Announce WHAT about the module. */
15940 : :
15941 : : void
15942 : 11143 : module_state::announce (const char *what) const
15943 : : {
15944 : 11143 : if (noisy_p ())
15945 : : {
15946 : 0 : fprintf (stderr, " %s:%s", what, get_flatname ());
15947 : 0 : fflush (stderr);
15948 : : }
15949 : 11143 : }
15950 : :
15951 : : /* A human-readable README section. The contents of this section to
15952 : : not contribute to the CRC, so the contents can change per
15953 : : compilation. That allows us to embed CWD, hostname, build time and
15954 : : what not. It is a STRTAB that may be extracted with:
15955 : : readelf -pgnu.c++.README $(module).gcm */
15956 : :
15957 : : void
15958 : 2398 : module_state::write_readme (elf_out *to, cpp_reader *reader, const char *dialect)
15959 : : {
15960 : 2398 : bytes_out readme (to);
15961 : :
15962 : 2398 : readme.begin (false);
15963 : :
15964 : 2398 : readme.printf ("GNU C++ %s",
15965 : 2398 : is_header () ? "header unit"
15966 : 1543 : : !is_partition () ? "primary interface"
15967 : 159 : : is_interface () ? "interface partition"
15968 : : : "internal partition");
15969 : :
15970 : : /* Compiler's version. */
15971 : 2398 : readme.printf ("compiler: %s", version_string);
15972 : :
15973 : : /* Module format version. */
15974 : 2398 : verstr_t string;
15975 : 2398 : version2string (MODULE_VERSION, string);
15976 : 2398 : readme.printf ("version: %s", string);
15977 : :
15978 : : /* Module information. */
15979 : 2398 : readme.printf ("module: %s", get_flatname ());
15980 : 2398 : readme.printf ("source: %s", main_input_filename);
15981 : 2398 : readme.printf ("dialect: %s", dialect);
15982 : 2398 : if (extensions)
15983 : 24 : readme.printf ("extensions: %s%s%s",
15984 : : extensions & SE_OPENMP ? "-fopenmp"
15985 : 6 : : extensions & SE_OPENMP_SIMD ? "-fopenmp-simd" : "",
15986 : : (extensions & SE_OPENACC)
15987 : 3 : && (extensions & (SE_OPENMP | SE_OPENMP_SIMD))
15988 : : ? " " : "",
15989 : 9 : extensions & SE_OPENACC ? "-fopenacc" : "");
15990 : :
15991 : : /* The following fields could be expected to change between
15992 : : otherwise identical compilations. Consider a distributed build
15993 : : system. We should have a way of overriding that. */
15994 : 2398 : if (char *cwd = getcwd (NULL, 0))
15995 : : {
15996 : 2398 : readme.printf ("cwd: %s", cwd);
15997 : 2398 : free (cwd);
15998 : : }
15999 : 4796 : readme.printf ("repository: %s", cmi_repo ? cmi_repo : ".");
16000 : : #if NETWORKING
16001 : : {
16002 : : char hostname[64];
16003 : : if (!gethostname (hostname, sizeof (hostname)))
16004 : : readme.printf ("host: %s", hostname);
16005 : : }
16006 : : #endif
16007 : 2398 : {
16008 : : /* This of course will change! */
16009 : 2398 : time_t stampy;
16010 : 2398 : auto kind = cpp_get_date (reader, &stampy);
16011 : 2398 : if (kind != CPP_time_kind::UNKNOWN)
16012 : : {
16013 : 2398 : struct tm *time;
16014 : :
16015 : 2398 : time = gmtime (&stampy);
16016 : 2398 : readme.print_time ("build", time, "UTC");
16017 : :
16018 : 2398 : if (kind == CPP_time_kind::DYNAMIC)
16019 : : {
16020 : 2398 : time = localtime (&stampy);
16021 : 2398 : readme.print_time ("local", time,
16022 : : #if defined (__USE_MISC) || defined (__USE_BSD) /* Is there a better way? */
16023 : : time->tm_zone
16024 : : #else
16025 : : ""
16026 : : #endif
16027 : : );
16028 : : }
16029 : : }
16030 : : }
16031 : :
16032 : : /* Its direct imports. */
16033 : 2958 : for (unsigned ix = 1; ix < modules->length (); ix++)
16034 : : {
16035 : 560 : module_state *state = (*modules)[ix];
16036 : :
16037 : 560 : if (state->is_direct ())
16038 : 829 : readme.printf ("%s: %s %s", state->exported_p ? "export" : "import",
16039 : : state->get_flatname (), state->filename);
16040 : : }
16041 : :
16042 : 2398 : readme.end (to, to->name (MOD_SNAME_PFX ".README"), NULL);
16043 : 2398 : }
16044 : :
16045 : : /* Sort environment var names in reverse order. */
16046 : :
16047 : : static int
16048 : 0 : env_var_cmp (const void *a_, const void *b_)
16049 : : {
16050 : 0 : const unsigned char *a = *(const unsigned char *const *)a_;
16051 : 0 : const unsigned char *b = *(const unsigned char *const *)b_;
16052 : :
16053 : 0 : for (unsigned ix = 0; ; ix++)
16054 : : {
16055 : 0 : bool a_end = !a[ix] || a[ix] == '=';
16056 : 0 : if (a[ix] == b[ix])
16057 : : {
16058 : 0 : if (a_end)
16059 : : break;
16060 : : }
16061 : : else
16062 : : {
16063 : 0 : bool b_end = !b[ix] || b[ix] == '=';
16064 : :
16065 : 0 : if (!a_end && !b_end)
16066 : 0 : return a[ix] < b[ix] ? +1 : -1;
16067 : 0 : if (a_end && b_end)
16068 : : break;
16069 : 0 : return a_end ? +1 : -1;
16070 : : }
16071 : 0 : }
16072 : :
16073 : : return 0;
16074 : : }
16075 : :
16076 : : /* Write the environment. It is a STRTAB that may be extracted with:
16077 : : readelf -pgnu.c++.ENV $(module).gcm */
16078 : :
16079 : : void
16080 : 0 : module_state::write_env (elf_out *to)
16081 : : {
16082 : 0 : vec<const char *> vars;
16083 : 0 : vars.create (20);
16084 : :
16085 : 0 : extern char **environ;
16086 : 0 : while (const char *var = environ[vars.length ()])
16087 : 0 : vars.safe_push (var);
16088 : 0 : vars.qsort (env_var_cmp);
16089 : :
16090 : 0 : bytes_out env (to);
16091 : 0 : env.begin (false);
16092 : 0 : while (vars.length ())
16093 : 0 : env.printf ("%s", vars.pop ());
16094 : 0 : env.end (to, to->name (MOD_SNAME_PFX ".ENV"), NULL);
16095 : :
16096 : 0 : vars.release ();
16097 : 0 : }
16098 : :
16099 : : /* Write the direct or indirect imports.
16100 : : u:N
16101 : : {
16102 : : u:index
16103 : : s:name
16104 : : u32:crc
16105 : : s:filename (direct)
16106 : : u:exported (direct)
16107 : : } imports[N]
16108 : : */
16109 : :
16110 : : void
16111 : 754 : module_state::write_imports (bytes_out &sec, bool direct)
16112 : : {
16113 : 754 : unsigned count = 0;
16114 : :
16115 : 1586 : for (unsigned ix = 1; ix < modules->length (); ix++)
16116 : : {
16117 : 832 : module_state *imp = (*modules)[ix];
16118 : :
16119 : 832 : if (imp->remap && imp->is_direct () == direct)
16120 : 401 : count++;
16121 : : }
16122 : :
16123 : 754 : gcc_assert (!direct || count);
16124 : :
16125 : 754 : sec.u (count);
16126 : 1586 : for (unsigned ix = 1; ix < modules->length (); ix++)
16127 : : {
16128 : 832 : module_state *imp = (*modules)[ix];
16129 : :
16130 : 832 : if (imp->remap && imp->is_direct () == direct)
16131 : : {
16132 : 539 : dump () && dump ("Writing %simport:%u->%u %M (crc=%x)",
16133 : : !direct ? "indirect "
16134 : 69 : : imp->exported_p ? "exported " : "",
16135 : : ix, imp->remap, imp, imp->crc);
16136 : 401 : sec.u (imp->remap);
16137 : 401 : sec.str (imp->get_flatname ());
16138 : 401 : sec.u32 (imp->crc);
16139 : 401 : if (direct)
16140 : : {
16141 : 396 : write_location (sec, imp->imported_from ());
16142 : 396 : sec.str (imp->filename);
16143 : 396 : int exportedness = 0;
16144 : 396 : if (imp->exported_p)
16145 : : exportedness = +1;
16146 : 236 : else if (!imp->is_purview_direct ())
16147 : 12 : exportedness = -1;
16148 : 396 : sec.i (exportedness);
16149 : : }
16150 : : }
16151 : : }
16152 : 754 : }
16153 : :
16154 : : /* READER, LMAPS != NULL == direct imports,
16155 : : == NUL == indirect imports. */
16156 : :
16157 : : unsigned
16158 : 636 : module_state::read_imports (bytes_in &sec, cpp_reader *reader, line_maps *lmaps)
16159 : : {
16160 : 636 : unsigned count = sec.u ();
16161 : 636 : unsigned loaded = 0;
16162 : :
16163 : 1605 : while (count--)
16164 : : {
16165 : 333 : unsigned ix = sec.u ();
16166 : 333 : if (ix >= slurp->remap->length () || !ix || (*slurp->remap)[ix])
16167 : : {
16168 : 0 : sec.set_overrun ();
16169 : 0 : break;
16170 : : }
16171 : :
16172 : 333 : const char *name = sec.str (NULL);
16173 : 333 : module_state *imp = get_module (name);
16174 : 333 : unsigned crc = sec.u32 ();
16175 : 333 : int exportedness = 0;
16176 : :
16177 : : /* If the import is a partition, it must be the same primary
16178 : : module as this TU. */
16179 : 333 : if (imp && imp->is_partition () &&
16180 : : (!named_module_p ()
16181 : 126 : || (get_primary ((*modules)[0]) != get_primary (imp))))
16182 : : imp = NULL;
16183 : :
16184 : 333 : if (!imp)
16185 : 0 : sec.set_overrun ();
16186 : 333 : if (sec.get_overrun ())
16187 : : break;
16188 : :
16189 : 333 : if (lmaps)
16190 : : {
16191 : : /* A direct import, maybe load it. */
16192 : 333 : location_t floc = read_location (sec);
16193 : 333 : const char *fname = sec.str (NULL);
16194 : 333 : exportedness = sec.i ();
16195 : :
16196 : 333 : if (sec.get_overrun ())
16197 : : break;
16198 : :
16199 : 333 : if (!imp->check_not_purview (loc))
16200 : 3 : continue;
16201 : :
16202 : 330 : if (imp->loadedness == ML_NONE)
16203 : : {
16204 : 262 : imp->loc = floc;
16205 : 262 : imp->crc = crc;
16206 : 262 : if (!imp->get_flatname ())
16207 : 223 : imp->set_flatname ();
16208 : :
16209 : 262 : unsigned n = dump.push (imp);
16210 : :
16211 : 262 : if (!imp->filename && fname)
16212 : 220 : imp->filename = xstrdup (fname);
16213 : :
16214 : 262 : if (imp->is_partition ())
16215 : 30 : dump () && dump ("Importing elided partition %M", imp);
16216 : :
16217 : 262 : if (!imp->do_import (reader, false))
16218 : 3 : imp = NULL;
16219 : 262 : dump.pop (n);
16220 : 262 : if (!imp)
16221 : 3 : continue;
16222 : : }
16223 : :
16224 : 327 : if (is_partition ())
16225 : : {
16226 : 54 : if (!imp->is_direct ())
16227 : 27 : imp->directness = MD_PARTITION_DIRECT;
16228 : 54 : if (exportedness > 0)
16229 : 3 : imp->exported_p = true;
16230 : : }
16231 : : }
16232 : : else
16233 : : {
16234 : : /* An indirect import, find it, it should already be here. */
16235 : 0 : if (imp->loadedness == ML_NONE)
16236 : : {
16237 : 0 : error_at (loc, "indirect import %qs is not already loaded", name);
16238 : 0 : continue;
16239 : : }
16240 : : }
16241 : :
16242 : 327 : if (imp->crc != crc)
16243 : 0 : error_at (loc, "import %qs has CRC mismatch", imp->get_flatname ());
16244 : :
16245 : 327 : (*slurp->remap)[ix] = (imp->mod << 1) | (lmaps != NULL);
16246 : :
16247 : 327 : if (lmaps && exportedness >= 0)
16248 : 315 : set_import (imp, bool (exportedness));
16249 : 477 : dump () && dump ("Found %simport:%u %M->%u", !lmaps ? "indirect "
16250 : 75 : : exportedness > 0 ? "exported "
16251 : 42 : : exportedness < 0 ? "gmf" : "", ix, imp,
16252 : : imp->mod);
16253 : 327 : loaded++;
16254 : : }
16255 : :
16256 : 636 : return loaded;
16257 : : }
16258 : :
16259 : : /* Write the import table to MOD_SNAME_PFX.imp. */
16260 : :
16261 : : void
16262 : 377 : module_state::write_imports (elf_out *to, unsigned *crc_ptr)
16263 : : {
16264 : 443 : dump () && dump ("Writing imports");
16265 : 377 : dump.indent ();
16266 : :
16267 : 377 : bytes_out sec (to);
16268 : 377 : sec.begin ();
16269 : :
16270 : 377 : write_imports (sec, true);
16271 : 377 : write_imports (sec, false);
16272 : :
16273 : 377 : sec.end (to, to->name (MOD_SNAME_PFX ".imp"), crc_ptr);
16274 : 377 : dump.outdent ();
16275 : 377 : }
16276 : :
16277 : : bool
16278 : 318 : module_state::read_imports (cpp_reader *reader, line_maps *lmaps)
16279 : : {
16280 : 318 : bytes_in sec;
16281 : :
16282 : 318 : if (!sec.begin (loc, from (), MOD_SNAME_PFX ".imp"))
16283 : : return false;
16284 : :
16285 : 390 : dump () && dump ("Reading %u imports", slurp->remap->length () - 1);
16286 : 318 : dump.indent ();
16287 : :
16288 : : /* Read the imports. */
16289 : 318 : unsigned direct = read_imports (sec, reader, lmaps);
16290 : 318 : unsigned indirect = read_imports (sec, NULL, NULL);
16291 : 318 : if (direct + indirect + 1 != slurp->remap->length ())
16292 : 6 : from ()->set_error (elf::E_BAD_IMPORT);
16293 : :
16294 : 318 : dump.outdent ();
16295 : 318 : if (!sec.end (from ()))
16296 : : return false;
16297 : : return true;
16298 : 318 : }
16299 : :
16300 : : /* We're the primary module interface, but have partitions. Document
16301 : : them so that non-partition module implementation units know which
16302 : : have already been loaded. */
16303 : :
16304 : : void
16305 : 111 : module_state::write_partitions (elf_out *to, unsigned count, unsigned *crc_ptr)
16306 : : {
16307 : 129 : dump () && dump ("Writing %u elided partitions", count);
16308 : 111 : dump.indent ();
16309 : :
16310 : 111 : bytes_out sec (to);
16311 : 111 : sec.begin ();
16312 : :
16313 : 285 : for (unsigned ix = 1; ix != modules->length (); ix++)
16314 : : {
16315 : 174 : module_state *imp = (*modules)[ix];
16316 : 174 : if (imp->is_partition ())
16317 : : {
16318 : 189 : dump () && dump ("Writing elided partition %M (crc=%x)",
16319 : : imp, imp->crc);
16320 : 159 : sec.str (imp->get_flatname ());
16321 : 159 : sec.u32 (imp->crc);
16322 : 309 : write_location (sec, imp->is_direct ()
16323 : 150 : ? imp->imported_from () : UNKNOWN_LOCATION);
16324 : 159 : sec.str (imp->filename);
16325 : : }
16326 : : }
16327 : :
16328 : 111 : sec.end (to, to->name (MOD_SNAME_PFX ".prt"), crc_ptr);
16329 : 111 : dump.outdent ();
16330 : 111 : }
16331 : :
16332 : : bool
16333 : 15 : module_state::read_partitions (unsigned count)
16334 : : {
16335 : 15 : bytes_in sec;
16336 : 15 : if (!sec.begin (loc, from (), MOD_SNAME_PFX ".prt"))
16337 : : return false;
16338 : :
16339 : 18 : dump () && dump ("Reading %u elided partitions", count);
16340 : 15 : dump.indent ();
16341 : :
16342 : 42 : while (count--)
16343 : : {
16344 : 27 : const char *name = sec.str (NULL);
16345 : 27 : unsigned crc = sec.u32 ();
16346 : 27 : location_t floc = read_location (sec);
16347 : 27 : const char *fname = sec.str (NULL);
16348 : :
16349 : 27 : if (sec.get_overrun ())
16350 : : break;
16351 : :
16352 : 33 : dump () && dump ("Reading elided partition %s (crc=%x)", name, crc);
16353 : :
16354 : 27 : module_state *imp = get_module (name);
16355 : 27 : if (!imp /* Partition should be ... */
16356 : 27 : || !imp->is_partition () /* a partition ... */
16357 : 27 : || imp->loadedness != ML_NONE /* that is not yet loaded ... */
16358 : 54 : || get_primary (imp) != this) /* whose primary is this. */
16359 : : {
16360 : 0 : sec.set_overrun ();
16361 : 0 : break;
16362 : : }
16363 : :
16364 : 27 : if (!imp->has_location ())
16365 : 24 : imp->loc = floc;
16366 : 27 : imp->crc = crc;
16367 : 27 : if (!imp->filename && fname[0])
16368 : 24 : imp->filename = xstrdup (fname);
16369 : : }
16370 : :
16371 : 15 : dump.outdent ();
16372 : 15 : if (!sec.end (from ()))
16373 : : return false;
16374 : : return true;
16375 : 15 : }
16376 : :
16377 : : /* Data for config reading and writing. */
16378 : : struct module_state_config {
16379 : : const char *dialect_str = get_dialect ();
16380 : : line_map_uint_t ordinary_locs = 0;
16381 : : line_map_uint_t macro_locs = 0;
16382 : : unsigned num_imports = 0;
16383 : : unsigned num_partitions = 0;
16384 : : unsigned num_entities = 0;
16385 : : unsigned loc_range_bits = 0;
16386 : : unsigned active_init = 0;
16387 : :
16388 : 93654 : static void release ()
16389 : : {
16390 : 93654 : XDELETEVEC (dialect);
16391 : 93654 : dialect = NULL;
16392 : : }
16393 : :
16394 : : private:
16395 : : static const char *get_dialect ();
16396 : : static char *dialect;
16397 : : };
16398 : :
16399 : : char *module_state_config::dialect;
16400 : :
16401 : : /* Generate a string of the significant compilation options.
16402 : : Generally assume the user knows what they're doing, in the same way
16403 : : that object files can be mixed. */
16404 : :
16405 : : const char *
16406 : 5159 : module_state_config::get_dialect ()
16407 : : {
16408 : 5159 : if (!dialect)
16409 : 8300 : dialect = concat (get_cxx_dialect_name (cxx_dialect),
16410 : : /* C++ implies these, only show if disabled. */
16411 : 4150 : flag_exceptions ? "" : "/no-exceptions",
16412 : 4150 : flag_rtti ? "" : "/no-rtti",
16413 : 4150 : flag_new_inheriting_ctors ? "" : "/old-inheriting-ctors",
16414 : : /* C++ 20 implies concepts and coroutines. */
16415 : 1328 : cxx_dialect < cxx20 && flag_concepts ? "/concepts" : "",
16416 : 4150 : (cxx_dialect < cxx20 && flag_coroutines
16417 : : ? "/coroutines" : ""),
16418 : 4150 : flag_module_implicit_inline ? "/implicit-inline" : "",
16419 : 4150 : flag_contracts ? "/contracts" : "",
16420 : : NULL);
16421 : :
16422 : 5159 : return dialect;
16423 : : }
16424 : :
16425 : : /* Contents of a cluster. */
16426 : : enum cluster_tag {
16427 : : ct_decl, /* A decl. */
16428 : : ct_defn, /* A definition. */
16429 : : ct_bind, /* A binding. */
16430 : : ct_hwm
16431 : : };
16432 : :
16433 : : /* Binding modifiers. */
16434 : : enum ct_bind_flags
16435 : : {
16436 : : cbf_export = 0x1, /* An exported decl. */
16437 : : cbf_hidden = 0x2, /* A hidden (friend) decl. */
16438 : : cbf_using = 0x4, /* A using decl. */
16439 : : };
16440 : :
16441 : : /* DEP belongs to a different cluster, seed it to prevent
16442 : : unfortunately timed duplicate import. */
16443 : : // FIXME: QOI For inter-cluster references we could just only pick
16444 : : // one entity from an earlier cluster. Even better track
16445 : : // dependencies between earlier clusters
16446 : :
16447 : : void
16448 : 5032781 : module_state::intercluster_seed (trees_out &sec, unsigned index_hwm, depset *dep)
16449 : : {
16450 : 5032781 : if (dep->is_tu_local ())
16451 : : /* We only stream placeholders for TU-local entities anyway. */;
16452 : 5032628 : else if (dep->is_import () || dep->cluster < index_hwm)
16453 : : {
16454 : 2247110 : tree ent = dep->get_entity ();
16455 : 2247110 : if (!TREE_VISITED (ent))
16456 : : {
16457 : 998548 : sec.tree_node (ent);
16458 : 998938 : dump (dumper::CLUSTER)
16459 : 390 : && dump ("Seeded %s %N",
16460 : 390 : dep->is_import () ? "import" : "intercluster", ent);
16461 : : }
16462 : : }
16463 : 5032781 : }
16464 : :
16465 : : /* Write the cluster of depsets in SCC[0-SIZE).
16466 : : dep->section -> section number
16467 : : dep->cluster -> entity number
16468 : : */
16469 : :
16470 : : unsigned
16471 : 223998 : module_state::write_cluster (elf_out *to, depset *scc[], unsigned size,
16472 : : depset::hash &table, unsigned *counts,
16473 : : unsigned *crc_ptr)
16474 : : {
16475 : 225252 : dump () && dump ("Writing section:%u %u depsets", table.section, size);
16476 : 223998 : dump.indent ();
16477 : :
16478 : 223998 : trees_out sec (to, this, table, table.section);
16479 : 223998 : sec.begin ();
16480 : 223998 : unsigned index_lwm = counts[MSC_entities];
16481 : :
16482 : : /* Determine entity numbers, mark for writing. */
16483 : 224280 : dump (dumper::CLUSTER) && dump ("Cluster members:") && (dump.indent (), true);
16484 : 1144965 : for (unsigned ix = 0; ix != size; ix++)
16485 : : {
16486 : 920967 : depset *b = scc[ix];
16487 : :
16488 : 920967 : switch (b->get_entity_kind ())
16489 : : {
16490 : 0 : default:
16491 : 0 : gcc_unreachable ();
16492 : :
16493 : 116353 : case depset::EK_BINDING:
16494 : 116353 : {
16495 : 116353 : dump (dumper::CLUSTER)
16496 : 195 : && dump ("[%u]=%s %P", ix, b->entity_kind_name (),
16497 : : b->get_entity (), b->get_name ());
16498 : 116353 : depset *ns_dep = b->deps[0];
16499 : 116353 : gcc_checking_assert (ns_dep->get_entity_kind ()
16500 : : == depset::EK_NAMESPACE
16501 : : && ns_dep->get_entity () == b->get_entity ());
16502 : 261988 : for (unsigned jx = b->deps.length (); --jx;)
16503 : : {
16504 : 145635 : depset *dep = b->deps[jx];
16505 : : // We could be declaring something that is also a
16506 : : // (merged) import
16507 : 159160 : gcc_checking_assert (dep->is_import ()
16508 : : || TREE_VISITED (dep->get_entity ())
16509 : : || (dep->get_entity_kind ()
16510 : : == depset::EK_USING));
16511 : : }
16512 : : }
16513 : : break;
16514 : :
16515 : 791089 : case depset::EK_DECL:
16516 : 791089 : case depset::EK_SPECIALIZATION:
16517 : 791089 : case depset::EK_PARTIAL:
16518 : 791089 : b->cluster = counts[MSC_entities]++;
16519 : 791089 : sec.mark_declaration (b->get_entity (), b->has_defn ());
16520 : : /* FALLTHROUGH */
16521 : :
16522 : 804614 : case depset::EK_USING:
16523 : 1609228 : gcc_checking_assert (!b->is_import ()
16524 : : && !b->is_unreached ());
16525 : 923139 : dump (dumper::CLUSTER)
16526 : 1014 : && dump ("[%u]=%s %s %N", ix, b->entity_kind_name (),
16527 : 639 : b->has_defn () ? "definition" : "declaration",
16528 : : b->get_entity ());
16529 : : break;
16530 : : }
16531 : : }
16532 : 224280 : dump (dumper::CLUSTER) && (dump.outdent (), true);
16533 : :
16534 : : /* Ensure every out-of-cluster decl is referenced before we start
16535 : : streaming. We must do both imports *and* earlier clusters,
16536 : : because the latter could reach into the former and cause a
16537 : : duplicate loop. */
16538 : 223998 : sec.set_importing (+1);
16539 : 1144965 : for (unsigned ix = 0; ix != size; ix++)
16540 : : {
16541 : 920967 : depset *b = scc[ix];
16542 : 10714677 : for (unsigned jx = b->is_special (); jx != b->deps.length (); jx++)
16543 : : {
16544 : 4438956 : depset *dep = b->deps[jx];
16545 : :
16546 : 4438956 : if (dep->is_binding ())
16547 : : {
16548 : 885095 : for (unsigned ix = dep->deps.length (); --ix;)
16549 : : {
16550 : 593825 : depset *bind = dep->deps[ix];
16551 : 593825 : if (bind->get_entity_kind () == depset::EK_USING)
16552 : 40016 : bind = bind->deps[1];
16553 : :
16554 : 593825 : intercluster_seed (sec, index_lwm, bind);
16555 : : }
16556 : : /* Also check the namespace itself. */
16557 : 145635 : dep = dep->deps[0];
16558 : : }
16559 : :
16560 : 4438956 : intercluster_seed (sec, index_lwm, dep);
16561 : : }
16562 : : }
16563 : 223998 : sec.tree_node (NULL_TREE);
16564 : : /* We're done importing now. */
16565 : 223998 : sec.set_importing (-1);
16566 : :
16567 : : /* Write non-definitions. */
16568 : 1144965 : for (unsigned ix = 0; ix != size; ix++)
16569 : : {
16570 : 920967 : depset *b = scc[ix];
16571 : 920967 : tree decl = b->get_entity ();
16572 : 920967 : switch (b->get_entity_kind ())
16573 : : {
16574 : 0 : default:
16575 : 0 : gcc_unreachable ();
16576 : 116353 : break;
16577 : :
16578 : 116353 : case depset::EK_BINDING:
16579 : 116353 : {
16580 : 116353 : gcc_assert (TREE_CODE (decl) == NAMESPACE_DECL);
16581 : 117383 : dump () && dump ("Depset:%u binding %C:%P", ix, TREE_CODE (decl),
16582 : : decl, b->get_name ());
16583 : 116353 : sec.u (ct_bind);
16584 : 116353 : sec.tree_node (decl);
16585 : 116353 : sec.tree_node (b->get_name ());
16586 : :
16587 : : /* Write in reverse order, so reading will see the exports
16588 : : first, thus building the overload chain will be
16589 : : optimized. */
16590 : 378341 : for (unsigned jx = b->deps.length (); --jx;)
16591 : : {
16592 : 145635 : depset *dep = b->deps[jx];
16593 : 145635 : tree bound = dep->get_entity ();
16594 : 145635 : unsigned flags = 0;
16595 : 145635 : if (dep->get_entity_kind () == depset::EK_USING)
16596 : : {
16597 : 13525 : tree ovl = bound;
16598 : 13525 : bound = OVL_FUNCTION (bound);
16599 : 13525 : if (!(TREE_CODE (bound) == CONST_DECL
16600 : 3747 : && UNSCOPED_ENUM_P (TREE_TYPE (bound))
16601 : 3142 : && decl == TYPE_NAME (TREE_TYPE (bound))))
16602 : : /* An unscoped enumerator in its enumeration's
16603 : : scope is not a using. */
16604 : : flags |= cbf_using;
16605 : 13525 : if (OVL_EXPORT_P (ovl))
16606 : 12674 : flags |= cbf_export;
16607 : : }
16608 : : else
16609 : : {
16610 : : /* An implicit typedef must be at one. */
16611 : 132110 : gcc_assert (!DECL_IMPLICIT_TYPEDEF_P (bound) || jx == 1);
16612 : 132110 : if (dep->is_hidden ())
16613 : : flags |= cbf_hidden;
16614 : 127872 : else if (DECL_MODULE_EXPORT_P (STRIP_TEMPLATE (bound)))
16615 : 117263 : flags |= cbf_export;
16616 : : }
16617 : :
16618 : 145635 : gcc_checking_assert (DECL_P (bound));
16619 : :
16620 : 145635 : sec.i (flags);
16621 : 145635 : sec.tree_node (bound);
16622 : : }
16623 : :
16624 : : /* Terminate the list. */
16625 : 116353 : sec.i (-1);
16626 : : }
16627 : 116353 : break;
16628 : :
16629 : 13525 : case depset::EK_USING:
16630 : 13549 : dump () && dump ("Depset:%u %s %C:%N", ix, b->entity_kind_name (),
16631 : 24 : TREE_CODE (decl), decl);
16632 : : break;
16633 : :
16634 : 791089 : case depset::EK_SPECIALIZATION:
16635 : 791089 : case depset::EK_PARTIAL:
16636 : 791089 : case depset::EK_DECL:
16637 : 793237 : dump () && dump ("Depset:%u %s entity:%u %C:%N", ix,
16638 : : b->entity_kind_name (), b->cluster,
16639 : 2148 : TREE_CODE (decl), decl);
16640 : :
16641 : 791089 : sec.u (ct_decl);
16642 : 791089 : sec.tree_node (decl);
16643 : :
16644 : 923115 : dump () && dump ("Wrote declaration entity:%u %C:%N",
16645 : 2148 : b->cluster, TREE_CODE (decl), decl);
16646 : : break;
16647 : : }
16648 : : }
16649 : :
16650 : : depset *namer = NULL;
16651 : :
16652 : : /* Write out definitions */
16653 : 1144965 : for (unsigned ix = 0; ix != size; ix++)
16654 : : {
16655 : 920967 : depset *b = scc[ix];
16656 : 920967 : tree decl = b->get_entity ();
16657 : 920967 : switch (b->get_entity_kind ())
16658 : : {
16659 : : default:
16660 : : break;
16661 : :
16662 : 791089 : case depset::EK_SPECIALIZATION:
16663 : 791089 : case depset::EK_PARTIAL:
16664 : 791089 : case depset::EK_DECL:
16665 : 791089 : if (!namer)
16666 : 216237 : namer = b;
16667 : :
16668 : 791089 : if (b->has_defn ())
16669 : : {
16670 : 306914 : sec.u (ct_defn);
16671 : 306914 : sec.tree_node (decl);
16672 : 307489 : dump () && dump ("Writing definition %N", decl);
16673 : 306914 : sec.write_definition (decl, b->refs_tu_local ());
16674 : :
16675 : 306914 : if (!namer->has_defn ())
16676 : 920967 : namer = b;
16677 : : }
16678 : : break;
16679 : : }
16680 : : }
16681 : :
16682 : : /* We don't find the section by name. Use depset's decl's name for
16683 : : human friendliness. */
16684 : 223998 : unsigned name = 0;
16685 : 223998 : tree naming_decl = NULL_TREE;
16686 : 223998 : if (namer)
16687 : : {
16688 : 216237 : naming_decl = namer->get_entity ();
16689 : 216237 : if (namer->get_entity_kind () == depset::EK_USING)
16690 : : /* This unfortunately names the section from the target of the
16691 : : using decl. But the name is only a guide, so Do Not Care. */
16692 : 0 : naming_decl = OVL_FUNCTION (naming_decl);
16693 : 216237 : if (DECL_IMPLICIT_TYPEDEF_P (naming_decl))
16694 : : /* Lose any anonymousness. */
16695 : 74109 : naming_decl = TYPE_NAME (TREE_TYPE (naming_decl));
16696 : 216237 : name = to->qualified_name (naming_decl, namer->has_defn ());
16697 : : }
16698 : :
16699 : 223998 : unsigned bytes = sec.pos;
16700 : 223998 : unsigned snum = sec.end (to, name, crc_ptr);
16701 : :
16702 : 1144965 : for (unsigned ix = size; ix--;)
16703 : 920967 : gcc_checking_assert (scc[ix]->section == snum);
16704 : :
16705 : 223998 : dump.outdent ();
16706 : 225252 : dump () && dump ("Wrote section:%u named-by:%N", table.section, naming_decl);
16707 : :
16708 : 223998 : return bytes;
16709 : 223998 : }
16710 : :
16711 : : /* Read a cluster from section SNUM. */
16712 : :
16713 : : bool
16714 : 165037 : module_state::read_cluster (unsigned snum)
16715 : : {
16716 : 165037 : trees_in sec (this);
16717 : :
16718 : 165037 : if (!sec.begin (loc, from (), snum))
16719 : : return false;
16720 : :
16721 : 166150 : dump () && dump ("Reading section:%u", snum);
16722 : 165037 : dump.indent ();
16723 : :
16724 : : /* We care about structural equality. */
16725 : 165037 : comparing_dependent_aliases++;
16726 : :
16727 : : /* First seed the imports. */
16728 : 949954 : while (tree import = sec.tree_node ())
16729 : 1735036 : dump (dumper::CLUSTER) && dump ("Seeded import %N", import);
16730 : :
16731 : 1122169 : while (!sec.get_overrun () && sec.more_p ())
16732 : : {
16733 : 957132 : unsigned ct = sec.u ();
16734 : 957132 : switch (ct)
16735 : : {
16736 : 0 : default:
16737 : 0 : sec.set_overrun ();
16738 : 0 : break;
16739 : :
16740 : 86352 : case ct_bind:
16741 : : /* A set of namespace bindings. */
16742 : 86352 : {
16743 : 86352 : tree ns = sec.tree_node ();
16744 : 86352 : tree name = sec.tree_node ();
16745 : 86352 : tree decls = NULL_TREE;
16746 : 86352 : tree visible = NULL_TREE;
16747 : 86352 : tree type = NULL_TREE;
16748 : 86352 : bool dedup = false;
16749 : 86352 : bool global_p = is_header ();
16750 : :
16751 : : /* We rely on the bindings being in the reverse order of
16752 : : the resulting overload set. */
16753 : 197709 : for (;;)
16754 : : {
16755 : 197709 : int flags = sec.i ();
16756 : 197709 : if (flags < 0)
16757 : : break;
16758 : :
16759 : 111357 : if ((flags & cbf_hidden)
16760 : 3604 : && (flags & (cbf_using | cbf_export)))
16761 : 0 : sec.set_overrun ();
16762 : :
16763 : 111357 : tree decl = sec.tree_node ();
16764 : 111357 : if (sec.get_overrun ())
16765 : : break;
16766 : :
16767 : 111357 : if (!global_p)
16768 : : {
16769 : : /* Check if the decl could require GM merging. */
16770 : 2628 : tree orig = get_originating_module_decl (decl);
16771 : 2628 : tree inner = STRIP_TEMPLATE (orig);
16772 : 2628 : if (!DECL_LANG_SPECIFIC (inner)
16773 : 5256 : || !DECL_MODULE_ATTACH_P (inner))
16774 : : global_p = true;
16775 : : }
16776 : :
16777 : 111357 : if (decls && TREE_CODE (decl) == TYPE_DECL)
16778 : : {
16779 : : /* Stat hack. */
16780 : 45 : if (type || !DECL_IMPLICIT_TYPEDEF_P (decl))
16781 : 0 : sec.set_overrun ();
16782 : :
16783 : 45 : if (flags & cbf_using)
16784 : : {
16785 : 3 : type = build_lang_decl_loc (UNKNOWN_LOCATION,
16786 : : USING_DECL,
16787 : 3 : DECL_NAME (decl),
16788 : : NULL_TREE);
16789 : 3 : USING_DECL_DECLS (type) = decl;
16790 : 3 : USING_DECL_SCOPE (type) = CP_DECL_CONTEXT (decl);
16791 : 3 : DECL_CONTEXT (type) = ns;
16792 : :
16793 : 3 : DECL_MODULE_PURVIEW_P (type) = true;
16794 : 3 : if (flags & cbf_export)
16795 : 3 : DECL_MODULE_EXPORT_P (type) = true;
16796 : : }
16797 : : else
16798 : : type = decl;
16799 : : }
16800 : : else
16801 : : {
16802 : 111312 : if ((flags & cbf_using) &&
16803 : 9769 : !DECL_DECLARES_FUNCTION_P (decl))
16804 : : {
16805 : : /* We should only see a single non-function using-decl
16806 : : for a binding; more than that would clash. */
16807 : 3788 : if (decls)
16808 : 0 : sec.set_overrun ();
16809 : :
16810 : : /* FIXME: Propagate the location of the using-decl
16811 : : for use in diagnostics. */
16812 : 3788 : decls = build_lang_decl_loc (UNKNOWN_LOCATION,
16813 : : USING_DECL,
16814 : 3788 : DECL_NAME (decl),
16815 : : NULL_TREE);
16816 : 3788 : USING_DECL_DECLS (decls) = decl;
16817 : : /* We don't currently record the actual scope of the
16818 : : using-declaration, but this approximation should
16819 : : generally be good enough. */
16820 : 3788 : USING_DECL_SCOPE (decls) = CP_DECL_CONTEXT (decl);
16821 : 3788 : DECL_CONTEXT (decls) = ns;
16822 : :
16823 : 3788 : DECL_MODULE_PURVIEW_P (decls) = true;
16824 : 3788 : if (flags & cbf_export)
16825 : 3782 : DECL_MODULE_EXPORT_P (decls) = true;
16826 : : }
16827 : 107524 : else if (decls
16828 : 82564 : || (flags & (cbf_hidden | cbf_using))
16829 : 185206 : || DECL_FUNCTION_TEMPLATE_P (decl))
16830 : : {
16831 : 41388 : decls = ovl_make (decl, decls);
16832 : 41388 : if (flags & cbf_using)
16833 : : {
16834 : 5981 : dedup = true;
16835 : 5981 : OVL_USING_P (decls) = true;
16836 : 5981 : OVL_PURVIEW_P (decls) = true;
16837 : 5981 : if (flags & cbf_export)
16838 : 5966 : OVL_EXPORT_P (decls) = true;
16839 : : }
16840 : :
16841 : 41388 : if (flags & cbf_hidden)
16842 : 3604 : OVL_HIDDEN_P (decls) = true;
16843 : 37784 : else if (dedup)
16844 : 5981 : OVL_DEDUP_P (decls) = true;
16845 : : }
16846 : : else
16847 : : decls = decl;
16848 : :
16849 : 111306 : if (flags & cbf_export
16850 : 111312 : || (!(flags & cbf_hidden)
16851 : 715 : && (is_module () || is_partition ())))
16852 : : visible = decls;
16853 : : }
16854 : : }
16855 : :
16856 : 86352 : if (!decls)
16857 : 0 : sec.set_overrun ();
16858 : :
16859 : 86352 : if (sec.get_overrun ())
16860 : : break; /* Bail. */
16861 : :
16862 : 87162 : dump () && dump ("Binding of %P", ns, name);
16863 : 86352 : if (!set_module_binding (ns, name, mod, global_p,
16864 : 86352 : is_module () || is_partition (),
16865 : : decls, type, visible))
16866 : 0 : sec.set_overrun ();
16867 : : }
16868 : : break;
16869 : :
16870 : 629867 : case ct_decl:
16871 : : /* A decl. */
16872 : 629867 : {
16873 : 629867 : tree decl = sec.tree_node ();
16874 : 1754909 : dump () && dump ("Read declaration of %N", decl);
16875 : : }
16876 : : break;
16877 : :
16878 : 240913 : case ct_defn:
16879 : 240913 : {
16880 : 240913 : tree decl = sec.tree_node ();
16881 : 241970 : dump () && dump ("Reading definition of %N", decl);
16882 : 240913 : sec.read_definition (decl);
16883 : : }
16884 : 240913 : break;
16885 : : }
16886 : : }
16887 : :
16888 : : /* When lazy loading is in effect, we can be in the middle of
16889 : : parsing or instantiating a function. Save it away.
16890 : : push_function_context does too much work. */
16891 : 165037 : tree old_cfd = current_function_decl;
16892 : 165037 : struct function *old_cfun = cfun;
16893 : 265787 : for (const post_process_data& pdata : sec.post_process ())
16894 : : {
16895 : 71530 : tree decl = pdata.decl;
16896 : :
16897 : 71530 : bool abstract = false;
16898 : 71530 : if (TREE_CODE (decl) == TEMPLATE_DECL)
16899 : : {
16900 : 39281 : abstract = true;
16901 : 39281 : decl = DECL_TEMPLATE_RESULT (decl);
16902 : : }
16903 : :
16904 : 71530 : current_function_decl = decl;
16905 : 71530 : allocate_struct_function (decl, abstract);
16906 : 71530 : cfun->language = ggc_cleared_alloc<language_function> ();
16907 : 71530 : cfun->language->base.x_stmt_tree.stmts_are_full_exprs_p = 1;
16908 : 71530 : cfun->function_start_locus = pdata.start_locus;
16909 : 71530 : cfun->function_end_locus = pdata.end_locus;
16910 : 71530 : cfun->language->returns_value = pdata.returns_value;
16911 : 71530 : cfun->language->returns_null = pdata.returns_null;
16912 : 71530 : cfun->language->returns_abnormally = pdata.returns_abnormally;
16913 : 71530 : cfun->language->infinite_loop = pdata.infinite_loop;
16914 : :
16915 : : /* Make sure we emit explicit instantiations.
16916 : : FIXME do we want to do this in expand_or_defer_fn instead? */
16917 : 71530 : if (DECL_EXPLICIT_INSTANTIATION (decl)
16918 : 71530 : && !DECL_EXTERNAL (decl))
16919 : 18 : setup_explicit_instantiation_definition_linkage (decl);
16920 : :
16921 : 71530 : if (abstract)
16922 : : ;
16923 : 32249 : else if (DECL_MAYBE_IN_CHARGE_CDTOR_P (decl))
16924 : 5413 : vec_safe_push (post_load_decls, decl);
16925 : : else
16926 : : {
16927 : 26836 : bool aggr = aggregate_value_p (DECL_RESULT (decl), decl);
16928 : : #ifdef PCC_STATIC_STRUCT_RETURN
16929 : : cfun->returns_pcc_struct = aggr;
16930 : : #endif
16931 : 26836 : cfun->returns_struct = aggr;
16932 : 26836 : expand_or_defer_fn (decl);
16933 : :
16934 : : /* If we first see this function after at_eof, it doesn't get
16935 : : note_vague_linkage_fn from tentative_decl_linkage, so the loop in
16936 : : c_parse_final_cleanups won't consider it. But with DECL_COMDAT we
16937 : : can just clear DECL_EXTERNAL and let cgraph decide.
16938 : : FIXME handle this outside module.cc after GCC 15. */
16939 : 2893 : if (at_eof && DECL_COMDAT (decl) && DECL_EXTERNAL (decl)
16940 : 27636 : && DECL_NOT_REALLY_EXTERN (decl))
16941 : 768 : DECL_EXTERNAL (decl) = false;
16942 : : }
16943 : :
16944 : : }
16945 : : /* Look, function.cc's interface to cfun does too much for us, we
16946 : : just need to restore the old value. I do not want to go
16947 : : redesigning that API right now. */
16948 : : #undef cfun
16949 : 165037 : cfun = old_cfun;
16950 : 165037 : current_function_decl = old_cfd;
16951 : 165037 : comparing_dependent_aliases--;
16952 : :
16953 : 165037 : dump.outdent ();
16954 : 166150 : dump () && dump ("Read section:%u", snum);
16955 : :
16956 : 165037 : loaded_clusters++;
16957 : :
16958 : 165037 : if (!sec.end (from ()))
16959 : : return false;
16960 : :
16961 : : return true;
16962 : 165037 : }
16963 : :
16964 : : void
16965 : 118280 : module_state::write_namespace (bytes_out &sec, depset *dep)
16966 : : {
16967 : 118280 : unsigned ns_num = dep->cluster;
16968 : 118280 : unsigned ns_import = 0;
16969 : :
16970 : 118280 : if (dep->is_import ())
16971 : 0 : ns_import = dep->section;
16972 : 118280 : else if (dep->get_entity () != global_namespace)
16973 : 73969 : ns_num++;
16974 : :
16975 : 118280 : sec.u (ns_import);
16976 : 118280 : sec.u (ns_num);
16977 : 118280 : }
16978 : :
16979 : : tree
16980 : 142457 : module_state::read_namespace (bytes_in &sec)
16981 : : {
16982 : 142457 : unsigned ns_import = sec.u ();
16983 : 142457 : unsigned ns_num = sec.u ();
16984 : 142457 : tree ns = NULL_TREE;
16985 : :
16986 : 142457 : if (ns_import || ns_num)
16987 : : {
16988 : 88022 : if (!ns_import)
16989 : 88022 : ns_num--;
16990 : :
16991 : 88022 : if (unsigned origin = slurp->remap_module (ns_import))
16992 : : {
16993 : 88022 : module_state *from = (*modules)[origin];
16994 : 88022 : if (ns_num < from->entity_num)
16995 : : {
16996 : 88022 : binding_slot &slot = (*entity_ary)[from->entity_lwm + ns_num];
16997 : :
16998 : 88022 : if (!slot.is_lazy ())
16999 : 88022 : ns = slot;
17000 : : }
17001 : : }
17002 : : else
17003 : 0 : sec.set_overrun ();
17004 : : }
17005 : : else
17006 : 54435 : ns = global_namespace;
17007 : :
17008 : 142457 : return ns;
17009 : : }
17010 : :
17011 : : /* SPACES is a sorted vector of namespaces. Write out the namespaces
17012 : : to MOD_SNAME_PFX.nms section. */
17013 : :
17014 : : void
17015 : 471 : module_state::write_namespaces (elf_out *to, vec<depset *> spaces,
17016 : : unsigned num, unsigned *crc_p)
17017 : : {
17018 : 508 : dump () && dump ("Writing namespaces");
17019 : 471 : dump.indent ();
17020 : :
17021 : 471 : bytes_out sec (to);
17022 : 471 : sec.begin ();
17023 : :
17024 : 471 : hash_map<tree, unsigned> ns_map;
17025 : :
17026 : 2398 : for (unsigned ix = 0; ix != num; ix++)
17027 : : {
17028 : 1927 : depset *b = spaces[ix];
17029 : 1927 : tree ns = b->get_entity ();
17030 : :
17031 : 1927 : ns_map.put (ns, ix);
17032 : :
17033 : : /* This could be an anonymous namespace even for a named module,
17034 : : since we can still emit no-linkage decls. */
17035 : 1927 : gcc_checking_assert (TREE_CODE (ns) == NAMESPACE_DECL);
17036 : :
17037 : 1927 : unsigned flags = 0;
17038 : 1927 : if (TREE_PUBLIC (ns))
17039 : 1881 : flags |= 1;
17040 : 1927 : if (DECL_NAMESPACE_INLINE_P (ns))
17041 : 354 : flags |= 2;
17042 : 1927 : if (DECL_MODULE_PURVIEW_P (ns))
17043 : 1597 : flags |= 4;
17044 : 1927 : if (DECL_MODULE_EXPORT_P (ns))
17045 : 1449 : flags |= 8;
17046 : :
17047 : 1997 : dump () && dump ("Writing namespace:%u %N%s%s%s%s",
17048 : : b->cluster, ns,
17049 : 70 : flags & 1 ? ", public" : "",
17050 : 70 : flags & 2 ? ", inline" : "",
17051 : 70 : flags & 4 ? ", purview" : "",
17052 : 70 : flags & 8 ? ", export" : "");
17053 : 1927 : sec.u (b->cluster);
17054 : 1927 : sec.u (to->name (DECL_NAME (ns)));
17055 : 1927 : write_namespace (sec, b->deps[0]);
17056 : :
17057 : 1927 : sec.u (flags);
17058 : 1927 : write_location (sec, DECL_SOURCE_LOCATION (ns));
17059 : :
17060 : 1927 : if (DECL_NAMESPACE_INLINE_P (ns))
17061 : : {
17062 : 354 : if (tree attr = lookup_attribute ("abi_tag", DECL_ATTRIBUTES (ns)))
17063 : : {
17064 : 110 : tree tags = TREE_VALUE (attr);
17065 : 110 : sec.u (list_length (tags));
17066 : 223 : for (tree tag = tags; tag; tag = TREE_CHAIN (tag))
17067 : 113 : sec.str (TREE_STRING_POINTER (TREE_VALUE (tag)));
17068 : : }
17069 : : else
17070 : 244 : sec.u (0);
17071 : : }
17072 : : }
17073 : :
17074 : : /* Now write exported using-directives, as a sequence of 1-origin indices in
17075 : : the spaces array (not entity indices): First the using namespace, then the
17076 : : used namespaces. And then a zero terminating the list. :: is
17077 : : represented as index -1. */
17078 : 2869 : auto emit_one_ns = [&](unsigned ix, tree ns) {
17079 : 2746 : for (auto udir: NAMESPACE_LEVEL (ns)->using_directives)
17080 : : {
17081 : 116 : if (TREE_CODE (udir) != USING_DECL || !DECL_MODULE_EXPORT_P (udir))
17082 : 40 : continue;
17083 : 76 : tree ns2 = USING_DECL_DECLS (udir);
17084 : 76 : dump() && dump ("Writing using-directive in %N for %N",
17085 : : ns, ns2);
17086 : 76 : sec.u (ix);
17087 : 76 : sec.u (*ns_map.get (ns2) + 1);
17088 : : }
17089 : 2869 : };
17090 : 471 : emit_one_ns (-1, global_namespace);
17091 : 2398 : for (unsigned ix = 0; ix != num; ix++)
17092 : : {
17093 : 1927 : depset *b = spaces[ix];
17094 : 1927 : tree ns = b->get_entity ();
17095 : 1927 : emit_one_ns (ix + 1, ns);
17096 : : }
17097 : 471 : sec.u (0);
17098 : :
17099 : 471 : sec.end (to, to->name (MOD_SNAME_PFX ".nms"), crc_p);
17100 : 471 : dump.outdent ();
17101 : 471 : }
17102 : :
17103 : : /* Read the namespace hierarchy from MOD_SNAME_PFX.namespace. Fill in
17104 : : SPACES from that data. */
17105 : :
17106 : : bool
17107 : 486 : module_state::read_namespaces (unsigned num)
17108 : : {
17109 : 486 : bytes_in sec;
17110 : :
17111 : 486 : if (!sec.begin (loc, from (), MOD_SNAME_PFX ".nms"))
17112 : : return false;
17113 : :
17114 : 541 : dump () && dump ("Reading namespaces");
17115 : 486 : dump.indent ();
17116 : :
17117 : 486 : tree *ns_map = XALLOCAVEC (tree, num);
17118 : :
17119 : 2683 : for (unsigned ix = 0; ix != num; ix++)
17120 : : {
17121 : 2197 : unsigned entity_index = sec.u ();
17122 : 2197 : unsigned name = sec.u ();
17123 : :
17124 : 2197 : tree parent = read_namespace (sec);
17125 : :
17126 : : /* See comment in write_namespace about why not bits. */
17127 : 2197 : unsigned flags = sec.u ();
17128 : 2197 : location_t src_loc = read_location (sec);
17129 : 2197 : unsigned tags_count = (flags & 2) ? sec.u () : 0;
17130 : :
17131 : 2197 : if (entity_index >= entity_num
17132 : 2197 : || !parent
17133 : 2197 : || (flags & 0xc) == 0x8)
17134 : 0 : sec.set_overrun ();
17135 : :
17136 : : tree tags = NULL_TREE;
17137 : 2320 : while (tags_count--)
17138 : : {
17139 : 123 : size_t len;
17140 : 123 : const char *str = sec.str (&len);
17141 : 123 : tags = tree_cons (NULL_TREE, build_string (len + 1, str), tags);
17142 : 123 : tags = nreverse (tags);
17143 : : }
17144 : :
17145 : 2197 : if (sec.get_overrun ())
17146 : : break;
17147 : :
17148 : 4348 : tree id = name ? get_identifier (from ()->name (name)) : NULL_TREE;
17149 : :
17150 : 2337 : dump () && dump ("Read namespace:%u %P%s%s%s%s",
17151 : : entity_index, parent, id,
17152 : 70 : flags & 1 ? ", public" : "",
17153 : : flags & 2 ? ", inline" : "",
17154 : 70 : flags & 4 ? ", purview" : "",
17155 : 70 : flags & 8 ? ", export" : "");
17156 : 2197 : bool visible_p = ((flags & 8)
17157 : 2197 : || ((flags & 1)
17158 : 458 : && (flags & 4)
17159 : 44 : && (is_partition () || is_module ())));
17160 : 2197 : tree inner = add_imported_namespace (parent, id, src_loc, mod,
17161 : : bool (flags & 2), visible_p);
17162 : 2197 : if (!inner)
17163 : : {
17164 : 0 : sec.set_overrun ();
17165 : 0 : break;
17166 : : }
17167 : :
17168 : 2197 : if (is_partition ())
17169 : : {
17170 : 30 : if (flags & 4)
17171 : 24 : DECL_MODULE_PURVIEW_P (inner) = true;
17172 : 30 : if (flags & 8)
17173 : 24 : DECL_MODULE_EXPORT_P (inner) = true;
17174 : : }
17175 : :
17176 : 2197 : if (tags)
17177 : 120 : DECL_ATTRIBUTES (inner)
17178 : 240 : = tree_cons (get_identifier ("abi_tag"), tags, DECL_ATTRIBUTES (inner));
17179 : :
17180 : 2197 : ns_map[ix] = inner;
17181 : :
17182 : : /* Install the namespace. */
17183 : 2197 : (*entity_ary)[entity_lwm + entity_index] = inner;
17184 : 2197 : if (DECL_MODULE_IMPORT_P (inner))
17185 : : {
17186 : 0 : bool existed;
17187 : 0 : unsigned *slot = &entity_map->get_or_insert
17188 : 0 : (DECL_UID (inner), &existed);
17189 : 0 : if (existed)
17190 : : /* If it existed, it should match. */
17191 : 0 : gcc_checking_assert (inner == (*entity_ary)[*slot]);
17192 : : else
17193 : 0 : *slot = entity_lwm + entity_index;
17194 : : }
17195 : : }
17196 : :
17197 : : /* Read the exported using-directives. */
17198 : 577 : while (unsigned ix = sec.u ())
17199 : : {
17200 : 91 : tree ns;
17201 : 91 : if (ix == (unsigned)-1)
17202 : 39 : ns = global_namespace;
17203 : : else
17204 : : {
17205 : 52 : if (--ix >= num)
17206 : : {
17207 : 0 : sec.set_overrun ();
17208 : 0 : break;
17209 : : }
17210 : 52 : ns = ns_map [ix];
17211 : : }
17212 : 91 : unsigned ix2 = sec.u ();
17213 : 91 : if (--ix2 >= num)
17214 : : {
17215 : 0 : sec.set_overrun ();
17216 : 0 : break;
17217 : : }
17218 : 91 : tree ns2 = ns_map [ix2];
17219 : 91 : if (directness)
17220 : : {
17221 : 79 : dump() && dump ("Reading using-directive in %N for %N",
17222 : : ns, ns2);
17223 : : /* In an export import this will mark the using-directive as
17224 : : exported, so it will be emitted again. */
17225 : 79 : add_using_namespace (ns, ns2);
17226 : : }
17227 : : else
17228 : : /* Ignore using-directives from indirect imports, we only want them
17229 : : from our own imports. */
17230 : 589 : dump() && dump ("Ignoring using-directive in %N for %N",
17231 : : ns, ns2);
17232 : : }
17233 : :
17234 : 486 : dump.outdent ();
17235 : 486 : if (!sec.end (from ()))
17236 : : return false;
17237 : : return true;
17238 : 486 : }
17239 : :
17240 : : /* Write the binding TABLE to MOD_SNAME_PFX.bnd */
17241 : :
17242 : : unsigned
17243 : 2398 : module_state::write_bindings (elf_out *to, vec<depset *> sccs, unsigned *crc_p)
17244 : : {
17245 : 2650 : dump () && dump ("Writing binding table");
17246 : 2398 : dump.indent ();
17247 : :
17248 : 2398 : unsigned num = 0;
17249 : 2398 : bytes_out sec (to);
17250 : 2398 : sec.begin ();
17251 : :
17252 : 1854902 : for (unsigned ix = 0; ix != sccs.length (); ix++)
17253 : : {
17254 : 925053 : depset *b = sccs[ix];
17255 : 925053 : if (b->is_binding ())
17256 : : {
17257 : 116353 : tree ns = b->get_entity ();
17258 : 117383 : dump () && dump ("Bindings %P section:%u", ns, b->get_name (),
17259 : : b->section);
17260 : 116353 : sec.u (to->name (b->get_name ()));
17261 : 116353 : write_namespace (sec, b->deps[0]);
17262 : 116353 : sec.u (b->section);
17263 : 116353 : num++;
17264 : : }
17265 : : }
17266 : :
17267 : 2398 : sec.end (to, to->name (MOD_SNAME_PFX ".bnd"), crc_p);
17268 : 2398 : dump.outdent ();
17269 : :
17270 : 2398 : return num;
17271 : 2398 : }
17272 : :
17273 : : /* Read the binding table from MOD_SNAME_PFX.bind. */
17274 : :
17275 : : bool
17276 : 2560 : module_state::read_bindings (unsigned num, unsigned lwm, unsigned hwm)
17277 : : {
17278 : 2560 : bytes_in sec;
17279 : :
17280 : 2560 : if (!sec.begin (loc, from (), MOD_SNAME_PFX ".bnd"))
17281 : : return false;
17282 : :
17283 : 3032 : dump () && dump ("Reading binding table");
17284 : 2560 : dump.indent ();
17285 : 142820 : for (; !sec.get_overrun () && num--;)
17286 : : {
17287 : 140260 : const char *name = from ()->name (sec.u ());
17288 : 140260 : tree ns = read_namespace (sec);
17289 : 140260 : unsigned snum = sec.u ();
17290 : :
17291 : 140260 : if (!ns || !name || (snum - lwm) >= (hwm - lwm))
17292 : 0 : sec.set_overrun ();
17293 : 140260 : if (!sec.get_overrun ())
17294 : : {
17295 : 140260 : tree id = get_identifier (name);
17296 : 141687 : dump () && dump ("Bindings %P section:%u", ns, id, snum);
17297 : 140260 : if (mod && !import_module_binding (ns, id, mod, snum))
17298 : : break;
17299 : : }
17300 : : }
17301 : :
17302 : 2560 : dump.outdent ();
17303 : 2560 : if (!sec.end (from ()))
17304 : : return false;
17305 : : return true;
17306 : 2560 : }
17307 : :
17308 : : /* Write the entity table to MOD_SNAME_PFX.ent
17309 : :
17310 : : Each entry is a section number. */
17311 : :
17312 : : void
17313 : 2156 : module_state::write_entities (elf_out *to, vec<depset *> depsets,
17314 : : unsigned count, unsigned *crc_p)
17315 : : {
17316 : 2361 : dump () && dump ("Writing entities");
17317 : 2156 : dump.indent ();
17318 : :
17319 : 2156 : bytes_out sec (to);
17320 : 2156 : sec.begin ();
17321 : :
17322 : 2156 : unsigned current = 0;
17323 : 927194 : for (unsigned ix = 0; ix < depsets.length (); ix++)
17324 : : {
17325 : 925038 : depset *d = depsets[ix];
17326 : :
17327 : 1720210 : switch (d->get_entity_kind ())
17328 : : {
17329 : : default:
17330 : : break;
17331 : :
17332 : 4083 : case depset::EK_NAMESPACE:
17333 : 4083 : if (!d->is_import () && d->get_entity () != global_namespace)
17334 : : {
17335 : 1927 : gcc_checking_assert (d->cluster == current);
17336 : 1927 : current++;
17337 : 1927 : sec.u (0);
17338 : : }
17339 : : break;
17340 : :
17341 : 791089 : case depset::EK_DECL:
17342 : 791089 : case depset::EK_SPECIALIZATION:
17343 : 791089 : case depset::EK_PARTIAL:
17344 : 1582178 : gcc_checking_assert (!d->is_unreached ()
17345 : : && !d->is_import ()
17346 : : && d->cluster == current
17347 : : && d->section);
17348 : 791089 : current++;
17349 : 791089 : sec.u (d->section);
17350 : 791089 : break;
17351 : : }
17352 : : }
17353 : 2156 : gcc_assert (count == current);
17354 : 2156 : sec.end (to, to->name (MOD_SNAME_PFX ".ent"), crc_p);
17355 : 2156 : dump.outdent ();
17356 : 2156 : }
17357 : :
17358 : : bool
17359 : 2355 : module_state::read_entities (unsigned count, unsigned lwm, unsigned hwm)
17360 : : {
17361 : 2355 : trees_in sec (this);
17362 : :
17363 : 2355 : if (!sec.begin (loc, from (), MOD_SNAME_PFX ".ent"))
17364 : : return false;
17365 : :
17366 : 2776 : dump () && dump ("Reading entities");
17367 : 2355 : dump.indent ();
17368 : :
17369 : 958067 : for (binding_slot *slot = entity_ary->begin () + entity_lwm; count--; slot++)
17370 : : {
17371 : 955712 : unsigned snum = sec.u ();
17372 : 955712 : if (snum && (snum - lwm) >= (hwm - lwm))
17373 : 0 : sec.set_overrun ();
17374 : 955712 : if (sec.get_overrun ())
17375 : : break;
17376 : :
17377 : 955712 : if (snum)
17378 : 953515 : slot->set_lazy (snum << 2);
17379 : : }
17380 : :
17381 : 2355 : dump.outdent ();
17382 : 2355 : if (!sec.end (from ()))
17383 : : return false;
17384 : : return true;
17385 : 2355 : }
17386 : :
17387 : : /* Write the pending table to MOD_SNAME_PFX.pnd
17388 : :
17389 : : The pending table holds information about clusters that need to be
17390 : : loaded because they contain information about something that is not
17391 : : found by namespace-scope lookup.
17392 : :
17393 : : The three cases are:
17394 : :
17395 : : (a) Template (maybe-partial) specializations that we have
17396 : : instantiated or defined. When an importer needs to instantiate
17397 : : that template, they /must have/ the partial, explicit & extern
17398 : : specializations available. If they have the other specializations
17399 : : available, they'll have less work to do. Thus, when we're about to
17400 : : instantiate FOO, we have to be able to ask 'are there any
17401 : : specialization of FOO in our imports?'.
17402 : :
17403 : : (b) (Maybe-implicit) member functions definitions. A class could
17404 : : be defined in one header, and an inline member defined in a
17405 : : different header (this occurs in the STL). Similarly, like the
17406 : : specialization case, an implicit member function could have been
17407 : : 'instantiated' in one module, and it'd be nice to not have to
17408 : : reinstantiate it in another.
17409 : :
17410 : : (c) Classes completed elsewhere. A class could be declared in one
17411 : : header and defined in another. We need to know to load the class
17412 : : definition before looking in it. It does highlight an issue --
17413 : : there could be an intermediate import between the outermost containing
17414 : : namespace-scope class and the innermost being-defined class. This is
17415 : : actually possible with all of these cases, so be aware -- we're not
17416 : : just talking of one level of import to get to the innermost namespace.
17417 : :
17418 : : This gets complicated fast, it took me multiple attempts to even
17419 : : get something remotely working. Partially because I focussed on
17420 : : optimizing what I think turns out to be a smaller problem, given
17421 : : the known need to do the more general case *anyway*. I document
17422 : : the smaller problem, because it does appear to be the natural way
17423 : : to do it. It's trap!
17424 : :
17425 : : **** THE TRAP
17426 : :
17427 : : Let's refer to the primary template or the containing class as the
17428 : : KEY. And the specialization or member as the PENDING-ENTITY. (To
17429 : : avoid having to say those mouthfuls all the time.)
17430 : :
17431 : : In either case, we have an entity and we need some way of mapping
17432 : : that to a set of entities that need to be loaded before we can
17433 : : proceed with whatever processing of the entity we were going to do.
17434 : :
17435 : : We need to link the key to the pending-entity in some way. Given a
17436 : : key, tell me the pending-entities I need to have loaded. However
17437 : : we tie the key to the pending-entity must not rely on the key being
17438 : : loaded -- that'd defeat the lazy loading scheme.
17439 : :
17440 : : As the key will be an import in we know its entity number (either
17441 : : because we imported it, or we're writing it out too). Thus we can
17442 : : generate a map of key-indices to pending-entities. The
17443 : : pending-entity indices will be into our span of the entity table,
17444 : : and thus allow them to be lazily loaded. The key index will be
17445 : : into another slot of the entity table. Notice that this checking
17446 : : could be expensive, we don't want to iterate over a bunch of
17447 : : pending-entity indices (across multiple imports), every time we're
17448 : : about do to the thing with the key. We need to quickly determine
17449 : : 'definitely nothing needed'.
17450 : :
17451 : : That's almost good enough, except that key indices are not unique
17452 : : in a couple of cases :( Specifically the Global Module or a module
17453 : : partition can result in multiple modules assigning an entity index
17454 : : for the key. The decl-merging on loading will detect that so we
17455 : : only have one Key loaded, and in the entity hash it'll indicate the
17456 : : entity index of first load. Which might be different to how we
17457 : : know it. Notice this is restricted to GM entities or this-module
17458 : : entities. Foreign imports cannot have this.
17459 : :
17460 : : We can simply resolve this in the direction of how this module
17461 : : referred to the key to how the importer knows it. Look in the
17462 : : entity table slot that we nominate, maybe lazy load it, and then
17463 : : lookup the resultant entity in the entity hash to learn how the
17464 : : importer knows it.
17465 : :
17466 : : But we need to go in the other direction :( Given the key, find all
17467 : : the index-aliases of that key. We can partially solve that by
17468 : : adding an alias hash table. Whenever we load a merged decl, add or
17469 : : augment a mapping from the entity (or its entity-index) to the
17470 : : newly-discovered index. Then when we look for pending entities of
17471 : : a key, we also iterate over this aliases this mapping provides.
17472 : :
17473 : : But that requires the alias to be loaded. And that's not
17474 : : necessarily true.
17475 : :
17476 : : *** THE SIMPLER WAY
17477 : :
17478 : : The remaining fixed thing we have is the innermost namespace
17479 : : containing the ultimate namespace-scope container of the key and
17480 : : the name of that container (which might be the key itself). I.e. a
17481 : : namespace-decl/identifier/module tuple. Let's call this the
17482 : : top-key. We'll discover that the module is not important here,
17483 : : because of cross-module possibilities mentioned in case #c above.
17484 : : We can't markup namespace-binding slots. The best we can do is
17485 : : mark the binding vector with 'there's something here', and have
17486 : : another map from namespace/identifier pairs to a vector of pending
17487 : : entity indices.
17488 : :
17489 : : Maintain a pending-entity map. This is keyed by top-key, and
17490 : : maps to a vector of pending-entity indices. On the binding vector
17491 : : have flags saying whether the pending-name-entity map has contents.
17492 : : (We might want to further extend the key to be GM-vs-Partition and
17493 : : specialization-vs-member, but let's not get ahead of ourselves.)
17494 : :
17495 : : For every key-like entity, find the outermost namespace-scope
17496 : : name. Use that to lookup in the pending-entity map and then make
17497 : : sure the specified entities are loaded.
17498 : :
17499 : : An optimization might be to have a flag in each key-entity saying
17500 : : that its top key might be in the entity table. It's not clear to
17501 : : me how to set that flag cheaply -- cheaper than just looking.
17502 : :
17503 : : FIXME: It'd be nice to have a bit in decls to tell us whether to
17504 : : even try this. We can have a 'already done' flag, that we set when
17505 : : we've done KLASS's lazy pendings. When we import a module that
17506 : : registers pendings on the same top-key as KLASS we need to clear
17507 : : the flag. A recursive walk of the top-key clearing the bit will
17508 : : suffice. Plus we only need to recurse on classes that have the bit
17509 : : set. (That means we need to set the bit on parents of KLASS here,
17510 : : don't forget.) However, first: correctness, second: efficiency. */
17511 : :
17512 : : unsigned
17513 : 2398 : module_state::write_pendings (elf_out *to, vec<depset *> depsets,
17514 : : depset::hash &table, unsigned *crc_p)
17515 : : {
17516 : 2650 : dump () && dump ("Writing pending-entities");
17517 : 2398 : dump.indent ();
17518 : :
17519 : 2398 : trees_out sec (to, this, table);
17520 : 2398 : sec.begin ();
17521 : :
17522 : 2398 : unsigned count = 0;
17523 : 2398 : tree cache_ns = NULL_TREE;
17524 : 2398 : tree cache_id = NULL_TREE;
17525 : 2398 : unsigned cache_section = ~0;
17526 : 927451 : for (unsigned ix = 0; ix < depsets.length (); ix++)
17527 : : {
17528 : 925053 : depset *d = depsets[ix];
17529 : :
17530 : 925053 : if (d->is_binding ())
17531 : 571695 : continue;
17532 : :
17533 : 808700 : if (d->is_import ())
17534 : 0 : continue;
17535 : :
17536 : 808700 : if (!d->is_pending_entity ())
17537 : 455342 : continue;
17538 : :
17539 : 353358 : tree key_decl = nullptr;
17540 : 353358 : tree key_ns = find_pending_key (d->get_entity (), &key_decl);
17541 : 353358 : tree key_name = DECL_NAME (key_decl);
17542 : :
17543 : 353358 : if (IDENTIFIER_ANON_P (key_name))
17544 : : {
17545 : 6 : gcc_checking_assert (IDENTIFIER_LAMBDA_P (key_name));
17546 : 12 : if (tree attached = LAMBDA_TYPE_EXTRA_SCOPE (TREE_TYPE (key_decl)))
17547 : 6 : key_name = DECL_NAME (attached);
17548 : : else
17549 : : {
17550 : : /* There's nothing to attach it to. Must
17551 : : always reinstantiate. */
17552 : 0 : dump ()
17553 : 0 : && dump ("Unattached lambda %N[%u] section:%u",
17554 : 0 : d->get_entity_kind () == depset::EK_DECL
17555 : : ? "Member" : "Specialization", d->get_entity (),
17556 : : d->cluster, d->section);
17557 : 0 : continue;
17558 : : }
17559 : : }
17560 : :
17561 : 353358 : char const *also = "";
17562 : 353358 : if (d->section == cache_section
17563 : 222211 : && key_ns == cache_ns
17564 : 222211 : && key_name == cache_id)
17565 : : /* Same section & key as previous, no need to repeat ourselves. */
17566 : : also = "also ";
17567 : : else
17568 : : {
17569 : 169287 : cache_ns = key_ns;
17570 : 169287 : cache_id = key_name;
17571 : 169287 : cache_section = d->section;
17572 : 169287 : gcc_checking_assert (table.find_dependency (cache_ns));
17573 : 169287 : sec.tree_node (cache_ns);
17574 : 169287 : sec.tree_node (cache_id);
17575 : 169287 : sec.u (d->cluster);
17576 : 169287 : count++;
17577 : : }
17578 : 354422 : dump () && dump ("Pending %s %N entity:%u section:%u %skeyed to %P",
17579 : 532 : d->get_entity_kind () == depset::EK_DECL
17580 : : ? "member" : "specialization", d->get_entity (),
17581 : : d->cluster, cache_section, also, cache_ns, cache_id);
17582 : : }
17583 : 2398 : sec.end (to, to->name (MOD_SNAME_PFX ".pnd"), crc_p);
17584 : 2398 : dump.outdent ();
17585 : :
17586 : 2398 : return count;
17587 : 2398 : }
17588 : :
17589 : : bool
17590 : 1349 : module_state::read_pendings (unsigned count)
17591 : : {
17592 : 1349 : trees_in sec (this);
17593 : :
17594 : 1349 : if (!sec.begin (loc, from (), MOD_SNAME_PFX ".pnd"))
17595 : : return false;
17596 : :
17597 : 1638 : dump () && dump ("Reading %u pendings", count);
17598 : 1349 : dump.indent ();
17599 : :
17600 : 202634 : for (unsigned ix = 0; ix != count; ix++)
17601 : : {
17602 : 201285 : pending_key key;
17603 : 201285 : unsigned index;
17604 : :
17605 : 201285 : key.ns = sec.tree_node ();
17606 : 201285 : key.id = sec.tree_node ();
17607 : 201285 : index = sec.u ();
17608 : :
17609 : 201285 : if (!key.ns || !key.id
17610 : 201285 : || !(TREE_CODE (key.ns) == NAMESPACE_DECL
17611 : 201285 : && !DECL_NAMESPACE_ALIAS (key.ns))
17612 : 201285 : || !identifier_p (key.id)
17613 : 402570 : || index >= entity_num)
17614 : 0 : sec.set_overrun ();
17615 : :
17616 : 201285 : if (sec.get_overrun ())
17617 : : break;
17618 : :
17619 : 201991 : dump () && dump ("Pending:%u keyed to %P", index, key.ns, key.id);
17620 : :
17621 : 201285 : index += entity_lwm;
17622 : 201285 : auto &vec = pending_table->get_or_insert (key);
17623 : 201285 : vec.safe_push (index);
17624 : : }
17625 : :
17626 : 1349 : dump.outdent ();
17627 : 1349 : if (!sec.end (from ()))
17628 : : return false;
17629 : : return true;
17630 : 1349 : }
17631 : :
17632 : : /* Read & write locations. */
17633 : : enum loc_kind {
17634 : : LK_ORDINARY,
17635 : : LK_MACRO,
17636 : : LK_IMPORT_ORDINARY,
17637 : : LK_IMPORT_MACRO,
17638 : : LK_ADHOC,
17639 : : LK_RESERVED,
17640 : : };
17641 : :
17642 : : static const module_state *
17643 : 3734 : module_for_ordinary_loc (location_t loc)
17644 : : {
17645 : 3734 : unsigned pos = 0;
17646 : 7468 : unsigned len = ool->length () - pos;
17647 : :
17648 : 3737 : while (len)
17649 : : {
17650 : 3737 : unsigned half = len / 2;
17651 : 3737 : module_state *probe = (*ool)[pos + half];
17652 : 3737 : if (loc < probe->ordinary_locs.first)
17653 : : len = half;
17654 : 3734 : else if (loc < probe->ordinary_locs.first + probe->ordinary_locs.second)
17655 : : return probe;
17656 : : else
17657 : : {
17658 : 0 : pos += half + 1;
17659 : 0 : len = len - (half + 1);
17660 : : }
17661 : : }
17662 : :
17663 : : return nullptr;
17664 : : }
17665 : :
17666 : : static const module_state *
17667 : 6 : module_for_macro_loc (location_t loc)
17668 : : {
17669 : 6 : unsigned pos = 1;
17670 : 6 : unsigned len = modules->length () - pos;
17671 : :
17672 : 6 : while (len)
17673 : : {
17674 : 6 : unsigned half = len / 2;
17675 : 6 : module_state *probe = (*modules)[pos + half];
17676 : 6 : if (loc < probe->macro_locs.first)
17677 : : {
17678 : 0 : pos += half + 1;
17679 : 0 : len = len - (half + 1);
17680 : : }
17681 : 6 : else if (loc >= probe->macro_locs.first + probe->macro_locs.second)
17682 : : len = half;
17683 : : else
17684 : : return probe;
17685 : : }
17686 : :
17687 : : return NULL;
17688 : : }
17689 : :
17690 : : location_t
17691 : 1095 : module_state::imported_from () const
17692 : : {
17693 : 1095 : location_t from = loc;
17694 : 1095 : line_map_ordinary const *fmap
17695 : 1095 : = linemap_check_ordinary (linemap_lookup (line_table, from));
17696 : :
17697 : 1095 : if (MAP_MODULE_P (fmap))
17698 : 1095 : from = linemap_included_from (fmap);
17699 : :
17700 : 1095 : return from;
17701 : : }
17702 : :
17703 : : /* Note that LOC will need writing. This allows us to prune locations
17704 : : that are not needed. */
17705 : :
17706 : : bool
17707 : 16672771 : module_state::note_location (location_t loc)
17708 : : {
17709 : 16672771 : bool added = false;
17710 : 16672771 : if (!macro_loc_table && !ord_loc_table)
17711 : : ;
17712 : 16672771 : else if (loc < RESERVED_LOCATION_COUNT)
17713 : : ;
17714 : 15158096 : else if (IS_ADHOC_LOC (loc))
17715 : : {
17716 : 1840606 : location_t locus = get_location_from_adhoc_loc (line_table, loc);
17717 : 1840606 : note_location (locus);
17718 : 1840606 : source_range range = get_range_from_loc (line_table, loc);
17719 : 1840606 : if (range.m_start != locus)
17720 : 1778781 : note_location (range.m_start);
17721 : 1840606 : note_location (range.m_finish);
17722 : : }
17723 : 13317490 : else if (loc >= LINEMAPS_MACRO_LOWEST_LOCATION (line_table))
17724 : : {
17725 : 1026527 : if (spans.macro (loc))
17726 : : {
17727 : 1026521 : const line_map *map = linemap_lookup (line_table, loc);
17728 : 1026521 : const line_map_macro *mac_map = linemap_check_macro (map);
17729 : 1026521 : hashval_t hv = macro_loc_traits::hash (mac_map);
17730 : 1026521 : macro_loc_info *slot
17731 : 1026521 : = macro_loc_table->find_slot_with_hash (mac_map, hv, INSERT);
17732 : 1026521 : if (!slot->src)
17733 : : {
17734 : 117192 : slot->src = mac_map;
17735 : 117192 : slot->remap = 0;
17736 : : // Expansion locations could themselves be from a
17737 : : // macro, we need to note them all.
17738 : 117192 : note_location (mac_map->m_expansion);
17739 : 117192 : gcc_checking_assert (mac_map->n_tokens);
17740 : 117192 : location_t tloc = UNKNOWN_LOCATION;
17741 : 4197302 : for (unsigned ix = mac_map->n_tokens * 2; ix--;)
17742 : 4080110 : if (mac_map->macro_locations[ix] != tloc)
17743 : : {
17744 : 2147290 : tloc = mac_map->macro_locations[ix];
17745 : 2147290 : note_location (tloc);
17746 : : }
17747 : : added = true;
17748 : : }
17749 : : }
17750 : : }
17751 : 12290963 : else if (IS_ORDINARY_LOC (loc))
17752 : : {
17753 : 12290963 : if (spans.ordinary (loc))
17754 : : {
17755 : 12287226 : const line_map *map = linemap_lookup (line_table, loc);
17756 : 12287226 : const line_map_ordinary *ord_map = linemap_check_ordinary (map);
17757 : 12287226 : ord_loc_info lkup;
17758 : 12287226 : lkup.src = ord_map;
17759 : 12287226 : lkup.span = loc_one << ord_map->m_column_and_range_bits;
17760 : 12287226 : lkup.offset = (loc - MAP_START_LOCATION (ord_map)) & ~(lkup.span - 1);
17761 : 12287226 : lkup.remap = 0;
17762 : 12287226 : ord_loc_info *slot = (ord_loc_table->find_slot_with_hash
17763 : 12287226 : (lkup, ord_loc_traits::hash (lkup), INSERT));
17764 : 12287226 : if (!slot->src)
17765 : : {
17766 : 1489831 : *slot = lkup;
17767 : 1489831 : added = true;
17768 : : }
17769 : : }
17770 : : }
17771 : : else
17772 : 0 : gcc_unreachable ();
17773 : 16672771 : return added;
17774 : : }
17775 : :
17776 : : /* If we're not streaming, record that we need location LOC.
17777 : : Otherwise stream it. */
17778 : :
17779 : : void
17780 : 24797513 : module_state::write_location (bytes_out &sec, location_t loc)
17781 : : {
17782 : 24797513 : if (!sec.streaming_p ())
17783 : : {
17784 : 8701202 : note_location (loc);
17785 : 8701202 : return;
17786 : : }
17787 : :
17788 : 16096311 : if (loc < RESERVED_LOCATION_COUNT)
17789 : : {
17790 : 1555200 : dump (dumper::LOCATION) && dump ("Reserved location %K", loc);
17791 : 1555182 : sec.loc (LK_RESERVED + loc);
17792 : : }
17793 : 14541129 : else if (IS_ADHOC_LOC (loc))
17794 : : {
17795 : 1795883 : dump (dumper::LOCATION) && dump ("Adhoc location");
17796 : 1795880 : sec.u (LK_ADHOC);
17797 : 1795880 : location_t locus = get_location_from_adhoc_loc (line_table, loc);
17798 : 1795880 : write_location (sec, locus);
17799 : 1795880 : source_range range = get_range_from_loc (line_table, loc);
17800 : 1795880 : if (range.m_start == locus)
17801 : : /* Compress. */
17802 : 58252 : range.m_start = UNKNOWN_LOCATION;
17803 : 1795880 : write_location (sec, range.m_start);
17804 : 1795880 : write_location (sec, range.m_finish);
17805 : 1795880 : unsigned discriminator = get_discriminator_from_adhoc_loc (line_table, loc);
17806 : 1795880 : sec.u (discriminator);
17807 : : }
17808 : 12745249 : else if (loc >= LINEMAPS_MACRO_LOWEST_LOCATION (line_table))
17809 : : {
17810 : 1014565 : const macro_loc_info *info = nullptr;
17811 : 1014565 : line_map_uint_t offset = 0;
17812 : 1014565 : if (unsigned hwm = macro_loc_remap->length ())
17813 : : {
17814 : 1014559 : info = macro_loc_remap->begin ();
17815 : 14781117 : while (hwm != 1)
17816 : : {
17817 : 12751999 : unsigned mid = hwm / 2;
17818 : 12751999 : if (MAP_START_LOCATION (info[mid].src) <= loc)
17819 : : {
17820 : 6527144 : info += mid;
17821 : 6527144 : hwm -= mid;
17822 : : }
17823 : : else
17824 : : hwm = mid;
17825 : : }
17826 : 1014559 : offset = loc - MAP_START_LOCATION (info->src);
17827 : 1014559 : if (offset > info->src->n_tokens)
17828 : 0 : info = nullptr;
17829 : : }
17830 : :
17831 : 1014565 : gcc_checking_assert (bool (info) == bool (spans.macro (loc)));
17832 : :
17833 : 1014565 : if (info)
17834 : : {
17835 : 1014559 : offset += info->remap;
17836 : 1014559 : sec.u (LK_MACRO);
17837 : 1014559 : sec.loc (offset);
17838 : 1014559 : dump (dumper::LOCATION)
17839 : 9 : && dump ("Macro location %K output %K", loc, offset);
17840 : : }
17841 : 6 : else if (const module_state *import = module_for_macro_loc (loc))
17842 : : {
17843 : 6 : auto off = loc - import->macro_locs.first;
17844 : 6 : sec.u (LK_IMPORT_MACRO);
17845 : 6 : sec.u (import->remap);
17846 : 6 : sec.loc (off);
17847 : 6 : dump (dumper::LOCATION)
17848 : 0 : && dump ("Imported macro location %K output %u:%K",
17849 : 0 : loc, import->remap, off);
17850 : : }
17851 : : else
17852 : 0 : gcc_unreachable ();
17853 : : }
17854 : 11730684 : else if (IS_ORDINARY_LOC (loc))
17855 : : {
17856 : : /* If we ran out of locations for imported decls, this location could
17857 : : be a module unit's location. In that case, remap the location
17858 : : to be where we imported the module from. */
17859 : 11730684 : if (spans.locations_exhausted_p () || CHECKING_P)
17860 : : {
17861 : 11730684 : const line_map_ordinary *map
17862 : 11730684 : = linemap_check_ordinary (linemap_lookup (line_table, loc));
17863 : 11730684 : if (MAP_MODULE_P (map) && loc == MAP_START_LOCATION (map))
17864 : : {
17865 : 0 : gcc_checking_assert (spans.locations_exhausted_p ());
17866 : 0 : write_location (sec, linemap_included_from (map));
17867 : 0 : return;
17868 : : }
17869 : : }
17870 : :
17871 : 11730684 : const ord_loc_info *info = nullptr;
17872 : 11730684 : line_map_uint_t offset = 0;
17873 : 11730684 : if (line_map_uint_t hwm = ord_loc_remap->length ())
17874 : : {
17875 : 11730684 : info = ord_loc_remap->begin ();
17876 : 169705209 : while (hwm != 1)
17877 : : {
17878 : 146243841 : auto mid = hwm / 2;
17879 : 146243841 : if (MAP_START_LOCATION (info[mid].src) + info[mid].offset <= loc)
17880 : : {
17881 : 77674577 : info += mid;
17882 : 77674577 : hwm -= mid;
17883 : : }
17884 : : else
17885 : : hwm = mid;
17886 : : }
17887 : 11730684 : offset = loc - MAP_START_LOCATION (info->src) - info->offset;
17888 : 11730684 : if (offset > info->span)
17889 : 3734 : info = nullptr;
17890 : : }
17891 : :
17892 : 11730684 : gcc_checking_assert (bool (info) == bool (spans.ordinary (loc)));
17893 : :
17894 : 11730684 : if (info)
17895 : : {
17896 : 11726950 : offset += info->remap;
17897 : 11726950 : sec.u (LK_ORDINARY);
17898 : 11726950 : sec.loc (offset);
17899 : :
17900 : 11726950 : dump (dumper::LOCATION)
17901 : 78 : && dump ("Ordinary location %K output %K", loc, offset);
17902 : : }
17903 : 3734 : else if (const module_state *import = module_for_ordinary_loc (loc))
17904 : : {
17905 : 3734 : auto off = loc - import->ordinary_locs.first;
17906 : 3734 : sec.u (LK_IMPORT_ORDINARY);
17907 : 3734 : sec.u (import->remap);
17908 : 3734 : sec.loc (off);
17909 : 3734 : dump (dumper::LOCATION)
17910 : 0 : && dump ("Imported ordinary location %K output %u:%K",
17911 : 0 : loc, import->remap, off);
17912 : : }
17913 : : else
17914 : 0 : gcc_unreachable ();
17915 : : }
17916 : : else
17917 : 0 : gcc_unreachable ();
17918 : : }
17919 : :
17920 : : location_t
17921 : 14566731 : module_state::read_location (bytes_in &sec) const
17922 : : {
17923 : 14566731 : location_t locus = UNKNOWN_LOCATION;
17924 : 14566731 : unsigned kind = sec.u ();
17925 : 14566731 : switch (kind)
17926 : : {
17927 : 1365266 : default:
17928 : 1365266 : {
17929 : 1365266 : if (kind < LK_RESERVED + RESERVED_LOCATION_COUNT)
17930 : 1365266 : locus = location_t (kind - LK_RESERVED);
17931 : : else
17932 : 0 : sec.set_overrun ();
17933 : 1365266 : dump (dumper::LOCATION)
17934 : 0 : && dump ("Reserved location %K", locus);
17935 : : }
17936 : : break;
17937 : :
17938 : 1558934 : case LK_ADHOC:
17939 : 1558934 : {
17940 : 1558934 : dump (dumper::LOCATION) && dump ("Adhoc location");
17941 : 1558934 : locus = read_location (sec);
17942 : 1558934 : source_range range;
17943 : 1558934 : range.m_start = read_location (sec);
17944 : 1558934 : if (range.m_start == UNKNOWN_LOCATION)
17945 : 46022 : range.m_start = locus;
17946 : 1558934 : range.m_finish = read_location (sec);
17947 : 1558934 : unsigned discriminator = sec.u ();
17948 : 1558934 : if (locus != loc && range.m_start != loc && range.m_finish != loc)
17949 : 1558934 : locus = line_table->get_or_create_combined_loc (locus, range,
17950 : : nullptr, discriminator);
17951 : : }
17952 : : break;
17953 : :
17954 : 1069298 : case LK_MACRO:
17955 : 1069298 : {
17956 : 1069298 : auto off = sec.loc ();
17957 : :
17958 : 1069298 : if (macro_locs.second)
17959 : : {
17960 : 1069298 : if (off < macro_locs.second)
17961 : 1069298 : locus = off + macro_locs.first;
17962 : : else
17963 : 0 : sec.set_overrun ();
17964 : : }
17965 : : else
17966 : 0 : locus = loc;
17967 : 1069298 : dump (dumper::LOCATION)
17968 : 0 : && dump ("Macro %K becoming %K", off, locus);
17969 : : }
17970 : : break;
17971 : :
17972 : 10572198 : case LK_ORDINARY:
17973 : 10572198 : {
17974 : 10572198 : auto off = sec.loc ();
17975 : 10572198 : if (ordinary_locs.second)
17976 : : {
17977 : 10572198 : if (off < ordinary_locs.second)
17978 : 10572198 : locus = off + ordinary_locs.first;
17979 : : else
17980 : 0 : sec.set_overrun ();
17981 : : }
17982 : : else
17983 : 0 : locus = loc;
17984 : :
17985 : 10572198 : dump (dumper::LOCATION)
17986 : 0 : && dump ("Ordinary location %K becoming %K", off, locus);
17987 : : }
17988 : : break;
17989 : :
17990 : 1035 : case LK_IMPORT_MACRO:
17991 : 1035 : case LK_IMPORT_ORDINARY:
17992 : 1035 : {
17993 : 1035 : unsigned mod = sec.u ();
17994 : 1035 : location_t off = sec.loc ();
17995 : 1035 : const module_state *import = NULL;
17996 : :
17997 : 1035 : if (!mod && !slurp->remap)
17998 : : /* This is an early read of a partition location during the
17999 : : read of our ordinary location map. */
18000 : : import = this;
18001 : : else
18002 : : {
18003 : 1035 : mod = slurp->remap_module (mod);
18004 : 1035 : if (!mod)
18005 : 0 : sec.set_overrun ();
18006 : : else
18007 : 1035 : import = (*modules)[mod];
18008 : : }
18009 : :
18010 : 1035 : if (import)
18011 : : {
18012 : 1035 : if (kind == LK_IMPORT_MACRO)
18013 : : {
18014 : 6 : if (!import->macro_locs.second)
18015 : 0 : locus = import->loc;
18016 : 6 : else if (off < import->macro_locs.second)
18017 : 6 : locus = off + import->macro_locs.first;
18018 : : else
18019 : 0 : sec.set_overrun ();
18020 : : }
18021 : : else
18022 : : {
18023 : 1029 : if (!import->ordinary_locs.second)
18024 : 0 : locus = import->loc;
18025 : 1029 : else if (off < import->ordinary_locs.second)
18026 : 1029 : locus = import->ordinary_locs.first + off;
18027 : : else
18028 : 0 : sec.set_overrun ();
18029 : : }
18030 : : }
18031 : : }
18032 : : break;
18033 : : }
18034 : :
18035 : 14566731 : return locus;
18036 : : }
18037 : :
18038 : : /* Allocate hash tables to record needed locations. */
18039 : :
18040 : : void
18041 : 2426 : module_state::write_init_maps ()
18042 : : {
18043 : 2426 : macro_loc_table = new hash_table<macro_loc_traits> (EXPERIMENT (1, 400));
18044 : 2426 : ord_loc_table = new hash_table<ord_loc_traits> (EXPERIMENT (1, 400));
18045 : 2426 : }
18046 : :
18047 : : /* Prepare the span adjustments. We prune unneeded locations -- at
18048 : : this point every needed location must have been seen by
18049 : : note_location. */
18050 : :
18051 : : range_t
18052 : 2398 : module_state::write_prepare_maps (module_state_config *cfg, bool has_partitions)
18053 : : {
18054 : 2650 : dump () && dump ("Preparing locations");
18055 : 2398 : dump.indent ();
18056 : :
18057 : 2650 : dump () && dump ("Reserved locations [%K,%K) macro [%K,%K)",
18058 : 252 : spans[loc_spans::SPAN_RESERVED].ordinary.first,
18059 : 252 : spans[loc_spans::SPAN_RESERVED].ordinary.second,
18060 : 252 : spans[loc_spans::SPAN_RESERVED].macro.first,
18061 : 252 : spans[loc_spans::SPAN_RESERVED].macro.second);
18062 : :
18063 : 2398 : range_t info {0, 0};
18064 : :
18065 : : // Sort the noted lines.
18066 : 2398 : vec_alloc (ord_loc_remap, ord_loc_table->size ());
18067 : 2398 : for (auto iter = ord_loc_table->begin (), end = ord_loc_table->end ();
18068 : 2960868 : iter != end; ++iter)
18069 : 1479235 : ord_loc_remap->quick_push (*iter);
18070 : 2398 : ord_loc_remap->qsort (&ord_loc_info::compare);
18071 : :
18072 : : // Note included-from maps.
18073 : 2398 : bool added = false;
18074 : 2398 : const line_map_ordinary *current = nullptr;
18075 : 1486429 : for (auto iter = ord_loc_remap->begin (), end = ord_loc_remap->end ();
18076 : 1481633 : iter != end; ++iter)
18077 : 1479235 : if (iter->src != current)
18078 : : {
18079 : 26121 : current = iter->src;
18080 : 10175 : for (auto probe = current;
18081 : 26121 : auto from = linemap_included_from (probe);
18082 : 10175 : probe = linemap_check_ordinary (linemap_lookup (line_table, from)))
18083 : : {
18084 : 23457 : if (has_partitions)
18085 : : {
18086 : : // Partition locations need to elide their module map
18087 : : // entry.
18088 : 180 : probe
18089 : 180 : = linemap_check_ordinary (linemap_lookup (line_table, from));
18090 : 180 : if (MAP_MODULE_P (probe))
18091 : 150 : from = linemap_included_from (probe);
18092 : : }
18093 : :
18094 : 23457 : if (!note_location (from))
18095 : : break;
18096 : 10175 : added = true;
18097 : 10175 : }
18098 : : }
18099 : 2398 : if (added)
18100 : : {
18101 : : // Reconstruct the line array as we added items to the hash table.
18102 : 451 : vec_free (ord_loc_remap);
18103 : 451 : vec_alloc (ord_loc_remap, ord_loc_table->size ());
18104 : 451 : for (auto iter = ord_loc_table->begin (), end = ord_loc_table->end ();
18105 : 2960193 : iter != end; ++iter)
18106 : 1479871 : ord_loc_remap->quick_push (*iter);
18107 : 451 : ord_loc_remap->qsort (&ord_loc_info::compare);
18108 : : }
18109 : 2398 : delete ord_loc_table;
18110 : 2398 : ord_loc_table = nullptr;
18111 : :
18112 : : // Merge (sufficiently) adjacent spans, and calculate remapping.
18113 : 2398 : constexpr line_map_uint_t adjacency = 2; // Allow 2 missing lines.
18114 : 4796 : auto begin = ord_loc_remap->begin (), end = ord_loc_remap->end ();
18115 : 2398 : auto dst = begin;
18116 : 2398 : line_map_uint_t offset = 0;
18117 : 2398 : unsigned range_bits = 0;
18118 : 2398 : ord_loc_info *base = nullptr;
18119 : 1491808 : for (auto iter = begin; iter != end; ++iter)
18120 : : {
18121 : 1489410 : if (base && iter->src == base->src)
18122 : : {
18123 : 2769853 : if (base->offset + base->span +
18124 : 1467347 : ((adjacency << base->src->m_column_and_range_bits)
18125 : : // If there are few c&r bits, allow further separation.
18126 : 1467347 : | (adjacency << 4))
18127 : 1467347 : >= iter->offset)
18128 : : {
18129 : : // Merge.
18130 : 1302506 : offset -= base->span;
18131 : 1302506 : base->span = iter->offset + iter->span - base->offset;
18132 : 1302506 : offset += base->span;
18133 : 1302506 : continue;
18134 : : }
18135 : : }
18136 : 22063 : else if (range_bits < iter->src->m_range_bits)
18137 : 2302 : range_bits = iter->src->m_range_bits;
18138 : :
18139 : 186904 : offset += ((loc_one << iter->src->m_range_bits) - 1);
18140 : 186904 : offset &= ~((loc_one << iter->src->m_range_bits) - 1);
18141 : 186904 : iter->remap = offset;
18142 : 186904 : offset += iter->span;
18143 : 186904 : base = dst;
18144 : 186904 : *dst++ = *iter;
18145 : : }
18146 : 2398 : ord_loc_remap->truncate (dst - begin);
18147 : :
18148 : 2398 : info.first = ord_loc_remap->length ();
18149 : 2398 : cfg->ordinary_locs = offset;
18150 : 2398 : cfg->loc_range_bits = range_bits;
18151 : 2650 : dump () && dump ("Ordinary maps:%K locs:%K range_bits:%u",
18152 : : info.first,
18153 : : cfg->ordinary_locs,
18154 : : cfg->loc_range_bits);
18155 : :
18156 : : // Remap the macro locations.
18157 : 2398 : vec_alloc (macro_loc_remap, macro_loc_table->size ());
18158 : 2398 : for (auto iter = macro_loc_table->begin (), end = macro_loc_table->end ();
18159 : 236782 : iter != end; ++iter)
18160 : 117192 : macro_loc_remap->quick_push (*iter);
18161 : 2398 : delete macro_loc_table;
18162 : 2398 : macro_loc_table = nullptr;
18163 : :
18164 : 2398 : macro_loc_remap->qsort (¯o_loc_info::compare);
18165 : 2398 : offset = 0;
18166 : 7194 : for (auto iter = macro_loc_remap->begin (), end = macro_loc_remap->end ();
18167 : 119590 : iter != end; ++iter)
18168 : : {
18169 : 117192 : auto mac = iter->src;
18170 : 117192 : iter->remap = offset;
18171 : 117192 : offset += mac->n_tokens;
18172 : : }
18173 : 2398 : info.second = macro_loc_remap->length ();
18174 : 2398 : cfg->macro_locs = offset;
18175 : :
18176 : 2650 : dump () && dump ("Macro maps:%K locs:%K", info.second, cfg->macro_locs);
18177 : :
18178 : 2398 : dump.outdent ();
18179 : :
18180 : : // If we have no ordinary locs, we must also have no macro locs.
18181 : 2398 : gcc_checking_assert (cfg->ordinary_locs || !cfg->macro_locs);
18182 : :
18183 : 2398 : return info;
18184 : : }
18185 : :
18186 : : bool
18187 : 2611 : module_state::read_prepare_maps (const module_state_config *cfg)
18188 : : {
18189 : 2611 : location_t ordinary = line_table->highest_location + 1;
18190 : 2611 : ordinary += cfg->ordinary_locs;
18191 : :
18192 : 2611 : location_t macro = LINEMAPS_MACRO_LOWEST_LOCATION (line_table);
18193 : 2611 : macro -= cfg->macro_locs;
18194 : :
18195 : 2611 : if (ordinary < LINE_MAP_MAX_LOCATION_WITH_COLS
18196 : 2611 : && macro >= LINE_MAP_MAX_LOCATION)
18197 : : /* OK, we have enough locations. */
18198 : : return true;
18199 : :
18200 : 0 : ordinary_locs.first = ordinary_locs.second = 0;
18201 : 0 : macro_locs.first = macro_locs.second = 0;
18202 : :
18203 : 0 : spans.report_location_exhaustion (loc);
18204 : :
18205 : : return false;
18206 : : }
18207 : :
18208 : : /* Write & read the location maps. Not called if there are no
18209 : : locations. */
18210 : :
18211 : : void
18212 : 2302 : module_state::write_ordinary_maps (elf_out *to, range_t &info,
18213 : : bool has_partitions, unsigned *crc_p)
18214 : : {
18215 : 2535 : dump () && dump ("Writing ordinary location maps");
18216 : 2302 : dump.indent ();
18217 : :
18218 : 2302 : vec<const char *> filenames;
18219 : 2302 : filenames.create (20);
18220 : :
18221 : : /* Determine the unique filenames. */
18222 : 2302 : const line_map_ordinary *current = nullptr;
18223 : 193810 : for (auto iter = ord_loc_remap->begin (), end = ord_loc_remap->end ();
18224 : 189206 : iter != end; ++iter)
18225 : 186904 : if (iter->src != current)
18226 : : {
18227 : 22063 : current = iter->src;
18228 : 22063 : const char *fname = ORDINARY_MAP_FILE_NAME (iter->src);
18229 : :
18230 : : /* We should never find a module linemap in an interval. */
18231 : 22063 : gcc_checking_assert (!MAP_MODULE_P (iter->src));
18232 : :
18233 : : /* We expect very few filenames, so just an array.
18234 : : (Not true when headers are still in play :() */
18235 : 1306814 : for (unsigned jx = filenames.length (); jx--;)
18236 : : {
18237 : 1272961 : const char *name = filenames[jx];
18238 : 1272961 : if (0 == strcmp (name, fname))
18239 : : {
18240 : : /* Reset the linemap's name, because for things like
18241 : : preprocessed input we could have multiple instances
18242 : : of the same name, and we'd rather not percolate
18243 : : that. */
18244 : 10273 : const_cast<line_map_ordinary *> (iter->src)->to_file = name;
18245 : 10273 : fname = NULL;
18246 : 10273 : break;
18247 : : }
18248 : : }
18249 : 22063 : if (fname)
18250 : 11790 : filenames.safe_push (fname);
18251 : : }
18252 : :
18253 : 2302 : bytes_out sec (to);
18254 : 2302 : sec.begin ();
18255 : :
18256 : : /* Write the filenames. */
18257 : 2302 : unsigned len = filenames.length ();
18258 : 2302 : sec.u (len);
18259 : 2535 : dump () && dump ("%u source file names", len);
18260 : 14092 : for (unsigned ix = 0; ix != len; ix++)
18261 : : {
18262 : 11790 : const char *fname = filenames[ix];
18263 : 11805 : dump (dumper::LOCATION) && dump ("Source file[%u]=%s", ix, fname);
18264 : 11790 : sec.str (fname);
18265 : : }
18266 : :
18267 : 2302 : sec.loc (info.first); /* Num maps. */
18268 : 2302 : const ord_loc_info *base = nullptr;
18269 : 193810 : for (auto iter = ord_loc_remap->begin (), end = ord_loc_remap->end ();
18270 : 189206 : iter != end; ++iter)
18271 : : {
18272 : 186904 : dump (dumper::LOCATION)
18273 : 36 : && dump ("Span:%K ordinary [%K+%K,+%K)->[%K,+%K)",
18274 : 36 : (location_t) (iter - ord_loc_remap->begin ()),
18275 : 18 : MAP_START_LOCATION (iter->src),
18276 : : iter->offset, iter->span, iter->remap,
18277 : : iter->span);
18278 : :
18279 : 186904 : if (!base || iter->src != base->src)
18280 : 22063 : base = iter;
18281 : 186904 : sec.loc (iter->offset - base->offset);
18282 : 186904 : if (base == iter)
18283 : : {
18284 : 22063 : sec.u (iter->src->sysp);
18285 : 22063 : sec.u (iter->src->m_range_bits);
18286 : 22063 : sec.u (iter->src->m_column_and_range_bits - iter->src->m_range_bits);
18287 : :
18288 : 22063 : const char *fname = ORDINARY_MAP_FILE_NAME (iter->src);
18289 : 4054440 : for (unsigned ix = 0; ix != filenames.length (); ix++)
18290 : 2027220 : if (filenames[ix] == fname)
18291 : : {
18292 : 22063 : sec.u (ix);
18293 : 22063 : break;
18294 : : }
18295 : 22063 : unsigned line = ORDINARY_MAP_STARTING_LINE_NUMBER (iter->src);
18296 : 22063 : line += iter->offset >> iter->src->m_column_and_range_bits;
18297 : 22063 : sec.u (line);
18298 : : }
18299 : 186904 : sec.loc (iter->remap);
18300 : 186904 : if (base == iter)
18301 : : {
18302 : : /* Write the included from location, which means reading it
18303 : : while reading in the ordinary maps. So we'd better not
18304 : : be getting ahead of ourselves. */
18305 : 22063 : location_t from = linemap_included_from (iter->src);
18306 : 22063 : gcc_checking_assert (from < MAP_START_LOCATION (iter->src));
18307 : 22063 : if (from != UNKNOWN_LOCATION && has_partitions)
18308 : : {
18309 : : /* A partition's span will have a from pointing at a
18310 : : MODULE_INC. Find that map's from. */
18311 : 174 : line_map_ordinary const *fmap
18312 : 174 : = linemap_check_ordinary (linemap_lookup (line_table, from));
18313 : 174 : if (MAP_MODULE_P (fmap))
18314 : 144 : from = linemap_included_from (fmap);
18315 : : }
18316 : 22063 : write_location (sec, from);
18317 : : }
18318 : : }
18319 : :
18320 : 2302 : filenames.release ();
18321 : :
18322 : 2302 : sec.end (to, to->name (MOD_SNAME_PFX ".olm"), crc_p);
18323 : 2302 : dump.outdent ();
18324 : 2302 : }
18325 : :
18326 : : /* Return the prefix to use for dumping a #pragma diagnostic change to DK. */
18327 : :
18328 : : static const char *
18329 : 816 : dk_string (diagnostic_t dk)
18330 : : {
18331 : 816 : gcc_assert (dk > DK_UNSPECIFIED && dk < DK_LAST_DIAGNOSTIC_KIND);
18332 : 816 : if (dk == DK_IGNORED)
18333 : : /* diagnostic.def has an empty string for ignored. */
18334 : : return "ignored: ";
18335 : : else
18336 : 0 : return get_diagnostic_kind_text (dk);
18337 : : }
18338 : :
18339 : : /* Dump one #pragma GCC diagnostic entry. */
18340 : :
18341 : : static bool
18342 : 1668 : dump_dc_change (unsigned index, unsigned opt, diagnostic_t dk)
18343 : : {
18344 : 1668 : if (dk == DK_POP)
18345 : 852 : return dump (" Index %u: pop from %d", index, opt);
18346 : : else
18347 : 816 : return dump (" Index %u: %s%s", index, dk_string (dk),
18348 : 1632 : cl_options[opt].opt_text);
18349 : : }
18350 : :
18351 : : /* Write out any #pragma GCC diagnostic info to the .dgc section. */
18352 : :
18353 : : void
18354 : 4700 : module_state::write_diagnostic_classification (elf_out *to,
18355 : : diagnostic_context *dc,
18356 : : unsigned *crc_p)
18357 : : {
18358 : 4700 : auto &changes = dc->get_classification_history ();
18359 : :
18360 : 4700 : bytes_out sec (to);
18361 : 4700 : if (sec.streaming_p ())
18362 : : {
18363 : 2302 : sec.begin ();
18364 : 2535 : dump () && dump ("Writing diagnostic change locations");
18365 : 2302 : dump.indent ();
18366 : : }
18367 : :
18368 : 4700 : unsigned len = changes.length ();
18369 : :
18370 : : /* We don't want to write out any entries that came from one of our imports.
18371 : : But then we need to adjust the total, and change DK_POP targets to match
18372 : : the index in our actual output. So remember how many lines we had skipped
18373 : : at each step, where -1 means this line itself is skipped. */
18374 : 4700 : int skips = 0;
18375 : 4700 : auto_vec<int> skips_at (len);
18376 : 4700 : skips_at.safe_grow (len);
18377 : :
18378 : 42568 : for (unsigned i = 0; i < len; ++i)
18379 : : {
18380 : 37868 : const auto &c = changes[i];
18381 : 37868 : skips_at[i] = skips;
18382 : 37868 : if (linemap_location_from_module_p (line_table, c.location))
18383 : : {
18384 : 6066 : ++skips;
18385 : 6066 : skips_at[i] = -1;
18386 : 6066 : continue;
18387 : : }
18388 : : }
18389 : :
18390 : 4700 : if (sec.streaming_p ())
18391 : : {
18392 : 2302 : sec.u (len - skips);
18393 : 2535 : dump () && dump ("Diagnostic changes: %u", len - skips);
18394 : : }
18395 : :
18396 : 42568 : for (unsigned i = 0; i < len; ++i)
18397 : : {
18398 : 37868 : if (skips_at[i] == -1)
18399 : 6066 : continue;
18400 : :
18401 : 31802 : const auto &c = changes[i];
18402 : 31802 : write_location (sec, c.location);
18403 : 31802 : if (sec.streaming_p ())
18404 : : {
18405 : 15901 : unsigned opt = c.option;
18406 : 15901 : if (c.kind == DK_POP)
18407 : 8093 : opt -= skips_at[opt];
18408 : 15901 : sec.u (opt);
18409 : 15901 : sec.u (c.kind);
18410 : 39481 : dump () && dump_dc_change (i - skips_at[i], opt, c.kind);
18411 : : }
18412 : : }
18413 : :
18414 : 4700 : if (sec.streaming_p ())
18415 : : {
18416 : 2302 : sec.end (to, to->name (MOD_SNAME_PFX ".dgc"), crc_p);
18417 : 2302 : dump.outdent ();
18418 : : }
18419 : 4700 : }
18420 : :
18421 : : /* Read any #pragma GCC diagnostic info from the .dgc section. */
18422 : :
18423 : : bool
18424 : 2547 : module_state::read_diagnostic_classification (diagnostic_context *dc)
18425 : : {
18426 : 2547 : bytes_in sec;
18427 : :
18428 : 2547 : if (!sec.begin (loc, from (), MOD_SNAME_PFX ".dgc"))
18429 : : return false;
18430 : :
18431 : 3007 : dump () && dump ("Reading diagnostic change locations");
18432 : 2547 : dump.indent ();
18433 : :
18434 : 2547 : unsigned len = sec.u ();
18435 : 3007 : dump () && dump ("Diagnostic changes: %u", len);
18436 : :
18437 : 2547 : auto &changes = dc->get_classification_history ();
18438 : 2547 : int offset = changes.length ();
18439 : 2547 : changes.reserve (len + 1);
18440 : 21175 : for (unsigned i = 0; i < len; ++i)
18441 : : {
18442 : 18628 : location_t loc = read_location (sec);
18443 : 18628 : int opt = sec.u ();
18444 : 18628 : diagnostic_t kind = (diagnostic_t) sec.u ();
18445 : 18628 : if (kind == DK_POP)
18446 : : /* For a pop, opt is the 'changes' index to return to. */
18447 : 9476 : opt += offset;
18448 : 18628 : changes.quick_push ({ loc, opt, kind });
18449 : 18683 : dump () && dump_dc_change (changes.length () - 1, opt, kind);
18450 : : }
18451 : :
18452 : : /* Did the import pop all its diagnostic changes? */
18453 : 2547 : bool last_was_reset = (len == 0);
18454 : 2547 : if (len)
18455 : 208 : for (int i = changes.length () - 1; ; --i)
18456 : : {
18457 : 8225 : gcc_checking_assert (i >= offset);
18458 : :
18459 : 8225 : const auto &c = changes[i];
18460 : 8225 : if (c.kind != DK_POP)
18461 : : break;
18462 : 8216 : else if (c.option == offset)
18463 : : {
18464 : : last_was_reset = true;
18465 : : break;
18466 : : }
18467 : : else
18468 : : /* As in update_effective_level_from_pragmas, the loop will decrement
18469 : : i so we actually jump to c.option - 1. */
18470 : 8121 : i = c.option;
18471 : 8121 : }
18472 : 2547 : if (!last_was_reset)
18473 : : {
18474 : : /* It didn't, so add a pop at its last location to avoid affecting later
18475 : : imports. */
18476 : 9 : location_t last_loc = ordinary_locs.first + ordinary_locs.second - 1;
18477 : 9 : changes.quick_push ({ last_loc, offset, DK_POP });
18478 : 15 : dump () && dump (" Adding final pop from index %d", offset);
18479 : : }
18480 : :
18481 : 2547 : dump.outdent ();
18482 : 2547 : if (!sec.end (from ()))
18483 : : return false;
18484 : :
18485 : : return true;
18486 : 2547 : }
18487 : :
18488 : : void
18489 : 117 : module_state::write_macro_maps (elf_out *to, range_t &info, unsigned *crc_p)
18490 : : {
18491 : 129 : dump () && dump ("Writing macro location maps");
18492 : 117 : dump.indent ();
18493 : :
18494 : 117 : bytes_out sec (to);
18495 : 117 : sec.begin ();
18496 : :
18497 : 129 : dump () && dump ("Macro maps:%K", info.second);
18498 : 117 : sec.loc (info.second);
18499 : :
18500 : 117 : line_map_uint_t macro_num = 0;
18501 : 234 : for (auto iter = macro_loc_remap->end (), begin = macro_loc_remap->begin ();
18502 : 117309 : iter-- != begin;)
18503 : : {
18504 : 117192 : auto mac = iter->src;
18505 : 117192 : sec.loc (iter->remap);
18506 : 117192 : sec.u (mac->n_tokens);
18507 : 117192 : sec.cpp_node (mac->macro);
18508 : 117192 : write_location (sec, mac->m_expansion);
18509 : 117192 : const location_t *locs = mac->macro_locations;
18510 : : /* There are lots of identical runs. */
18511 : 117192 : location_t prev = UNKNOWN_LOCATION;
18512 : 117192 : unsigned count = 0;
18513 : 117192 : unsigned runs = 0;
18514 : 4197302 : for (unsigned jx = mac->n_tokens * 2; jx--;)
18515 : : {
18516 : 4080110 : location_t tok_loc = locs[jx];
18517 : 4080110 : if (tok_loc == prev)
18518 : : {
18519 : 1932820 : count++;
18520 : 1932820 : continue;
18521 : : }
18522 : 2147290 : runs++;
18523 : 2147290 : sec.u (count);
18524 : 2147290 : count = 1;
18525 : 2147290 : prev = tok_loc;
18526 : 2147290 : write_location (sec, tok_loc);
18527 : : }
18528 : 117192 : sec.u (count);
18529 : 117192 : dump (dumper::LOCATION)
18530 : 9 : && dump ("Macro:%K %I %u/%u*2 locations [%K,%K)->%K",
18531 : 9 : macro_num, identifier (mac->macro),
18532 : : runs, mac->n_tokens,
18533 : : MAP_START_LOCATION (mac),
18534 : 9 : MAP_START_LOCATION (mac) + mac->n_tokens,
18535 : : iter->remap);
18536 : 117192 : macro_num++;
18537 : : }
18538 : 117 : gcc_assert (macro_num == info.second);
18539 : :
18540 : 117 : sec.end (to, to->name (MOD_SNAME_PFX ".mlm"), crc_p);
18541 : 117 : dump.outdent ();
18542 : 117 : }
18543 : :
18544 : : bool
18545 : 2547 : module_state::read_ordinary_maps (line_map_uint_t num_ord_locs,
18546 : : unsigned range_bits)
18547 : : {
18548 : 2547 : bytes_in sec;
18549 : :
18550 : 2547 : if (!sec.begin (loc, from (), MOD_SNAME_PFX ".olm"))
18551 : : return false;
18552 : 3007 : dump () && dump ("Reading ordinary location maps");
18553 : 2547 : dump.indent ();
18554 : :
18555 : : /* Read the filename table. */
18556 : 2547 : unsigned len = sec.u ();
18557 : 3007 : dump () && dump ("%u source file names", len);
18558 : 2547 : vec<const char *> filenames;
18559 : 2547 : filenames.create (len);
18560 : 16313 : for (unsigned ix = 0; ix != len; ix++)
18561 : : {
18562 : 13766 : size_t l;
18563 : 13766 : const char *buf = sec.str (&l);
18564 : 13766 : char *fname = XNEWVEC (char, l + 1);
18565 : 13766 : memcpy (fname, buf, l + 1);
18566 : 13766 : dump (dumper::LOCATION) && dump ("Source file[%u]=%s", ix, fname);
18567 : : /* We leak these names into the line-map table. But it
18568 : : doesn't own them. */
18569 : 13766 : filenames.quick_push (fname);
18570 : : }
18571 : :
18572 : 2547 : line_map_uint_t num_ordinary = sec.loc ();
18573 : 3007 : dump () && dump ("Ordinary maps:%K, range_bits:%u",
18574 : : num_ordinary, range_bits);
18575 : :
18576 : 2547 : location_t offset = line_table->highest_location + 1;
18577 : 2547 : offset += ((loc_one << range_bits) - 1);
18578 : 2547 : offset &= ~((loc_one << range_bits) - 1);
18579 : 2547 : ordinary_locs.first = offset;
18580 : :
18581 : 2547 : bool propagated = spans.maybe_propagate (this, offset);
18582 : 2547 : line_map_ordinary *maps = static_cast<line_map_ordinary *>
18583 : 2547 : (line_map_new_raw (line_table, false, num_ordinary));
18584 : :
18585 : 2547 : const line_map_ordinary *base = nullptr;
18586 : 230545 : for (line_map_uint_t ix = 0; ix != num_ordinary && !sec.get_overrun (); ix++)
18587 : : {
18588 : 227998 : line_map_ordinary *map = &maps[ix];
18589 : :
18590 : 227998 : location_t offset = sec.loc ();
18591 : 227998 : if (!offset)
18592 : : {
18593 : 26123 : map->reason = LC_RENAME;
18594 : 26123 : map->sysp = sec.u ();
18595 : 26123 : map->m_range_bits = sec.u ();
18596 : 26123 : map->m_column_and_range_bits = sec.u () + map->m_range_bits;
18597 : 26123 : unsigned fnum = sec.u ();
18598 : 52246 : map->to_file = (fnum < filenames.length () ? filenames[fnum] : "");
18599 : 26123 : map->to_line = sec.u ();
18600 : 26123 : base = map;
18601 : : }
18602 : : else
18603 : : {
18604 : 201875 : *map = *base;
18605 : 201875 : map->to_line += offset >> map->m_column_and_range_bits;
18606 : : }
18607 : 227998 : location_t remap = sec.loc ();
18608 : 227998 : map->start_location = remap + ordinary_locs.first;
18609 : 227998 : if (base == map)
18610 : : {
18611 : : /* Root the outermost map at our location. */
18612 : 26123 : ordinary_locs.second = remap;
18613 : 26123 : location_t from = read_location (sec);
18614 : 26123 : map->included_from = from != UNKNOWN_LOCATION ? from : loc;
18615 : : }
18616 : : }
18617 : :
18618 : 2547 : ordinary_locs.second = num_ord_locs;
18619 : : /* highest_location is the one handed out, not the next one to
18620 : : hand out. */
18621 : 2547 : line_table->highest_location = ordinary_locs.first + ordinary_locs.second - 1;
18622 : :
18623 : 2547 : if (line_table->highest_location >= LINE_MAP_MAX_LOCATION_WITH_COLS)
18624 : : /* We shouldn't run out of locations, as we checked before
18625 : : starting. */
18626 : 0 : sec.set_overrun ();
18627 : 3007 : dump () && dump ("Ordinary location [%K,+%K)",
18628 : : ordinary_locs.first, ordinary_locs.second);
18629 : :
18630 : 2547 : if (propagated)
18631 : 144 : spans.close ();
18632 : :
18633 : 2547 : filenames.release ();
18634 : :
18635 : 2547 : dump.outdent ();
18636 : 2547 : if (!sec.end (from ()))
18637 : : return false;
18638 : :
18639 : : return true;
18640 : 2547 : }
18641 : :
18642 : : bool
18643 : 127 : module_state::read_macro_maps (line_map_uint_t num_macro_locs)
18644 : : {
18645 : 127 : bytes_in sec;
18646 : :
18647 : 127 : if (!sec.begin (loc, from (), MOD_SNAME_PFX ".mlm"))
18648 : : return false;
18649 : 133 : dump () && dump ("Reading macro location maps");
18650 : 127 : dump.indent ();
18651 : :
18652 : 127 : line_map_uint_t num_macros = sec.loc ();
18653 : 133 : dump () && dump ("Macro maps:%K locs:%K",
18654 : : num_macros, num_macro_locs);
18655 : :
18656 : 254 : bool propagated = spans.maybe_propagate (this,
18657 : 127 : line_table->highest_location + 1);
18658 : :
18659 : 127 : location_t offset = LINEMAPS_MACRO_LOWEST_LOCATION (line_table);
18660 : 127 : macro_locs.second = num_macro_locs;
18661 : 127 : macro_locs.first = offset - num_macro_locs;
18662 : :
18663 : 133 : dump () && dump ("Macro loc delta %K", offset);
18664 : 133 : dump () && dump ("Macro locations [%K,%K)",
18665 : : macro_locs.first, macro_locs.second);
18666 : :
18667 : 147808 : for (line_map_uint_t ix = 0; ix != num_macros && !sec.get_overrun (); ix++)
18668 : : {
18669 : 147681 : location_t offset = sec.loc ();
18670 : 147681 : unsigned n_tokens = sec.u ();
18671 : 147681 : cpp_hashnode *node = sec.cpp_node ();
18672 : 147681 : location_t exp_loc = read_location (sec);
18673 : :
18674 : 147681 : const line_map_macro *macro
18675 : 147681 : = linemap_enter_macro (line_table, node, exp_loc, n_tokens);
18676 : 147681 : if (!macro)
18677 : : /* We shouldn't run out of locations, as we checked that we
18678 : : had enough before starting. */
18679 : : break;
18680 : 147681 : gcc_checking_assert (MAP_START_LOCATION (macro)
18681 : : == offset + macro_locs.first);
18682 : :
18683 : 147681 : location_t *locs = macro->macro_locations;
18684 : 147681 : location_t tok_loc = UNKNOWN_LOCATION;
18685 : 147681 : unsigned count = sec.u ();
18686 : 147681 : unsigned runs = 0;
18687 : 5207339 : for (unsigned jx = macro->n_tokens * 2; jx-- && !sec.get_overrun ();)
18688 : : {
18689 : 7724754 : while (!count-- && !sec.get_overrun ())
18690 : : {
18691 : 2665096 : runs++;
18692 : 2665096 : tok_loc = read_location (sec);
18693 : 2665096 : count = sec.u ();
18694 : : }
18695 : 5059658 : locs[jx] = tok_loc;
18696 : : }
18697 : 147681 : if (count)
18698 : 0 : sec.set_overrun ();
18699 : 147708 : dump (dumper::LOCATION)
18700 : 0 : && dump ("Macro:%K %I %u/%u*2 locations [%K,%K)",
18701 : : ix, identifier (node), runs, n_tokens,
18702 : : MAP_START_LOCATION (macro),
18703 : 0 : MAP_START_LOCATION (macro) + n_tokens);
18704 : : }
18705 : :
18706 : 133 : dump () && dump ("Macro location lwm:%K", macro_locs.first);
18707 : 127 : if (propagated)
18708 : 3 : spans.close ();
18709 : :
18710 : 127 : dump.outdent ();
18711 : 127 : if (!sec.end (from ()))
18712 : : return false;
18713 : :
18714 : : return true;
18715 : 127 : }
18716 : :
18717 : : /* Serialize the definition of MACRO. */
18718 : :
18719 : : void
18720 : 67009 : module_state::write_define (bytes_out &sec, const cpp_macro *macro)
18721 : : {
18722 : 67009 : sec.u (macro->count);
18723 : :
18724 : 67009 : bytes_out::bits_out bits = sec.stream_bits ();
18725 : 67009 : bits.b (macro->fun_like);
18726 : 67009 : bits.b (macro->variadic);
18727 : 67009 : bits.b (macro->syshdr);
18728 : 67009 : bits.bflush ();
18729 : :
18730 : 67009 : write_location (sec, macro->line);
18731 : 67009 : if (macro->fun_like)
18732 : : {
18733 : 9021 : sec.u (macro->paramc);
18734 : 9021 : const cpp_hashnode *const *parms = macro->parm.params;
18735 : 22724 : for (unsigned ix = 0; ix != macro->paramc; ix++)
18736 : 13703 : sec.cpp_node (parms[ix]);
18737 : : }
18738 : :
18739 : : unsigned len = 0;
18740 : 220694 : for (unsigned ix = 0; ix != macro->count; ix++)
18741 : : {
18742 : 153685 : const cpp_token *token = ¯o->exp.tokens[ix];
18743 : 153685 : write_location (sec, token->src_loc);
18744 : 153685 : sec.u (token->type);
18745 : 153685 : sec.u (token->flags);
18746 : 153685 : switch (cpp_token_val_index (token))
18747 : : {
18748 : 0 : default:
18749 : 0 : gcc_unreachable ();
18750 : :
18751 : 12002 : case CPP_TOKEN_FLD_ARG_NO:
18752 : : /* An argument reference. */
18753 : 12002 : sec.u (token->val.macro_arg.arg_no);
18754 : 12002 : sec.cpp_node (token->val.macro_arg.spelling);
18755 : 12002 : break;
18756 : :
18757 : 30125 : case CPP_TOKEN_FLD_NODE:
18758 : : /* An identifier. */
18759 : 30125 : sec.cpp_node (token->val.node.node);
18760 : 30125 : if (token->val.node.spelling == token->val.node.node)
18761 : : /* The spelling will usually be the same. so optimize
18762 : : that. */
18763 : 30125 : sec.str (NULL, 0);
18764 : : else
18765 : 0 : sec.cpp_node (token->val.node.spelling);
18766 : : break;
18767 : :
18768 : : case CPP_TOKEN_FLD_NONE:
18769 : : break;
18770 : :
18771 : 47787 : case CPP_TOKEN_FLD_STR:
18772 : : /* A string, number or comment. Not always NUL terminated,
18773 : : we stream out in a single contatenation with embedded
18774 : : NULs as that's a safe default. */
18775 : 47787 : len += token->val.str.len + 1;
18776 : 47787 : sec.u (token->val.str.len);
18777 : 47787 : break;
18778 : :
18779 : 0 : case CPP_TOKEN_FLD_SOURCE:
18780 : 0 : case CPP_TOKEN_FLD_TOKEN_NO:
18781 : 0 : case CPP_TOKEN_FLD_PRAGMA:
18782 : : /* These do not occur inside a macro itself. */
18783 : 0 : gcc_unreachable ();
18784 : : }
18785 : : }
18786 : :
18787 : 67009 : if (len)
18788 : : {
18789 : 44317 : char *ptr = reinterpret_cast<char *> (sec.buf (len));
18790 : 44317 : len = 0;
18791 : 135640 : for (unsigned ix = 0; ix != macro->count; ix++)
18792 : : {
18793 : 91323 : const cpp_token *token = ¯o->exp.tokens[ix];
18794 : 91323 : if (cpp_token_val_index (token) == CPP_TOKEN_FLD_STR)
18795 : : {
18796 : 47787 : memcpy (ptr + len, token->val.str.text,
18797 : 47787 : token->val.str.len);
18798 : 47787 : len += token->val.str.len;
18799 : 47787 : ptr[len++] = 0;
18800 : : }
18801 : : }
18802 : : }
18803 : 67009 : }
18804 : :
18805 : : /* Read a macro definition. */
18806 : :
18807 : : cpp_macro *
18808 : 317 : module_state::read_define (bytes_in &sec, cpp_reader *reader) const
18809 : : {
18810 : 317 : unsigned count = sec.u ();
18811 : : /* We rely on knowing cpp_reader's hash table is ident_hash, and
18812 : : its subobject allocator is stringpool_ggc_alloc and that is just
18813 : : a wrapper for ggc_alloc_atomic. */
18814 : 317 : cpp_macro *macro
18815 : 634 : = (cpp_macro *)ggc_alloc_atomic (sizeof (cpp_macro)
18816 : 317 : + sizeof (cpp_token) * (count - !!count));
18817 : 317 : memset (macro, 0, sizeof (cpp_macro) + sizeof (cpp_token) * (count - !!count));
18818 : :
18819 : 317 : macro->count = count;
18820 : 317 : macro->kind = cmk_macro;
18821 : 317 : macro->imported_p = true;
18822 : :
18823 : 317 : bytes_in::bits_in bits = sec.stream_bits ();
18824 : 317 : macro->fun_like = bits.b ();
18825 : 317 : macro->variadic = bits.b ();
18826 : 317 : macro->syshdr = bits.b ();
18827 : 317 : bits.bflush ();
18828 : :
18829 : 317 : macro->line = read_location (sec);
18830 : :
18831 : 317 : if (macro->fun_like)
18832 : : {
18833 : 66 : unsigned paramc = sec.u ();
18834 : 66 : cpp_hashnode **params
18835 : 66 : = (cpp_hashnode **)ggc_alloc_atomic (sizeof (cpp_hashnode *) * paramc);
18836 : 66 : macro->paramc = paramc;
18837 : 66 : macro->parm.params = params;
18838 : 141 : for (unsigned ix = 0; ix != paramc; ix++)
18839 : 75 : params[ix] = sec.cpp_node ();
18840 : : }
18841 : :
18842 : : unsigned len = 0;
18843 : 1116 : for (unsigned ix = 0; ix != count && !sec.get_overrun (); ix++)
18844 : : {
18845 : 799 : cpp_token *token = ¯o->exp.tokens[ix];
18846 : 799 : token->src_loc = read_location (sec);
18847 : 799 : token->type = cpp_ttype (sec.u ());
18848 : 799 : token->flags = sec.u ();
18849 : 799 : switch (cpp_token_val_index (token))
18850 : : {
18851 : 0 : default:
18852 : 0 : sec.set_overrun ();
18853 : 0 : break;
18854 : :
18855 : 62 : case CPP_TOKEN_FLD_ARG_NO:
18856 : : /* An argument reference. */
18857 : 62 : {
18858 : 62 : unsigned arg_no = sec.u ();
18859 : 62 : if (arg_no - 1 >= macro->paramc)
18860 : 0 : sec.set_overrun ();
18861 : 62 : token->val.macro_arg.arg_no = arg_no;
18862 : 62 : token->val.macro_arg.spelling = sec.cpp_node ();
18863 : : }
18864 : 62 : break;
18865 : :
18866 : 209 : case CPP_TOKEN_FLD_NODE:
18867 : : /* An identifier. */
18868 : 209 : token->val.node.node = sec.cpp_node ();
18869 : 209 : token->val.node.spelling = sec.cpp_node ();
18870 : 209 : if (!token->val.node.spelling)
18871 : 209 : token->val.node.spelling = token->val.node.node;
18872 : : break;
18873 : :
18874 : : case CPP_TOKEN_FLD_NONE:
18875 : : break;
18876 : :
18877 : 176 : case CPP_TOKEN_FLD_STR:
18878 : : /* A string, number or comment. */
18879 : 176 : token->val.str.len = sec.u ();
18880 : 176 : len += token->val.str.len + 1;
18881 : 176 : break;
18882 : : }
18883 : : }
18884 : :
18885 : 317 : if (len)
18886 : 175 : if (const char *ptr = reinterpret_cast<const char *> (sec.buf (len)))
18887 : : {
18888 : : /* There should be a final NUL. */
18889 : 175 : if (ptr[len-1])
18890 : 0 : sec.set_overrun ();
18891 : : /* cpp_alloc_token_string will add a final NUL. */
18892 : 175 : const unsigned char *buf
18893 : 175 : = cpp_alloc_token_string (reader, (const unsigned char *)ptr, len - 1);
18894 : 175 : len = 0;
18895 : 653 : for (unsigned ix = 0; ix != count && !sec.get_overrun (); ix++)
18896 : : {
18897 : 478 : cpp_token *token = ¯o->exp.tokens[ix];
18898 : 478 : if (cpp_token_val_index (token) == CPP_TOKEN_FLD_STR)
18899 : : {
18900 : 176 : token->val.str.text = buf + len;
18901 : 176 : len += token->val.str.len;
18902 : 176 : if (buf[len++])
18903 : 0 : sec.set_overrun ();
18904 : : }
18905 : : }
18906 : : }
18907 : :
18908 : 317 : if (sec.get_overrun ())
18909 : 0 : return NULL;
18910 : : return macro;
18911 : 317 : }
18912 : :
18913 : : /* Exported macro data. */
18914 : : struct GTY(()) macro_export {
18915 : : cpp_macro *def;
18916 : : location_t undef_loc;
18917 : :
18918 : 96143 : macro_export ()
18919 : 96143 : :def (NULL), undef_loc (UNKNOWN_LOCATION)
18920 : : {
18921 : : }
18922 : : };
18923 : :
18924 : : /* Imported macro data. */
18925 : : class macro_import {
18926 : : public:
18927 : : struct slot {
18928 : : #if defined (WORDS_BIGENDIAN) && SIZEOF_VOID_P == 8
18929 : : int offset;
18930 : : #endif
18931 : : /* We need to ensure we don't use the LSB for representation, as
18932 : : that's the union discriminator below. */
18933 : : unsigned bits;
18934 : :
18935 : : #if !(defined (WORDS_BIGENDIAN) && SIZEOF_VOID_P == 8)
18936 : : int offset;
18937 : : #endif
18938 : :
18939 : : public:
18940 : : enum Layout {
18941 : : L_DEF = 1,
18942 : : L_UNDEF = 2,
18943 : : L_BOTH = 3,
18944 : : L_MODULE_SHIFT = 2
18945 : : };
18946 : :
18947 : : public:
18948 : : /* Not a regular ctor, because we put it in a union, and that's
18949 : : not allowed in C++ 98. */
18950 : 167195 : static slot ctor (unsigned module, unsigned defness)
18951 : : {
18952 : 167195 : gcc_checking_assert (defness);
18953 : 167195 : slot s;
18954 : 167195 : s.bits = defness | (module << L_MODULE_SHIFT);
18955 : 167195 : s.offset = -1;
18956 : 167195 : return s;
18957 : : }
18958 : :
18959 : : public:
18960 : 135532 : unsigned get_defness () const
18961 : : {
18962 : 135532 : return bits & L_BOTH;
18963 : : }
18964 : 97877 : unsigned get_module () const
18965 : : {
18966 : 97877 : return bits >> L_MODULE_SHIFT;
18967 : : }
18968 : 12 : void become_undef ()
18969 : : {
18970 : 12 : bits &= ~unsigned (L_DEF);
18971 : 12 : bits |= unsigned (L_UNDEF);
18972 : : }
18973 : : };
18974 : :
18975 : : private:
18976 : : typedef vec<slot, va_heap, vl_embed> ary_t;
18977 : : union either {
18978 : : /* Discriminated by bits 0|1 != 0. The expected case is that
18979 : : there will be exactly one slot per macro, hence the effort of
18980 : : packing that. */
18981 : : ary_t *ary;
18982 : : slot single;
18983 : : } u;
18984 : :
18985 : : public:
18986 : 133643 : macro_import ()
18987 : 133643 : {
18988 : 133643 : u.ary = NULL;
18989 : : }
18990 : :
18991 : : private:
18992 : 7345886 : bool single_p () const
18993 : : {
18994 : 7345886 : return u.single.bits & slot::L_BOTH;
18995 : : }
18996 : 7479535 : bool occupied_p () const
18997 : : {
18998 : 7479535 : return u.ary != NULL;
18999 : : }
19000 : :
19001 : : public:
19002 : 909 : unsigned length () const
19003 : : {
19004 : 909 : gcc_checking_assert (occupied_p ());
19005 : 909 : return single_p () ? 1 : u.ary->length ();
19006 : : }
19007 : 7215605 : slot &operator[] (unsigned ix)
19008 : : {
19009 : 7215605 : gcc_checking_assert (occupied_p ());
19010 : 7215605 : if (single_p ())
19011 : : {
19012 : 7129720 : gcc_checking_assert (!ix);
19013 : 7129720 : return u.single;
19014 : : }
19015 : : else
19016 : 85885 : return (*u.ary)[ix];
19017 : : }
19018 : :
19019 : : public:
19020 : : slot &exported ();
19021 : : slot &append (unsigned module, unsigned defness);
19022 : : };
19023 : :
19024 : : /* O is a new import to append to the list for. If we're an empty
19025 : : set, initialize us. */
19026 : :
19027 : : macro_import::slot &
19028 : 167195 : macro_import::append (unsigned module, unsigned defness)
19029 : : {
19030 : 167195 : if (!occupied_p ())
19031 : : {
19032 : 133643 : u.single = slot::ctor (module, defness);
19033 : 133643 : return u.single;
19034 : : }
19035 : : else
19036 : : {
19037 : 33552 : bool single = single_p ();
19038 : 33552 : ary_t *m = single ? NULL : u.ary;
19039 : 33552 : vec_safe_reserve (m, 1 + single);
19040 : 33552 : if (single)
19041 : 33549 : m->quick_push (u.single);
19042 : 33552 : u.ary = m;
19043 : 33552 : return *u.ary->quick_push (slot::ctor (module, defness));
19044 : : }
19045 : : }
19046 : :
19047 : : /* We're going to export something. Make sure the first import slot
19048 : : is us. */
19049 : :
19050 : : macro_import::slot &
19051 : 95826 : macro_import::exported ()
19052 : : {
19053 : 95826 : if (occupied_p () && !(*this)[0].get_module ())
19054 : : {
19055 : 6 : slot &res = (*this)[0];
19056 : 6 : res.bits |= slot::L_DEF;
19057 : 6 : return res;
19058 : : }
19059 : :
19060 : 95820 : slot *a = &append (0, slot::L_DEF);
19061 : 95820 : if (!single_p ())
19062 : : {
19063 : 29360 : slot &f = (*this)[0];
19064 : 29360 : std::swap (f, *a);
19065 : 29360 : a = &f;
19066 : : }
19067 : : return *a;
19068 : : }
19069 : :
19070 : : /* The import (&exported) macros. cpp_hasnode's deferred field
19071 : : indexes this array (offset by 1, so zero means 'not present'. */
19072 : :
19073 : : static vec<macro_import, va_heap, vl_embed> *macro_imports;
19074 : :
19075 : : /* The exported macros. A macro_import slot's zeroth element's offset
19076 : : indexes this array. If the zeroth slot is not for module zero,
19077 : : there is no export. */
19078 : :
19079 : : static GTY(()) vec<macro_export, va_gc> *macro_exports;
19080 : :
19081 : : /* The reachable set of header imports from this TU. */
19082 : :
19083 : : static GTY(()) bitmap headers;
19084 : :
19085 : : /* Get the (possibly empty) macro imports for NODE. */
19086 : :
19087 : : static macro_import &
19088 : 137841 : get_macro_imports (cpp_hashnode *node)
19089 : : {
19090 : 137841 : if (node->deferred)
19091 : 4198 : return (*macro_imports)[node->deferred - 1];
19092 : :
19093 : 133643 : vec_safe_reserve (macro_imports, 1);
19094 : 133643 : node->deferred = macro_imports->length () + 1;
19095 : 133643 : return *vec_safe_push (macro_imports, macro_import ());
19096 : : }
19097 : :
19098 : : /* Get the macro export for export EXP of NODE. */
19099 : :
19100 : : static macro_export &
19101 : 95826 : get_macro_export (macro_import::slot &slot)
19102 : : {
19103 : 95826 : if (slot.offset >= 0)
19104 : 6 : return (*macro_exports)[slot.offset];
19105 : :
19106 : 95820 : vec_safe_reserve (macro_exports, 1);
19107 : 95820 : slot.offset = macro_exports->length ();
19108 : 95820 : return *macro_exports->quick_push (macro_export ());
19109 : : }
19110 : :
19111 : : /* If NODE is an exportable macro, add it to the export set. */
19112 : :
19113 : : static int
19114 : 3696612 : maybe_add_macro (cpp_reader *, cpp_hashnode *node, void *data_)
19115 : : {
19116 : 3696612 : bool exporting = false;
19117 : :
19118 : 3696612 : if (cpp_user_macro_p (node))
19119 : 475629 : if (cpp_macro *macro = node->value.macro)
19120 : : /* Ignore imported, builtins, command line and forced header macros. */
19121 : 475188 : if (!macro->imported_p
19122 : 475188 : && !macro->lazy && macro->line >= spans.main_start ())
19123 : : {
19124 : 66466 : gcc_checking_assert (macro->kind == cmk_macro);
19125 : : /* I don't want to deal with this corner case, that I suspect is
19126 : : a devil's advocate reading of the standard. */
19127 : 66466 : gcc_checking_assert (!macro->extra_tokens);
19128 : :
19129 : 66466 : macro_import::slot &slot = get_macro_imports (node).exported ();
19130 : 66466 : macro_export &exp = get_macro_export (slot);
19131 : 66466 : exp.def = macro;
19132 : 66466 : exporting = true;
19133 : : }
19134 : :
19135 : 3630146 : if (!exporting && node->deferred)
19136 : : {
19137 : 581 : macro_import &imports = (*macro_imports)[node->deferred - 1];
19138 : 581 : macro_import::slot &slot = imports[0];
19139 : 581 : if (!slot.get_module ())
19140 : : {
19141 : 550 : gcc_checking_assert (slot.get_defness ());
19142 : : exporting = true;
19143 : : }
19144 : : }
19145 : :
19146 : 66466 : if (exporting)
19147 : 67016 : static_cast<vec<cpp_hashnode *> *> (data_)->safe_push (node);
19148 : :
19149 : 3696612 : return 1; /* Don't stop. */
19150 : : }
19151 : :
19152 : : /* Order cpp_hashnodes A_ and B_ by their exported macro locations. */
19153 : :
19154 : : static int
19155 : 3477022 : macro_loc_cmp (const void *a_, const void *b_)
19156 : : {
19157 : 3477022 : const cpp_hashnode *node_a = *(const cpp_hashnode *const *)a_;
19158 : 3477022 : macro_import &import_a = (*macro_imports)[node_a->deferred - 1];
19159 : 3477022 : const macro_export &export_a = (*macro_exports)[import_a[0].offset];
19160 : 3477022 : location_t loc_a = export_a.def ? export_a.def->line : export_a.undef_loc;
19161 : :
19162 : 3477022 : const cpp_hashnode *node_b = *(const cpp_hashnode *const *)b_;
19163 : 3477022 : macro_import &import_b = (*macro_imports)[node_b->deferred - 1];
19164 : 3477022 : const macro_export &export_b = (*macro_exports)[import_b[0].offset];
19165 : 3477022 : location_t loc_b = export_b.def ? export_b.def->line : export_b.undef_loc;
19166 : :
19167 : 3477022 : if (loc_a < loc_b)
19168 : : return +1;
19169 : 1786421 : else if (loc_a > loc_b)
19170 : : return -1;
19171 : : else
19172 : 0 : return 0;
19173 : : }
19174 : :
19175 : : /* Gather the macro definitions and undefinitions that we will need to
19176 : : write out. */
19177 : :
19178 : : vec<cpp_hashnode *> *
19179 : 855 : module_state::prepare_macros (cpp_reader *reader)
19180 : : {
19181 : 855 : vec<cpp_hashnode *> *macros;
19182 : 855 : vec_alloc (macros, 100);
19183 : :
19184 : 855 : cpp_forall_identifiers (reader, maybe_add_macro, macros);
19185 : :
19186 : 879 : dump (dumper::MACRO) && dump ("No more than %u macros", macros->length ());
19187 : :
19188 : 855 : macros->qsort (macro_loc_cmp);
19189 : :
19190 : : // Note the locations.
19191 : 68726 : for (unsigned ix = macros->length (); ix--;)
19192 : : {
19193 : 67016 : cpp_hashnode *node = (*macros)[ix];
19194 : 67016 : macro_import::slot &slot = (*macro_imports)[node->deferred - 1][0];
19195 : 67016 : macro_export &mac = (*macro_exports)[slot.offset];
19196 : :
19197 : 67016 : if (IDENTIFIER_KEYWORD_P (identifier (node)))
19198 : 1 : continue;
19199 : :
19200 : 67015 : if (mac.undef_loc != UNKNOWN_LOCATION)
19201 : 12 : note_location (mac.undef_loc);
19202 : 67015 : if (mac.def)
19203 : : {
19204 : 67009 : note_location (mac.def->line);
19205 : 220694 : for (unsigned ix = 0; ix != mac.def->count; ix++)
19206 : 153685 : note_location (mac.def->exp.tokens[ix].src_loc);
19207 : : }
19208 : : }
19209 : :
19210 : 855 : return macros;
19211 : : }
19212 : :
19213 : : /* Write out the exported defines. This is two sections, one
19214 : : containing the definitions, the other a table of node names. */
19215 : :
19216 : : unsigned
19217 : 855 : module_state::write_macros (elf_out *to, vec<cpp_hashnode *> *macros,
19218 : : unsigned *crc_p)
19219 : : {
19220 : 922 : dump () && dump ("Writing macros");
19221 : 855 : dump.indent ();
19222 : :
19223 : : /* Write the defs */
19224 : 855 : bytes_out sec (to);
19225 : 855 : sec.begin ();
19226 : :
19227 : 855 : unsigned count = 0;
19228 : 68726 : for (unsigned ix = macros->length (); ix--;)
19229 : : {
19230 : 67016 : cpp_hashnode *node = (*macros)[ix];
19231 : 67016 : macro_import::slot &slot = (*macro_imports)[node->deferred - 1][0];
19232 : 67016 : gcc_assert (!slot.get_module () && slot.get_defness ());
19233 : :
19234 : 67016 : macro_export &mac = (*macro_exports)[slot.offset];
19235 : 67016 : gcc_assert (!!(slot.get_defness () & macro_import::slot::L_UNDEF)
19236 : : == (mac.undef_loc != UNKNOWN_LOCATION)
19237 : : && !!(slot.get_defness () & macro_import::slot::L_DEF)
19238 : : == (mac.def != NULL));
19239 : :
19240 : 67016 : if (IDENTIFIER_KEYWORD_P (identifier (node)))
19241 : : {
19242 : 1 : warning_at (mac.def->line, 0,
19243 : : "not exporting %<#define %E%> as it is a keyword",
19244 : : identifier (node));
19245 : 1 : slot.offset = 0;
19246 : 1 : continue;
19247 : : }
19248 : :
19249 : 67015 : count++;
19250 : 67015 : slot.offset = sec.pos;
19251 : 67015 : dump (dumper::MACRO)
19252 : 24 : && dump ("Writing macro %s%s%s %I at %u",
19253 : 24 : slot.get_defness () & macro_import::slot::L_UNDEF
19254 : : ? "#undef" : "",
19255 : 24 : slot.get_defness () == macro_import::slot::L_BOTH
19256 : : ? " & " : "",
19257 : 24 : slot.get_defness () & macro_import::slot::L_DEF
19258 : : ? "#define" : "",
19259 : : identifier (node), slot.offset);
19260 : 67015 : if (mac.undef_loc != UNKNOWN_LOCATION)
19261 : 12 : write_location (sec, mac.undef_loc);
19262 : 67015 : if (mac.def)
19263 : 67009 : write_define (sec, mac.def);
19264 : : }
19265 : 855 : if (count)
19266 : : // We may have ended on a tokenless macro with a very short
19267 : : // location, that will cause problems reading its bit flags.
19268 : 142 : sec.u (0);
19269 : 855 : sec.end (to, to->name (MOD_SNAME_PFX ".def"), crc_p);
19270 : :
19271 : 855 : if (count)
19272 : : {
19273 : : /* Write the table. */
19274 : 142 : bytes_out sec (to);
19275 : 142 : sec.begin ();
19276 : 142 : sec.u (count);
19277 : :
19278 : 67299 : for (unsigned ix = macros->length (); ix--;)
19279 : : {
19280 : 67015 : const cpp_hashnode *node = (*macros)[ix];
19281 : 67015 : macro_import::slot &slot = (*macro_imports)[node->deferred - 1][0];
19282 : :
19283 : 67015 : if (slot.offset)
19284 : : {
19285 : 67015 : sec.cpp_node (node);
19286 : 67015 : sec.u (slot.get_defness ());
19287 : 67015 : sec.u (slot.offset);
19288 : : }
19289 : : }
19290 : 142 : sec.end (to, to->name (MOD_SNAME_PFX ".mac"), crc_p);
19291 : 142 : }
19292 : :
19293 : 855 : dump.outdent ();
19294 : 855 : return count;
19295 : 855 : }
19296 : :
19297 : : bool
19298 : 889 : module_state::read_macros ()
19299 : : {
19300 : : /* Get the def section. */
19301 : 889 : if (!slurp->macro_defs.begin (loc, from (), MOD_SNAME_PFX ".def"))
19302 : : return false;
19303 : :
19304 : : /* Get the tbl section, if there are defs. */
19305 : 889 : if (slurp->macro_defs.more_p ()
19306 : 889 : && !slurp->macro_tbl.begin (loc, from (), MOD_SNAME_PFX ".mac"))
19307 : : return false;
19308 : :
19309 : : return true;
19310 : : }
19311 : :
19312 : : /* Install the macro name table. */
19313 : :
19314 : : void
19315 : 895 : module_state::install_macros ()
19316 : : {
19317 : 895 : bytes_in &sec = slurp->macro_tbl;
19318 : 895 : if (!sec.size)
19319 : : return;
19320 : :
19321 : 200 : dump () && dump ("Reading macro table %M", this);
19322 : 178 : dump.indent ();
19323 : :
19324 : 178 : unsigned count = sec.u ();
19325 : 200 : dump () && dump ("%u macros", count);
19326 : 71553 : while (count--)
19327 : : {
19328 : 71375 : cpp_hashnode *node = sec.cpp_node ();
19329 : 71375 : macro_import &imp = get_macro_imports (node);
19330 : 71375 : unsigned flags = sec.u () & macro_import::slot::L_BOTH;
19331 : 71375 : if (!flags)
19332 : 0 : sec.set_overrun ();
19333 : :
19334 : 71375 : if (sec.get_overrun ())
19335 : : break;
19336 : :
19337 : 71375 : macro_import::slot &slot = imp.append (mod, flags);
19338 : 71375 : slot.offset = sec.u ();
19339 : :
19340 : 71375 : dump (dumper::MACRO)
19341 : 84 : && dump ("Read %s macro %s%s%s %I at %u",
19342 : 30 : imp.length () > 1 ? "add" : "new",
19343 : 27 : flags & macro_import::slot::L_UNDEF ? "#undef" : "",
19344 : : flags == macro_import::slot::L_BOTH ? " & " : "",
19345 : 30 : flags & macro_import::slot::L_DEF ? "#define" : "",
19346 : : identifier (node), slot.offset);
19347 : :
19348 : : /* We'll leak an imported definition's TOKEN_FLD_STR's data
19349 : : here. But that only happens when we've had to resolve the
19350 : : deferred macro before this import -- why are you doing
19351 : : that? */
19352 : 71375 : if (cpp_macro *cur = cpp_set_deferred_macro (node))
19353 : 29348 : if (!cur->imported_p)
19354 : : {
19355 : 29348 : macro_import::slot &slot = imp.exported ();
19356 : 29348 : macro_export &exp = get_macro_export (slot);
19357 : 29348 : exp.def = cur;
19358 : 100901 : dump (dumper::MACRO)
19359 : 0 : && dump ("Saving current #define %I", identifier (node));
19360 : : }
19361 : : }
19362 : :
19363 : : /* We're now done with the table. */
19364 : 178 : elf_in::release (slurp->from, sec);
19365 : :
19366 : 178 : dump.outdent ();
19367 : : }
19368 : :
19369 : : /* Import the transitive macros. */
19370 : :
19371 : : void
19372 : 853 : module_state::import_macros ()
19373 : : {
19374 : 853 : bitmap_ior_into (headers, slurp->headers);
19375 : :
19376 : 853 : bitmap_iterator bititer;
19377 : 853 : unsigned bitnum;
19378 : 1748 : EXECUTE_IF_SET_IN_BITMAP (slurp->headers, 0, bitnum, bititer)
19379 : 895 : (*modules)[bitnum]->install_macros ();
19380 : 853 : }
19381 : :
19382 : : /* NODE is being undefined at LOC. Record it in the export table, if
19383 : : necessary. */
19384 : :
19385 : : void
19386 : 193018 : module_state::undef_macro (cpp_reader *, location_t loc, cpp_hashnode *node)
19387 : : {
19388 : 193018 : if (!node->deferred)
19389 : : /* The macro is not imported, so our undef is irrelevant. */
19390 : : return;
19391 : :
19392 : 12 : unsigned n = dump.push (NULL);
19393 : :
19394 : 12 : macro_import::slot &slot = (*macro_imports)[node->deferred - 1].exported ();
19395 : 12 : macro_export &exp = get_macro_export (slot);
19396 : :
19397 : 12 : exp.undef_loc = loc;
19398 : 12 : slot.become_undef ();
19399 : 12 : exp.def = NULL;
19400 : :
19401 : 18 : dump (dumper::MACRO) && dump ("Recording macro #undef %I", identifier (node));
19402 : :
19403 : 12 : dump.pop (n);
19404 : : }
19405 : :
19406 : : /* NODE is a deferred macro node. Determine the definition and return
19407 : : it, with NULL if undefined. May issue diagnostics.
19408 : :
19409 : : This can leak memory, when merging declarations -- the string
19410 : : contents (TOKEN_FLD_STR) of each definition are allocated in
19411 : : unreclaimable cpp objstack. Only one will win. However, I do not
19412 : : expect this to be common -- mostly macros have a single point of
19413 : : definition. Perhaps we could restore the objstack to its position
19414 : : after the first imported definition (if that wins)? The macros
19415 : : themselves are GC'd. */
19416 : :
19417 : : cpp_macro *
19418 : 293 : module_state::deferred_macro (cpp_reader *reader, location_t loc,
19419 : : cpp_hashnode *node)
19420 : : {
19421 : 293 : macro_import &imports = (*macro_imports)[node->deferred - 1];
19422 : :
19423 : 293 : unsigned n = dump.push (NULL);
19424 : 299 : dump (dumper::MACRO) && dump ("Deferred macro %I", identifier (node));
19425 : :
19426 : 293 : bitmap visible (BITMAP_GGC_ALLOC ());
19427 : :
19428 : 293 : if (!((imports[0].get_defness () & macro_import::slot::L_UNDEF)
19429 : 0 : && !imports[0].get_module ()))
19430 : : {
19431 : : /* Calculate the set of visible header imports. */
19432 : 293 : bitmap_copy (visible, headers);
19433 : 750 : for (unsigned ix = imports.length (); ix--;)
19434 : : {
19435 : 457 : const macro_import::slot &slot = imports[ix];
19436 : 457 : unsigned mod = slot.get_module ();
19437 : 457 : if ((slot.get_defness () & macro_import::slot::L_UNDEF)
19438 : 457 : && bitmap_bit_p (visible, mod))
19439 : : {
19440 : 12 : bitmap arg = mod ? (*modules)[mod]->slurp->headers : headers;
19441 : 12 : bitmap_and_compl_into (visible, arg);
19442 : 12 : bitmap_set_bit (visible, mod);
19443 : : }
19444 : : }
19445 : : }
19446 : 293 : bitmap_set_bit (visible, 0);
19447 : :
19448 : : /* Now find the macros that are still visible. */
19449 : 293 : bool failed = false;
19450 : 293 : cpp_macro *def = NULL;
19451 : 293 : vec<macro_export> defs;
19452 : 293 : defs.create (imports.length ());
19453 : 750 : for (unsigned ix = imports.length (); ix--;)
19454 : : {
19455 : 457 : const macro_import::slot &slot = imports[ix];
19456 : 457 : unsigned mod = slot.get_module ();
19457 : 457 : if (bitmap_bit_p (visible, mod))
19458 : : {
19459 : 445 : macro_export *pushed = NULL;
19460 : 445 : if (mod)
19461 : : {
19462 : 323 : const module_state *imp = (*modules)[mod];
19463 : 323 : bytes_in &sec = imp->slurp->macro_defs;
19464 : 323 : if (!sec.get_overrun ())
19465 : : {
19466 : 323 : dump (dumper::MACRO)
19467 : 6 : && dump ("Reading macro %s%s%s %I module %M at %u",
19468 : 6 : slot.get_defness () & macro_import::slot::L_UNDEF
19469 : : ? "#undef" : "",
19470 : 6 : slot.get_defness () == macro_import::slot::L_BOTH
19471 : : ? " & " : "",
19472 : 6 : slot.get_defness () & macro_import::slot::L_DEF
19473 : : ? "#define" : "",
19474 : 6 : identifier (node), imp, slot.offset);
19475 : 323 : sec.random_access (slot.offset);
19476 : :
19477 : 323 : macro_export exp;
19478 : 323 : if (slot.get_defness () & macro_import::slot::L_UNDEF)
19479 : 12 : exp.undef_loc = imp->read_location (sec);
19480 : 323 : if (slot.get_defness () & macro_import::slot::L_DEF)
19481 : 317 : exp.def = imp->read_define (sec, reader);
19482 : 323 : if (sec.get_overrun ())
19483 : 0 : error_at (loc, "macro definitions of %qE corrupted",
19484 : 0 : imp->name);
19485 : : else
19486 : 323 : pushed = defs.quick_push (exp);
19487 : : }
19488 : : }
19489 : : else
19490 : 122 : pushed = defs.quick_push ((*macro_exports)[slot.offset]);
19491 : 445 : if (pushed && pushed->def)
19492 : : {
19493 : 439 : if (!def)
19494 : : def = pushed->def;
19495 : 149 : else if (cpp_compare_macros (def, pushed->def))
19496 : 457 : failed = true;
19497 : : }
19498 : : }
19499 : : }
19500 : :
19501 : 293 : if (failed)
19502 : : {
19503 : : /* If LOC is the first loc, this is the end of file check, which
19504 : : is a warning. */
19505 : 15 : auto_diagnostic_group d;
19506 : 15 : if (loc == MAP_START_LOCATION (LINEMAPS_ORDINARY_MAP_AT (line_table, 0)))
19507 : 9 : warning_at (loc, OPT_Winvalid_imported_macros,
19508 : : "inconsistent imported macro definition %qE",
19509 : : identifier (node));
19510 : : else
19511 : 6 : error_at (loc, "inconsistent imported macro definition %qE",
19512 : : identifier (node));
19513 : 60 : for (unsigned ix = defs.length (); ix--;)
19514 : : {
19515 : 30 : macro_export &exp = defs[ix];
19516 : 30 : if (exp.undef_loc)
19517 : 0 : inform (exp.undef_loc, "%<#undef %E%>", identifier (node));
19518 : 30 : if (exp.def)
19519 : 30 : inform (exp.def->line, "%<#define %s%>",
19520 : : cpp_macro_definition (reader, node, exp.def));
19521 : : }
19522 : 15 : def = NULL;
19523 : 15 : }
19524 : :
19525 : 293 : defs.release ();
19526 : :
19527 : 293 : dump.pop (n);
19528 : :
19529 : 293 : return def;
19530 : : }
19531 : :
19532 : : /* Stream the static aggregates. Sadly some headers (ahem:
19533 : : iostream) contain static vars, and rely on them to run global
19534 : : ctors. */
19535 : : unsigned
19536 : 855 : module_state::write_inits (elf_out *to, depset::hash &table, unsigned *crc_ptr)
19537 : : {
19538 : 855 : if (!static_aggregates && !tls_aggregates)
19539 : : return 0;
19540 : :
19541 : 45 : dump () && dump ("Writing initializers");
19542 : 45 : dump.indent ();
19543 : :
19544 : 45 : static_aggregates = nreverse (static_aggregates);
19545 : 45 : tls_aggregates = nreverse (tls_aggregates);
19546 : :
19547 : 45 : unsigned count = 0;
19548 : 45 : trees_out sec (to, this, table, ~0u);
19549 : 45 : sec.begin ();
19550 : :
19551 : 45 : tree list = static_aggregates;
19552 : 135 : for (int passes = 0; passes != 2; passes++)
19553 : : {
19554 : 258 : for (tree init = list; init; init = TREE_CHAIN (init))
19555 : 168 : if (TREE_LANG_FLAG_0 (init))
19556 : : {
19557 : 144 : if (STATIC_INIT_DECOMP_BASE_P (init))
19558 : : {
19559 : : /* Ensure that in the returned result chain if the
19560 : : STATIC_INIT_DECOMP_*BASE_P flags are set, there is
19561 : : always one or more STATIC_INIT_DECOMP_BASE_P TREE_LIST
19562 : : followed by one or more STATIC_INIT_DECOMP_NONBASE_P. */
19563 : 21 : int phase = 0;
19564 : 21 : tree last = NULL_TREE;
19565 : 21 : for (tree init2 = TREE_CHAIN (init);
19566 : 126 : init2; init2 = TREE_CHAIN (init2))
19567 : : {
19568 : 147 : if (phase == 0 && STATIC_INIT_DECOMP_BASE_P (init2))
19569 : : ;
19570 : 126 : else if (phase == 0
19571 : 147 : && STATIC_INIT_DECOMP_NONBASE_P (init2))
19572 : : {
19573 : 120 : phase = TREE_LANG_FLAG_0 (init2) ? 2 : 1;
19574 : : last = init2;
19575 : : }
19576 : 105 : else if (IN_RANGE (phase, 1, 2)
19577 : 210 : && STATIC_INIT_DECOMP_NONBASE_P (init2))
19578 : : {
19579 : 84 : if (TREE_LANG_FLAG_0 (init2))
19580 : 81 : phase = 2;
19581 : : last = init2;
19582 : : }
19583 : : else
19584 : : break;
19585 : : }
19586 : 21 : if (phase == 2)
19587 : : {
19588 : : /* In that case, add markers about it so that the
19589 : : STATIC_INIT_DECOMP_BASE_P and
19590 : : STATIC_INIT_DECOMP_NONBASE_P flags can be restored. */
19591 : 21 : sec.tree_node (build_int_cst (integer_type_node,
19592 : 21 : 2 * passes + 1));
19593 : 21 : phase = 1;
19594 : 147 : for (tree init2 = init; init2 != TREE_CHAIN (last);
19595 : 126 : init2 = TREE_CHAIN (init2))
19596 : 126 : if (TREE_LANG_FLAG_0 (init2))
19597 : : {
19598 : 102 : tree decl = TREE_VALUE (init2);
19599 : 102 : if (phase == 1
19600 : 102 : && STATIC_INIT_DECOMP_NONBASE_P (init2))
19601 : : {
19602 : 21 : sec.tree_node (build_int_cst (integer_type_node,
19603 : 21 : 2 * passes + 2));
19604 : 21 : phase = 2;
19605 : : }
19606 : 102 : dump ("Initializer:%u for %N", count, decl);
19607 : 102 : sec.tree_node (decl);
19608 : 102 : ++count;
19609 : : }
19610 : 21 : sec.tree_node (integer_zero_node);
19611 : 21 : init = last;
19612 : 21 : continue;
19613 : 21 : }
19614 : : }
19615 : :
19616 : 123 : tree decl = TREE_VALUE (init);
19617 : :
19618 : 123 : dump ("Initializer:%u for %N", count, decl);
19619 : 123 : sec.tree_node (decl);
19620 : 123 : ++count;
19621 : : }
19622 : :
19623 : 90 : list = tls_aggregates;
19624 : : }
19625 : :
19626 : 45 : sec.end (to, to->name (MOD_SNAME_PFX ".ini"), crc_ptr);
19627 : 45 : dump.outdent ();
19628 : :
19629 : 45 : return count;
19630 : 45 : }
19631 : :
19632 : : /* We have to defer some post-load processing until we've completed
19633 : : reading, because they can cause more reading. */
19634 : :
19635 : : static void
19636 : 7665 : post_load_processing ()
19637 : : {
19638 : : /* We mustn't cause a GC, our caller should have arranged for that
19639 : : not to happen. */
19640 : 7665 : gcc_checking_assert (function_depth);
19641 : :
19642 : 7665 : if (!post_load_decls)
19643 : : return;
19644 : :
19645 : 3801 : tree old_cfd = current_function_decl;
19646 : 3801 : struct function *old_cfun = cfun;
19647 : 9214 : while (post_load_decls->length ())
19648 : : {
19649 : 5413 : tree decl = post_load_decls->pop ();
19650 : :
19651 : 5459 : dump () && dump ("Post-load processing of %N", decl);
19652 : :
19653 : 5413 : gcc_checking_assert (DECL_MAYBE_IN_CHARGE_CDTOR_P (decl));
19654 : 5413 : expand_or_defer_fn (decl);
19655 : : /* As in module_state::read_cluster. */
19656 : 518 : if (at_eof && DECL_COMDAT (decl) && DECL_EXTERNAL (decl)
19657 : 5443 : && DECL_NOT_REALLY_EXTERN (decl))
19658 : 8 : DECL_EXTERNAL (decl) = false;
19659 : : }
19660 : :
19661 : 3801 : cfun = old_cfun;
19662 : 3801 : current_function_decl = old_cfd;
19663 : : }
19664 : :
19665 : : bool
19666 : 45 : module_state::read_inits (unsigned count)
19667 : : {
19668 : 45 : trees_in sec (this);
19669 : 45 : if (!sec.begin (loc, from (), from ()->find (MOD_SNAME_PFX ".ini")))
19670 : : return false;
19671 : 57 : dump () && dump ("Reading %u initializers", count);
19672 : 45 : dump.indent ();
19673 : :
19674 : 45 : lazy_snum = ~0u;
19675 : 45 : int decomp_phase = 0;
19676 : 45 : tree *aggrp = NULL;
19677 : 270 : for (unsigned ix = 0; ix != count; ix++)
19678 : : {
19679 : 225 : tree last = NULL_TREE;
19680 : 225 : if (decomp_phase)
19681 : 102 : last = *aggrp;
19682 : : /* Merely referencing the decl causes its initializer to be read
19683 : : and added to the correct list. */
19684 : 225 : tree decl = sec.tree_node ();
19685 : : /* module_state::write_inits can add special INTEGER_CST markers in
19686 : : between the decls. 1 means STATIC_INIT_DECOMP_BASE_P entries
19687 : : follow in static_aggregates, 2 means STATIC_INIT_DECOMP_NONBASE_P
19688 : : entries follow in static_aggregates, 3 means
19689 : : STATIC_INIT_DECOMP_BASE_P entries follow in tls_aggregates,
19690 : : 4 means STATIC_INIT_DECOMP_NONBASE_P follow in tls_aggregates,
19691 : : 0 means end of STATIC_INIT_DECOMP_{,NON}BASE_P sequence. */
19692 : 225 : if (tree_fits_shwi_p (decl))
19693 : : {
19694 : 63 : if (sec.get_overrun ())
19695 : : break;
19696 : 63 : decomp_phase = tree_to_shwi (decl);
19697 : 63 : if (decomp_phase)
19698 : : {
19699 : 42 : aggrp = decomp_phase > 2 ? &tls_aggregates : &static_aggregates;
19700 : : last = *aggrp;
19701 : : }
19702 : 63 : decl = sec.tree_node ();
19703 : : }
19704 : :
19705 : 225 : if (sec.get_overrun ())
19706 : : break;
19707 : 225 : if (decl)
19708 : 225 : dump ("Initializer:%u for %N", ix, decl);
19709 : 225 : if (decomp_phase)
19710 : : {
19711 : 102 : tree init = *aggrp;
19712 : 102 : gcc_assert (TREE_VALUE (init) == decl && TREE_CHAIN (init) == last);
19713 : 102 : if ((decomp_phase & 1) != 0)
19714 : 21 : STATIC_INIT_DECOMP_BASE_P (init) = 1;
19715 : : else
19716 : 81 : STATIC_INIT_DECOMP_NONBASE_P (init) = 1;
19717 : : }
19718 : : }
19719 : 45 : if (decomp_phase && !sec.get_overrun ())
19720 : : {
19721 : 0 : tree decl = sec.tree_node ();
19722 : 0 : gcc_assert (integer_zerop (decl));
19723 : : }
19724 : 45 : lazy_snum = 0;
19725 : 45 : post_load_processing ();
19726 : 45 : dump.outdent ();
19727 : 45 : if (!sec.end (from ()))
19728 : : return false;
19729 : : return true;
19730 : 45 : }
19731 : :
19732 : : void
19733 : 2398 : module_state::write_counts (elf_out *to, unsigned counts[MSC_HWM],
19734 : : unsigned *crc_ptr)
19735 : : {
19736 : 2398 : bytes_out cfg (to);
19737 : :
19738 : 2398 : cfg.begin ();
19739 : :
19740 : 21582 : for (unsigned ix = MSC_HWM; ix--;)
19741 : 19184 : cfg.u (counts[ix]);
19742 : :
19743 : 2398 : if (dump ())
19744 : : {
19745 : 252 : dump ("Cluster sections are [%u,%u)",
19746 : : counts[MSC_sec_lwm], counts[MSC_sec_hwm]);
19747 : 252 : dump ("Bindings %u", counts[MSC_bindings]);
19748 : 252 : dump ("Pendings %u", counts[MSC_pendings]);
19749 : 252 : dump ("Entities %u", counts[MSC_entities]);
19750 : 252 : dump ("Namespaces %u", counts[MSC_namespaces]);
19751 : 252 : dump ("Macros %u", counts[MSC_macros]);
19752 : 252 : dump ("Initializers %u", counts[MSC_inits]);
19753 : : }
19754 : :
19755 : 2398 : cfg.end (to, to->name (MOD_SNAME_PFX ".cnt"), crc_ptr);
19756 : 2398 : }
19757 : :
19758 : : bool
19759 : 2560 : module_state::read_counts (unsigned counts[MSC_HWM])
19760 : : {
19761 : 2560 : bytes_in cfg;
19762 : :
19763 : 2560 : if (!cfg.begin (loc, from (), MOD_SNAME_PFX ".cnt"))
19764 : : return false;
19765 : :
19766 : 23040 : for (unsigned ix = MSC_HWM; ix--;)
19767 : 20480 : counts[ix] = cfg.u ();
19768 : :
19769 : 2560 : if (dump ())
19770 : : {
19771 : 472 : dump ("Declaration sections are [%u,%u)",
19772 : : counts[MSC_sec_lwm], counts[MSC_sec_hwm]);
19773 : 472 : dump ("Bindings %u", counts[MSC_bindings]);
19774 : 472 : dump ("Pendings %u", counts[MSC_pendings]);
19775 : 472 : dump ("Entities %u", counts[MSC_entities]);
19776 : 472 : dump ("Namespaces %u", counts[MSC_namespaces]);
19777 : 472 : dump ("Macros %u", counts[MSC_macros]);
19778 : 472 : dump ("Initializers %u", counts[MSC_inits]);
19779 : : }
19780 : :
19781 : 2560 : return cfg.end (from ());
19782 : 2560 : }
19783 : :
19784 : : /* Tool configuration: MOD_SNAME_PFX .config
19785 : :
19786 : : This is data that confirms current state (or fails). */
19787 : :
19788 : : void
19789 : 2398 : module_state::write_config (elf_out *to, module_state_config &config,
19790 : : unsigned inner_crc)
19791 : : {
19792 : 2398 : bytes_out cfg (to);
19793 : :
19794 : 2398 : cfg.begin ();
19795 : :
19796 : : /* Write version and inner crc as u32 values, for easier
19797 : : debug inspection. */
19798 : 2650 : dump () && dump ("Writing version=%V, inner_crc=%x",
19799 : : MODULE_VERSION, inner_crc);
19800 : 2398 : cfg.u32 (unsigned (MODULE_VERSION));
19801 : 2398 : cfg.u32 (inner_crc);
19802 : :
19803 : 2398 : cfg.u (to->name (is_header () ? "" : get_flatname ()));
19804 : :
19805 : : /* Configuration. */
19806 : 2650 : dump () && dump ("Writing target='%s', host='%s'",
19807 : : TARGET_MACHINE, HOST_MACHINE);
19808 : 2398 : unsigned target = to->name (TARGET_MACHINE);
19809 : 2398 : unsigned host = (!strcmp (TARGET_MACHINE, HOST_MACHINE)
19810 : : ? target : to->name (HOST_MACHINE));
19811 : 2398 : cfg.u (target);
19812 : 2398 : cfg.u (host);
19813 : :
19814 : 2398 : cfg.str (config.dialect_str);
19815 : 2398 : cfg.u (extensions);
19816 : :
19817 : : /* Global tree information. We write the globals crc separately,
19818 : : rather than mix it directly into the overall crc, as it is used
19819 : : to ensure data match between instances of the compiler, not
19820 : : integrity of the file. */
19821 : 2650 : dump () && dump ("Writing globals=%u, crc=%x",
19822 : : fixed_trees->length (), global_crc);
19823 : 2398 : cfg.u (fixed_trees->length ());
19824 : 2398 : cfg.u32 (global_crc);
19825 : :
19826 : 2398 : if (is_partition ())
19827 : 159 : cfg.u (is_interface ());
19828 : :
19829 : 2398 : cfg.u (config.num_imports);
19830 : 2398 : cfg.u (config.num_partitions);
19831 : 2398 : cfg.u (config.num_entities);
19832 : :
19833 : 2398 : cfg.loc (config.ordinary_locs);
19834 : 2398 : cfg.loc (config.macro_locs);
19835 : 2398 : cfg.u (config.loc_range_bits);
19836 : :
19837 : 2398 : cfg.u (config.active_init);
19838 : :
19839 : : /* Now generate CRC, we'll have incorporated the inner CRC because
19840 : : of its serialization above. */
19841 : 2398 : cfg.end (to, to->name (MOD_SNAME_PFX ".cfg"), &crc);
19842 : 2650 : dump () && dump ("Writing CRC=%x", crc);
19843 : 2398 : }
19844 : :
19845 : : void
19846 : 37 : module_state::note_cmi_name ()
19847 : : {
19848 : 37 : if (!cmi_noted_p && filename)
19849 : : {
19850 : 37 : cmi_noted_p = true;
19851 : 37 : inform (loc, "compiled module file is %qs",
19852 : : maybe_add_cmi_prefix (filename));
19853 : : }
19854 : 37 : }
19855 : :
19856 : : bool
19857 : 2624 : module_state::read_config (module_state_config &config)
19858 : : {
19859 : 2624 : bytes_in cfg;
19860 : :
19861 : 2624 : if (!cfg.begin (loc, from (), MOD_SNAME_PFX ".cfg"))
19862 : : return false;
19863 : :
19864 : : /* Check version. */
19865 : 2624 : unsigned my_ver = MODULE_VERSION;
19866 : 2624 : unsigned their_ver = cfg.u32 ();
19867 : 3096 : dump () && dump (my_ver == their_ver ? "Version %V"
19868 : : : "Expecting %V found %V", my_ver, their_ver);
19869 : 2624 : if (their_ver != my_ver)
19870 : : {
19871 : : /* The compiler versions differ. Close enough? */
19872 : 0 : verstr_t my_string, their_string;
19873 : :
19874 : 0 : version2string (my_ver, my_string);
19875 : 0 : version2string (their_ver, their_string);
19876 : :
19877 : : /* Reject when either is non-experimental or when experimental
19878 : : major versions differ. */
19879 : 0 : auto_diagnostic_group d;
19880 : 0 : bool reject_p = ((!IS_EXPERIMENTAL (my_ver)
19881 : : || !IS_EXPERIMENTAL (their_ver)
19882 : 0 : || MODULE_MAJOR (my_ver) != MODULE_MAJOR (their_ver))
19883 : : /* The 'I know what I'm doing' switch. */
19884 : 0 : && !flag_module_version_ignore);
19885 : 0 : bool inform_p = true;
19886 : 0 : if (reject_p)
19887 : : {
19888 : 0 : cfg.set_overrun ();
19889 : 0 : error_at (loc, "compiled module is %sversion %s",
19890 : : IS_EXPERIMENTAL (their_ver) ? "experimental " : "",
19891 : : their_string);
19892 : : }
19893 : : else
19894 : 0 : inform_p = warning_at (loc, 0, "compiled module is %sversion %s",
19895 : : IS_EXPERIMENTAL (their_ver) ? "experimental " : "",
19896 : : their_string);
19897 : :
19898 : 0 : if (inform_p)
19899 : : {
19900 : 0 : inform (loc, "compiler is %sversion %s%s%s",
19901 : : IS_EXPERIMENTAL (my_ver) ? "experimental " : "",
19902 : : my_string,
19903 : 0 : reject_p ? "" : flag_module_version_ignore
19904 : 0 : ? ", be it on your own head!" : ", close enough?",
19905 : : reject_p ? "" : " \xc2\xaf\\_(\xe3\x83\x84)_/\xc2\xaf");
19906 : 0 : note_cmi_name ();
19907 : : }
19908 : :
19909 : 0 : if (reject_p)
19910 : 0 : goto done;
19911 : 0 : }
19912 : :
19913 : : /* We wrote the inner crc merely to merge it, so simply read it
19914 : : back and forget it. */
19915 : 2624 : cfg.u32 ();
19916 : :
19917 : : /* Check module name. */
19918 : 2624 : {
19919 : 2624 : const char *their_name = from ()->name (cfg.u ());
19920 : 2624 : const char *our_name = "";
19921 : :
19922 : 2624 : if (!is_header ())
19923 : 1681 : our_name = get_flatname ();
19924 : :
19925 : : /* Header units can be aliased, so name checking is
19926 : : inappropriate. */
19927 : 2624 : if (0 != strcmp (their_name, our_name))
19928 : : {
19929 : 0 : error_at (loc,
19930 : 0 : their_name[0] && our_name[0] ? G_("module %qs found")
19931 : : : their_name[0]
19932 : : ? G_("header module expected, module %qs found")
19933 : : : G_("module %qs expected, header module found"),
19934 : 0 : their_name[0] ? their_name : our_name);
19935 : 0 : cfg.set_overrun ();
19936 : 0 : goto done;
19937 : : }
19938 : : }
19939 : :
19940 : : /* Check the CRC after the above sanity checks, so that the user is
19941 : : clued in. */
19942 : 2624 : {
19943 : 2624 : unsigned e_crc = crc;
19944 : 2624 : crc = cfg.get_crc ();
19945 : 3096 : dump () && dump ("Reading CRC=%x", crc);
19946 : 2624 : if (!is_direct () && crc != e_crc)
19947 : : {
19948 : 3 : error_at (loc, "module %qs CRC mismatch", get_flatname ());
19949 : 3 : cfg.set_overrun ();
19950 : 3 : goto done;
19951 : : }
19952 : : }
19953 : :
19954 : : /* Check target & host. */
19955 : 2621 : {
19956 : 2621 : const char *their_target = from ()->name (cfg.u ());
19957 : 2621 : const char *their_host = from ()->name (cfg.u ());
19958 : 3093 : dump () && dump ("Read target='%s', host='%s'", their_target, their_host);
19959 : 2621 : if (strcmp (their_target, TARGET_MACHINE)
19960 : 2621 : || strcmp (their_host, HOST_MACHINE))
19961 : : {
19962 : 0 : error_at (loc, "target & host is %qs:%qs, expected %qs:%qs",
19963 : : their_target, TARGET_MACHINE, their_host, HOST_MACHINE);
19964 : 0 : cfg.set_overrun ();
19965 : 0 : goto done;
19966 : : }
19967 : : }
19968 : :
19969 : : /* Check compilation dialect. This must match. */
19970 : 2621 : {
19971 : 2621 : const char *their_dialect = cfg.str ();
19972 : 2621 : if (strcmp (their_dialect, config.dialect_str))
19973 : : {
19974 : 1 : error_at (loc, "language dialect differs %qs, expected %qs",
19975 : : their_dialect, config.dialect_str);
19976 : 1 : cfg.set_overrun ();
19977 : 1 : goto done;
19978 : : }
19979 : : }
19980 : :
19981 : : /* Check for extensions. If they set any, we must have them set
19982 : : too. */
19983 : 2620 : {
19984 : 2620 : unsigned ext = cfg.u ();
19985 : 2620 : unsigned allowed = (flag_openmp ? SE_OPENMP | SE_OPENMP_SIMD : 0);
19986 : 2620 : if (flag_openmp_simd)
19987 : 3 : allowed |= SE_OPENMP_SIMD;
19988 : 2620 : if (flag_openacc)
19989 : 3 : allowed |= SE_OPENACC;
19990 : :
19991 : 2620 : if (unsigned bad = ext & ~allowed)
19992 : : {
19993 : 9 : if (bad & SE_OPENMP)
19994 : 3 : error_at (loc, "module contains OpenMP, use %<-fopenmp%> to enable");
19995 : 6 : else if (bad & SE_OPENMP_SIMD)
19996 : 3 : error_at (loc, "module contains OpenMP, use %<-fopenmp%> or "
19997 : : "%<-fopenmp-simd%> to enable");
19998 : 9 : if (bad & SE_OPENACC)
19999 : 3 : error_at (loc, "module contains OpenACC, use %<-fopenacc%> to "
20000 : : "enable");
20001 : 9 : cfg.set_overrun ();
20002 : 9 : goto done;
20003 : : }
20004 : 2611 : extensions = ext;
20005 : : }
20006 : :
20007 : : /* Check global trees. */
20008 : 2611 : {
20009 : 2611 : unsigned their_fixed_length = cfg.u ();
20010 : 2611 : unsigned their_fixed_crc = cfg.u32 ();
20011 : 3083 : dump () && dump ("Read globals=%u, crc=%x",
20012 : : their_fixed_length, their_fixed_crc);
20013 : 2611 : if (!flag_preprocess_only
20014 : 2611 : && (their_fixed_length != fixed_trees->length ()
20015 : 2572 : || their_fixed_crc != global_crc))
20016 : : {
20017 : 0 : error_at (loc, "fixed tree mismatch");
20018 : 0 : cfg.set_overrun ();
20019 : 0 : goto done;
20020 : : }
20021 : : }
20022 : :
20023 : : /* All non-partitions are interfaces. */
20024 : 2611 : interface_p = !is_partition () || cfg.u ();
20025 : :
20026 : 2611 : config.num_imports = cfg.u ();
20027 : 2611 : config.num_partitions = cfg.u ();
20028 : 2611 : config.num_entities = cfg.u ();
20029 : :
20030 : 2611 : config.ordinary_locs = cfg.loc ();
20031 : 2611 : config.macro_locs = cfg.loc ();
20032 : 2611 : config.loc_range_bits = cfg.u ();
20033 : :
20034 : 2611 : config.active_init = cfg.u ();
20035 : :
20036 : 2624 : done:
20037 : 2624 : return cfg.end (from ());
20038 : 2624 : }
20039 : :
20040 : : /* Comparator for ordering the Ordered Ordinary Location array. */
20041 : :
20042 : : static int
20043 : 96 : ool_cmp (const void *a_, const void *b_)
20044 : : {
20045 : 96 : auto *a = *static_cast<const module_state *const *> (a_);
20046 : 96 : auto *b = *static_cast<const module_state *const *> (b_);
20047 : 96 : if (a == b)
20048 : : return 0;
20049 : 96 : else if (a->ordinary_locs.first < b->ordinary_locs.first)
20050 : : return -1;
20051 : : else
20052 : 38 : return +1;
20053 : : }
20054 : :
20055 : : /* Use ELROND format to record the following sections:
20056 : : qualified-names : binding value(s)
20057 : : MOD_SNAME_PFX.README : human readable, strings
20058 : : MOD_SNAME_PFX.ENV : environment strings, strings
20059 : : MOD_SNAME_PFX.nms : namespace hierarchy
20060 : : MOD_SNAME_PFX.bnd : binding table
20061 : : MOD_SNAME_PFX.spc : specialization table
20062 : : MOD_SNAME_PFX.imp : import table
20063 : : MOD_SNAME_PFX.ent : entity table
20064 : : MOD_SNAME_PFX.prt : partitions table
20065 : : MOD_SNAME_PFX.olm : ordinary line maps
20066 : : MOD_SNAME_PFX.mlm : macro line maps
20067 : : MOD_SNAME_PFX.def : macro definitions
20068 : : MOD_SNAME_PFX.mac : macro index
20069 : : MOD_SNAME_PFX.ini : inits
20070 : : MOD_SNAME_PFX.cnt : counts
20071 : : MOD_SNAME_PFX.cfg : config data
20072 : : */
20073 : :
20074 : : bool
20075 : 2426 : module_state::write_begin (elf_out *to, cpp_reader *reader,
20076 : : module_state_config &config, unsigned &crc)
20077 : : {
20078 : : /* Figure out remapped module numbers, which might elide
20079 : : partitions. */
20080 : 2426 : bitmap partitions = NULL;
20081 : 2426 : if (!is_header () && !is_partition ())
20082 : 1412 : partitions = BITMAP_GGC_ALLOC ();
20083 : 2426 : write_init_maps ();
20084 : :
20085 : 2426 : unsigned mod_hwm = 1;
20086 : 2989 : for (unsigned ix = 1; ix != modules->length (); ix++)
20087 : : {
20088 : 563 : module_state *imp = (*modules)[ix];
20089 : :
20090 : : /* Promote any non-partition direct import from a partition, unless
20091 : : we're a partition. */
20092 : 521 : if (!is_partition () && !imp->is_partition ()
20093 : 925 : && imp->is_partition_direct ())
20094 : 9 : imp->directness = MD_PURVIEW_DIRECT;
20095 : :
20096 : : /* Write any import that is not a partition, unless we're a
20097 : : partition. */
20098 : 563 : if (!partitions || !imp->is_partition ())
20099 : 404 : imp->remap = mod_hwm++;
20100 : : else
20101 : : {
20102 : 189 : dump () && dump ("Partition %M %u", imp, ix);
20103 : 159 : bitmap_set_bit (partitions, ix);
20104 : 159 : imp->remap = 0;
20105 : : /* All interface partitions must be exported. */
20106 : 159 : if (imp->is_interface () && !bitmap_bit_p (exports, imp->mod))
20107 : : {
20108 : 3 : error_at (imp->loc, "interface partition is not exported");
20109 : 3 : bitmap_set_bit (exports, imp->mod);
20110 : : }
20111 : :
20112 : : /* All the partition entities should have been loaded when
20113 : : loading the partition. */
20114 : : if (CHECKING_P)
20115 : 879 : for (unsigned jx = 0; jx != imp->entity_num; jx++)
20116 : : {
20117 : 720 : binding_slot *slot = &(*entity_ary)[imp->entity_lwm + jx];
20118 : 720 : gcc_checking_assert (!slot->is_lazy ());
20119 : : }
20120 : : }
20121 : :
20122 : 563 : if (imp->is_direct () && (imp->remap || imp->is_partition ()))
20123 : 549 : note_location (imp->imported_from ());
20124 : : }
20125 : :
20126 : 2426 : if (partitions && bitmap_empty_p (partitions))
20127 : : /* No partitions present. */
20128 : : partitions = nullptr;
20129 : :
20130 : : /* Find the set of decls we must write out. */
20131 : 2426 : depset::hash table (DECL_NAMESPACE_BINDINGS (global_namespace)->size () * 8);
20132 : : /* Add the specializations before the writables, so that we can
20133 : : detect injected friend specializations. */
20134 : 2426 : table.add_specializations (true);
20135 : 2426 : table.add_specializations (false);
20136 : 2426 : if (partial_specializations)
20137 : : {
20138 : 194 : table.add_partial_entities (partial_specializations);
20139 : 194 : partial_specializations = NULL;
20140 : : }
20141 : 2426 : table.add_namespace_entities (global_namespace, partitions);
20142 : 2426 : if (class_members)
20143 : : {
20144 : 12 : table.add_class_entities (class_members);
20145 : 12 : class_members = NULL;
20146 : : }
20147 : :
20148 : : /* Now join everything up. */
20149 : 2426 : table.find_dependencies (this);
20150 : :
20151 : 2426 : if (!table.finalize_dependencies ())
20152 : : return false;
20153 : :
20154 : : #if CHECKING_P
20155 : : /* We're done verifying at-most once reading, reset to verify
20156 : : at-most once writing. */
20157 : 2398 : note_defs = note_defs_table_t::create_ggc (1000);
20158 : : #endif
20159 : :
20160 : : /* Determine Strongly Connected Components. This will also strip any
20161 : : unnecessary dependencies on imported or TU-local entities. */
20162 : 2398 : vec<depset *> sccs = table.connect ();
20163 : :
20164 : 2398 : vec_alloc (ool, modules->length ());
20165 : 2958 : for (unsigned ix = modules->length (); --ix;)
20166 : : {
20167 : 560 : auto *import = (*modules)[ix];
20168 : 560 : if (import->loadedness > ML_NONE
20169 : 560 : && !(partitions && bitmap_bit_p (partitions, import->mod)))
20170 : 401 : ool->quick_push (import);
20171 : : }
20172 : 2398 : ool->qsort (ool_cmp);
20173 : :
20174 : 2398 : write_diagnostic_classification (nullptr, global_dc, nullptr);
20175 : :
20176 : 2398 : vec<cpp_hashnode *> *macros = nullptr;
20177 : 2398 : if (is_header ())
20178 : 855 : macros = prepare_macros (reader);
20179 : :
20180 : 2398 : config.num_imports = mod_hwm;
20181 : 2398 : config.num_partitions = modules->length () - mod_hwm;
20182 : 2398 : auto map_info = write_prepare_maps (&config, bool (config.num_partitions));
20183 : 2398 : unsigned counts[MSC_HWM];
20184 : 2398 : memset (counts, 0, sizeof (counts));
20185 : :
20186 : : /* depset::cluster is the cluster number,
20187 : : depset::section is unspecified scratch value.
20188 : :
20189 : : The following loops make use of the tarjan property that
20190 : : dependencies will be earlier in the SCCS array. */
20191 : :
20192 : : /* This first loop determines the number of depsets in each SCC, and
20193 : : also the number of namespaces we're dealing with. During the
20194 : : loop, the meaning of a couple of depset fields now change:
20195 : :
20196 : : depset::cluster -> size_of cluster, if first of cluster & !namespace
20197 : : depset::section -> section number of cluster (if !namespace). */
20198 : :
20199 : 2398 : unsigned n_spaces = 0;
20200 : 2398 : counts[MSC_sec_lwm] = counts[MSC_sec_hwm] = to->get_section_limit ();
20201 : 230482 : for (unsigned size, ix = 0; ix < sccs.length (); ix += size)
20202 : : {
20203 : 228084 : depset **base = &sccs[ix];
20204 : :
20205 : 431271 : if (base[0]->get_entity_kind () == depset::EK_NAMESPACE)
20206 : : {
20207 : 4086 : n_spaces++;
20208 : 4086 : size = 1;
20209 : : }
20210 : : else
20211 : : {
20212 : : /* Count the members in this cluster. */
20213 : 920967 : for (size = 1; ix + size < sccs.length (); size++)
20214 : 918864 : if (base[size]->cluster != base[0]->cluster)
20215 : : break;
20216 : :
20217 : 1144965 : for (unsigned jx = 0; jx != size; jx++)
20218 : : {
20219 : : /* Set the section number. */
20220 : 920967 : base[jx]->cluster = ~(~0u >> 1); /* A bad value. */
20221 : 920967 : base[jx]->section = counts[MSC_sec_hwm];
20222 : : }
20223 : :
20224 : : /* Save the size in the first member's cluster slot. */
20225 : 223998 : base[0]->cluster = size;
20226 : :
20227 : 223998 : counts[MSC_sec_hwm]++;
20228 : : }
20229 : : }
20230 : :
20231 : : /* Write the clusters. Namespace decls are put in the spaces array.
20232 : : The meaning of depset::cluster changes to provide the
20233 : : unnamed-decl count of the depset's decl (and remains zero for
20234 : : non-decls and non-unnamed). */
20235 : 2398 : unsigned bytes = 0;
20236 : 2398 : vec<depset *> spaces;
20237 : 2398 : spaces.create (n_spaces);
20238 : :
20239 : 230482 : for (unsigned size, ix = 0; ix < sccs.length (); ix += size)
20240 : : {
20241 : 228084 : depset **base = &sccs[ix];
20242 : :
20243 : 228084 : if (base[0]->get_entity_kind () == depset::EK_NAMESPACE)
20244 : : {
20245 : 4086 : tree decl = base[0]->get_entity ();
20246 : 4086 : if (decl == global_namespace)
20247 : 2159 : base[0]->cluster = 0;
20248 : 1927 : else if (!base[0]->is_import ())
20249 : : {
20250 : 1927 : base[0]->cluster = counts[MSC_entities]++;
20251 : 1927 : spaces.quick_push (base[0]);
20252 : 1927 : counts[MSC_namespaces]++;
20253 : 1927 : if (CHECKING_P)
20254 : : {
20255 : : /* Add it to the entity map, such that we can tell it is
20256 : : part of us. */
20257 : 1927 : bool existed;
20258 : 1927 : unsigned *slot = &entity_map->get_or_insert
20259 : 1927 : (DECL_UID (decl), &existed);
20260 : 1927 : if (existed)
20261 : : /* It must have come from a partition. */
20262 : 0 : gcc_checking_assert
20263 : : (import_entity_module (*slot)->is_partition ());
20264 : 1927 : *slot = ~base[0]->cluster;
20265 : : }
20266 : 230041 : dump (dumper::CLUSTER) && dump ("Cluster namespace %N", decl);
20267 : : }
20268 : : size = 1;
20269 : : }
20270 : : else
20271 : : {
20272 : 223998 : size = base[0]->cluster;
20273 : :
20274 : : /* Cluster is now used to number entities. */
20275 : 223998 : base[0]->cluster = ~(~0u >> 1); /* A bad value. */
20276 : :
20277 : 223998 : sort_cluster (&table, base, size);
20278 : :
20279 : : /* Record the section for consistency checking during stream
20280 : : out -- we don't want to start writing decls in different
20281 : : sections. */
20282 : 223998 : table.section = base[0]->section;
20283 : 223998 : bytes += write_cluster (to, base, size, table, counts, &crc);
20284 : 223998 : table.section = 0;
20285 : : }
20286 : : }
20287 : :
20288 : : /* depset::cluster - entity number (on entities)
20289 : : depset::section - cluster number */
20290 : : /* We'd better have written as many sections and found as many
20291 : : namespaces as we predicted. */
20292 : 4796 : gcc_assert (counts[MSC_sec_hwm] == to->get_section_limit ()
20293 : : && spaces.length () == counts[MSC_namespaces]);
20294 : :
20295 : : /* Write the entitites. None happens if we contain namespaces or
20296 : : nothing. */
20297 : 2398 : config.num_entities = counts[MSC_entities];
20298 : 2398 : if (counts[MSC_entities])
20299 : 2156 : write_entities (to, sccs, counts[MSC_entities], &crc);
20300 : :
20301 : : /* Write the namespaces. */
20302 : 2398 : if (counts[MSC_namespaces])
20303 : 471 : write_namespaces (to, spaces, counts[MSC_namespaces], &crc);
20304 : :
20305 : : /* Write the bindings themselves. */
20306 : 2398 : counts[MSC_bindings] = write_bindings (to, sccs, &crc);
20307 : :
20308 : : /* Write the unnamed. */
20309 : 2398 : counts[MSC_pendings] = write_pendings (to, sccs, table, &crc);
20310 : :
20311 : : /* Write the import table. */
20312 : 2398 : if (config.num_imports > 1)
20313 : 377 : write_imports (to, &crc);
20314 : :
20315 : : /* Write elided partition table. */
20316 : 2398 : if (config.num_partitions)
20317 : 111 : write_partitions (to, config.num_partitions, &crc);
20318 : :
20319 : : /* Write the line maps. */
20320 : 2398 : if (config.ordinary_locs)
20321 : : {
20322 : 2302 : write_ordinary_maps (to, map_info, bool (config.num_partitions), &crc);
20323 : 2302 : write_diagnostic_classification (to, global_dc, &crc);
20324 : : }
20325 : 2398 : if (config.macro_locs)
20326 : 117 : write_macro_maps (to, map_info, &crc);
20327 : :
20328 : 2398 : if (is_header ())
20329 : : {
20330 : 855 : counts[MSC_macros] = write_macros (to, macros, &crc);
20331 : 855 : counts[MSC_inits] = write_inits (to, table, &crc);
20332 : 855 : vec_free (macros);
20333 : : }
20334 : :
20335 : 2398 : unsigned clusters = counts[MSC_sec_hwm] - counts[MSC_sec_lwm];
20336 : 2398 : dump () && dump ("Wrote %u clusters, average %u bytes/cluster",
20337 : 252 : clusters, (bytes + clusters / 2) / (clusters + !clusters));
20338 : 2398 : trees_out::instrument ();
20339 : :
20340 : 2398 : write_counts (to, counts, &crc);
20341 : :
20342 : 2398 : spaces.release ();
20343 : 2398 : sccs.release ();
20344 : :
20345 : 2398 : vec_free (macro_loc_remap);
20346 : 2398 : vec_free (ord_loc_remap);
20347 : 2398 : vec_free (ool);
20348 : :
20349 : : // FIXME:QOI: Have a command line switch to control more detailed
20350 : : // information (which might leak data you do not want to leak).
20351 : : // Perhaps (some of) the write_readme contents should also be
20352 : : // so-controlled.
20353 : 2398 : if (false)
20354 : : write_env (to);
20355 : :
20356 : 2398 : return true;
20357 : 2426 : }
20358 : :
20359 : : // Finish module writing after we've emitted all dynamic initializers.
20360 : :
20361 : : void
20362 : 2398 : module_state::write_end (elf_out *to, cpp_reader *reader,
20363 : : module_state_config &config, unsigned &crc)
20364 : : {
20365 : : /* And finish up. */
20366 : 2398 : write_config (to, config, crc);
20367 : :
20368 : : /* Human-readable info. */
20369 : 2398 : write_readme (to, reader, config.dialect_str);
20370 : :
20371 : 2650 : dump () && dump ("Wrote %u sections", to->get_section_limit ());
20372 : 2398 : }
20373 : :
20374 : : /* Initial read of a CMI. Checks config, loads up imports and line
20375 : : maps. */
20376 : :
20377 : : bool
20378 : 2624 : module_state::read_initial (cpp_reader *reader)
20379 : : {
20380 : 2624 : module_state_config config;
20381 : 2624 : bool ok = true;
20382 : :
20383 : 2624 : if (ok && !from ()->begin (loc))
20384 : : ok = false;
20385 : :
20386 : 2624 : if (ok && !read_config (config))
20387 : : ok = false;
20388 : :
20389 : 2611 : bool have_locs = ok && read_prepare_maps (&config);
20390 : :
20391 : : /* Ordinary maps before the imports. */
20392 : 2611 : if (!(have_locs && config.ordinary_locs))
20393 : 77 : ordinary_locs.first = line_table->highest_location + 1;
20394 : 2547 : else if (!read_ordinary_maps (config.ordinary_locs, config.loc_range_bits))
20395 : : ok = false;
20396 : :
20397 : 2611 : if (ok && have_locs && config.ordinary_locs
20398 : 5171 : && !read_diagnostic_classification (global_dc))
20399 : : ok = false;
20400 : :
20401 : : /* Allocate the REMAP vector. */
20402 : 2624 : slurp->alloc_remap (config.num_imports);
20403 : :
20404 : 2624 : if (ok)
20405 : : {
20406 : : /* Read the import table. Decrement current to stop this CMI
20407 : : from being evicted during the import. */
20408 : 2611 : slurp->current--;
20409 : 2611 : if (config.num_imports > 1 && !read_imports (reader, line_table))
20410 : : ok = false;
20411 : 2611 : slurp->current++;
20412 : : }
20413 : :
20414 : : /* Read the elided partition table, if we're the primary partition. */
20415 : 2611 : if (ok && config.num_partitions && is_module ()
20416 : 2626 : && !read_partitions (config.num_partitions))
20417 : : ok = false;
20418 : :
20419 : : /* Determine the module's number. */
20420 : 2624 : gcc_checking_assert (mod == MODULE_UNKNOWN);
20421 : 2624 : gcc_checking_assert (this != (*modules)[0]);
20422 : :
20423 : 2624 : {
20424 : : /* Allocate space in the entities array now -- that array must be
20425 : : monotonically in step with the modules array. */
20426 : 2624 : entity_lwm = vec_safe_length (entity_ary);
20427 : 2624 : entity_num = config.num_entities;
20428 : 2624 : gcc_checking_assert (modules->length () == 1
20429 : : || modules->last ()->entity_lwm <= entity_lwm);
20430 : 2624 : vec_safe_reserve (entity_ary, config.num_entities);
20431 : :
20432 : 2624 : binding_slot slot;
20433 : 2624 : slot.u.binding = NULL_TREE;
20434 : 958357 : for (unsigned count = config.num_entities; count--;)
20435 : 955733 : entity_ary->quick_push (slot);
20436 : : }
20437 : :
20438 : : /* We'll run out of other resources before we run out of module
20439 : : indices. */
20440 : 2624 : mod = modules->length ();
20441 : 2624 : vec_safe_push (modules, this);
20442 : :
20443 : : /* We always import and export ourselves. */
20444 : 2624 : bitmap_set_bit (imports, mod);
20445 : 2624 : bitmap_set_bit (exports, mod);
20446 : :
20447 : 2624 : if (ok)
20448 : 2611 : (*slurp->remap)[0] = mod << 1;
20449 : 3096 : dump () && dump ("Assigning %M module number %u", this, mod);
20450 : :
20451 : : /* We should not have been frozen during the importing done by
20452 : : read_config. */
20453 : 2624 : gcc_assert (!from ()->is_frozen ());
20454 : :
20455 : : /* Macro maps after the imports. */
20456 : 2624 : if (!(ok && have_locs && config.macro_locs))
20457 : 2497 : macro_locs.first = LINEMAPS_MACRO_LOWEST_LOCATION (line_table);
20458 : 127 : else if (!read_macro_maps (config.macro_locs))
20459 : : ok = false;
20460 : :
20461 : : /* Note whether there's an active initializer. */
20462 : 2624 : active_init_p = !is_header () && bool (config.active_init);
20463 : :
20464 : 2624 : gcc_assert (slurp->current == ~0u);
20465 : 2624 : return ok;
20466 : : }
20467 : :
20468 : : /* Read a preprocessor state. */
20469 : :
20470 : : bool
20471 : 895 : module_state::read_preprocessor (bool outermost)
20472 : : {
20473 : 895 : gcc_checking_assert (is_header () && slurp
20474 : : && slurp->remap_module (0) == mod);
20475 : :
20476 : 895 : if (loadedness == ML_PREPROCESSOR)
20477 : 6 : return !(from () && from ()->get_error ());
20478 : :
20479 : 889 : bool ok = true;
20480 : :
20481 : : /* Read direct header imports. */
20482 : 889 : unsigned len = slurp->remap->length ();
20483 : 931 : for (unsigned ix = 1; ok && ix != len; ix++)
20484 : : {
20485 : 42 : unsigned map = (*slurp->remap)[ix];
20486 : 42 : if (map & 1)
20487 : : {
20488 : 42 : module_state *import = (*modules)[map >> 1];
20489 : 42 : if (import->is_header ())
20490 : : {
20491 : 42 : ok = import->read_preprocessor (false);
20492 : 42 : bitmap_ior_into (slurp->headers, import->slurp->headers);
20493 : : }
20494 : : }
20495 : : }
20496 : :
20497 : : /* Record as a direct header. */
20498 : 889 : if (ok)
20499 : 889 : bitmap_set_bit (slurp->headers, mod);
20500 : :
20501 : 889 : if (ok && !read_macros ())
20502 : : ok = false;
20503 : :
20504 : 889 : loadedness = ML_PREPROCESSOR;
20505 : 889 : announce ("macros");
20506 : :
20507 : 889 : if (flag_preprocess_only)
20508 : : /* We're done with the string table. */
20509 : 39 : from ()->release ();
20510 : :
20511 : 889 : return check_read (outermost, ok);
20512 : : }
20513 : :
20514 : : /* Read language state. */
20515 : :
20516 : : bool
20517 : 2628 : module_state::read_language (bool outermost)
20518 : : {
20519 : 2628 : gcc_checking_assert (!lazy_snum);
20520 : :
20521 : 2628 : if (loadedness == ML_LANGUAGE)
20522 : 68 : return !(slurp && from () && from ()->get_error ());
20523 : :
20524 : 2560 : gcc_checking_assert (slurp && slurp->current == ~0u
20525 : : && slurp->remap_module (0) == mod);
20526 : :
20527 : 2560 : bool ok = true;
20528 : :
20529 : : /* Read direct imports. */
20530 : 2560 : unsigned len = slurp->remap->length ();
20531 : 2887 : for (unsigned ix = 1; ok && ix != len; ix++)
20532 : : {
20533 : 327 : unsigned map = (*slurp->remap)[ix];
20534 : 327 : if (map & 1)
20535 : : {
20536 : 327 : module_state *import = (*modules)[map >> 1];
20537 : 327 : if (!import->read_language (false))
20538 : 327 : ok = false;
20539 : : }
20540 : : }
20541 : :
20542 : 2560 : unsigned counts[MSC_HWM];
20543 : :
20544 : 2560 : if (ok && !read_counts (counts))
20545 : : ok = false;
20546 : :
20547 : 2560 : function_depth++; /* Prevent unexpected GCs. */
20548 : :
20549 : 2560 : if (ok && counts[MSC_entities] != entity_num)
20550 : : ok = false;
20551 : 2560 : if (ok && counts[MSC_entities]
20552 : 2355 : && !read_entities (counts[MSC_entities],
20553 : : counts[MSC_sec_lwm], counts[MSC_sec_hwm]))
20554 : : ok = false;
20555 : :
20556 : : /* Read the namespace hierarchy. */
20557 : 2560 : if (ok && counts[MSC_namespaces]
20558 : 3046 : && !read_namespaces (counts[MSC_namespaces]))
20559 : : ok = false;
20560 : :
20561 : 2560 : if (ok && !read_bindings (counts[MSC_bindings],
20562 : : counts[MSC_sec_lwm], counts[MSC_sec_hwm]))
20563 : : ok = false;
20564 : :
20565 : : /* And unnamed. */
20566 : 2560 : if (ok && counts[MSC_pendings] && !read_pendings (counts[MSC_pendings]))
20567 : : ok = false;
20568 : :
20569 : 2560 : if (ok)
20570 : : {
20571 : 2560 : slurp->remaining = counts[MSC_sec_hwm] - counts[MSC_sec_lwm];
20572 : 2560 : available_clusters += counts[MSC_sec_hwm] - counts[MSC_sec_lwm];
20573 : : }
20574 : :
20575 : 2560 : if (!flag_module_lazy
20576 : 2560 : || (is_partition ()
20577 : 198 : && module_interface_p ()
20578 : 183 : && !module_partition_p ()))
20579 : : {
20580 : : /* Read the sections in forward order, so that dependencies are read
20581 : : first. See note about tarjan_connect. */
20582 : 479 : ggc_collect ();
20583 : :
20584 : 479 : lazy_snum = ~0u;
20585 : :
20586 : 479 : unsigned hwm = counts[MSC_sec_hwm];
20587 : 136017 : for (unsigned ix = counts[MSC_sec_lwm]; ok && ix != hwm; ix++)
20588 : 135538 : if (!load_section (ix, NULL))
20589 : : {
20590 : : ok = false;
20591 : : break;
20592 : : }
20593 : 479 : lazy_snum = 0;
20594 : 479 : post_load_processing ();
20595 : :
20596 : 479 : ggc_collect ();
20597 : :
20598 : 479 : if (ok && CHECKING_P)
20599 : 494102 : for (unsigned ix = 0; ix != entity_num; ix++)
20600 : 493623 : gcc_assert (!(*entity_ary)[ix + entity_lwm].is_lazy ());
20601 : : }
20602 : :
20603 : : // If the import is a header-unit, we need to register initializers
20604 : : // of any static objects it contains (looking at you _Ioinit).
20605 : : // Notice, the ordering of these initializers will be that of a
20606 : : // dynamic initializer at this point in the current TU. (Other
20607 : : // instances of these objects in other TUs will be initialized as
20608 : : // part of that TU's global initializers.)
20609 : 2560 : if (ok && counts[MSC_inits] && !read_inits (counts[MSC_inits]))
20610 : : ok = false;
20611 : :
20612 : 2560 : function_depth--;
20613 : :
20614 : 2880 : announce (flag_module_lazy ? "lazy" : "imported");
20615 : 2560 : loadedness = ML_LANGUAGE;
20616 : :
20617 : 2560 : gcc_assert (slurp->current == ~0u);
20618 : :
20619 : : /* We're done with the string table. */
20620 : 2560 : from ()->release ();
20621 : :
20622 : 2560 : return check_read (outermost, ok);
20623 : : }
20624 : :
20625 : : bool
20626 : 165037 : module_state::maybe_defrost ()
20627 : : {
20628 : 165037 : bool ok = true;
20629 : 165037 : if (from ()->is_frozen ())
20630 : : {
20631 : 9 : if (lazy_open >= lazy_limit)
20632 : 3 : freeze_an_elf ();
20633 : 18 : dump () && dump ("Defrosting '%s'", filename);
20634 : 9 : ok = from ()->defrost (maybe_add_cmi_prefix (filename));
20635 : 9 : lazy_open++;
20636 : : }
20637 : :
20638 : 165037 : return ok;
20639 : : }
20640 : :
20641 : : /* Load section SNUM, dealing with laziness. It doesn't matter if we
20642 : : have multiple concurrent loads, because we do not use TREE_VISITED
20643 : : when reading back in. */
20644 : :
20645 : : bool
20646 : 165037 : module_state::load_section (unsigned snum, binding_slot *mslot)
20647 : : {
20648 : 165037 : if (from ()->get_error ())
20649 : : return false;
20650 : :
20651 : 165037 : if (snum >= slurp->current)
20652 : 0 : from ()->set_error (elf::E_BAD_LAZY);
20653 : 165037 : else if (maybe_defrost ())
20654 : : {
20655 : 165037 : unsigned old_current = slurp->current;
20656 : 165037 : slurp->current = snum;
20657 : 165037 : slurp->lru = 0; /* Do not swap out. */
20658 : 165037 : slurp->remaining--;
20659 : 165037 : read_cluster (snum);
20660 : 165037 : slurp->lru = ++lazy_lru;
20661 : 165037 : slurp->current = old_current;
20662 : : }
20663 : :
20664 : 165037 : if (mslot && mslot->is_lazy ())
20665 : : {
20666 : : /* Oops, the section didn't set this slot. */
20667 : 0 : from ()->set_error (elf::E_BAD_DATA);
20668 : 0 : *mslot = NULL_TREE;
20669 : : }
20670 : :
20671 : 165037 : bool ok = !from ()->get_error ();
20672 : 165037 : if (!ok)
20673 : : {
20674 : 0 : error_at (loc, "failed to read compiled module cluster %u: %s",
20675 : 0 : snum, from ()->get_error (filename));
20676 : 0 : note_cmi_name ();
20677 : : }
20678 : :
20679 : 165037 : maybe_completed_reading ();
20680 : :
20681 : 165037 : return ok;
20682 : : }
20683 : :
20684 : : void
20685 : 171094 : module_state::maybe_completed_reading ()
20686 : : {
20687 : 171094 : if (loadedness == ML_LANGUAGE && slurp->current == ~0u && !slurp->remaining)
20688 : : {
20689 : 2129 : lazy_open--;
20690 : : /* We no longer need the macros, all tokenizing has been done. */
20691 : 2129 : slurp->release_macros ();
20692 : :
20693 : 2129 : from ()->end ();
20694 : 2129 : slurp->close ();
20695 : 2129 : slurped ();
20696 : : }
20697 : 171094 : }
20698 : :
20699 : : /* After a reading operation, make sure things are still ok. If not,
20700 : : emit an error and clean up. */
20701 : :
20702 : : bool
20703 : 6091 : module_state::check_read (bool outermost, bool ok)
20704 : : {
20705 : 6091 : gcc_checking_assert (!outermost || slurp->current == ~0u);
20706 : :
20707 : 6091 : if (!ok)
20708 : 13 : from ()->set_error ();
20709 : :
20710 : 6091 : if (int e = from ()->get_error ())
20711 : : {
20712 : 37 : auto_diagnostic_group d;
20713 : 37 : error_at (loc, "failed to read compiled module: %s",
20714 : 37 : from ()->get_error (filename));
20715 : 37 : note_cmi_name ();
20716 : :
20717 : 37 : if (e == EMFILE
20718 : 37 : || e == ENFILE
20719 : : #if MAPPED_READING
20720 : 37 : || e == ENOMEM
20721 : : #endif
20722 : : || false)
20723 : 0 : inform (loc, "consider using %<-fno-module-lazy%>,"
20724 : : " increasing %<-param-lazy-modules=%u%> value,"
20725 : : " or increasing the per-process file descriptor limit",
20726 : : param_lazy_modules);
20727 : 37 : else if (e == ENOENT)
20728 : 18 : inform (loc, "imports must be built before being imported");
20729 : :
20730 : 37 : if (outermost)
20731 : 34 : fatal_error (loc, "returning to the gate for a mechanical issue");
20732 : :
20733 : 3 : ok = false;
20734 : 3 : }
20735 : :
20736 : 6057 : maybe_completed_reading ();
20737 : :
20738 : 6057 : return ok;
20739 : : }
20740 : :
20741 : : /* Return the IDENTIFIER_NODE naming module IX. This is the name
20742 : : including dots. */
20743 : :
20744 : : char const *
20745 : 270 : module_name (unsigned ix, bool header_ok)
20746 : : {
20747 : 270 : if (modules)
20748 : : {
20749 : 270 : module_state *imp = (*modules)[ix];
20750 : :
20751 : 270 : if (ix && !imp->name)
20752 : 0 : imp = imp->parent;
20753 : :
20754 : 270 : if (header_ok || !imp->is_header ())
20755 : 270 : return imp->get_flatname ();
20756 : : }
20757 : :
20758 : : return NULL;
20759 : : }
20760 : :
20761 : : /* Return the bitmap describing what modules are imported. Remember,
20762 : : we always import ourselves. */
20763 : :
20764 : : bitmap
20765 : 70995 : get_import_bitmap ()
20766 : : {
20767 : 70995 : return (*modules)[0]->imports;
20768 : : }
20769 : :
20770 : : /* Return the visible imports and path of instantiation for an
20771 : : instantiation at TINST. If TINST is nullptr, we're not in an
20772 : : instantiation, and thus will return the visible imports of the
20773 : : current TU (and NULL *PATH_MAP_P). We cache the information on
20774 : : the tinst level itself. */
20775 : :
20776 : : static bitmap
20777 : 91792 : path_of_instantiation (tinst_level *tinst, bitmap *path_map_p)
20778 : : {
20779 : 91792 : gcc_checking_assert (modules_p ());
20780 : :
20781 : 91792 : if (!tinst)
20782 : : {
20783 : : /* Not inside an instantiation, just the regular case. */
20784 : 43472 : *path_map_p = nullptr;
20785 : 43472 : return get_import_bitmap ();
20786 : : }
20787 : :
20788 : 48320 : if (!tinst->path)
20789 : : {
20790 : : /* Calculate. */
20791 : 13591 : bitmap visible = path_of_instantiation (tinst->next, path_map_p);
20792 : 13591 : bitmap path_map = *path_map_p;
20793 : :
20794 : 13591 : if (!path_map)
20795 : : {
20796 : 2006 : path_map = BITMAP_GGC_ALLOC ();
20797 : 2006 : bitmap_set_bit (path_map, 0);
20798 : : }
20799 : :
20800 : 13591 : tree decl = tinst->tldcl;
20801 : 13591 : if (TREE_CODE (decl) == TREE_LIST)
20802 : 0 : decl = TREE_PURPOSE (decl);
20803 : 13591 : if (TYPE_P (decl))
20804 : 1627 : decl = TYPE_NAME (decl);
20805 : :
20806 : 13591 : if (unsigned mod = get_originating_module (decl))
20807 : 1692 : if (!bitmap_bit_p (path_map, mod))
20808 : : {
20809 : : /* This is brand new information! */
20810 : 73 : bitmap new_path = BITMAP_GGC_ALLOC ();
20811 : 73 : bitmap_copy (new_path, path_map);
20812 : 73 : bitmap_set_bit (new_path, mod);
20813 : 73 : path_map = new_path;
20814 : :
20815 : 73 : bitmap imports = (*modules)[mod]->imports;
20816 : 73 : if (bitmap_intersect_compl_p (imports, visible))
20817 : : {
20818 : : /* IMPORTS contains additional modules to VISIBLE. */
20819 : 6 : bitmap new_visible = BITMAP_GGC_ALLOC ();
20820 : :
20821 : 6 : bitmap_ior (new_visible, visible, imports);
20822 : 6 : visible = new_visible;
20823 : : }
20824 : : }
20825 : :
20826 : 13591 : tinst->path = path_map;
20827 : 13591 : tinst->visible = visible;
20828 : : }
20829 : :
20830 : 48320 : *path_map_p = tinst->path;
20831 : 48320 : return tinst->visible;
20832 : : }
20833 : :
20834 : : /* Return the bitmap describing what modules are visible along the
20835 : : path of instantiation. If we're not an instantiation, this will be
20836 : : the visible imports of the TU. *PATH_MAP_P is filled in with the
20837 : : modules owning the instantiation path -- we see the module-linkage
20838 : : entities of those modules. */
20839 : :
20840 : : bitmap
20841 : 20545587 : visible_instantiation_path (bitmap *path_map_p)
20842 : : {
20843 : 20545587 : if (!modules_p ())
20844 : : return NULL;
20845 : :
20846 : 78201 : return path_of_instantiation (current_instantiation (), path_map_p);
20847 : : }
20848 : :
20849 : : /* We've just directly imported IMPORT. Update our import/export
20850 : : bitmaps. IS_EXPORT is true if we're reexporting the OTHER. */
20851 : :
20852 : : void
20853 : 2692 : module_state::set_import (module_state const *import, bool is_export)
20854 : : {
20855 : 2692 : gcc_checking_assert (this != import);
20856 : :
20857 : : /* We see IMPORT's exports (which includes IMPORT). If IMPORT is
20858 : : the primary interface or a partition we'll see its imports. */
20859 : 2692 : bitmap_ior_into (imports, import->is_module () || import->is_partition ()
20860 : : ? import->imports : import->exports);
20861 : :
20862 : 2692 : if (is_export)
20863 : : /* We'll export OTHER's exports. */
20864 : 392 : bitmap_ior_into (exports, import->exports);
20865 : 2692 : }
20866 : :
20867 : : /* Return the declaring entity of DECL. That is the decl determining
20868 : : how to decorate DECL with module information. Returns NULL_TREE if
20869 : : it's the global module. */
20870 : :
20871 : : tree
20872 : 159728111 : get_originating_module_decl (tree decl)
20873 : : {
20874 : : /* An enumeration constant. */
20875 : 159728111 : if (TREE_CODE (decl) == CONST_DECL
20876 : 4846 : && DECL_CONTEXT (decl)
20877 : 159732957 : && (TREE_CODE (DECL_CONTEXT (decl)) == ENUMERAL_TYPE))
20878 : 4327 : decl = TYPE_NAME (DECL_CONTEXT (decl));
20879 : 159723784 : else if (TREE_CODE (decl) == FIELD_DECL
20880 : 159715347 : || TREE_CODE (decl) == USING_DECL
20881 : 319431703 : || CONST_DECL_USING_P (decl))
20882 : : {
20883 : 16384 : decl = DECL_CONTEXT (decl);
20884 : 16384 : if (TREE_CODE (decl) != FUNCTION_DECL)
20885 : 16384 : decl = TYPE_NAME (decl);
20886 : : }
20887 : :
20888 : 159728111 : gcc_checking_assert (TREE_CODE (decl) == TEMPLATE_DECL
20889 : : || TREE_CODE (decl) == FUNCTION_DECL
20890 : : || TREE_CODE (decl) == TYPE_DECL
20891 : : || TREE_CODE (decl) == VAR_DECL
20892 : : || TREE_CODE (decl) == CONCEPT_DECL
20893 : : || TREE_CODE (decl) == NAMESPACE_DECL);
20894 : :
20895 : 160766349 : for (;;)
20896 : : {
20897 : : /* Uninstantiated template friends are owned by the befriending
20898 : : class -- not their context. */
20899 : 160247230 : if (TREE_CODE (decl) == TEMPLATE_DECL
20900 : 160247230 : && DECL_UNINSTANTIATED_TEMPLATE_FRIEND_P (decl))
20901 : 5852 : decl = TYPE_NAME (DECL_CHAIN (decl));
20902 : :
20903 : : /* An imported temploid friend is attached to the same module the
20904 : : befriending class was. */
20905 : 160247230 : if (imported_temploid_friends)
20906 : 1958636 : if (tree *slot = imported_temploid_friends->get (decl))
20907 : 351 : decl = *slot;
20908 : :
20909 : 160247230 : int use;
20910 : 160247230 : if (tree ti = node_template_info (decl, use))
20911 : : {
20912 : 424973 : decl = TI_TEMPLATE (ti);
20913 : 424973 : if (TREE_CODE (decl) != TEMPLATE_DECL)
20914 : : {
20915 : : /* A friend template specialization. */
20916 : 7 : gcc_checking_assert (OVL_P (decl));
20917 : 7 : return global_namespace;
20918 : : }
20919 : : }
20920 : : else
20921 : : {
20922 : 159822257 : tree ctx = CP_DECL_CONTEXT (decl);
20923 : 159822257 : if (TREE_CODE (ctx) == NAMESPACE_DECL)
20924 : : break;
20925 : :
20926 : 94153 : if (TYPE_P (ctx))
20927 : : {
20928 : 88086 : ctx = TYPE_NAME (ctx);
20929 : 88086 : if (!ctx)
20930 : : {
20931 : : /* Some kind of internal type. */
20932 : 0 : gcc_checking_assert (DECL_ARTIFICIAL (decl));
20933 : 0 : return global_namespace;
20934 : : }
20935 : : }
20936 : 94153 : decl = ctx;
20937 : : }
20938 : 519119 : }
20939 : :
20940 : 159728104 : return decl;
20941 : : }
20942 : :
20943 : : /* If DECL is imported, return which module imported it, or 0 for the current
20944 : : module. Except that if GLOBAL_M1, return -1 for decls attached to the
20945 : : global module. */
20946 : :
20947 : : int
20948 : 687150 : get_originating_module (tree decl, bool global_m1)
20949 : : {
20950 : 687150 : tree owner = get_originating_module_decl (decl);
20951 : 687150 : tree not_tmpl = STRIP_TEMPLATE (owner);
20952 : :
20953 : 687150 : if (!DECL_LANG_SPECIFIC (not_tmpl))
20954 : 164687 : return global_m1 ? -1 : 0;
20955 : :
20956 : 1012930 : if (global_m1 && !DECL_MODULE_ATTACH_P (not_tmpl))
20957 : : return -1;
20958 : :
20959 : 85296 : int mod = !DECL_MODULE_IMPORT_P (not_tmpl) ? 0 : get_importing_module (owner);
20960 : 85296 : gcc_checking_assert (!global_m1 || !(*modules)[mod]->is_header ());
20961 : : return mod;
20962 : : }
20963 : :
20964 : : /* DECL is imported, return which module imported it.
20965 : : If FLEXIBLE, return -1 if not found, otherwise checking ICE. */
20966 : :
20967 : : unsigned
20968 : 15200 : get_importing_module (tree decl, bool flexible)
20969 : : {
20970 : 15200 : unsigned index = import_entity_index (decl, flexible);
20971 : 15200 : if (index == ~(~0u >> 1))
20972 : : return -1;
20973 : 15200 : module_state *module = import_entity_module (index);
20974 : :
20975 : 15200 : return module->mod;
20976 : : }
20977 : :
20978 : : /* Is it permissible to redeclare OLDDECL with NEWDECL.
20979 : :
20980 : : If NEWDECL is NULL, assumes that OLDDECL will be redeclared using
20981 : : the current scope's module and attachment. */
20982 : :
20983 : : bool
20984 : 138385 : module_may_redeclare (tree olddecl, tree newdecl)
20985 : : {
20986 : 138385 : tree decl = olddecl;
20987 : 154103 : for (;;)
20988 : : {
20989 : 146244 : tree ctx = CP_DECL_CONTEXT (decl);
20990 : 146244 : if (TREE_CODE (ctx) == NAMESPACE_DECL)
20991 : : // Found the namespace-scope decl.
20992 : : break;
20993 : 14803 : if (!CLASS_TYPE_P (ctx))
20994 : : // We've met a non-class scope. Such a thing is not
20995 : : // reopenable, so we must be ok.
20996 : : return true;
20997 : 7859 : decl = TYPE_NAME (ctx);
20998 : 7859 : }
20999 : :
21000 : 131441 : int use_tpl = 0;
21001 : 131441 : if (node_template_info (STRIP_TEMPLATE (decl), use_tpl) && use_tpl)
21002 : : // Specializations of any kind can be redeclared anywhere.
21003 : : // FIXME: Should we be checking this in more places on the scope chain?
21004 : : return true;
21005 : :
21006 : 87280 : module_state *old_mod = (*modules)[0];
21007 : 87280 : module_state *new_mod = old_mod;
21008 : :
21009 : 87280 : tree old_origin = get_originating_module_decl (decl);
21010 : 87280 : tree old_inner = STRIP_TEMPLATE (old_origin);
21011 : 87280 : bool olddecl_attached_p = (DECL_LANG_SPECIFIC (old_inner)
21012 : 139831 : && DECL_MODULE_ATTACH_P (old_inner));
21013 : 139831 : if (DECL_LANG_SPECIFIC (old_inner) && DECL_MODULE_IMPORT_P (old_inner))
21014 : : {
21015 : 396 : unsigned index = import_entity_index (old_origin);
21016 : 396 : old_mod = import_entity_module (index);
21017 : : }
21018 : :
21019 : 87280 : bool newdecl_attached_p = module_attach_p ();
21020 : 87280 : if (newdecl)
21021 : : {
21022 : 24014 : tree new_origin = get_originating_module_decl (newdecl);
21023 : 24014 : tree new_inner = STRIP_TEMPLATE (new_origin);
21024 : 24014 : newdecl_attached_p = (DECL_LANG_SPECIFIC (new_inner)
21025 : 46830 : && DECL_MODULE_ATTACH_P (new_inner));
21026 : 46830 : if (DECL_LANG_SPECIFIC (new_inner) && DECL_MODULE_IMPORT_P (new_inner))
21027 : : {
21028 : 116 : unsigned index = import_entity_index (new_origin);
21029 : 116 : new_mod = import_entity_module (index);
21030 : : }
21031 : : }
21032 : :
21033 : : /* Module attachment needs to match. */
21034 : 87280 : if (olddecl_attached_p == newdecl_attached_p)
21035 : : {
21036 : 87238 : if (!olddecl_attached_p)
21037 : : /* Both are GM entities, OK. */
21038 : : return true;
21039 : :
21040 : 1098 : if (new_mod == old_mod
21041 : 1344 : || (new_mod && get_primary (new_mod) == get_primary (old_mod)))
21042 : : /* Both attached to same named module, OK. */
21043 : : return true;
21044 : : }
21045 : :
21046 : : /* Attached to different modules, error. */
21047 : 45 : decl = newdecl ? newdecl : olddecl;
21048 : 45 : location_t loc = newdecl ? DECL_SOURCE_LOCATION (newdecl) : input_location;
21049 : 45 : if (DECL_IS_UNDECLARED_BUILTIN (olddecl))
21050 : : {
21051 : 3 : if (newdecl_attached_p)
21052 : 3 : error_at (loc, "declaring %qD in module %qs conflicts with builtin "
21053 : : "in global module", decl, new_mod->get_flatname ());
21054 : : else
21055 : 0 : error_at (loc, "declaration %qD conflicts with builtin", decl);
21056 : : }
21057 : 81 : else if (DECL_LANG_SPECIFIC (old_inner) && DECL_MODULE_IMPORT_P (old_inner))
21058 : : {
21059 : 27 : auto_diagnostic_group d;
21060 : 27 : if (newdecl_attached_p)
21061 : 3 : error_at (loc, "redeclaring %qD in module %qs conflicts with import",
21062 : : decl, new_mod->get_flatname ());
21063 : : else
21064 : 24 : error_at (loc, "redeclaring %qD in global module conflicts with import",
21065 : : decl);
21066 : :
21067 : 27 : if (olddecl_attached_p)
21068 : 27 : inform (DECL_SOURCE_LOCATION (olddecl),
21069 : : "import declared attached to module %qs",
21070 : : old_mod->get_flatname ());
21071 : : else
21072 : 0 : inform (DECL_SOURCE_LOCATION (olddecl),
21073 : : "import declared in global module");
21074 : 27 : }
21075 : : else
21076 : : {
21077 : 15 : auto_diagnostic_group d;
21078 : 15 : if (newdecl_attached_p)
21079 : 15 : error_at (loc, "conflicting declaration of %qD in module %qs",
21080 : : decl, new_mod->get_flatname ());
21081 : : else
21082 : 0 : error_at (loc, "conflicting declaration of %qD in global module",
21083 : : decl);
21084 : :
21085 : 15 : if (olddecl_attached_p)
21086 : 0 : inform (DECL_SOURCE_LOCATION (olddecl),
21087 : : "previously declared in module %qs",
21088 : : old_mod->get_flatname ());
21089 : : else
21090 : 15 : inform (DECL_SOURCE_LOCATION (olddecl),
21091 : : "previously declared in global module");
21092 : 15 : }
21093 : : return false;
21094 : : }
21095 : :
21096 : : /* DECL is being created by this TU. Record it came from here. We
21097 : : record module purview, so we can see if partial or explicit
21098 : : specialization needs to be written out, even though its purviewness
21099 : : comes from the most general template. */
21100 : :
21101 : : void
21102 : 759849591 : set_instantiating_module (tree decl)
21103 : : {
21104 : 759849591 : gcc_assert (TREE_CODE (decl) == FUNCTION_DECL
21105 : : || VAR_P (decl)
21106 : : || TREE_CODE (decl) == TYPE_DECL
21107 : : || TREE_CODE (decl) == CONCEPT_DECL
21108 : : || TREE_CODE (decl) == TEMPLATE_DECL
21109 : : || TREE_CODE (decl) == CONST_DECL
21110 : : || (TREE_CODE (decl) == NAMESPACE_DECL
21111 : : && DECL_NAMESPACE_ALIAS (decl)));
21112 : :
21113 : 759849591 : if (!modules_p ())
21114 : : return;
21115 : :
21116 : 2402694 : decl = STRIP_TEMPLATE (decl);
21117 : :
21118 : 2402694 : if (!DECL_LANG_SPECIFIC (decl) && module_purview_p ())
21119 : 305034 : retrofit_lang_decl (decl);
21120 : :
21121 : 2402694 : if (DECL_LANG_SPECIFIC (decl))
21122 : : {
21123 : 1902919 : DECL_MODULE_PURVIEW_P (decl) = module_purview_p ();
21124 : : /* If this was imported, we'll still be in the entity_hash. */
21125 : 1902919 : DECL_MODULE_IMPORT_P (decl) = false;
21126 : : }
21127 : : }
21128 : :
21129 : : /* If DECL is a class member, whose class is not defined in this TU
21130 : : (it was imported), remember this decl. */
21131 : :
21132 : : void
21133 : 118700 : set_defining_module (tree decl)
21134 : : {
21135 : 118700 : gcc_checking_assert (!DECL_LANG_SPECIFIC (decl)
21136 : : || !DECL_MODULE_IMPORT_P (decl));
21137 : :
21138 : 118700 : if (module_maybe_has_cmi_p ())
21139 : : {
21140 : : /* We need to track all declarations within a module, not just those
21141 : : in the module purview, because we don't necessarily know yet if
21142 : : this module will require a CMI while in the global fragment. */
21143 : 79781 : tree ctx = DECL_CONTEXT (decl);
21144 : 79781 : if (ctx
21145 : 79781 : && (TREE_CODE (ctx) == RECORD_TYPE || TREE_CODE (ctx) == UNION_TYPE)
21146 : 13558 : && DECL_LANG_SPECIFIC (TYPE_NAME (ctx))
21147 : 89929 : && DECL_MODULE_IMPORT_P (TYPE_NAME (ctx)))
21148 : : {
21149 : : /* This entity's context is from an import. We may need to
21150 : : record this entity to make sure we emit it in the CMI.
21151 : : Template specializations are in the template hash tables,
21152 : : so we don't need to record them here as well. */
21153 : 12 : int use_tpl = -1;
21154 : 12 : tree ti = node_template_info (decl, use_tpl);
21155 : 12 : if (use_tpl <= 0)
21156 : : {
21157 : 12 : if (ti)
21158 : : {
21159 : 3 : gcc_checking_assert (!use_tpl);
21160 : : /* Get to the TEMPLATE_DECL. */
21161 : 3 : decl = TI_TEMPLATE (ti);
21162 : : }
21163 : :
21164 : : /* Record it on the class_members list. */
21165 : 12 : vec_safe_push (class_members, decl);
21166 : : }
21167 : : }
21168 : : }
21169 : 118700 : }
21170 : :
21171 : : /* Also remember DECL if it's a newly declared class template partial
21172 : : specialization, because these are not necessarily added to the
21173 : : instantiation tables. */
21174 : :
21175 : : void
21176 : 7072492 : set_defining_module_for_partial_spec (tree decl)
21177 : : {
21178 : 7072492 : if (module_maybe_has_cmi_p ()
21179 : 19294 : && DECL_IMPLICIT_TYPEDEF_P (decl)
21180 : 7089389 : && CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (decl)))
21181 : 16897 : vec_safe_push (partial_specializations, decl);
21182 : 7072492 : }
21183 : :
21184 : : void
21185 : 266897829 : set_originating_module (tree decl, bool friend_p ATTRIBUTE_UNUSED)
21186 : : {
21187 : 266897829 : set_instantiating_module (decl);
21188 : :
21189 : 266897829 : if (!DECL_NAMESPACE_SCOPE_P (decl))
21190 : : return;
21191 : :
21192 : 162836583 : gcc_checking_assert (friend_p || decl == get_originating_module_decl (decl));
21193 : :
21194 : 162836583 : if (module_attach_p ())
21195 : : {
21196 : 3805 : retrofit_lang_decl (decl);
21197 : 3805 : DECL_MODULE_ATTACH_P (decl) = true;
21198 : : }
21199 : :
21200 : : /* It is ill-formed to export a declaration with internal linkage. However,
21201 : : at the point this function is called we don't yet always know whether this
21202 : : declaration has internal linkage; instead we defer this check for callers
21203 : : to do once visibility has been determined. */
21204 : 162836583 : if (module_exporting_p ())
21205 : 195234 : DECL_MODULE_EXPORT_P (decl) = true;
21206 : : }
21207 : :
21208 : : /* Checks whether DECL within a module unit has valid linkage for its kind.
21209 : : Must be called after visibility for DECL has been finalised. */
21210 : :
21211 : : void
21212 : 329371159 : check_module_decl_linkage (tree decl)
21213 : : {
21214 : 329371159 : if (!module_has_cmi_p ())
21215 : : return;
21216 : :
21217 : : /* A header unit shall not contain a definition of a non-inline function
21218 : : or variable (not template) whose name has external linkage. */
21219 : 453922 : if (header_module_p ()
21220 : 406307 : && !processing_template_decl
21221 : 135313 : && ((TREE_CODE (decl) == FUNCTION_DECL
21222 : 79171 : && !DECL_DECLARED_INLINE_P (decl))
21223 : 104674 : || (TREE_CODE (decl) == VAR_DECL
21224 : 31348 : && !DECL_INLINE_VAR_P (decl)))
21225 : 38713 : && decl_defined_p (decl)
21226 : 3761 : && !(DECL_LANG_SPECIFIC (decl)
21227 : 3761 : && DECL_TEMPLATE_INSTANTIATION (decl))
21228 : 457683 : && decl_linkage (decl) == lk_external)
21229 : 60 : error_at (DECL_SOURCE_LOCATION (decl),
21230 : : "external linkage definition of %qD in header module must "
21231 : : "be declared %<inline%>", decl);
21232 : :
21233 : : /* An internal-linkage declaration cannot be generally be exported.
21234 : : But it's OK to export any declaration from a header unit, including
21235 : : internal linkage declarations. */
21236 : 453922 : if (!header_module_p () && DECL_MODULE_EXPORT_P (decl))
21237 : : {
21238 : : /* Let's additionally treat any exported declaration within an
21239 : : internal namespace as exporting a declaration with internal
21240 : : linkage, as this would also implicitly export the internal
21241 : : linkage namespace. */
21242 : 1771 : if (decl_internal_context_p (decl))
21243 : : {
21244 : 50 : error_at (DECL_SOURCE_LOCATION (decl),
21245 : : "exporting declaration %qD declared in unnamed namespace",
21246 : : decl);
21247 : 50 : DECL_MODULE_EXPORT_P (decl) = false;
21248 : : }
21249 : 1721 : else if (decl_linkage (decl) == lk_internal)
21250 : : {
21251 : 17 : error_at (DECL_SOURCE_LOCATION (decl),
21252 : : "exporting declaration %qD with internal linkage", decl);
21253 : 17 : DECL_MODULE_EXPORT_P (decl) = false;
21254 : : }
21255 : : }
21256 : : }
21257 : :
21258 : : /* DECL is keyed to CTX for odr purposes. */
21259 : :
21260 : : void
21261 : 902390 : maybe_key_decl (tree ctx, tree decl)
21262 : : {
21263 : 902390 : if (!modules_p ())
21264 : : return;
21265 : :
21266 : : /* We only need to deal with lambdas attached to var, field,
21267 : : parm, type, or concept decls. */
21268 : 6280 : if (TREE_CODE (ctx) != VAR_DECL
21269 : 6280 : && TREE_CODE (ctx) != FIELD_DECL
21270 : 6037 : && TREE_CODE (ctx) != PARM_DECL
21271 : 6025 : && TREE_CODE (ctx) != TYPE_DECL
21272 : 5997 : && TREE_CODE (ctx) != CONCEPT_DECL)
21273 : : return;
21274 : :
21275 : : /* For fields, key it to the containing type to handle deduplication
21276 : : correctly. */
21277 : 320 : if (TREE_CODE (ctx) == FIELD_DECL)
21278 : 6 : ctx = TYPE_NAME (DECL_CONTEXT (ctx));
21279 : :
21280 : 320 : if (!keyed_table)
21281 : 76 : keyed_table = new keyed_map_t (EXPERIMENT (1, 400));
21282 : :
21283 : 320 : auto &vec = keyed_table->get_or_insert (ctx);
21284 : 320 : if (!vec.length ())
21285 : : {
21286 : 306 : retrofit_lang_decl (ctx);
21287 : 306 : DECL_MODULE_KEYED_DECLS_P (ctx) = true;
21288 : : }
21289 : 320 : vec.safe_push (decl);
21290 : : }
21291 : :
21292 : : /* DECL is an instantiated friend that should be attached to the same
21293 : : module that ORIG is. */
21294 : :
21295 : : void
21296 : 2746226 : propagate_defining_module (tree decl, tree orig)
21297 : : {
21298 : 2746226 : if (!modules_p ())
21299 : : return;
21300 : :
21301 : 4691 : tree not_tmpl = STRIP_TEMPLATE (orig);
21302 : 9361 : if (DECL_LANG_SPECIFIC (not_tmpl) && DECL_MODULE_ATTACH_P (not_tmpl))
21303 : : {
21304 : 99 : tree inner = STRIP_TEMPLATE (decl);
21305 : 99 : retrofit_lang_decl (inner);
21306 : 99 : DECL_MODULE_ATTACH_P (inner) = true;
21307 : : }
21308 : :
21309 : 9361 : if (DECL_LANG_SPECIFIC (not_tmpl) && DECL_MODULE_IMPORT_P (not_tmpl))
21310 : : {
21311 : 217 : bool exists = imported_temploid_friends->put (decl, orig);
21312 : :
21313 : : /* We should only be called if lookup for an existing decl
21314 : : failed, in which case there shouldn't already be an entry
21315 : : in the map. */
21316 : 217 : gcc_assert (!exists);
21317 : : }
21318 : : }
21319 : :
21320 : : /* DECL is being freed, clear data we don't need anymore. */
21321 : :
21322 : : void
21323 : 28483 : remove_defining_module (tree decl)
21324 : : {
21325 : 28483 : if (!modules_p ())
21326 : : return;
21327 : :
21328 : 28483 : if (imported_temploid_friends)
21329 : 28483 : imported_temploid_friends->remove (decl);
21330 : : }
21331 : :
21332 : : /* Create the flat name string. It is simplest to have it handy. */
21333 : :
21334 : : void
21335 : 5570 : module_state::set_flatname ()
21336 : : {
21337 : 5570 : gcc_checking_assert (!flatname);
21338 : 5570 : if (parent)
21339 : : {
21340 : 584 : auto_vec<tree,5> ids;
21341 : 584 : size_t len = 0;
21342 : 584 : char const *primary = NULL;
21343 : 584 : size_t pfx_len = 0;
21344 : :
21345 : 584 : for (module_state *probe = this;
21346 : 1461 : probe;
21347 : 877 : probe = probe->parent)
21348 : 1300 : if (is_partition () && !probe->is_partition ())
21349 : : {
21350 : 423 : primary = probe->get_flatname ();
21351 : 423 : pfx_len = strlen (primary);
21352 : 423 : break;
21353 : : }
21354 : : else
21355 : : {
21356 : 877 : ids.safe_push (probe->name);
21357 : 877 : len += IDENTIFIER_LENGTH (probe->name) + 1;
21358 : : }
21359 : :
21360 : 584 : char *flat = XNEWVEC (char, pfx_len + len + is_partition ());
21361 : 584 : flatname = flat;
21362 : :
21363 : 584 : if (primary)
21364 : : {
21365 : 423 : memcpy (flat, primary, pfx_len);
21366 : 423 : flat += pfx_len;
21367 : 423 : *flat++ = ':';
21368 : : }
21369 : :
21370 : 1461 : for (unsigned len = 0; ids.length ();)
21371 : : {
21372 : 877 : if (len)
21373 : 293 : flat[len++] = '.';
21374 : 877 : tree elt = ids.pop ();
21375 : 877 : unsigned l = IDENTIFIER_LENGTH (elt);
21376 : 877 : memcpy (flat + len, IDENTIFIER_POINTER (elt), l + 1);
21377 : 877 : len += l;
21378 : : }
21379 : 584 : }
21380 : 4986 : else if (is_header ())
21381 : 1822 : flatname = TREE_STRING_POINTER (name);
21382 : : else
21383 : 3164 : flatname = IDENTIFIER_POINTER (name);
21384 : 5570 : }
21385 : :
21386 : : /* Read the CMI file for a module. */
21387 : :
21388 : : bool
21389 : 2642 : module_state::do_import (cpp_reader *reader, bool outermost)
21390 : : {
21391 : 2642 : gcc_assert (global_namespace == current_scope () && loadedness == ML_NONE);
21392 : :
21393 : 2642 : loc = linemap_module_loc (line_table, loc, get_flatname ());
21394 : :
21395 : 2642 : if (lazy_open >= lazy_limit)
21396 : 9 : freeze_an_elf ();
21397 : :
21398 : 2642 : int fd = -1;
21399 : 2642 : int e = ENOENT;
21400 : 2642 : if (filename)
21401 : : {
21402 : 2642 : const char *file = maybe_add_cmi_prefix (filename);
21403 : 3114 : dump () && dump ("CMI is %s", file);
21404 : 2642 : if (note_module_cmi_yes || inform_cmi_p)
21405 : 12 : inform (loc, "reading CMI %qs", file);
21406 : : /* Add the CMI file to the dependency tracking. */
21407 : 2642 : if (cpp_get_deps (reader))
21408 : 12 : deps_add_dep (cpp_get_deps (reader), file);
21409 : 2642 : fd = open (file, O_RDONLY | O_CLOEXEC | O_BINARY);
21410 : 2642 : e = errno;
21411 : : }
21412 : :
21413 : 2642 : gcc_checking_assert (!slurp);
21414 : 5266 : slurp = new slurping (new elf_in (fd, e));
21415 : :
21416 : 2642 : bool ok = true;
21417 : 2642 : if (!from ()->get_error ())
21418 : : {
21419 : 2624 : announce ("importing");
21420 : 2624 : loadedness = ML_CONFIG;
21421 : 2624 : lazy_open++;
21422 : 2624 : ok = read_initial (reader);
21423 : 2624 : slurp->lru = ++lazy_lru;
21424 : : }
21425 : :
21426 : 2642 : gcc_assert (slurp->current == ~0u);
21427 : :
21428 : 2642 : return check_read (outermost, ok);
21429 : : }
21430 : :
21431 : : /* Attempt to increase the file descriptor limit. */
21432 : :
21433 : : static bool
21434 : 4324 : try_increase_lazy (unsigned want)
21435 : : {
21436 : 4324 : gcc_checking_assert (lazy_open >= lazy_limit);
21437 : :
21438 : : /* If we're increasing, saturate at hard limit. */
21439 : 4324 : if (want > lazy_hard_limit && lazy_limit < lazy_hard_limit)
21440 : 4324 : want = lazy_hard_limit;
21441 : :
21442 : : #if HAVE_SETRLIMIT
21443 : 4324 : if ((!lazy_limit || !param_lazy_modules)
21444 : 4312 : && lazy_hard_limit
21445 : 4312 : && want <= lazy_hard_limit)
21446 : : {
21447 : 4312 : struct rlimit rlimit;
21448 : 4312 : rlimit.rlim_cur = want + LAZY_HEADROOM;
21449 : 4312 : rlimit.rlim_max = lazy_hard_limit + LAZY_HEADROOM;
21450 : 4312 : if (!setrlimit (RLIMIT_NOFILE, &rlimit))
21451 : 4312 : lazy_limit = want;
21452 : : }
21453 : : #endif
21454 : :
21455 : 4324 : return lazy_open < lazy_limit;
21456 : : }
21457 : :
21458 : : /* Pick a victim module to freeze its reader. */
21459 : :
21460 : : void
21461 : 12 : module_state::freeze_an_elf ()
21462 : : {
21463 : 12 : if (try_increase_lazy (lazy_open * 2))
21464 : : return;
21465 : :
21466 : 12 : module_state *victim = NULL;
21467 : 12 : for (unsigned ix = modules->length (); ix--;)
21468 : : {
21469 : 30 : module_state *candidate = (*modules)[ix];
21470 : 30 : if (candidate && candidate->slurp && candidate->slurp->lru
21471 : 60 : && candidate->from ()->is_freezable ()
21472 : 39 : && (!victim || victim->slurp->lru > candidate->slurp->lru))
21473 : : victim = candidate;
21474 : : }
21475 : :
21476 : 12 : if (victim)
21477 : : {
21478 : 18 : dump () && dump ("Freezing '%s'", victim->filename);
21479 : 9 : if (victim->slurp->macro_defs.size)
21480 : : /* Save the macro definitions to a buffer. */
21481 : 0 : victim->from ()->preserve (victim->slurp->macro_defs);
21482 : 9 : if (victim->slurp->macro_tbl.size)
21483 : : /* Save the macro definitions to a buffer. */
21484 : 0 : victim->from ()->preserve (victim->slurp->macro_tbl);
21485 : 9 : victim->from ()->freeze ();
21486 : 9 : lazy_open--;
21487 : : }
21488 : : else
21489 : 3 : dump () && dump ("No module available for freezing");
21490 : : }
21491 : :
21492 : : /* Load the lazy slot *MSLOT, INDEX'th slot of the module. */
21493 : :
21494 : : bool
21495 : 26964 : module_state::lazy_load (unsigned index, binding_slot *mslot)
21496 : : {
21497 : 26964 : unsigned n = dump.push (this);
21498 : :
21499 : 26964 : gcc_checking_assert (function_depth);
21500 : :
21501 : 26964 : unsigned cookie = mslot->get_lazy ();
21502 : 26964 : unsigned snum = cookie >> 2;
21503 : 27272 : dump () && dump ("Loading entity %M[%u] section:%u", this, index, snum);
21504 : :
21505 : 26964 : bool ok = load_section (snum, mslot);
21506 : :
21507 : 26964 : dump.pop (n);
21508 : :
21509 : 26964 : return ok;
21510 : : }
21511 : :
21512 : : /* Load MOD's binding for NS::ID into *MSLOT. *MSLOT contains the
21513 : : lazy cookie. OUTER is true if this is the outermost lazy, (used
21514 : : for diagnostics). */
21515 : :
21516 : : void
21517 : 2535 : lazy_load_binding (unsigned mod, tree ns, tree id, binding_slot *mslot)
21518 : : {
21519 : 2535 : int count = errorcount + warningcount;
21520 : :
21521 : 2535 : bool timer_running = timevar_cond_start (TV_MODULE_IMPORT);
21522 : :
21523 : : /* Make sure lazy loading from a template context behaves as if
21524 : : from a non-template context. */
21525 : 2535 : processing_template_decl_sentinel ptds;
21526 : :
21527 : : /* Stop GC happening, even in outermost loads (because our caller
21528 : : could well be building up a lookup set). */
21529 : 2535 : function_depth++;
21530 : :
21531 : 2535 : gcc_checking_assert (mod);
21532 : 2535 : module_state *module = (*modules)[mod];
21533 : 2535 : unsigned n = dump.push (module);
21534 : :
21535 : 2535 : unsigned snum = mslot->get_lazy ();
21536 : 2833 : dump () && dump ("Lazily binding %P@%N section:%u", ns, id,
21537 : : module->name, snum);
21538 : :
21539 : 2535 : bool ok = !recursive_lazy (snum);
21540 : 2535 : if (ok)
21541 : : {
21542 : 2535 : ok = module->load_section (snum, mslot);
21543 : 2535 : lazy_snum = 0;
21544 : 2535 : post_load_processing ();
21545 : : }
21546 : :
21547 : 2535 : dump.pop (n);
21548 : :
21549 : 2535 : function_depth--;
21550 : :
21551 : 2535 : timevar_cond_stop (TV_MODULE_IMPORT, timer_running);
21552 : :
21553 : 2535 : if (!ok)
21554 : 0 : fatal_error (input_location,
21555 : 0 : module->is_header ()
21556 : : ? G_("failed to load binding %<%E%s%E%>")
21557 : : : G_("failed to load binding %<%E%s%E@%s%>"),
21558 : 0 : ns, &"::"[ns == global_namespace ? 2 : 0], id,
21559 : : module->get_flatname ());
21560 : :
21561 : 2535 : if (count != errorcount + warningcount)
21562 : 21 : inform (input_location,
21563 : 21 : module->is_header ()
21564 : : ? G_("during load of binding %<%E%s%E%>")
21565 : : : G_("during load of binding %<%E%s%E@%s%>"),
21566 : 21 : ns, &"::"[ns == global_namespace ? 2 : 0], id,
21567 : : module->get_flatname ());
21568 : 2535 : }
21569 : :
21570 : : /* Load any pending entities keyed to the top-key of DECL. */
21571 : :
21572 : : void
21573 : 24327332 : lazy_load_pendings (tree decl)
21574 : : {
21575 : : /* Make sure lazy loading from a template context behaves as if
21576 : : from a non-template context. */
21577 : 24327332 : processing_template_decl_sentinel ptds;
21578 : :
21579 : 24327332 : tree key_decl;
21580 : 24327332 : pending_key key;
21581 : 24327332 : key.ns = find_pending_key (decl, &key_decl);
21582 : 24327332 : key.id = DECL_NAME (key_decl);
21583 : :
21584 : 24327332 : auto *pending_vec = pending_table ? pending_table->get (key) : nullptr;
21585 : 24327332 : if (!pending_vec)
21586 : 24322726 : return;
21587 : :
21588 : 4606 : int count = errorcount + warningcount;
21589 : :
21590 : 4606 : bool timer_running = timevar_cond_start (TV_MODULE_IMPORT);
21591 : 4606 : bool ok = !recursive_lazy ();
21592 : 4606 : if (ok)
21593 : : {
21594 : 4606 : function_depth++; /* Prevent GC */
21595 : 4606 : unsigned n = dump.push (NULL);
21596 : 5030 : dump () && dump ("Reading %u pending entities keyed to %P",
21597 : : pending_vec->length (), key.ns, key.id);
21598 : 4606 : for (unsigned ix = pending_vec->length (); ix--;)
21599 : : {
21600 : 50093 : unsigned index = (*pending_vec)[ix];
21601 : 50093 : binding_slot *slot = &(*entity_ary)[index];
21602 : :
21603 : 50093 : if (slot->is_lazy ())
21604 : : {
21605 : 2587 : module_state *import = import_entity_module (index);
21606 : 2587 : if (!import->lazy_load (index - import->entity_lwm, slot))
21607 : 50093 : ok = false;
21608 : : }
21609 : 102205 : else if (dump ())
21610 : : {
21611 : 314 : module_state *import = import_entity_module (index);
21612 : 314 : dump () && dump ("Entity %M[%u] already loaded",
21613 : 314 : import, index - import->entity_lwm);
21614 : : }
21615 : : }
21616 : :
21617 : 4606 : pending_table->remove (key);
21618 : 4606 : dump.pop (n);
21619 : 4606 : lazy_snum = 0;
21620 : 4606 : post_load_processing ();
21621 : 4606 : function_depth--;
21622 : : }
21623 : :
21624 : 4606 : timevar_cond_stop (TV_MODULE_IMPORT, timer_running);
21625 : :
21626 : 4606 : if (!ok)
21627 : 0 : fatal_error (input_location, "failed to load pendings for %<%E%s%E%>",
21628 : 0 : key.ns, &"::"[key.ns == global_namespace ? 2 : 0], key.id);
21629 : :
21630 : 4606 : if (count != errorcount + warningcount)
21631 : 0 : inform (input_location, "during load of pendings for %<%E%s%E%>",
21632 : 0 : key.ns, &"::"[key.ns == global_namespace ? 2 : 0], key.id);
21633 : 24327332 : }
21634 : :
21635 : : static void
21636 : 2411 : direct_import (module_state *import, cpp_reader *reader)
21637 : : {
21638 : 2411 : timevar_start (TV_MODULE_IMPORT);
21639 : 2411 : unsigned n = dump.push (import);
21640 : :
21641 : 2411 : gcc_checking_assert (import->is_direct () && import->has_location ());
21642 : 2411 : if (import->loadedness == ML_NONE)
21643 : 1518 : if (!import->do_import (reader, true))
21644 : 0 : gcc_unreachable ();
21645 : :
21646 : 2377 : if (import->loadedness < ML_LANGUAGE)
21647 : : {
21648 : 2301 : if (!keyed_table)
21649 : 2019 : keyed_table = new keyed_map_t (EXPERIMENT (1, 400));
21650 : 2301 : import->read_language (true);
21651 : : }
21652 : :
21653 : 2377 : (*modules)[0]->set_import (import, import->exported_p);
21654 : :
21655 : 2377 : dump.pop (n);
21656 : 2377 : timevar_stop (TV_MODULE_IMPORT);
21657 : 2377 : }
21658 : :
21659 : : /* Import module IMPORT. */
21660 : :
21661 : : void
21662 : 2204 : import_module (module_state *import, location_t from_loc, bool exporting_p,
21663 : : tree, cpp_reader *reader)
21664 : : {
21665 : 2204 : if (!import->check_not_purview (from_loc))
21666 : : return;
21667 : :
21668 : 2195 : if (!import->is_header () && current_lang_depth ())
21669 : : /* Only header units should appear inside language
21670 : : specifications. The std doesn't specify this, but I think
21671 : : that's an error in resolving US 033, because language linkage
21672 : : is also our escape clause to getting things into the global
21673 : : module, so we don't want to confuse things by having to think
21674 : : about whether 'extern "C++" { import foo; }' puts foo's
21675 : : contents into the global module all of a sudden. */
21676 : 6 : warning (0, "import of named module %qs inside language-linkage block",
21677 : : import->get_flatname ());
21678 : :
21679 : 2195 : if (exporting_p || module_exporting_p ())
21680 : 272 : import->exported_p = true;
21681 : :
21682 : 2195 : if (import->loadedness != ML_NONE)
21683 : : {
21684 : 890 : from_loc = ordinary_loc_of (line_table, from_loc);
21685 : 890 : linemap_module_reparent (line_table, import->loc, from_loc);
21686 : : }
21687 : 2195 : gcc_checking_assert (!import->module_p);
21688 : 2195 : gcc_checking_assert (import->is_direct () && import->has_location ());
21689 : :
21690 : 2195 : direct_import (import, reader);
21691 : : }
21692 : :
21693 : : /* Declare the name of the current module to be NAME. EXPORTING_p is
21694 : : true if this TU is the exporting module unit. */
21695 : :
21696 : : void
21697 : 2763 : declare_module (module_state *module, location_t from_loc, bool exporting_p,
21698 : : tree, cpp_reader *reader)
21699 : : {
21700 : 2763 : gcc_assert (global_namespace == current_scope ());
21701 : :
21702 : 2763 : module_state *current = (*modules)[0];
21703 : 2763 : if (module_purview_p () || module->loadedness > ML_CONFIG)
21704 : : {
21705 : 6 : auto_diagnostic_group d;
21706 : 12 : error_at (from_loc, module_purview_p ()
21707 : : ? G_("module already declared")
21708 : : : G_("module already imported"));
21709 : 6 : if (module_purview_p ())
21710 : 0 : module = current;
21711 : 12 : inform (module->loc, module_purview_p ()
21712 : : ? G_("module %qs declared here")
21713 : : : G_("module %qs imported here"),
21714 : : module->get_flatname ());
21715 : 6 : return;
21716 : 6 : }
21717 : :
21718 : 2757 : gcc_checking_assert (module->module_p);
21719 : 2757 : gcc_checking_assert (module->is_direct () && module->has_location ());
21720 : :
21721 : : /* Yer a module, 'arry. */
21722 : 2757 : module_kind = module->is_header () ? MK_HEADER : MK_NAMED | MK_ATTACH;
21723 : :
21724 : : // Even in header units, we consider the decls to be purview
21725 : 2757 : module_kind |= MK_PURVIEW;
21726 : :
21727 : 2757 : if (module->is_partition ())
21728 : 165 : module_kind |= MK_PARTITION;
21729 : 2757 : if (exporting_p)
21730 : : {
21731 : 2494 : module->interface_p = true;
21732 : 2494 : module_kind |= MK_INTERFACE;
21733 : : }
21734 : :
21735 : 2757 : if (module_has_cmi_p ())
21736 : : {
21737 : : /* Copy the importing information we may have already done. We
21738 : : do not need to separate out the imports that only happen in
21739 : : the GMF, inspite of what the literal wording of the std
21740 : : might imply. See p2191, the core list had a discussion
21741 : : where the module implementors agreed that the GMF of a named
21742 : : module is invisible to importers. */
21743 : 2541 : module->imports = current->imports;
21744 : :
21745 : 2541 : module->mod = 0;
21746 : 2541 : (*modules)[0] = module;
21747 : : }
21748 : : else
21749 : : {
21750 : 216 : module->interface_p = true;
21751 : 216 : current->parent = module; /* So mangler knows module identity. */
21752 : 216 : direct_import (module, reader);
21753 : : }
21754 : : }
21755 : :
21756 : : /* Return true IFF we must emit a module global initializer function
21757 : : (which will be called by importers' init code). */
21758 : :
21759 : : bool
21760 : 100549 : module_global_init_needed ()
21761 : : {
21762 : 100549 : return module_has_cmi_p () && !header_module_p ();
21763 : : }
21764 : :
21765 : : /* Calculate which, if any, import initializers need calling. */
21766 : :
21767 : : bool
21768 : 93654 : module_determine_import_inits ()
21769 : : {
21770 : 93654 : if (!modules || header_module_p ())
21771 : : return false;
21772 : :
21773 : : /* Prune active_init_p. We need the same bitmap allocation
21774 : : scheme as for the imports member. */
21775 : 3303 : function_depth++; /* Disable GC. */
21776 : 3303 : bitmap covered_imports (BITMAP_GGC_ALLOC ());
21777 : :
21778 : 3303 : bool any = false;
21779 : :
21780 : : /* Because indirect imports are before their direct import, and
21781 : : we're scanning the array backwards, we only need one pass! */
21782 : 5764 : for (unsigned ix = modules->length (); --ix;)
21783 : : {
21784 : 2461 : module_state *import = (*modules)[ix];
21785 : :
21786 : 2461 : if (!import->active_init_p)
21787 : : ;
21788 : 43 : else if (bitmap_bit_p (covered_imports, ix))
21789 : 6 : import->active_init_p = false;
21790 : : else
21791 : : {
21792 : : /* Everything this imports is therefore handled by its
21793 : : initializer, so doesn't need initializing by us. */
21794 : 37 : bitmap_ior_into (covered_imports, import->imports);
21795 : 37 : any = true;
21796 : : }
21797 : : }
21798 : 3303 : function_depth--;
21799 : :
21800 : 3303 : return any;
21801 : : }
21802 : :
21803 : : /* Emit calls to each direct import's global initializer. Including
21804 : : direct imports of directly imported header units. The initializers
21805 : : of (static) entities in header units will be called by their
21806 : : importing modules (for the instance contained within that), or by
21807 : : the current TU (for the instances we've brought in). Of course
21808 : : such header unit behaviour is evil, but iostream went through that
21809 : : door some time ago. */
21810 : :
21811 : : void
21812 : 37 : module_add_import_initializers ()
21813 : : {
21814 : 37 : if (!modules || header_module_p ())
21815 : 0 : return;
21816 : :
21817 : 37 : tree fntype = build_function_type (void_type_node, void_list_node);
21818 : 37 : releasing_vec args; // There are no args
21819 : :
21820 : 83 : for (unsigned ix = modules->length (); --ix;)
21821 : : {
21822 : 46 : module_state *import = (*modules)[ix];
21823 : 46 : if (import->active_init_p)
21824 : : {
21825 : 37 : tree name = mangle_module_global_init (ix);
21826 : 37 : tree fndecl = build_lang_decl (FUNCTION_DECL, name, fntype);
21827 : :
21828 : 37 : DECL_CONTEXT (fndecl) = FROB_CONTEXT (global_namespace);
21829 : 37 : SET_DECL_ASSEMBLER_NAME (fndecl, name);
21830 : 37 : TREE_PUBLIC (fndecl) = true;
21831 : 37 : determine_visibility (fndecl);
21832 : :
21833 : 37 : tree call = cp_build_function_call_vec (fndecl, &args,
21834 : : tf_warning_or_error);
21835 : 37 : finish_expr_stmt (call);
21836 : : }
21837 : : }
21838 : 37 : }
21839 : :
21840 : : /* NAME & LEN are a preprocessed header name, possibly including the
21841 : : surrounding "" or <> characters. Return the raw string name of the
21842 : : module to which it refers. This will be an absolute path, or begin
21843 : : with ./, so it is immediately distinguishable from a (non-header
21844 : : unit) module name. If READER is non-null, ask the preprocessor to
21845 : : locate the header to which it refers using the appropriate include
21846 : : path. Note that we do never do \ processing of the string, as that
21847 : : matches the preprocessor's behaviour. */
21848 : :
21849 : : static const char *
21850 : 23122 : canonicalize_header_name (cpp_reader *reader, location_t loc, bool unquoted,
21851 : : const char *str, size_t &len_r)
21852 : : {
21853 : 23122 : size_t len = len_r;
21854 : 23122 : static char *buf = 0;
21855 : 23122 : static size_t alloc = 0;
21856 : :
21857 : 23122 : if (!unquoted)
21858 : : {
21859 : 4 : gcc_checking_assert (len >= 2
21860 : : && ((reader && str[0] == '<' && str[len-1] == '>')
21861 : : || (str[0] == '"' && str[len-1] == '"')));
21862 : 4 : str += 1;
21863 : 4 : len -= 2;
21864 : : }
21865 : :
21866 : 23122 : if (reader)
21867 : : {
21868 : 4 : gcc_assert (!unquoted);
21869 : :
21870 : 4 : if (len >= alloc)
21871 : : {
21872 : 4 : alloc = len + 1;
21873 : 4 : buf = XRESIZEVEC (char, buf, alloc);
21874 : : }
21875 : 4 : memcpy (buf, str, len);
21876 : 4 : buf[len] = 0;
21877 : :
21878 : 8 : if (const char *hdr
21879 : 4 : = cpp_probe_header_unit (reader, buf, str[-1] == '<', loc))
21880 : : {
21881 : 4 : len = strlen (hdr);
21882 : 4 : str = hdr;
21883 : : }
21884 : : else
21885 : 0 : str = buf;
21886 : : }
21887 : :
21888 : 23122 : if (!(str[0] == '.' ? IS_DIR_SEPARATOR (str[1]) : IS_ABSOLUTE_PATH (str)))
21889 : : {
21890 : : /* Prepend './' */
21891 : 9 : if (len + 3 > alloc)
21892 : : {
21893 : 9 : alloc = len + 3;
21894 : 9 : buf = XRESIZEVEC (char, buf, alloc);
21895 : : }
21896 : :
21897 : 9 : buf[0] = '.';
21898 : 9 : buf[1] = DIR_SEPARATOR;
21899 : 9 : memmove (buf + 2, str, len);
21900 : 9 : len += 2;
21901 : 9 : buf[len] = 0;
21902 : 9 : str = buf;
21903 : : }
21904 : :
21905 : 23122 : len_r = len;
21906 : 23122 : return str;
21907 : : }
21908 : :
21909 : : /* Set the CMI name from a cody packet. Issue an error if
21910 : : ill-formed. */
21911 : :
21912 : 5077 : void module_state::set_filename (const Cody::Packet &packet)
21913 : : {
21914 : 5077 : if (packet.GetCode () == Cody::Client::PC_PATHNAME)
21915 : : {
21916 : : /* If we've seen this import before we better have the same CMI. */
21917 : 5074 : const std::string &path = packet.GetString ();
21918 : 5074 : if (!filename)
21919 : 5071 : filename = xstrdup (packet.GetString ().c_str ());
21920 : 3 : else if (filename != path)
21921 : 0 : error_at (loc, "mismatching compiled module interface: "
21922 : : "had %qs, got %qs", filename, path.c_str ());
21923 : : }
21924 : : else
21925 : : {
21926 : 3 : gcc_checking_assert (packet.GetCode () == Cody::Client::PC_ERROR);
21927 : 3 : fatal_error (loc, "unknown compiled module interface: %s",
21928 : 3 : packet.GetString ().c_str ());
21929 : : }
21930 : 5074 : }
21931 : :
21932 : : /* Figure out whether to treat HEADER as an include or an import. */
21933 : :
21934 : : static char *
21935 : 22239 : maybe_translate_include (cpp_reader *reader, line_maps *lmaps, location_t loc,
21936 : : const char *path)
21937 : : {
21938 : 22239 : if (!modules_p ())
21939 : : {
21940 : : /* Turn off. */
21941 : 0 : cpp_get_callbacks (reader)->translate_include = NULL;
21942 : 0 : return nullptr;
21943 : : }
21944 : :
21945 : 22239 : dump.push (NULL);
21946 : :
21947 : 24126 : dump () && dump ("Checking include translation '%s'", path);
21948 : 22239 : auto *mapper = get_mapper (cpp_main_loc (reader), cpp_get_deps (reader));
21949 : :
21950 : 22239 : size_t len = strlen (path);
21951 : 22239 : path = canonicalize_header_name (NULL, loc, true, path, len);
21952 : 22239 : auto packet = mapper->IncludeTranslate (path, Cody::Flags::None, len);
21953 : :
21954 : 22239 : enum class xlate_kind {
21955 : : unknown, text, import,
21956 : 22239 : } translate = xlate_kind::unknown;
21957 : :
21958 : 22239 : if (packet.GetCode () == Cody::Client::PC_BOOL)
21959 : 22192 : translate = packet.GetInteger () ? xlate_kind::text : xlate_kind::unknown;
21960 : 47 : else if (packet.GetCode () == Cody::Client::PC_PATHNAME)
21961 : : {
21962 : : /* Record the CMI name for when we do the import.
21963 : : We may already know about this import, but libcpp doesn't yet. */
21964 : 47 : module_state *import = get_module (build_string (len, path));
21965 : 47 : import->set_filename (packet);
21966 : 47 : translate = xlate_kind::import;
21967 : : }
21968 : : else
21969 : : {
21970 : 0 : gcc_checking_assert (packet.GetCode () == Cody::Client::PC_ERROR);
21971 : 0 : error_at (loc, "cannot determine %<#include%> translation of %s: %s",
21972 : 0 : path, packet.GetString ().c_str ());
21973 : : }
21974 : :
21975 : 22239 : bool note = false;
21976 : 22239 : if (note_include_translate_yes && translate == xlate_kind::import)
21977 : : note = true;
21978 : 22236 : else if (note_include_translate_no && translate == xlate_kind::unknown)
21979 : : note = true;
21980 : 22230 : else if (note_includes)
21981 : : /* We do not expect the note_includes vector to be large, so O(N)
21982 : : iteration. */
21983 : 412 : for (unsigned ix = note_includes->length (); !note && ix--;)
21984 : 206 : if (!strcmp ((*note_includes)[ix], path))
21985 : 1 : note = true;
21986 : :
21987 : 206 : if (note)
21988 : 16 : inform (loc, translate == xlate_kind::import
21989 : : ? G_("include %qs translated to import")
21990 : : : G_("include %qs processed textually"), path);
21991 : :
21992 : 26010 : dump () && dump (translate == xlate_kind::import
21993 : : ? "Translating include to import"
21994 : : : "Keeping include as include");
21995 : 22239 : dump.pop (0);
21996 : :
21997 : 22239 : if (translate != xlate_kind::import)
21998 : : return nullptr;
21999 : :
22000 : : /* Create the translation text. */
22001 : 47 : loc = ordinary_loc_of (lmaps, loc);
22002 : 47 : const line_map_ordinary *map
22003 : 47 : = linemap_check_ordinary (linemap_lookup (lmaps, loc));
22004 : 47 : unsigned col = SOURCE_COLUMN (map, loc);
22005 : 47 : col -= (col != 0); /* Columns are 1-based. */
22006 : :
22007 : 47 : unsigned alloc = len + col + 60;
22008 : 47 : char *res = XNEWVEC (char, alloc);
22009 : :
22010 : 47 : strcpy (res, "__import");
22011 : 47 : unsigned actual = 8;
22012 : 47 : if (col > actual)
22013 : : {
22014 : : /* Pad out so the filename appears at the same position. */
22015 : 44 : memset (res + actual, ' ', col - actual);
22016 : 44 : actual = col;
22017 : : }
22018 : : /* No need to encode characters, that's not how header names are
22019 : : handled. */
22020 : 47 : actual += snprintf (res + actual, alloc - actual,
22021 : : "\"%s\" [[__translated]];\n", path);
22022 : 47 : gcc_checking_assert (actual < alloc);
22023 : :
22024 : : /* cpplib will delete the buffer. */
22025 : : return res;
22026 : 22239 : }
22027 : :
22028 : : static void
22029 : 879 : begin_header_unit (cpp_reader *reader)
22030 : : {
22031 : : /* Set the module header name from the main_input_filename. */
22032 : 879 : const char *main = main_input_filename;
22033 : 879 : size_t len = strlen (main);
22034 : 879 : main = canonicalize_header_name (NULL, 0, true, main, len);
22035 : 879 : module_state *module = get_module (build_string (len, main));
22036 : :
22037 : 879 : preprocess_module (module, cpp_main_loc (reader), false, false, true, reader);
22038 : 879 : }
22039 : :
22040 : : /* We've just properly entered the main source file. I.e. after the
22041 : : command line, builtins and forced headers. Record the line map and
22042 : : location of this map. Note we may be called more than once. The
22043 : : first call sticks. */
22044 : :
22045 : : void
22046 : 95474 : module_begin_main_file (cpp_reader *reader, line_maps *lmaps,
22047 : : const line_map_ordinary *map)
22048 : : {
22049 : 95474 : gcc_checking_assert (lmaps == line_table);
22050 : 95474 : if (modules_p () && !spans.init_p ())
22051 : : {
22052 : 4312 : unsigned n = dump.push (NULL);
22053 : 4312 : spans.init (lmaps, map);
22054 : 4312 : dump.pop (n);
22055 : 4312 : if (flag_header_unit && !cpp_get_options (reader)->preprocessed)
22056 : : {
22057 : : /* Tell the preprocessor this is an include file. */
22058 : 870 : cpp_retrofit_as_include (reader);
22059 : 870 : begin_header_unit (reader);
22060 : : }
22061 : : }
22062 : 95474 : }
22063 : :
22064 : : /* Process the pending_import queue, making sure we know the
22065 : : filenames. */
22066 : :
22067 : : static void
22068 : 5174 : name_pending_imports (cpp_reader *reader)
22069 : : {
22070 : 5174 : auto *mapper = get_mapper (cpp_main_loc (reader), cpp_get_deps (reader));
22071 : :
22072 : 5174 : if (!vec_safe_length (pending_imports))
22073 : : /* Not doing anything. */
22074 : : return;
22075 : :
22076 : 4405 : timevar_start (TV_MODULE_MAPPER);
22077 : :
22078 : 4405 : auto n = dump.push (NULL);
22079 : 4958 : dump () && dump ("Resolving direct import names");
22080 : 4405 : bool want_deps = (bool (mapper->get_flags () & Cody::Flags::NameOnly)
22081 : 4405 : || cpp_get_deps (reader));
22082 : 4405 : bool any = false;
22083 : :
22084 : 9602 : for (unsigned ix = 0; ix != pending_imports->length (); ix++)
22085 : : {
22086 : 5197 : module_state *module = (*pending_imports)[ix];
22087 : 5197 : gcc_checking_assert (module->is_direct ());
22088 : 5197 : if (!module->filename && !module->visited_p)
22089 : : {
22090 : 5096 : bool export_p = (module->module_p
22091 : 5096 : && (module->is_partition () || module->exported_p));
22092 : :
22093 : 5096 : Cody::Flags flags = Cody::Flags::None;
22094 : 5096 : if (flag_preprocess_only
22095 : 5096 : && !(module->is_header () && !export_p))
22096 : : {
22097 : 129 : if (!want_deps)
22098 : 66 : continue;
22099 : : flags = Cody::Flags::NameOnly;
22100 : : }
22101 : :
22102 : 5030 : if (!any)
22103 : : {
22104 : 4335 : any = true;
22105 : 4335 : mapper->Cork ();
22106 : : }
22107 : 5030 : if (export_p)
22108 : 2586 : mapper->ModuleExport (module->get_flatname (), flags);
22109 : : else
22110 : 2444 : mapper->ModuleImport (module->get_flatname (), flags);
22111 : 5030 : module->visited_p = true;
22112 : : }
22113 : : }
22114 : :
22115 : 4405 : if (any)
22116 : : {
22117 : 4335 : auto response = mapper->Uncork ();
22118 : 4335 : auto r_iter = response.begin ();
22119 : 9447 : for (unsigned ix = 0; ix != pending_imports->length (); ix++)
22120 : : {
22121 : 5115 : module_state *module = (*pending_imports)[ix];
22122 : 5115 : if (module->visited_p)
22123 : : {
22124 : 5030 : module->visited_p = false;
22125 : 5030 : gcc_checking_assert (!module->filename);
22126 : :
22127 : 5030 : module->set_filename (*r_iter);
22128 : 5027 : ++r_iter;
22129 : : }
22130 : : }
22131 : 4332 : }
22132 : :
22133 : 4402 : dump.pop (n);
22134 : :
22135 : 4402 : timevar_stop (TV_MODULE_MAPPER);
22136 : : }
22137 : :
22138 : : /* We've just lexed a module-specific control line for MODULE. Mark
22139 : : the module as a direct import, and possibly load up its macro
22140 : : state. Returns the primary module, if this is a module
22141 : : declaration. */
22142 : : /* Perhaps we should offer a preprocessing mode where we read the
22143 : : directives from the header unit, rather than require the header's
22144 : : CMI. */
22145 : :
22146 : : module_state *
22147 : 5216 : preprocess_module (module_state *module, location_t from_loc,
22148 : : bool in_purview, bool is_import, bool is_export,
22149 : : cpp_reader *reader)
22150 : : {
22151 : 5216 : if (!is_import)
22152 : : {
22153 : 2901 : if (module->loc)
22154 : : /* It's already been mentioned, so ignore its module-ness. */
22155 : : is_import = true;
22156 : : else
22157 : : {
22158 : : /* Record it is the module. */
22159 : 2880 : module->module_p = true;
22160 : 2880 : if (is_export)
22161 : : {
22162 : 2584 : module->exported_p = true;
22163 : 2584 : module->interface_p = true;
22164 : : }
22165 : : }
22166 : : }
22167 : :
22168 : 5216 : if (module->directness < MD_DIRECT + in_purview)
22169 : : {
22170 : : /* Mark as a direct import. */
22171 : 5173 : module->directness = module_directness (MD_DIRECT + in_purview);
22172 : :
22173 : : /* Set the location to be most informative for users. */
22174 : 5173 : from_loc = ordinary_loc_of (line_table, from_loc);
22175 : 5173 : if (module->loadedness != ML_NONE)
22176 : 6 : linemap_module_reparent (line_table, module->loc, from_loc);
22177 : : else
22178 : : {
22179 : 5167 : module->loc = from_loc;
22180 : 5167 : if (!module->flatname)
22181 : 5140 : module->set_flatname ();
22182 : : }
22183 : : }
22184 : :
22185 : 5216 : auto desired = ML_CONFIG;
22186 : 5216 : if (is_import
22187 : 2336 : && module->is_header ()
22188 : 6091 : && (!cpp_get_options (reader)->preprocessed
22189 : 3 : || cpp_get_options (reader)->directives_only))
22190 : : /* We need preprocessor state now. */
22191 : : desired = ML_PREPROCESSOR;
22192 : :
22193 : 5216 : if (!is_import || module->loadedness < desired)
22194 : : {
22195 : 5197 : vec_safe_push (pending_imports, module);
22196 : :
22197 : 5197 : if (desired == ML_PREPROCESSOR)
22198 : : {
22199 : 856 : unsigned n = dump.push (NULL);
22200 : :
22201 : 1055 : dump () && dump ("Reading %M preprocessor state", module);
22202 : 856 : name_pending_imports (reader);
22203 : :
22204 : : /* Preserve the state of the line-map. */
22205 : 856 : auto pre_hwm = LINEMAPS_ORDINARY_USED (line_table);
22206 : :
22207 : : /* We only need to close the span, if we're going to emit a
22208 : : CMI. But that's a little tricky -- our token scanner
22209 : : needs to be smarter -- and this isn't much state.
22210 : : Remember, we've not parsed anything at this point, so
22211 : : our module state flags are inadequate. */
22212 : 856 : spans.maybe_init ();
22213 : 856 : spans.close ();
22214 : :
22215 : 856 : timevar_start (TV_MODULE_IMPORT);
22216 : :
22217 : : /* Load the config of each pending import -- we must assign
22218 : : module numbers monotonically. */
22219 : 1897 : for (unsigned ix = 0; ix != pending_imports->length (); ix++)
22220 : : {
22221 : 1041 : auto *import = (*pending_imports)[ix];
22222 : 1217 : if (!(import->module_p
22223 : 176 : && (import->is_partition () || import->exported_p))
22224 : 871 : && import->loadedness == ML_NONE
22225 : 1912 : && (import->is_header () || !flag_preprocess_only))
22226 : : {
22227 : 862 : unsigned n = dump.push (import);
22228 : 862 : import->do_import (reader, true);
22229 : 862 : dump.pop (n);
22230 : : }
22231 : : }
22232 : 856 : vec_free (pending_imports);
22233 : :
22234 : : /* Restore the line-map state. */
22235 : 856 : spans.open (linemap_module_restore (line_table, pre_hwm));
22236 : :
22237 : : /* Now read the preprocessor state of this particular
22238 : : import. */
22239 : 856 : if (module->loadedness == ML_CONFIG
22240 : 856 : && module->read_preprocessor (true))
22241 : 853 : module->import_macros ();
22242 : :
22243 : 856 : timevar_stop (TV_MODULE_IMPORT);
22244 : :
22245 : 856 : dump.pop (n);
22246 : : }
22247 : : }
22248 : :
22249 : 5216 : return is_import ? NULL : get_primary (module);
22250 : : }
22251 : :
22252 : : /* We've completed phase-4 translation. Emit any dependency
22253 : : information for the not-yet-loaded direct imports, and fill in
22254 : : their file names. We'll have already loaded up the direct header
22255 : : unit wavefront. */
22256 : :
22257 : : void
22258 : 4318 : preprocessed_module (cpp_reader *reader)
22259 : : {
22260 : 4318 : unsigned n = dump.push (NULL);
22261 : :
22262 : 4835 : dump () && dump ("Completed phase-4 (tokenization) processing");
22263 : :
22264 : 4318 : name_pending_imports (reader);
22265 : 4315 : vec_free (pending_imports);
22266 : :
22267 : 4315 : spans.maybe_init ();
22268 : 4315 : spans.close ();
22269 : :
22270 : 4315 : using iterator = hash_table<module_state_hash>::iterator;
22271 : 4315 : if (mkdeps *deps = cpp_get_deps (reader))
22272 : : {
22273 : : /* Walk the module hash, informing the dependency machinery. */
22274 : 51 : iterator end = modules_hash->end ();
22275 : 294 : for (iterator iter = modules_hash->begin (); iter != end; ++iter)
22276 : : {
22277 : 96 : module_state *module = *iter;
22278 : :
22279 : 96 : if (module->is_direct ())
22280 : : {
22281 : 81 : if (module->is_module ()
22282 : 81 : && (module->is_interface () || module->is_partition ()))
22283 : 33 : deps_add_module_target (deps, module->get_flatname (),
22284 : 33 : maybe_add_cmi_prefix (module->filename),
22285 : 33 : module->is_header (),
22286 : 33 : module->is_exported ());
22287 : : else
22288 : 48 : deps_add_module_dep (deps, module->get_flatname ());
22289 : : }
22290 : : }
22291 : : }
22292 : :
22293 : 4315 : if (flag_header_unit && !flag_preprocess_only)
22294 : : {
22295 : : /* Find the main module -- remember, it's not yet in the module
22296 : : array. */
22297 : 867 : iterator end = modules_hash->end ();
22298 : 1842 : for (iterator iter = modules_hash->begin (); iter != end; ++iter)
22299 : : {
22300 : 921 : module_state *module = *iter;
22301 : 921 : if (module->is_module ())
22302 : : {
22303 : 867 : declare_module (module, cpp_main_loc (reader), true, NULL, reader);
22304 : 867 : module_kind |= MK_EXPORTING;
22305 : 867 : break;
22306 : : }
22307 : : }
22308 : : }
22309 : :
22310 : 4315 : dump.pop (n);
22311 : 4315 : }
22312 : :
22313 : : /* VAL is a global tree, add it to the global vec if it is
22314 : : interesting. Add some of its targets, if they too are
22315 : : interesting. We do not add identifiers, as they can be re-found
22316 : : via the identifier hash table. There is a cost to the number of
22317 : : global trees. */
22318 : :
22319 : : static int
22320 : 2671458 : maybe_add_global (tree val, unsigned &crc)
22321 : : {
22322 : 2671458 : int v = 0;
22323 : :
22324 : 2671458 : if (val && !(identifier_p (val) || TREE_VISITED (val)))
22325 : : {
22326 : 823354 : TREE_VISITED (val) = true;
22327 : 823354 : crc = crc32_unsigned (crc, fixed_trees->length ());
22328 : 823354 : vec_safe_push (fixed_trees, val);
22329 : 823354 : v++;
22330 : :
22331 : 823354 : if (CODE_CONTAINS_STRUCT (TREE_CODE (val), TS_TYPED))
22332 : 823354 : v += maybe_add_global (TREE_TYPE (val), crc);
22333 : 823354 : if (CODE_CONTAINS_STRUCT (TREE_CODE (val), TS_TYPE_COMMON))
22334 : 539750 : v += maybe_add_global (TYPE_NAME (val), crc);
22335 : : }
22336 : :
22337 : 2671458 : return v;
22338 : : }
22339 : :
22340 : : /* Initialize module state. Create the hash table, determine the
22341 : : global trees. Create the module for current TU. */
22342 : :
22343 : : void
22344 : 4318 : init_modules (cpp_reader *reader)
22345 : : {
22346 : : /* PCH should not be reachable because of lang-specs, but the
22347 : : user could have overriden that. */
22348 : 4318 : if (pch_file)
22349 : 0 : fatal_error (input_location,
22350 : : "C++ modules are incompatible with precompiled headers");
22351 : :
22352 : 4318 : if (cpp_get_options (reader)->traditional)
22353 : 0 : fatal_error (input_location,
22354 : : "C++ modules are incompatible with traditional preprocessing");
22355 : :
22356 : : /* :: is always exported. */
22357 : 4318 : DECL_MODULE_EXPORT_P (global_namespace) = true;
22358 : :
22359 : 4318 : modules_hash = hash_table<module_state_hash>::create_ggc (31);
22360 : 4318 : vec_safe_reserve (modules, 20);
22361 : :
22362 : : /* Create module for current TU. */
22363 : 4318 : module_state *current
22364 : 4318 : = new (ggc_alloc<module_state> ()) module_state (NULL_TREE, NULL, false);
22365 : 4318 : current->mod = 0;
22366 : 4318 : bitmap_set_bit (current->imports, 0);
22367 : 4318 : modules->quick_push (current);
22368 : :
22369 : 4318 : gcc_checking_assert (!fixed_trees);
22370 : :
22371 : 4318 : headers = BITMAP_GGC_ALLOC ();
22372 : :
22373 : 4318 : if (note_includes)
22374 : : /* Canonicalize header names. */
22375 : 2 : for (unsigned ix = 0; ix != note_includes->length (); ix++)
22376 : : {
22377 : 1 : const char *hdr = (*note_includes)[ix];
22378 : 1 : size_t len = strlen (hdr);
22379 : :
22380 : 1 : bool system = hdr[0] == '<';
22381 : 1 : bool user = hdr[0] == '"';
22382 : 1 : bool delimed = system || user;
22383 : :
22384 : 1 : if (len <= (delimed ? 2 : 0)
22385 : 1 : || (delimed && hdr[len-1] != (system ? '>' : '"')))
22386 : 0 : error ("invalid header name %qs", hdr);
22387 : :
22388 : 1 : hdr = canonicalize_header_name (delimed ? reader : NULL,
22389 : : 0, !delimed, hdr, len);
22390 : 1 : char *path = XNEWVEC (char, len + 1);
22391 : 1 : memcpy (path, hdr, len);
22392 : 1 : path[len] = 0;
22393 : :
22394 : 1 : (*note_includes)[ix] = path;
22395 : : }
22396 : :
22397 : 4318 : if (note_cmis)
22398 : : /* Canonicalize & mark module names. */
22399 : 12 : for (unsigned ix = 0; ix != note_cmis->length (); ix++)
22400 : : {
22401 : 6 : const char *name = (*note_cmis)[ix];
22402 : 6 : size_t len = strlen (name);
22403 : :
22404 : 6 : bool is_system = name[0] == '<';
22405 : 6 : bool is_user = name[0] == '"';
22406 : 6 : bool is_pathname = false;
22407 : 6 : if (!(is_system || is_user))
22408 : 12 : for (unsigned ix = len; !is_pathname && ix--;)
22409 : 9 : is_pathname = IS_DIR_SEPARATOR (name[ix]);
22410 : 6 : if (is_system || is_user || is_pathname)
22411 : : {
22412 : 3 : if (len <= (is_pathname ? 0 : 2)
22413 : 3 : || (!is_pathname && name[len-1] != (is_system ? '>' : '"')))
22414 : : {
22415 : 0 : error ("invalid header name %qs", name);
22416 : 0 : continue;
22417 : : }
22418 : : else
22419 : 3 : name = canonicalize_header_name (is_pathname ? nullptr : reader,
22420 : : 0, is_pathname, name, len);
22421 : : }
22422 : 6 : if (auto module = get_module (name))
22423 : 6 : module->inform_cmi_p = 1;
22424 : : else
22425 : 0 : error ("invalid module name %qs", name);
22426 : : }
22427 : :
22428 : 4318 : dump.push (NULL);
22429 : :
22430 : : /* Determine lazy handle bound. */
22431 : 4318 : {
22432 : 4318 : unsigned limit = 1000;
22433 : : #if HAVE_GETRLIMIT
22434 : 4318 : struct rlimit rlimit;
22435 : 4318 : if (!getrlimit (RLIMIT_NOFILE, &rlimit))
22436 : : {
22437 : 4318 : lazy_hard_limit = (rlimit.rlim_max < 1000000
22438 : 4318 : ? unsigned (rlimit.rlim_max) : 1000000);
22439 : 4318 : lazy_hard_limit = (lazy_hard_limit > LAZY_HEADROOM
22440 : 4318 : ? lazy_hard_limit - LAZY_HEADROOM : 0);
22441 : 4318 : if (rlimit.rlim_cur < limit)
22442 : 0 : limit = unsigned (rlimit.rlim_cur);
22443 : : }
22444 : : #endif
22445 : 4318 : limit = limit > LAZY_HEADROOM ? limit - LAZY_HEADROOM : 1;
22446 : :
22447 : 4318 : if (unsigned parm = param_lazy_modules)
22448 : : {
22449 : 4318 : if (parm <= limit || !lazy_hard_limit || !try_increase_lazy (parm))
22450 : 6 : lazy_limit = parm;
22451 : : }
22452 : : else
22453 : 0 : lazy_limit = limit;
22454 : : }
22455 : :
22456 : 4318 : if (dump ())
22457 : : {
22458 : 517 : verstr_t ver;
22459 : 517 : version2string (MODULE_VERSION, ver);
22460 : 517 : dump ("Source: %s", main_input_filename);
22461 : 517 : dump ("Compiler: %s", version_string);
22462 : 517 : dump ("Modules: %s", ver);
22463 : 517 : dump ("Checking: %s",
22464 : : #if CHECKING_P
22465 : : "checking"
22466 : : #elif ENABLE_ASSERT_CHECKING
22467 : : "asserting"
22468 : : #else
22469 : : "release"
22470 : : #endif
22471 : : );
22472 : 517 : dump ("Compiled by: "
22473 : : #ifdef __GNUC__
22474 : : "GCC %d.%d, %s", __GNUC__, __GNUC_MINOR__,
22475 : : #ifdef __OPTIMIZE__
22476 : : "optimizing"
22477 : : #else
22478 : : "not optimizing"
22479 : : #endif
22480 : : #else
22481 : : "not GCC"
22482 : : #endif
22483 : : );
22484 : 517 : dump ("Reading: %s", MAPPED_READING ? "mmap" : "fileio");
22485 : 517 : dump ("Writing: %s", MAPPED_WRITING ? "mmap" : "fileio");
22486 : 517 : dump ("Lazy limit: %u", lazy_limit);
22487 : 517 : dump ("Lazy hard limit: %u", lazy_hard_limit);
22488 : 517 : dump ("");
22489 : : }
22490 : :
22491 : : /* Construct the global tree array. This is an array of unique
22492 : : global trees (& types). Do this now, rather than lazily, as
22493 : : some global trees are lazily created and we don't want that to
22494 : : mess with our syndrome of fixed trees. */
22495 : 4318 : unsigned crc = 0;
22496 : 4318 : vec_alloc (fixed_trees, 250);
22497 : :
22498 : 4835 : dump () && dump ("+Creating globals");
22499 : : /* Insert the TRANSLATION_UNIT_DECL. */
22500 : 4318 : TREE_VISITED (DECL_CONTEXT (global_namespace)) = true;
22501 : 4318 : fixed_trees->quick_push (DECL_CONTEXT (global_namespace));
22502 : 25908 : for (unsigned jx = 0; global_tree_arys[jx].first; jx++)
22503 : : {
22504 : 21590 : const tree *ptr = global_tree_arys[jx].first;
22505 : 21590 : unsigned limit = global_tree_arys[jx].second;
22506 : :
22507 : 1316990 : for (unsigned ix = 0; ix != limit; ix++, ptr++)
22508 : : {
22509 : 1295400 : !(ix & 31) && dump ("") && dump ("+\t%u:%u:", jx, ix);
22510 : 1295400 : unsigned v = maybe_add_global (*ptr, crc);
22511 : 1450500 : dump () && dump ("+%u", v);
22512 : : }
22513 : : }
22514 : : /* OS- and machine-specific types are dynamically registered at
22515 : : runtime, so cannot be part of global_tree_arys. */
22516 : 4318 : registered_builtin_types && dump ("") && dump ("+\tB:");
22517 : 17272 : for (tree t = registered_builtin_types; t; t = TREE_CHAIN (t))
22518 : : {
22519 : 12954 : unsigned v = maybe_add_global (TREE_VALUE (t), crc);
22520 : 14505 : dump () && dump ("+%u", v);
22521 : : }
22522 : 4318 : global_crc = crc32_unsigned (crc, fixed_trees->length ());
22523 : 4318 : dump ("") && dump ("Created %u unique globals, crc=%x",
22524 : : fixed_trees->length (), global_crc);
22525 : 831990 : for (unsigned ix = fixed_trees->length (); ix--;)
22526 : 827672 : TREE_VISITED ((*fixed_trees)[ix]) = false;
22527 : :
22528 : 4318 : dump.pop (0);
22529 : :
22530 : 4318 : if (!flag_module_lazy)
22531 : : /* Get the mapper now, if we're not being lazy. */
22532 : 278 : get_mapper (cpp_main_loc (reader), cpp_get_deps (reader));
22533 : :
22534 : 4318 : if (!flag_preprocess_only)
22535 : : {
22536 : 4207 : pending_table = new pending_map_t (EXPERIMENT (1, 400));
22537 : 4207 : entity_map = new entity_map_t (EXPERIMENT (1, 400));
22538 : 4207 : vec_safe_reserve (entity_ary, EXPERIMENT (1, 400));
22539 : 4207 : imported_temploid_friends
22540 : 4207 : = decl_tree_cache_map::create_ggc (EXPERIMENT (1, 400));
22541 : : }
22542 : :
22543 : : #if CHECKING_P
22544 : 4318 : note_defs = note_defs_table_t::create_ggc (1000);
22545 : : #endif
22546 : :
22547 : 4318 : if (flag_header_unit && cpp_get_options (reader)->preprocessed)
22548 : 9 : begin_header_unit (reader);
22549 : :
22550 : : /* Collect here to make sure things are tagged correctly (when
22551 : : aggressively GC'd). */
22552 : 4318 : ggc_collect ();
22553 : 4318 : }
22554 : :
22555 : : /* If NODE is a deferred macro, load it. */
22556 : :
22557 : : static int
22558 : 82561 : load_macros (cpp_reader *reader, cpp_hashnode *node, void *)
22559 : : {
22560 : 82561 : location_t main_loc
22561 : 82561 : = MAP_START_LOCATION (LINEMAPS_ORDINARY_MAP_AT (line_table, 0));
22562 : :
22563 : 82561 : if (cpp_user_macro_p (node)
22564 : 82561 : && !node->value.macro)
22565 : : {
22566 : 72 : cpp_macro *macro = cpp_get_deferred_macro (reader, node, main_loc);
22567 : 72 : dump () && dump ("Loaded macro #%s %I",
22568 : : macro ? "define" : "undef", identifier (node));
22569 : : }
22570 : :
22571 : 82561 : return 1;
22572 : : }
22573 : :
22574 : : /* At the end of tokenizing, we no longer need the macro tables of
22575 : : imports. But the user might have requested some checking. */
22576 : :
22577 : : void
22578 : 93854 : maybe_check_all_macros (cpp_reader *reader)
22579 : : {
22580 : 93854 : if (!warn_imported_macros)
22581 : : return;
22582 : :
22583 : : /* Force loading of any remaining deferred macros. This will
22584 : : produce diagnostics if they are ill-formed. */
22585 : 21 : unsigned n = dump.push (NULL);
22586 : 21 : cpp_forall_identifiers (reader, load_macros, NULL);
22587 : 21 : dump.pop (n);
22588 : : }
22589 : :
22590 : : // State propagated from finish_module_processing to fini_modules
22591 : :
22592 : : struct module_processing_cookie
22593 : : {
22594 : : elf_out out;
22595 : : module_state_config config;
22596 : : char *cmi_name;
22597 : : char *tmp_name;
22598 : : unsigned crc;
22599 : : bool began;
22600 : :
22601 : 2535 : module_processing_cookie (char *cmi, char *tmp, int fd, int e)
22602 : 2535 : : out (fd, e), cmi_name (cmi), tmp_name (tmp), crc (0), began (false)
22603 : : {
22604 : : }
22605 : 2535 : ~module_processing_cookie ()
22606 : : {
22607 : 2535 : XDELETEVEC (tmp_name);
22608 : 2535 : XDELETEVEC (cmi_name);
22609 : 2535 : }
22610 : : };
22611 : :
22612 : : /* Write the CMI, if we're a module interface. */
22613 : :
22614 : : void *
22615 : 93654 : finish_module_processing (cpp_reader *reader)
22616 : : {
22617 : 93654 : module_processing_cookie *cookie = nullptr;
22618 : :
22619 : 93654 : if (header_module_p ())
22620 : 867 : module_kind &= ~MK_EXPORTING;
22621 : :
22622 : 93654 : if (!modules || !(*modules)[0]->name)
22623 : : {
22624 : 91116 : if (flag_module_only)
22625 : 6 : warning (0, "%<-fmodule-only%> used for non-interface");
22626 : : }
22627 : 2538 : else if (!flag_syntax_only)
22628 : : {
22629 : 2535 : int fd = -1;
22630 : 2535 : int e = -1;
22631 : :
22632 : 2535 : timevar_start (TV_MODULE_EXPORT);
22633 : :
22634 : : /* Force a valid but empty line map at the end. This simplifies
22635 : : the line table preparation and writing logic. */
22636 : 2535 : linemap_add (line_table, LC_ENTER, false, "", 0);
22637 : :
22638 : : /* We write to a tmpname, and then atomically rename. */
22639 : 2535 : char *cmi_name = NULL;
22640 : 2535 : char *tmp_name = NULL;
22641 : 2535 : module_state *state = (*modules)[0];
22642 : :
22643 : 2535 : unsigned n = dump.push (state);
22644 : 2535 : state->announce ("creating");
22645 : 2535 : if (state->filename)
22646 : : {
22647 : 2535 : size_t len = 0;
22648 : 2535 : cmi_name = xstrdup (maybe_add_cmi_prefix (state->filename, &len));
22649 : 2535 : tmp_name = XNEWVEC (char, len + 3);
22650 : 2535 : memcpy (tmp_name, cmi_name, len);
22651 : 2535 : strcpy (&tmp_name[len], "~");
22652 : :
22653 : 2535 : if (!errorcount)
22654 : 34 : for (unsigned again = 2; ; again--)
22655 : : {
22656 : 2466 : fd = open (tmp_name,
22657 : : O_RDWR | O_CREAT | O_TRUNC | O_CLOEXEC | O_BINARY,
22658 : : S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP|S_IROTH|S_IWOTH);
22659 : 2466 : e = errno;
22660 : 2466 : if (fd >= 0 || !again || e != ENOENT)
22661 : : break;
22662 : 34 : create_dirs (tmp_name);
22663 : : }
22664 : 2535 : if (note_module_cmi_yes || state->inform_cmi_p)
22665 : 3 : inform (state->loc, "writing CMI %qs", cmi_name);
22666 : 2787 : dump () && dump ("CMI is %s", cmi_name);
22667 : : }
22668 : :
22669 : 2535 : cookie = new module_processing_cookie (cmi_name, tmp_name, fd, e);
22670 : :
22671 : 2535 : if (errorcount)
22672 : : /* Don't write the module if we have reported errors. */;
22673 : 2432 : else if (erroneous_templates
22674 : 2432 : && !erroneous_templates->is_empty ())
22675 : : {
22676 : : /* Don't write the module if it contains an erroneous template.
22677 : : Also emit notes about where errors occurred in case
22678 : : -Wno-template-body was passed. */
22679 : 6 : auto_diagnostic_group d;
22680 : 6 : error_at (state->loc, "not writing module %qs due to errors "
22681 : : "in template bodies", state->get_flatname ());
22682 : 6 : if (!warn_template_body)
22683 : 3 : inform (state->loc, "enable %<-Wtemplate-body%> for more details");
22684 : 12 : for (auto e : *erroneous_templates)
22685 : 6 : inform (e.second, "first error in %qD appeared here", e.first);
22686 : 6 : }
22687 : 2426 : else if (cookie->out.begin ())
22688 : : {
22689 : : /* So crashes finger-point the module decl. */
22690 : 2426 : iloc_sentinel ils = state->loc;
22691 : 2426 : if (state->write_begin (&cookie->out, reader, cookie->config,
22692 : 2426 : cookie->crc))
22693 : 2398 : cookie->began = true;
22694 : 2426 : }
22695 : :
22696 : 2535 : dump.pop (n);
22697 : 2535 : timevar_stop (TV_MODULE_EXPORT);
22698 : :
22699 : 2535 : ggc_collect ();
22700 : : }
22701 : :
22702 : 93654 : if (modules)
22703 : : {
22704 : 4170 : unsigned n = dump.push (NULL);
22705 : 4687 : dump () && dump ("Imported %u modules", modules->length () - 1);
22706 : 4687 : dump () && dump ("Containing %u clusters", available_clusters);
22707 : 4170 : dump () && dump ("Loaded %u clusters (%u%%)", loaded_clusters,
22708 : 517 : (loaded_clusters * 100 + available_clusters / 2) /
22709 : 517 : (available_clusters + !available_clusters));
22710 : 4170 : dump.pop (n);
22711 : : }
22712 : :
22713 : 93654 : return cookie;
22714 : : }
22715 : :
22716 : : // Do the final emission of a module. At this point we know whether
22717 : : // the module static initializer is a NOP or not.
22718 : :
22719 : : static void
22720 : 2535 : late_finish_module (cpp_reader *reader, module_processing_cookie *cookie,
22721 : : bool init_fn_non_empty)
22722 : : {
22723 : 2535 : timevar_start (TV_MODULE_EXPORT);
22724 : :
22725 : 2535 : module_state *state = (*modules)[0];
22726 : 2535 : unsigned n = dump.push (state);
22727 : 2535 : state->announce ("finishing");
22728 : :
22729 : 2535 : cookie->config.active_init = init_fn_non_empty;
22730 : 2535 : if (cookie->began)
22731 : 2398 : state->write_end (&cookie->out, reader, cookie->config, cookie->crc);
22732 : :
22733 : 2535 : if (cookie->out.end () && cookie->cmi_name)
22734 : : {
22735 : : /* Some OS's do not replace NEWNAME if it already exists.
22736 : : This'll have a race condition in erroneous concurrent
22737 : : builds. */
22738 : 2432 : unlink (cookie->cmi_name);
22739 : 2432 : if (rename (cookie->tmp_name, cookie->cmi_name))
22740 : : {
22741 : 0 : dump () && dump ("Rename ('%s','%s') errno=%u",
22742 : 0 : cookie->tmp_name, cookie->cmi_name, errno);
22743 : 0 : cookie->out.set_error (errno);
22744 : : }
22745 : : }
22746 : :
22747 : 2535 : if (cookie->out.get_error () && cookie->began)
22748 : : {
22749 : 0 : error_at (state->loc, "failed to write compiled module: %s",
22750 : 0 : cookie->out.get_error (state->filename));
22751 : 0 : state->note_cmi_name ();
22752 : : }
22753 : :
22754 : 2535 : if (!errorcount)
22755 : : {
22756 : 2395 : auto *mapper = get_mapper (cpp_main_loc (reader), cpp_get_deps (reader));
22757 : 2395 : mapper->ModuleCompiled (state->get_flatname ());
22758 : : }
22759 : 140 : else if (cookie->cmi_name)
22760 : : {
22761 : : /* We failed, attempt to erase all evidence we even tried. */
22762 : 140 : unlink (cookie->tmp_name);
22763 : 140 : unlink (cookie->cmi_name);
22764 : : }
22765 : :
22766 : 2535 : delete cookie;
22767 : 2535 : dump.pop (n);
22768 : 2535 : timevar_stop (TV_MODULE_EXPORT);
22769 : 2535 : }
22770 : :
22771 : : void
22772 : 93654 : fini_modules (cpp_reader *reader, void *cookie, bool has_inits)
22773 : : {
22774 : 93654 : if (cookie)
22775 : 2535 : late_finish_module (reader,
22776 : : static_cast<module_processing_cookie *> (cookie),
22777 : : has_inits);
22778 : :
22779 : : /* We're done with the macro tables now. */
22780 : 93654 : vec_free (macro_exports);
22781 : 93654 : vec_free (macro_imports);
22782 : 93654 : headers = NULL;
22783 : :
22784 : : /* We're now done with everything but the module names. */
22785 : 93654 : set_cmi_repo (NULL);
22786 : 93654 : if (mapper)
22787 : : {
22788 : 4170 : timevar_start (TV_MODULE_MAPPER);
22789 : 4170 : module_client::close_module_client (0, mapper);
22790 : 4170 : mapper = nullptr;
22791 : 4170 : timevar_stop (TV_MODULE_MAPPER);
22792 : : }
22793 : 93654 : module_state_config::release ();
22794 : :
22795 : : #if CHECKING_P
22796 : 93654 : note_defs = NULL;
22797 : : #endif
22798 : :
22799 : 93654 : if (modules)
22800 : 6736 : for (unsigned ix = modules->length (); --ix;)
22801 : 2566 : if (module_state *state = (*modules)[ix])
22802 : 2566 : state->release ();
22803 : :
22804 : : /* No need to lookup modules anymore. */
22805 : 93654 : modules_hash = NULL;
22806 : :
22807 : : /* Or entity array. We still need the entity map to find import numbers. */
22808 : 93654 : vec_free (entity_ary);
22809 : 93654 : entity_ary = NULL;
22810 : :
22811 : : /* Or remember any pending entities. */
22812 : 97824 : delete pending_table;
22813 : 93654 : pending_table = NULL;
22814 : :
22815 : : /* Or any keys -- Let it go! */
22816 : 95749 : delete keyed_table;
22817 : 93654 : keyed_table = NULL;
22818 : :
22819 : : /* Allow a GC, we've possibly made much data unreachable. */
22820 : 93654 : ggc_collect ();
22821 : 93654 : }
22822 : :
22823 : : /* If CODE is a module option, handle it & return true. Otherwise
22824 : : return false. For unknown reasons I cannot get the option
22825 : : generation machinery to set fmodule-mapper or -fmodule-header to
22826 : : make a string type option variable. */
22827 : :
22828 : : bool
22829 : 1861652 : handle_module_option (unsigned code, const char *str, int)
22830 : : {
22831 : 1861652 : auto hdr = CMS_header;
22832 : :
22833 : 1861652 : switch (opt_code (code))
22834 : : {
22835 : 42 : case OPT_fmodule_mapper_:
22836 : 42 : module_mapper_name = str;
22837 : 42 : return true;
22838 : :
22839 : 11 : case OPT_fmodule_header_:
22840 : 11 : {
22841 : 11 : if (!strcmp (str, "user"))
22842 : : hdr = CMS_user;
22843 : 11 : else if (!strcmp (str, "system"))
22844 : : hdr = CMS_system;
22845 : : else
22846 : 0 : error ("unknown header kind %qs", str);
22847 : : }
22848 : : /* Fallthrough. */
22849 : :
22850 : 882 : case OPT_fmodule_header:
22851 : 882 : flag_header_unit = hdr;
22852 : 882 : flag_modules = 1;
22853 : 882 : return true;
22854 : :
22855 : 1 : case OPT_flang_info_include_translate_:
22856 : 1 : vec_safe_push (note_includes, str);
22857 : 1 : return true;
22858 : :
22859 : 6 : case OPT_flang_info_module_cmi_:
22860 : 6 : vec_safe_push (note_cmis, str);
22861 : 6 : return true;
22862 : :
22863 : : default:
22864 : : return false;
22865 : : }
22866 : : }
22867 : :
22868 : : /* Set preprocessor callbacks and options for modules. */
22869 : :
22870 : : void
22871 : 95280 : module_preprocess_options (cpp_reader *reader)
22872 : : {
22873 : 95280 : gcc_checking_assert (!lang_hooks.preprocess_undef);
22874 : 95280 : if (modules_p ())
22875 : : {
22876 : 4318 : auto *cb = cpp_get_callbacks (reader);
22877 : :
22878 : 4318 : cb->translate_include = maybe_translate_include;
22879 : 4318 : cb->user_deferred_macro = module_state::deferred_macro;
22880 : 4318 : if (flag_header_unit)
22881 : : {
22882 : : /* If the preprocessor hook is already in use, that
22883 : : implementation will call the undef langhook. */
22884 : 879 : if (cb->undef)
22885 : 0 : lang_hooks.preprocess_undef = module_state::undef_macro;
22886 : : else
22887 : 879 : cb->undef = module_state::undef_macro;
22888 : : }
22889 : 4318 : auto *opt = cpp_get_options (reader);
22890 : 4318 : opt->module_directives = true;
22891 : 4318 : if (flag_no_output)
22892 : 12 : opt->directives_only = true;
22893 : 4318 : if (opt->main_search == CMS_none)
22894 : 4318 : opt->main_search = cpp_main_search (flag_header_unit);
22895 : : }
22896 : 95280 : }
22897 : :
22898 : : #include "gt-cp-module.h"
|