MK_enum, /* Found by CTX, & 1stMemberNAME. */
MK_keyed, /* Found by key & index. */
+ MK_local_type, /* Found by CTX, index. */
MK_friend_spec, /* Like named, but has a tmpl & args too. */
MK_local_friend, /* Found by CTX, index. */
"unique", "named", "field", "vtable", /* 0...3 */
"asbase", "partial", "enum", "attached", /* 4...7 */
- "friend spec", "local friend", NULL, NULL, /* 8...11 */
+ "local type", "friend spec", "local friend", NULL, /* 8...11 */
NULL, NULL, NULL, NULL,
"type spec", "type tmpl spec", /* 16,17 type (template). */
unsigned binfo_mergeable (tree *);
private:
+ tree key_local_type (const merge_key&, tree, tree);
uintptr_t *find_duplicate (tree existing);
void register_duplicate (tree decl, tree existing);
/* Mark as an already diagnosed bad duplicate. */
void binfo_mergeable (tree binfo);
private:
+ void key_local_type (merge_key&, tree, tree);
bool decl_node (tree, walk_kind ref);
void type_node (tree);
void tree_value (tree);
trees_out::chained_decls (tree decls)
{
for (; decls; decls = DECL_CHAIN (decls))
- {
- if (VAR_OR_FUNCTION_DECL_P (decls)
- && DECL_LOCAL_DECL_P (decls))
- {
- /* Make sure this is the first encounter, and mark for
- walk-by-value. */
- gcc_checking_assert (!TREE_VISITED (decls)
- && !DECL_TEMPLATE_INFO (decls));
- mark_by_value (decls);
- }
- tree_node (decls);
- }
+ tree_node (decls);
tree_node (NULL_TREE);
}
/* DECL_LOCAL_DECL_P decls are first encountered here and
streamed by value. */
- chained_decls (t->block.vars);
+ for (tree decls = t->block.vars; decls; decls = DECL_CHAIN (decls))
+ {
+ if (VAR_OR_FUNCTION_DECL_P (decls)
+ && DECL_LOCAL_DECL_P (decls))
+ {
+ /* Make sure this is the first encounter, and mark for
+ walk-by-value. */
+ gcc_checking_assert (!TREE_VISITED (decls)
+ && !DECL_TEMPLATE_INFO (decls));
+ mark_by_value (decls);
+ }
+ tree_node (decls);
+ }
+ tree_node (NULL_TREE);
+
/* nonlocalized_vars is a middle-end thing. */
WT (t->block.subblocks);
WT (t->block.supercontext);
case BLOCK:
t->block.locus = state->read_location (*this);
t->block.end_locus = state->read_location (*this);
- t->block.vars = chained_decls ();
+
+ for (tree *chain = &t->block.vars;;)
+ if (tree decl = tree_node ())
+ {
+ /* For a deduplicated local type or enumerator, chain the
+ duplicate decl instead of the canonical in-TU decl. Seeing
+ a duplicate here means the containing function whose body
+ we're streaming in is a duplicate too, so we'll end up
+ discarding this BLOCK (and the rest of the duplicate function
+ body) anyway. */
+ decl = maybe_duplicate (decl);
+
+ if (!DECL_P (decl) || DECL_CHAIN (decl))
+ {
+ set_overrun ();
+ break;
+ }
+ *chain = decl;
+ chain = &DECL_CHAIN (decl);
+ }
+ else
+ break;
+
/* nonlocalized_vars is middle-end. */
RT (t->block.subblocks);
RT (t->block.supercontext);
}
}
+/* Encode into KEY the position of the local type (class or enum)
+ declaration DECL within FN. The position is encoded as the
+ index of the innermost BLOCK (numbered in BFS order) along with
+ the index within its BLOCK_VARS list. */
+
+void
+trees_out::key_local_type (merge_key& key, tree decl, tree fn)
+{
+ auto_vec<tree, 4> blocks;
+ blocks.quick_push (DECL_INITIAL (fn));
+ unsigned block_ix = 0;
+ while (block_ix != blocks.length ())
+ {
+ tree block = blocks[block_ix];
+ unsigned decl_ix = 0;
+ for (tree var = BLOCK_VARS (block); var; var = DECL_CHAIN (var))
+ {
+ if (TREE_CODE (var) != TYPE_DECL)
+ continue;
+ if (var == decl)
+ {
+ key.index = (block_ix << 10) | decl_ix;
+ return;
+ }
+ ++decl_ix;
+ }
+ for (tree sub = BLOCK_SUBBLOCKS (block); sub; sub = BLOCK_CHAIN (sub))
+ blocks.safe_push (sub);
+ ++block_ix;
+ }
+
+ /* Not-found value. */
+ key.index = 1023;
+}
+
+/* Look up the local type corresponding at the position encoded by
+ KEY within FN and named NAME. */
+
+tree
+trees_in::key_local_type (const merge_key& key, tree fn, tree name)
+{
+ if (!DECL_INITIAL (fn))
+ return NULL_TREE;
+
+ const unsigned block_pos = key.index >> 10;
+ const unsigned decl_pos = key.index & 1023;
+
+ if (decl_pos == 1023)
+ return NULL_TREE;
+
+ auto_vec<tree, 4> blocks;
+ blocks.quick_push (DECL_INITIAL (fn));
+ unsigned block_ix = 0;
+ while (block_ix != blocks.length ())
+ {
+ tree block = blocks[block_ix];
+ if (block_ix == block_pos)
+ {
+ unsigned decl_ix = 0;
+ for (tree var = BLOCK_VARS (block); var; var = DECL_CHAIN (var))
+ {
+ if (TREE_CODE (var) != TYPE_DECL)
+ continue;
+ /* Prefer using the identifier as the key for more robustness
+ to ODR violations, except for anonymous types since their
+ compiler-generated identifiers aren't stable. */
+ if (IDENTIFIER_ANON_P (name)
+ ? decl_ix == decl_pos
+ : DECL_NAME (var) == name)
+ return var;
+ ++decl_ix;
+ }
+ return NULL_TREE;
+ }
+ for (tree sub = BLOCK_SUBBLOCKS (block); sub; sub = BLOCK_CHAIN (sub))
+ blocks.safe_push (sub);
+ ++block_ix;
+ }
+
+ return NULL_TREE;
+}
+
/* DEP is the depset of some decl we're streaming by value. Determine
the merging behaviour. */
gcc_unreachable ();
case FUNCTION_DECL:
- // FIXME: This can occur for (a) voldemorty TYPE_DECLS
- // (which are returned from a function), or (b)
- // block-scope class definitions in template functions.
- // These are as unique as the containing function. While
- // on read-back we can discover if the CTX was a
- // duplicate, we don't have a mechanism to get from the
- // existing CTX to the existing version of this decl.
gcc_checking_assert
(DECL_IMPLICIT_TYPEDEF_P (STRIP_TEMPLATE (decl)));
- mk = MK_unique;
+ mk = MK_local_type;
break;
case RECORD_TYPE:
}
break;
+ case MK_local_type:
+ key_local_type (key, STRIP_TEMPLATE (decl), container);
+ break;
+
case MK_enum:
{
/* Anonymous enums are located by their first identifier,
break;
case FUNCTION_DECL:
- // FIXME: What about a voldemort? how do we find what it
- // duplicates? Do we have to number vmorts relative to
- // their containing function? But how would that work
- // when matching an in-TU declaration?
- kind = "unique";
+ gcc_checking_assert (mk == MK_local_type);
+ existing = key_local_type (key, container, name);
+ if (existing && inner != decl)
+ existing = TYPE_TI_TEMPLATE (TREE_TYPE (existing));
break;
case TYPE_DECL:
/* Just like duplicate_decls, presum the user knows what
they're doing in overriding a builtin. */
TREE_TYPE (existing) = TREE_TYPE (decl);
+ else if (decl_function_context (decl))
+ /* The type of a mergeable local entity (such as a function scope
+ capturing lambda's closure type fields) can depend on an
+ unmergeable local entity (such as a local variable), so type
+ equality isn't feasible in general for local entities. */;
else
{
// FIXME:QOI Might be template specialization from a module,
uintptr_t &slot = duplicates->get_or_insert (existing, &existed);
gcc_checking_assert (!existed);
slot = reinterpret_cast<uintptr_t> (decl);
+
+ if (TREE_CODE (decl) == TEMPLATE_DECL)
+ /* Also register the DECL_TEMPLATE_RESULT as a duplicate so
+ that passing decl's _RESULT to maybe_duplicate naturally
+ gives us existing's _RESULT back. */
+ register_duplicate (DECL_TEMPLATE_RESULT (decl),
+ DECL_TEMPLATE_RESULT (existing));
}
/* We've read a definition of MAYBE_EXISTING. If not a duplicate,
{
/* Both are bindings. Order by identifier hash. */
gcc_checking_assert (a->get_name () != b->get_name ());
- return (IDENTIFIER_HASH_VALUE (a->get_name ())
- < IDENTIFIER_HASH_VALUE (b->get_name ())
- ? -1 : +1);
+ hashval_t ah = IDENTIFIER_HASH_VALUE (a->get_name ());
+ hashval_t bh = IDENTIFIER_HASH_VALUE (b->get_name ());
+ return (ah == bh ? 0 : ah < bh ? -1 : +1);
}
/* They are the same decl. This can happen with two using decls