2011-05-27 Diego Novillo <dnovillo@google.com>
[official-gcc.git] / gcc / cp / pph-streamer-in.c
blobd9b9f1d1adbf68728ee0f95f281db30028728f79
1 /* Routines for reading PPH data.
2 Copyright (C) 2011 Free Software Foundation, Inc.
3 Contributed by Diego Novillo <dnovillo@google.com>.
5 This file is part of GCC.
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.
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.
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/>. */
21 #include "config.h"
22 #include "system.h"
23 #include "coretypes.h"
24 #include "tree.h"
25 #include "langhooks.h"
26 #include "tree-iterator.h"
27 #include "tree-pretty-print.h"
28 #include "lto-streamer.h"
29 #include "pph-streamer.h"
30 #include "pph.h"
31 #include "tree-pass.h"
32 #include "version.h"
33 #include "cppbuiltin.h"
35 /* Wrapper for memory allocation calls that should have their results
36 registered in the PPH streamer cache. DATA is the pointer returned
37 by the memory allocation call in ALLOC_EXPR. IX is the cache slot
38 in STREAM where the newly allocated DATA should be registered at. */
39 #define ALLOC_AND_REGISTER(STREAM, IX, DATA, ALLOC_EXPR) \
40 do { \
41 (DATA) = (ALLOC_EXPR); \
42 pph_stream_register_shared_data (STREAM, DATA, IX); \
43 } while (0)
45 /* Callback for unpacking value fields in ASTs. BP is the bitpack
46 we are unpacking from. EXPR is the tree to unpack. */
48 void
49 pph_stream_unpack_value_fields (struct bitpack_d *bp, tree expr)
51 if (TYPE_P (expr))
53 TYPE_LANG_FLAG_0 (expr) = bp_unpack_value (bp, 1);
54 TYPE_LANG_FLAG_1 (expr) = bp_unpack_value (bp, 1);
55 TYPE_LANG_FLAG_2 (expr) = bp_unpack_value (bp, 1);
56 TYPE_LANG_FLAG_3 (expr) = bp_unpack_value (bp, 1);
57 TYPE_LANG_FLAG_4 (expr) = bp_unpack_value (bp, 1);
58 TYPE_LANG_FLAG_5 (expr) = bp_unpack_value (bp, 1);
59 TYPE_LANG_FLAG_6 (expr) = bp_unpack_value (bp, 1);
61 else if (DECL_P (expr))
63 DECL_LANG_FLAG_0 (expr) = bp_unpack_value (bp, 1);
64 DECL_LANG_FLAG_1 (expr) = bp_unpack_value (bp, 1);
65 DECL_LANG_FLAG_2 (expr) = bp_unpack_value (bp, 1);
66 DECL_LANG_FLAG_3 (expr) = bp_unpack_value (bp, 1);
67 DECL_LANG_FLAG_4 (expr) = bp_unpack_value (bp, 1);
68 DECL_LANG_FLAG_5 (expr) = bp_unpack_value (bp, 1);
69 DECL_LANG_FLAG_6 (expr) = bp_unpack_value (bp, 1);
70 DECL_LANG_FLAG_7 (expr) = bp_unpack_value (bp, 1);
71 DECL_LANG_FLAG_8 (expr) = bp_unpack_value (bp, 1);
74 TREE_LANG_FLAG_0 (expr) = bp_unpack_value (bp, 1);
75 TREE_LANG_FLAG_1 (expr) = bp_unpack_value (bp, 1);
76 TREE_LANG_FLAG_2 (expr) = bp_unpack_value (bp, 1);
77 TREE_LANG_FLAG_3 (expr) = bp_unpack_value (bp, 1);
78 TREE_LANG_FLAG_4 (expr) = bp_unpack_value (bp, 1);
79 TREE_LANG_FLAG_5 (expr) = bp_unpack_value (bp, 1);
80 TREE_LANG_FLAG_6 (expr) = bp_unpack_value (bp, 1);
84 /* Get the section with name NAME and type SECTION_TYPE from FILE_DATA.
85 Return a pointer to the start of the section contents and store
86 the length of the section in *LEN_P.
88 FIXME pph, this does not currently handle multiple sections. It
89 assumes that the file has exactly one section. */
91 static const char *
92 pph_get_section_data (struct lto_file_decl_data *file_data,
93 enum lto_section_type section_type ATTRIBUTE_UNUSED,
94 const char *name ATTRIBUTE_UNUSED,
95 size_t *len)
97 /* FIXME pph - Stop abusing lto_file_decl_data fields. */
98 const pph_stream *stream = (const pph_stream *) file_data->file_name;
99 *len = stream->file_size - sizeof (pph_file_header);
100 return (const char *) stream->file_data + sizeof (pph_file_header);
104 /* Free the section data from FILE_DATA of SECTION_TYPE and NAME that
105 starts at OFFSET and has LEN bytes. */
107 static void
108 pph_free_section_data (struct lto_file_decl_data *file_data,
109 enum lto_section_type section_type ATTRIBUTE_UNUSED,
110 const char *name ATTRIBUTE_UNUSED,
111 const char *offset ATTRIBUTE_UNUSED,
112 size_t len ATTRIBUTE_UNUSED)
114 /* FIXME pph - Stop abusing lto_file_decl_data fields. */
115 const pph_stream *stream = (const pph_stream *) file_data->file_name;
116 free (stream->file_data);
120 /* Read into memory the contents of the file in STREAM. Initialize
121 internal tables and data structures needed to re-construct the
122 ASTs in the file. */
124 void
125 pph_stream_init_read (pph_stream *stream)
127 struct stat st;
128 size_t i, bytes_read, strtab_size, body_size;
129 int retcode;
130 pph_file_header *header;
131 const char *strtab, *body;
133 lto_reader_init ();
135 /* Read STREAM->NAME into the memory buffer STREAM->FILE_DATA.
136 FIXME pph, we are reading the whole file at once. This seems
137 wasteful. */
138 retcode = fstat (fileno (stream->file), &st);
139 gcc_assert (retcode == 0);
140 stream->file_size = (size_t) st.st_size;
141 stream->file_data = XCNEWVEC (char, stream->file_size);
142 bytes_read = fread (stream->file_data, 1, stream->file_size, stream->file);
143 gcc_assert (bytes_read == stream->file_size);
145 /* Set LTO callbacks to read the PPH file. */
146 stream->pph_sections = XCNEWVEC (struct lto_file_decl_data *,
147 PPH_NUM_SECTIONS);
148 for (i = 0; i < PPH_NUM_SECTIONS; i++)
150 stream->pph_sections[i] = XCNEW (struct lto_file_decl_data);
151 /* FIXME pph - Stop abusing fields in lto_file_decl_data. */
152 stream->pph_sections[i]->file_name = (const char *) stream;
155 lto_set_in_hooks (stream->pph_sections, pph_get_section_data,
156 pph_free_section_data);
158 header = (pph_file_header *) stream->file_data;
159 strtab = (const char *) header + sizeof (pph_file_header);
160 strtab_size = header->strtab_size;
161 body = strtab + strtab_size;
162 gcc_assert (stream->file_size >= strtab_size + sizeof (pph_file_header));
163 body_size = stream->file_size - strtab_size - sizeof (pph_file_header);
165 /* Create an input block structure pointing right after the string
166 table. */
167 stream->ib = XCNEW (struct lto_input_block);
168 LTO_INIT_INPUT_BLOCK_PTR (stream->ib, body, 0, body_size);
169 stream->data_in = lto_data_in_create (stream->pph_sections[0], strtab,
170 strtab_size, NULL);
172 /* Associate STREAM with STREAM->DATA_IN so we can recover it from
173 the streamer hooks. */
174 stream->data_in->sdata = (void *) stream;
178 /* Read and return a record marker from STREAM. When a PPH_RECORD_START
179 marker is read, the next word read is an index into the streamer
180 cache where the rematerialized data structure should be stored.
181 When the writer stored this data structure for the first time, it
182 added it to its own streamer cache at slot number *CACHE_IX.
184 This way, if the same data structure was written a second time to
185 the stream, instead of writing the whole structure again, only the
186 index *CACHE_IX is written as a PPH_RECORD_SHARED record.
188 Therefore, when reading a PPH_RECORD_START marker, *CACHE_IX will
189 contain the slot number where the materialized data should be
190 cached at. When reading a PPH_RECORD_SHARED marker, *CACHE_IX will
191 contain the slot number the reader can find the previously
192 materialized structure. */
194 static inline enum pph_record_marker
195 pph_start_record (pph_stream *stream, unsigned *cache_ix)
197 enum pph_record_marker marker;
199 marker = (enum pph_record_marker) pph_input_uchar (stream);
201 /* For PPH_RECORD_START and PPH_RECORD_SHARED markers, read the
202 streamer cache slot where we should store or find the
203 rematerialized data structure (see description above). */
204 if (marker == PPH_RECORD_START || marker == PPH_RECORD_SHARED)
205 *cache_ix = pph_input_uint (stream);
206 else
207 gcc_assert (marker == PPH_RECORD_END);
209 return marker;
213 /* Return a shared pointer from the streamer cache in STREAM. This is
214 called when pph_start_record returns PPH_RECORD_SHARED. It means
215 that the data structure we are about to read has been instantiated
216 before and is present in the streamer cache. */
218 static void *
219 pph_stream_read_shared_data (pph_stream *stream, unsigned ix)
221 return pph_stream_cache_get (stream, ix);
225 /* Register DATA in STREAM's cache slot IX. This is called when a
226 potentially shared data structure is first read from STREAM.
227 Subsequent reads of this data structure will get the index from the
228 table cache where this data was saved. */
230 static void
231 pph_stream_register_shared_data (pph_stream *stream, void *data, unsigned ix)
233 pph_stream_cache_insert_at (stream, data, ix);
237 /* Read all fields in lang_decl_base instance LDB from STREAM. */
239 static void
240 pph_stream_read_ld_base (pph_stream *stream, struct lang_decl_base *ldb)
242 struct bitpack_d bp;
244 bp = pph_input_bitpack (stream);
245 ldb->selector = bp_unpack_value (&bp, 16);
246 ldb->language = (enum languages) bp_unpack_value (&bp, 4);
247 ldb->use_template = bp_unpack_value (&bp, 2);
248 ldb->not_really_extern = bp_unpack_value (&bp, 1);
249 ldb->initialized_in_class = bp_unpack_value (&bp, 1);
250 ldb->repo_available_p = bp_unpack_value (&bp, 1);
251 ldb->threadprivate_or_deleted_p = bp_unpack_value (&bp, 1);
252 ldb->anticipated_p = bp_unpack_value (&bp, 1);
253 ldb->friend_attr = bp_unpack_value (&bp, 1);
254 ldb->template_conv_p = bp_unpack_value (&bp, 1);
255 ldb->odr_used = bp_unpack_value (&bp, 1);
256 ldb->u2sel = bp_unpack_value (&bp, 1);
260 /* Read all the fields in lang_decl_min instance LDM from STREAM. */
262 static void
263 pph_stream_read_ld_min (pph_stream *stream, struct lang_decl_min *ldm)
265 ldm->template_info = pph_input_tree (stream);
266 if (ldm->base.u2sel == 0)
267 ldm->u2.access = pph_input_tree (stream);
268 else if (ldm->base.u2sel == 1)
269 ldm->u2.discriminator = pph_input_uint (stream);
270 else
271 gcc_unreachable ();
275 /* Read and return a gc VEC of trees from STREAM. */
277 VEC(tree,gc) *
278 pph_stream_read_tree_vec (pph_stream *stream)
280 unsigned i, num;
281 VEC(tree,gc) *v;
283 num = pph_input_uint (stream);
284 v = NULL;
285 for (i = 0; i < num; i++)
287 tree t = pph_input_tree (stream);
288 VEC_safe_push (tree, gc, v, t);
291 return v;
295 /* Read and return a gc VEC of qualified_typedef_usage_t from STREAM. */
297 static VEC(qualified_typedef_usage_t,gc) *
298 pph_stream_read_qual_use_vec (pph_stream *stream)
300 unsigned i, num;
301 VEC(qualified_typedef_usage_t,gc) *v;
303 num = pph_input_uint (stream);
304 v = NULL;
305 for (i = 0; i < num; i++)
307 qualified_typedef_usage_t q;
308 q.typedef_decl = pph_input_tree (stream);
309 q.context = pph_input_tree (stream);
310 /* FIXME pph: also read location. */
311 VEC_safe_push (qualified_typedef_usage_t, gc, v, &q);
314 return v;
318 /* Forward declaration to break cyclic dependencies. */
319 static struct cp_binding_level *pph_stream_read_binding_level (pph_stream *);
321 /* Helper for pph_stream_read_cxx_binding. Read and return a cxx_binding
322 instance from STREAM. */
324 static cxx_binding *
325 pph_stream_read_cxx_binding_1 (pph_stream *stream)
327 struct bitpack_d bp;
328 cxx_binding *cb;
329 tree value, type;
330 enum pph_record_marker marker;
331 unsigned ix;
333 marker = pph_start_record (stream, &ix);
334 if (marker == PPH_RECORD_END)
335 return NULL;
336 else if (marker == PPH_RECORD_SHARED)
337 return (cxx_binding *) pph_stream_read_shared_data (stream, ix);
339 value = pph_input_tree (stream);
340 type = pph_input_tree (stream);
341 cb = cxx_binding_make (value, type);
342 cb->scope = pph_stream_read_binding_level (stream);
343 bp = pph_input_bitpack (stream);
344 cb->value_is_inherited = bp_unpack_value (&bp, 1);
345 cb->is_local = bp_unpack_value (&bp, 1);
347 pph_stream_register_shared_data (stream, cb, ix);
349 return cb;
353 /* Read and return an instance of cxx_binding from STREAM. */
355 static cxx_binding *
356 pph_stream_read_cxx_binding (pph_stream *stream)
358 unsigned i, num_bindings;
359 cxx_binding *curr, *cb;
361 /* Read the list of previous bindings. */
362 num_bindings = pph_input_uint (stream);
363 for (curr = NULL, i = 0; i < num_bindings; i++)
365 cxx_binding *prev = pph_stream_read_cxx_binding_1 (stream);
366 if (curr)
367 curr->previous = prev;
368 curr = prev;
371 /* Read the current binding at the end. Note that we do not need
372 to call pph_stream_register_shared_data as it is already done
373 by pph_stream_read_cxx_binding_1. */
374 cb = pph_stream_read_cxx_binding_1 (stream);
375 if (cb)
376 cb->previous = curr;
378 return cb;
382 /* Read all the fields of cp_class_binding instance CB to OB. */
384 static cp_class_binding *
385 pph_stream_read_class_binding (pph_stream *stream)
387 cp_class_binding *cb;
388 enum pph_record_marker marker;
389 unsigned ix;
391 marker = pph_start_record (stream, &ix);
392 if (marker == PPH_RECORD_END)
393 return NULL;
394 else if (marker == PPH_RECORD_SHARED)
395 return (cp_class_binding *) pph_stream_read_shared_data (stream, ix);
397 cb = ggc_alloc_cleared_cp_class_binding ();
398 memcpy (&cb->base, pph_stream_read_cxx_binding (stream),
399 sizeof (cxx_binding));
400 cb->identifier = pph_input_tree (stream);
402 pph_stream_register_shared_data (stream, cb, ix);
404 return cb;
408 /* Read and return an instance of cp_label_binding from STREAM. */
410 static cp_label_binding *
411 pph_stream_read_label_binding (pph_stream *stream)
413 cp_label_binding *lb;
414 enum pph_record_marker marker;
415 unsigned ix;
417 marker = pph_start_record (stream, &ix);
418 if (marker == PPH_RECORD_END)
419 return NULL;
420 else if (marker == PPH_RECORD_SHARED)
421 return (cp_label_binding *) pph_stream_read_shared_data (stream, ix);
423 lb = ggc_alloc_cleared_cp_label_binding ();
424 lb->label = pph_input_tree (stream);
425 lb->prev_value = pph_input_tree (stream);
427 pph_stream_register_shared_data (stream, lb, ix);
429 return lb;
433 /* Read and return an instance of cp_binding_level from STREAM. */
435 static struct cp_binding_level *
436 pph_stream_read_binding_level (pph_stream *stream)
438 unsigned i, num, ix;
439 cp_label_binding *sl;
440 struct cp_binding_level *bl;
441 struct bitpack_d bp;
442 enum pph_record_marker marker;
444 marker = pph_start_record (stream, &ix);
445 if (marker == PPH_RECORD_END)
446 return NULL;
447 else if (marker == PPH_RECORD_SHARED)
448 return (struct cp_binding_level *) pph_stream_read_shared_data (stream, ix);
450 ALLOC_AND_REGISTER (stream, ix, bl, ggc_alloc_cleared_cp_binding_level ());
452 bl->names = pph_input_chain (stream);
453 bl->names_size = pph_input_uint (stream);
454 bl->namespaces = pph_input_chain (stream);
456 bl->static_decls = pph_stream_read_tree_vec (stream);
458 bl->usings = pph_input_chain (stream);
459 bl->using_directives = pph_input_chain (stream);
461 num = pph_input_uint (stream);
462 bl->class_shadowed = NULL;
463 for (i = 0; i < num; i++)
465 cp_class_binding *cb = pph_stream_read_class_binding (stream);
466 VEC_safe_push (cp_class_binding, gc, bl->class_shadowed, cb);
469 bl->type_shadowed = pph_input_tree (stream);
471 num = pph_input_uint (stream);
472 bl->shadowed_labels = NULL;
473 for (i = 0; VEC_iterate (cp_label_binding, bl->shadowed_labels, i, sl); i++)
475 cp_label_binding *sl = pph_stream_read_label_binding (stream);
476 VEC_safe_push (cp_label_binding, gc, bl->shadowed_labels, sl);
479 bl->blocks = pph_input_chain (stream);
480 bl->this_entity = pph_input_tree (stream);
481 bl->level_chain = pph_stream_read_binding_level (stream);
482 bl->dead_vars_from_for = pph_stream_read_tree_vec (stream);
483 bl->statement_list = pph_input_chain (stream);
484 bl->binding_depth = pph_input_uint (stream);
486 bp = pph_input_bitpack (stream);
487 bl->kind = (enum scope_kind) bp_unpack_value (&bp, 4);
488 bl->keep = bp_unpack_value (&bp, 1);
489 bl->more_cleanups_ok = bp_unpack_value (&bp, 1);
490 bl->have_cleanups = bp_unpack_value (&bp, 1);
492 return bl;
496 /* Read and return an instance of struct c_language_function from STREAM. */
498 static struct c_language_function *
499 pph_stream_read_c_language_function (pph_stream *stream)
501 struct c_language_function *clf;
502 enum pph_record_marker marker;
503 unsigned ix;
505 marker = pph_start_record (stream, &ix);
506 if (marker == PPH_RECORD_END)
507 return NULL;
508 else if (marker == PPH_RECORD_SHARED)
509 return (struct c_language_function *) pph_stream_read_shared_data (stream,
510 ix);
512 ALLOC_AND_REGISTER (stream, ix, clf,
513 ggc_alloc_cleared_c_language_function ());
514 clf->x_stmt_tree.x_cur_stmt_list = pph_input_tree (stream);
515 clf->x_stmt_tree.stmts_are_full_exprs_p = pph_input_uint (stream);
517 return clf;
521 /* Read and return an instance of struct language_function from STREAM. */
523 static struct language_function *
524 pph_stream_read_language_function (pph_stream *stream)
526 struct bitpack_d bp;
527 struct language_function *lf;
528 enum pph_record_marker marker;
529 unsigned ix;
531 marker = pph_start_record (stream, &ix);
532 if (marker == PPH_RECORD_END)
533 return NULL;
534 else if (marker == PPH_RECORD_SHARED)
535 return (struct language_function *) pph_stream_read_shared_data (stream,
536 ix);
538 ALLOC_AND_REGISTER (stream, ix, lf, ggc_alloc_cleared_language_function ());
539 memcpy (&lf->base, pph_stream_read_c_language_function (stream),
540 sizeof (struct c_language_function));
541 lf->x_cdtor_label = pph_input_tree (stream);
542 lf->x_current_class_ptr = pph_input_tree (stream);
543 lf->x_current_class_ref = pph_input_tree (stream);
544 lf->x_eh_spec_block = pph_input_tree (stream);
545 lf->x_in_charge_parm = pph_input_tree (stream);
546 lf->x_vtt_parm = pph_input_tree (stream);
547 lf->x_return_value = pph_input_tree (stream);
548 bp = pph_input_bitpack (stream);
549 lf->x_returns_value = bp_unpack_value (&bp, 1);
550 lf->x_returns_null = bp_unpack_value (&bp, 1);
551 lf->x_returns_abnormally = bp_unpack_value (&bp, 1);
552 lf->x_in_function_try_handler = bp_unpack_value (&bp, 1);
553 lf->x_in_base_initializer = bp_unpack_value (&bp, 1);
554 lf->can_throw = bp_unpack_value (&bp, 1);
556 /* FIXME pph. We are not reading lf->x_named_labels. */
558 lf->bindings = pph_stream_read_binding_level (stream);
559 lf->x_local_names = pph_stream_read_tree_vec (stream);
561 /* FIXME pph. We are not reading lf->extern_decl_map. */
563 return lf;
567 /* Read all the fields of lang_decl_fn instance LDF from STREAM. */
569 static void
570 pph_stream_read_ld_fn (pph_stream *stream, struct lang_decl_fn *ldf)
572 struct bitpack_d bp;
574 bp = pph_input_bitpack (stream);
575 ldf->operator_code = (enum tree_code) bp_unpack_value (&bp, 16);
576 ldf->global_ctor_p = bp_unpack_value (&bp, 1);
577 ldf->global_dtor_p = bp_unpack_value (&bp, 1);
578 ldf->constructor_attr = bp_unpack_value (&bp, 1);
579 ldf->destructor_attr = bp_unpack_value (&bp, 1);
580 ldf->assignment_operator_p = bp_unpack_value (&bp, 1);
581 ldf->static_function = bp_unpack_value (&bp, 1);
582 ldf->pure_virtual = bp_unpack_value (&bp, 1);
583 ldf->defaulted_p = bp_unpack_value (&bp, 1);
584 ldf->has_in_charge_parm_p = bp_unpack_value (&bp, 1);
585 ldf->has_vtt_parm_p = bp_unpack_value (&bp, 1);
586 ldf->pending_inline_p = bp_unpack_value (&bp, 1);
587 ldf->nonconverting = bp_unpack_value (&bp, 1);
588 ldf->thunk_p = bp_unpack_value (&bp, 1);
589 ldf->this_thunk_p = bp_unpack_value (&bp, 1);
590 ldf->hidden_friend_p = bp_unpack_value (&bp, 1);
592 ldf->befriending_classes = pph_input_tree (stream);
593 ldf->context = pph_input_tree (stream);
595 if (ldf->thunk_p == 0)
596 ldf->u5.cloned_function = pph_input_tree (stream);
597 else if (ldf->thunk_p == 1)
598 ldf->u5.fixed_offset = pph_input_uint (stream);
599 else
600 gcc_unreachable ();
602 if (ldf->pending_inline_p == 1)
603 ldf->u.pending_inline_info = pth_load_token_cache (stream);
604 else if (ldf->pending_inline_p == 0)
605 ldf->u.saved_language_function = pph_stream_read_language_function (stream);
609 /* Read all the fields of lang_decl_ns instance LDNS from STREAM. */
611 static void
612 pph_stream_read_ld_ns (pph_stream *stream, struct lang_decl_ns *ldns)
614 ldns->level = pph_stream_read_binding_level (stream);
618 /* Read all the fields of lang_decl_parm instance LDP from STREAM. */
620 static void
621 pph_stream_read_ld_parm (pph_stream *stream, struct lang_decl_parm *ldp)
623 ldp->level = pph_input_uint (stream);
624 ldp->index = pph_input_uint (stream);
628 /* Read language specific data in DECL from STREAM. */
630 static void
631 pph_stream_read_lang_specific (pph_stream *stream, tree decl)
633 struct lang_decl *ld;
634 struct lang_decl_base *ldb;
635 enum pph_record_marker marker;
636 unsigned ix;
638 marker = pph_start_record (stream, &ix);
639 if (marker == PPH_RECORD_END)
640 return;
642 /* Since lang_decl is embedded in every decl, LD cannot
643 be shared. */
644 gcc_assert (marker != PPH_RECORD_SHARED);
646 /* Allocate a lang_decl structure for DECL. */
647 retrofit_lang_decl (decl);
649 ld = DECL_LANG_SPECIFIC (decl);
650 ldb = &ld->u.base;
652 /* Read all the fields in lang_decl_base. */
653 pph_stream_read_ld_base (stream, ldb);
655 if (ldb->selector == 0)
657 /* Read all the fields in lang_decl_min. */
658 pph_stream_read_ld_min (stream, &ld->u.min);
660 else if (ldb->selector == 1)
662 /* Read all the fields in lang_decl_fn. */
663 pph_stream_read_ld_fn (stream, &ld->u.fn);
665 else if (ldb->selector == 2)
667 /* Read all the fields in lang_decl_ns. */
668 pph_stream_read_ld_ns (stream, &ld->u.ns);
670 else if (ldb->selector == 3)
672 /* Read all the fields in lang_decl_parm. */
673 pph_stream_read_ld_parm (stream, &ld->u.parm);
675 else
676 gcc_unreachable ();
680 /* Allocate a tree node with code CODE. IB and DATA_IN are used to
681 read more data from the stream, if needed to build this node.
682 Return NULL if we did not want to handle this node. In that case,
683 the caller will call make_node to allocate this tree. */
685 tree
686 pph_stream_alloc_tree (enum tree_code code,
687 struct lto_input_block *ib ATTRIBUTE_UNUSED,
688 struct data_in *data_in)
690 pph_stream *stream = (pph_stream *) data_in->sdata;
692 if (code == CALL_EXPR)
694 unsigned nargs = pph_input_uint (stream);
695 return build_vl_exp (CALL_EXPR, nargs + 3);
698 return NULL_TREE;
702 /* Read all the fields in lang_type_header instance LTH from STREAM. */
704 static void
705 pph_stream_read_lang_type_header (pph_stream *stream,
706 struct lang_type_header *lth)
708 struct bitpack_d bp;
710 bp = pph_input_bitpack (stream);
711 lth->is_lang_type_class = bp_unpack_value (&bp, 1);
712 lth->has_type_conversion = bp_unpack_value (&bp, 1);
713 lth->has_copy_ctor = bp_unpack_value (&bp, 1);
714 lth->has_default_ctor = bp_unpack_value (&bp, 1);
715 lth->const_needs_init = bp_unpack_value (&bp, 1);
716 lth->ref_needs_init = bp_unpack_value (&bp, 1);
717 lth->has_const_copy_assign = bp_unpack_value (&bp, 1);
721 /* Read the vector V of tree_pair_s instances from STREAM. */
723 static VEC(tree_pair_s,gc) *
724 pph_stream_read_tree_pair_vec (pph_stream *stream)
726 unsigned i, num;
727 VEC(tree_pair_s,gc) *v;
729 num = pph_input_uint (stream);
730 for (i = 0, v = NULL; i < num; i++)
732 tree_pair_s p;
733 p.purpose = pph_input_tree (stream);
734 p.value = pph_input_tree (stream);
735 VEC_safe_push (tree_pair_s, gc, v, &p);
738 return v;
742 /* Read a struct sorted_fields_type instance SFT to STREAM. REF_P is
743 true if the tree nodes should be written as references. */
745 static struct sorted_fields_type *
746 pph_stream_read_sorted_fields_type (pph_stream *stream)
748 unsigned i, num_fields;
749 struct sorted_fields_type *v;
750 enum pph_record_marker marker;
751 unsigned ix;
753 marker = pph_start_record (stream, &ix);
754 if (marker == PPH_RECORD_END)
755 return NULL;
756 else if (marker == PPH_RECORD_SHARED)
757 return (struct sorted_fields_type *) pph_stream_read_shared_data (stream,
758 ix);
760 num_fields = pph_input_uint (stream);
761 ALLOC_AND_REGISTER (stream, ix, v, sorted_fields_type_new (num_fields));
762 for (i = 0; i < num_fields; i++)
763 v->elts[i] = pph_input_tree (stream);
765 return v;
769 /* Read all the fields in lang_type_class instance LTC to STREAM.
770 REF_P is true if all the trees in the structure should be written
771 as references. */
773 static void
774 pph_stream_read_lang_type_class (pph_stream *stream,
775 struct lang_type_class *ltc)
777 struct bitpack_d bp;
778 enum pph_record_marker marker;
779 unsigned ix;
781 ltc->align = pph_input_uchar (stream);
783 bp = pph_input_bitpack (stream);
784 ltc->has_mutable = bp_unpack_value (&bp, 1);
785 ltc->com_interface = bp_unpack_value (&bp, 1);
786 ltc->non_pod_class = bp_unpack_value (&bp, 1);
787 ltc->nearly_empty_p = bp_unpack_value (&bp, 1);
788 ltc->user_align = bp_unpack_value (&bp, 1);
789 ltc->has_copy_assign = bp_unpack_value (&bp, 1);
790 ltc->has_new = bp_unpack_value (&bp, 1);
791 ltc->has_array_new = bp_unpack_value (&bp, 1);
792 ltc->gets_delete = bp_unpack_value (&bp, 2);
793 ltc->interface_only = bp_unpack_value (&bp, 1);
794 ltc->interface_unknown = bp_unpack_value (&bp, 1);
795 ltc->contains_empty_class_p = bp_unpack_value (&bp, 1);
796 ltc->anon_aggr = bp_unpack_value (&bp, 1);
797 ltc->non_zero_init = bp_unpack_value (&bp, 1);
798 ltc->empty_p = bp_unpack_value (&bp, 1);
799 ltc->vec_new_uses_cookie = bp_unpack_value (&bp, 1);
800 ltc->declared_class = bp_unpack_value (&bp, 1);
801 ltc->diamond_shaped = bp_unpack_value (&bp, 1);
802 ltc->repeated_base = bp_unpack_value (&bp, 1);
803 ltc->being_defined = bp_unpack_value (&bp, 1);
804 ltc->java_interface = bp_unpack_value (&bp, 1);
805 ltc->debug_requested = bp_unpack_value (&bp, 1);
806 ltc->fields_readonly = bp_unpack_value (&bp, 1);
807 ltc->use_template = bp_unpack_value (&bp, 2);
808 ltc->ptrmemfunc_flag = bp_unpack_value (&bp, 1);
809 ltc->was_anonymous = bp_unpack_value (&bp, 1);
810 ltc->lazy_default_ctor = bp_unpack_value (&bp, 1);
811 ltc->lazy_copy_ctor = bp_unpack_value (&bp, 1);
812 ltc->lazy_copy_assign = bp_unpack_value (&bp, 1);
813 ltc->lazy_destructor = bp_unpack_value (&bp, 1);
814 ltc->has_const_copy_ctor = bp_unpack_value (&bp, 1);
815 ltc->has_complex_copy_ctor = bp_unpack_value (&bp, 1);
816 ltc->has_complex_copy_assign = bp_unpack_value (&bp, 1);
817 ltc->non_aggregate = bp_unpack_value (&bp, 1);
818 ltc->has_complex_dflt = bp_unpack_value (&bp, 1);
819 ltc->has_list_ctor = bp_unpack_value (&bp, 1);
820 ltc->non_std_layout = bp_unpack_value (&bp, 1);
821 ltc->is_literal = bp_unpack_value (&bp, 1);
822 ltc->lazy_move_ctor = bp_unpack_value (&bp, 1);
823 ltc->lazy_move_assign = bp_unpack_value (&bp, 1);
824 ltc->has_complex_move_ctor = bp_unpack_value (&bp, 1);
825 ltc->has_complex_move_assign = bp_unpack_value (&bp, 1);
826 ltc->has_constexpr_ctor = bp_unpack_value (&bp, 1);
828 ltc->primary_base = pph_input_tree (stream);
829 ltc->vcall_indices = pph_stream_read_tree_pair_vec (stream);
830 ltc->vtables = pph_input_tree (stream);
831 ltc->typeinfo_var = pph_input_tree (stream);
832 ltc->vbases = pph_stream_read_tree_vec (stream);
834 marker = pph_start_record (stream, &ix);
835 if (marker == PPH_RECORD_START)
837 ltc->nested_udts = pph_stream_read_binding_table (stream);
838 pph_stream_register_shared_data (stream, ltc->nested_udts, ix);
840 else if (marker == PPH_RECORD_SHARED)
841 ltc->nested_udts = (binding_table) pph_stream_read_shared_data (stream, ix);
843 ltc->as_base = pph_input_tree (stream);
844 ltc->pure_virtuals = pph_stream_read_tree_vec (stream);
845 ltc->friend_classes = pph_input_tree (stream);
846 ltc->methods = pph_stream_read_tree_vec (stream);
847 ltc->key_method = pph_input_tree (stream);
848 ltc->decl_list = pph_input_tree (stream);
849 ltc->template_info = pph_input_tree (stream);
850 ltc->befriending_classes = pph_input_tree (stream);
851 ltc->objc_info = pph_input_tree (stream);
852 ltc->sorted_fields = pph_stream_read_sorted_fields_type (stream);
853 ltc->lambda_expr = pph_input_tree (stream);
857 /* Read all fields of struct lang_type_ptrmem instance LTP from STREAM. */
859 static void
860 pph_stream_read_lang_type_ptrmem (pph_stream *stream,
861 struct lang_type_ptrmem *ltp)
863 ltp->record = pph_input_tree (stream);
867 /* Read all the fields in struct lang_type from STREAM. */
869 static struct lang_type *
870 pph_stream_read_lang_type (pph_stream *stream)
872 struct lang_type *lt;
873 enum pph_record_marker marker;
874 unsigned ix;
876 marker = pph_start_record (stream, &ix);
877 if (marker == PPH_RECORD_END)
878 return NULL;
879 else if (marker == PPH_RECORD_SHARED)
880 return (struct lang_type *) pph_stream_read_shared_data (stream, ix);
882 ALLOC_AND_REGISTER (stream, ix, lt,
883 ggc_alloc_cleared_lang_type (sizeof (struct lang_type)));
885 pph_stream_read_lang_type_header (stream, &lt->u.h);
886 if (lt->u.h.is_lang_type_class)
887 pph_stream_read_lang_type_class (stream, &lt->u.c);
888 else
889 pph_stream_read_lang_type_ptrmem (stream, &lt->u.ptrmem);
891 return lt;
895 /* Callback for reading ASTs from a stream. This reads all the fields
896 that are not processed by default by the common tree pickler.
897 IB, DATA_IN are as in lto_read_tree. EXPR is the partially materialized
898 tree. */
900 void
901 pph_stream_read_tree (struct lto_input_block *ib ATTRIBUTE_UNUSED,
902 struct data_in *data_in, tree expr)
904 pph_stream *stream = (pph_stream *) data_in->sdata;
906 switch (TREE_CODE (expr))
908 /* TREES NEEDING EXTRA WORK */
910 /* tcc_declaration */
912 case DEBUG_EXPR_DECL:
913 case IMPORTED_DECL:
914 case LABEL_DECL:
915 case RESULT_DECL:
916 DECL_INITIAL (expr) = pph_input_tree (stream);
917 break;
919 case CONST_DECL:
920 case FIELD_DECL:
921 case NAMESPACE_DECL:
922 case PARM_DECL:
923 case USING_DECL:
924 case VAR_DECL:
925 /* FIXME pph: Should we merge DECL_INITIAL into lang_specific? */
926 DECL_INITIAL (expr) = pph_input_tree (stream);
927 pph_stream_read_lang_specific (stream, expr);
928 break;
930 case FUNCTION_DECL:
931 DECL_INITIAL (expr) = pph_input_tree (stream);
932 pph_stream_read_lang_specific (stream, expr);
933 DECL_SAVED_TREE (expr) = pph_input_tree (stream);
934 break;
936 case TYPE_DECL:
937 DECL_INITIAL (expr) = pph_input_tree (stream);
938 pph_stream_read_lang_specific (stream, expr);
939 DECL_ORIGINAL_TYPE (expr) = pph_input_tree (stream);
940 break;
942 case TEMPLATE_DECL:
943 DECL_INITIAL (expr) = pph_input_tree (stream);
944 pph_stream_read_lang_specific (stream, expr);
945 DECL_TEMPLATE_RESULT (expr) = pph_input_tree (stream);
946 DECL_TEMPLATE_PARMS (expr) = pph_input_tree (stream);
947 DECL_CONTEXT (expr) = pph_input_tree (stream);
948 break;
950 /* tcc_type */
952 case ARRAY_TYPE:
953 case BOOLEAN_TYPE:
954 case COMPLEX_TYPE:
955 case ENUMERAL_TYPE:
956 case FIXED_POINT_TYPE:
957 case FUNCTION_TYPE:
958 case INTEGER_TYPE:
959 case LANG_TYPE:
960 case METHOD_TYPE:
961 case NULLPTR_TYPE:
962 case OFFSET_TYPE:
963 case POINTER_TYPE:
964 case REAL_TYPE:
965 case REFERENCE_TYPE:
966 case VECTOR_TYPE:
967 case VOID_TYPE:
968 TYPE_LANG_SPECIFIC (expr) = pph_stream_read_lang_type (stream);
969 break;
971 case QUAL_UNION_TYPE:
972 case RECORD_TYPE:
973 case UNION_TYPE:
974 TYPE_LANG_SPECIFIC (expr) = pph_stream_read_lang_type (stream);
975 TYPE_BINFO (expr) = pph_input_tree (stream);
976 break;
978 case BOUND_TEMPLATE_TEMPLATE_PARM:
979 case DECLTYPE_TYPE:
980 case TEMPLATE_TEMPLATE_PARM:
981 case TEMPLATE_TYPE_PARM:
982 case TYPENAME_TYPE:
983 case TYPEOF_TYPE:
984 TYPE_LANG_SPECIFIC (expr) = pph_stream_read_lang_type (stream);
985 TYPE_CACHED_VALUES (expr) = pph_input_tree (stream);
986 /* Note that we are using TYPED_CACHED_VALUES for it access to
987 the generic .values field of types. */
988 break;
990 /* tcc_statement */
992 case STATEMENT_LIST:
994 HOST_WIDE_INT i, num_trees = pph_input_uint (stream);
995 for (i = 0; i < num_trees; i++)
997 tree stmt = pph_input_tree (stream);
998 append_to_statement_list (stmt, &expr);
1001 break;
1003 /* tcc_expression */
1005 /* tcc_unary */
1007 /* tcc_vl_exp */
1009 /* tcc_reference */
1011 /* tcc_constant */
1013 /* tcc_exceptional */
1015 case OVERLOAD:
1016 OVL_FUNCTION (expr) = pph_input_tree (stream);
1017 break;
1019 case IDENTIFIER_NODE:
1021 struct lang_identifier *id = LANG_IDENTIFIER_CAST (expr);
1022 id->namespace_bindings = pph_stream_read_cxx_binding (stream);
1023 id->bindings = pph_stream_read_cxx_binding (stream);
1024 id->class_template_info = pph_input_tree (stream);
1025 id->label_value = pph_input_tree (stream);
1027 break;
1029 case BASELINK:
1030 BASELINK_BINFO (expr) = pph_input_tree (stream);
1031 BASELINK_FUNCTIONS (expr) = pph_input_tree (stream);
1032 BASELINK_ACCESS_BINFO (expr) = pph_input_tree (stream);
1033 break;
1035 case TEMPLATE_INFO:
1036 TI_TYPEDEFS_NEEDING_ACCESS_CHECKING (expr)
1037 = pph_stream_read_qual_use_vec (stream);
1038 break;
1040 case TEMPLATE_PARM_INDEX:
1042 template_parm_index *p = TEMPLATE_PARM_INDEX_CAST (expr);
1043 p->index = pph_input_uint (stream);
1044 p->level = pph_input_uint (stream);
1045 p->orig_level = pph_input_uint (stream);
1046 p->num_siblings = pph_input_uint (stream);
1047 p->decl = pph_input_tree (stream);
1048 /* FIXME pph: Is TEMPLATE_PARM_PARAMETER_PACK using TREE_LANG_FLAG_0
1049 already handled? */
1051 break;
1053 /* TREES ALREADY HANDLED */
1055 /* tcc_declaration */
1057 case TRANSLATION_UNIT_DECL:
1059 /* tcc_exceptional */
1061 case TREE_BINFO:
1062 case TREE_LIST:
1063 case TREE_VEC:
1065 break;
1067 /* TREES UNIMPLEMENTED */
1069 /* tcc_declaration */
1071 /* tcc_type */
1073 case TYPE_ARGUMENT_PACK:
1074 case TYPE_PACK_EXPANSION:
1075 case UNBOUND_CLASS_TEMPLATE:
1077 /* tcc_statement */
1079 case USING_STMT:
1080 case TRY_BLOCK:
1081 case EH_SPEC_BLOCK:
1082 case HANDLER:
1083 case CLEANUP_STMT:
1084 case IF_STMT:
1085 case FOR_STMT:
1086 case RANGE_FOR_STMT:
1087 case WHILE_STMT:
1088 case DO_STMT:
1089 case BREAK_STMT:
1090 case CONTINUE_STMT:
1091 case SWITCH_STMT:
1093 /* tcc_expression */
1095 case NEW_EXPR:
1096 case VEC_NEW_EXPR:
1097 case DELETE_EXPR:
1098 case VEC_DELETE_EXPR:
1099 case TYPE_EXPR:
1100 case VEC_INIT_EXPR:
1101 case THROW_EXPR:
1102 case EMPTY_CLASS_EXPR:
1103 case TEMPLATE_ID_EXPR:
1104 case PSEUDO_DTOR_EXPR:
1105 case MODOP_EXPR:
1106 case DOTSTAR_EXPR:
1107 case TYPEID_EXPR:
1108 case NON_DEPENDENT_EXPR:
1109 case CTOR_INITIALIZER:
1110 case MUST_NOT_THROW_EXPR:
1111 case EXPR_STMT:
1112 case TAG_DEFN:
1113 case OFFSETOF_EXPR:
1114 case SIZEOF_EXPR:
1115 case ARROW_EXPR:
1116 case ALIGNOF_EXPR:
1117 case AT_ENCODE_EXPR:
1118 case STMT_EXPR:
1119 case NONTYPE_ARGUMENT_PACK:
1120 case EXPR_PACK_EXPANSION:
1122 /* tcc_unary */
1124 case CAST_EXPR:
1125 case REINTERPRET_CAST_EXPR:
1126 case CONST_CAST_EXPR:
1127 case STATIC_CAST_EXPR:
1128 case DYNAMIC_CAST_EXPR:
1129 case NOEXCEPT_EXPR:
1130 case UNARY_PLUS_EXPR:
1132 /* tcc_reference */
1134 case MEMBER_REF:
1135 case OFFSET_REF:
1136 case SCOPE_REF:
1138 /* tcc_constant */
1140 case PTRMEM_CST:
1142 /* tcc_vl_exp */
1144 case AGGR_INIT_EXPR:
1146 /* tcc_exceptional */
1148 case DEFAULT_ARG:
1149 case STATIC_ASSERT:
1150 case ARGUMENT_PACK_SELECT:
1151 case TRAIT_EXPR:
1152 case LAMBDA_EXPR:
1154 if (flag_pph_untree)
1155 fprintf (pph_logfile, "PPH: unimplemented tree node %s\n",
1156 tree_code_name[TREE_CODE (expr)]);
1157 break;
1159 /* TREES UNRECOGNIZED */
1161 default:
1162 if (flag_pph_untree)
1163 fprintf (pph_logfile, "PPH: unrecognized tree node %s\n",
1164 tree_code_name[TREE_CODE (expr)]);