Skip to content

Commit

Permalink
PR c++/80891 (#1,#5)
Browse files Browse the repository at this point in the history
	* cp-tree.h (lookup_maybe_add): Add DEDUPING argument.
	* name-lookup.c (name_lookup): Add deduping field.
	(name_lookup::preserve_state, name_lookup::restore_state): Deal
	with deduping.
	(name_lookup::add_overload): New.
	(name_lookup::add_value, name_lookup::add_fns): Call add_overload.
	(name_lookup::search_adl): Set deduping.  Don't unmark here.
	* pt.c (most_specialized_instantiation): Revert previous change,
	Assert not given duplicates.
	* tree.c (lookup_mark): Just mark the underlying decls.
	(lookup_maybe_add): Dedup using marked decls.

	PR c++/80891 (#5)
	* g++.dg/lookup/pr80891-5.C: New.


git-svn-id: svn+ssh://gcc.gnu.org/svn/gcc/trunk@248578 138bc75d-0d04-0410-961f-82ee72b054a4
  • Loading branch information
nathan committed May 29, 2017
1 parent 18eaba0 commit 6fbf0ce
Show file tree
Hide file tree
Showing 7 changed files with 200 additions and 105 deletions.
13 changes: 13 additions & 0 deletions gcc/cp/ChangeLog
Original file line number Diff line number Diff line change
@@ -1,5 +1,18 @@
2017-05-29 Nathan Sidwell <nathan@acm.org>

PR c++/80891 (#1,#5)
* cp-tree.h (lookup_maybe_add): Add DEDUPING argument.
* name-lookup.c (name_lookup): Add deduping field.
(name_lookup::preserve_state, name_lookup::restore_state): Deal
with deduping.
(name_lookup::add_overload): New.
(name_lookup::add_value, name_lookup::add_fns): Call add_overload.
(name_lookup::search_adl): Set deduping. Don't unmark here.
* pt.c (most_specialized_instantiation): Revert previous change,
Assert not given duplicates.
* tree.c (lookup_mark): Just mark the underlying decls.
(lookup_maybe_add): Dedup using marked decls.

PR c++/80891 (#4)
* ptree.c (cxx_print_xnode): Show internal OVERLOAD structure.
* tree.c (ovl_insert, ovl_iterator_remove_node): Fix copying assert.
Expand Down
3 changes: 2 additions & 1 deletion gcc/cp/cp-tree.h
Original file line number Diff line number Diff line change
Expand Up @@ -6916,7 +6916,8 @@ extern tree ovl_insert (tree fn, tree maybe_ovl,
extern tree ovl_skip_hidden (tree) ATTRIBUTE_PURE;
extern void lookup_mark (tree lookup, bool val);
extern tree lookup_add (tree fns, tree lookup);
extern tree lookup_maybe_add (tree fns, tree lookup);
extern tree lookup_maybe_add (tree fns, tree lookup,
bool deduping);
extern void lookup_keep (tree lookup, bool keep);
extern int is_overloaded_fn (tree) ATTRIBUTE_PURE;
extern bool really_overloaded_fn (tree) ATTRIBUTE_PURE;
Expand Down
68 changes: 56 additions & 12 deletions gcc/cp/name-lookup.c
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,11 @@ static void set_identifier_type_value_with_scope (tree id, tree decl,
#define MAYBE_STAT_DECL(N) (STAT_HACK_P (N) ? STAT_DECL (N) : N)
#define MAYBE_STAT_TYPE(N) (STAT_HACK_P (N) ? STAT_TYPE (N) : NULL_TREE)

static tree stat_hack (tree decl = NULL_TREE, tree type = NULL_TREE)
/* Create a STAT_HACK node with DECL as the value binding and TYPE as
the type binding. */

static tree
stat_hack (tree decl = NULL_TREE, tree type = NULL_TREE)
{
tree result = make_node (OVERLOAD);

Expand Down Expand Up @@ -179,6 +183,8 @@ struct name_lookup
tree value; /* A (possibly ambiguous) set of things found. */
tree type; /* A type that has been found. */
int flags; /* Lookup flags. */
bool deduping; /* Full deduping is needed because using declarations
are in play. */
vec<tree, va_heap, vl_embed> *scopes;
name_lookup *previous; /* Previously active lookup. */

Expand All @@ -191,7 +197,7 @@ struct name_lookup
public:
name_lookup (tree n, int f = 0)
: name (n), value (NULL_TREE), type (NULL_TREE), flags (f),
scopes (NULL), previous (NULL)
deduping (false), scopes (NULL), previous (NULL)
{
preserve_state ();
}
Expand Down Expand Up @@ -235,6 +241,7 @@ struct name_lookup

private:
static tree ambiguous (tree thing, tree current);
void add_overload (tree fns);
void add_value (tree new_val);
void add_type (tree new_type);
bool process_binding (tree val_bind, tree type_bind);
Expand Down Expand Up @@ -321,7 +328,8 @@ name_lookup::preserve_state ()
}

/* Unmark the outer partial lookup. */
lookup_mark (previous->value, false);
if (previous->deduping)
lookup_mark (previous->value, false);
}
else
scopes = shared_scopes;
Expand All @@ -333,6 +341,9 @@ name_lookup::preserve_state ()
void
name_lookup::restore_state ()
{
if (deduping)
lookup_mark (value, false);

/* Unmark and empty this lookup's scope stack. */
for (unsigned ix = vec_safe_length (scopes); ix--;)
{
Expand Down Expand Up @@ -371,7 +382,8 @@ name_lookup::restore_state ()
}

/* Remark the outer partial lookup. */
lookup_mark (previous->value, true);
if (previous->deduping)
lookup_mark (previous->value, true);
}
else
shared_scopes = scopes;
Expand Down Expand Up @@ -415,23 +427,53 @@ name_lookup::ambiguous (tree thing, tree current)
return current;
}

/* FNS is a new overload set to add to the exising set. */

void
name_lookup::add_overload (tree fns)
{
if (!deduping && TREE_CODE (fns) == OVERLOAD)
{
tree probe = fns;
if (flags & LOOKUP_HIDDEN)
probe = ovl_skip_hidden (probe);
if (probe && TREE_CODE (probe) == OVERLOAD && OVL_USING_P (probe))
{
/* We're about to add something found by a using
declaration, so need to engage deduping mode. */
lookup_mark (value, true);
deduping = true;
}
}

value = lookup_maybe_add (fns, value, deduping);
}

/* Add a NEW_VAL, a found value binding into the current value binding. */

void
name_lookup::add_value (tree new_val)
{
if (!value)
if (OVL_P (new_val) && (!value || OVL_P (value)))
add_overload (new_val);
else if (!value)
value = new_val;
else if (value == new_val)
;
else if ((TREE_CODE (value) == TYPE_DECL
&& TREE_CODE (new_val) == TYPE_DECL
&& same_type_p (TREE_TYPE (value), TREE_TYPE (new_val))))
;
else if (OVL_P (value) && OVL_P (new_val))
value = lookup_add (new_val, value);
else
value = ambiguous (new_val, value);
{
if (deduping)
{
/* Disengage deduping mode. */
lookup_mark (value, false);
deduping = false;
}
value = ambiguous (new_val, value);
}
}

/* Add a NEW_TYPE, a found type binding into the current type binding. */
Expand Down Expand Up @@ -703,8 +745,7 @@ name_lookup::add_fns (tree fns)
else if (!DECL_DECLARES_FUNCTION_P (fns))
return;

/* Only add those that aren't already there. */
value = lookup_maybe_add (fns, value);
add_overload (fns);
}

/* Add functions of a namespace to the lookup structure. */
Expand Down Expand Up @@ -1004,7 +1045,11 @@ name_lookup::adl_template_arg (tree arg)
tree
name_lookup::search_adl (tree fns, vec<tree, va_gc> *args)
{
lookup_mark (fns, true);
if (fns)
{
deduping = true;
lookup_mark (fns, true);
}
value = fns;

unsigned ix;
Expand All @@ -1019,7 +1064,6 @@ name_lookup::search_adl (tree fns, vec<tree, va_gc> *args)
adl_expr (arg);

fns = value;
lookup_mark (fns, false);

return fns;
}
Expand Down
42 changes: 21 additions & 21 deletions gcc/cp/pt.c
Original file line number Diff line number Diff line change
Expand Up @@ -21728,32 +21728,32 @@ most_specialized_instantiation (tree templates)

champ = templates;
for (fn = TREE_CHAIN (templates); fn; fn = TREE_CHAIN (fn))
if (TREE_VALUE (champ) != TREE_VALUE (fn))
{
int fate = more_specialized_inst (TREE_VALUE (champ), TREE_VALUE (fn));
if (fate == -1)
{
gcc_assert (TREE_VALUE (champ) != TREE_VALUE (fn));
int fate = more_specialized_inst (TREE_VALUE (champ), TREE_VALUE (fn));
if (fate == -1)
champ = fn;
else if (!fate)
{
/* Equally specialized, move to next function. If there
is no next function, nothing's most specialized. */
fn = TREE_CHAIN (fn);
champ = fn;
else if (!fate)
{
/* Equally specialized, move to next function. If there
is no next function, nothing's most specialized. */
fn = TREE_CHAIN (fn);
champ = fn;
if (!fn)
break;
}
}
if (!fn)
break;
}
}

if (champ)
/* Now verify that champ is better than everything earlier in the
instantiation list. */
for (fn = templates; fn != champ; fn = TREE_CHAIN (fn))
if (TREE_VALUE (champ) != TREE_VALUE (fn)
&& more_specialized_inst (TREE_VALUE (champ), TREE_VALUE (fn)) != 1)
{
champ = NULL_TREE;
break;
}
for (fn = templates; fn != champ; fn = TREE_CHAIN (fn)) {
if (more_specialized_inst (TREE_VALUE (champ), TREE_VALUE (fn)) != 1)
{
champ = NULL_TREE;
break;
}
}

processing_template_decl--;

Expand Down
106 changes: 35 additions & 71 deletions gcc/cp/tree.c
Original file line number Diff line number Diff line change
Expand Up @@ -2293,21 +2293,10 @@ ovl_iterator::remove_node (tree overload, tree node)
void
lookup_mark (tree ovl, bool val)
{
/* For every node that is a lookup, mark the thing it points to. */
for (; ovl && TREE_CODE (ovl) == OVERLOAD && OVL_LOOKUP_P (ovl);
ovl = OVL_CHAIN (ovl))
{
tree targ = OVL_FUNCTION (ovl);
gcc_checking_assert (LOOKUP_SEEN_P (targ) != val);
LOOKUP_SEEN_P (targ) = val;
}

if (ovl && (TREE_CODE (ovl) == OVERLOAD ||
TREE_CODE (ovl) == FUNCTION_DECL))
for (lkp_iterator iter (ovl); iter; ++iter)
{
/* Mark the overload itsef. */
gcc_checking_assert (LOOKUP_SEEN_P (ovl) != val);
LOOKUP_SEEN_P (ovl) = val;
gcc_checking_assert (LOOKUP_SEEN_P (*iter) != val);
LOOKUP_SEEN_P (*iter) = val;
}
}

Expand All @@ -2327,73 +2316,48 @@ lookup_add (tree fns, tree lookup)
return lookup;
}

/* FNS is a new overload set, add it to LOOKUP, if it is not already
present there. */
/* FNS is a new overload set, add them to LOOKUP, if they are not
already present there. */

tree
lookup_maybe_add (tree fns, tree lookup)
lookup_maybe_add (tree fns, tree lookup, bool deduping)
{
if (LOOKUP_SEEN_P (fns))
return lookup;

if (lookup && TREE_CODE (fns) == OVERLOAD)
{
/* Determine if we already have some part of this overload in
the overload set. If so fix things up so we only have the
overload set once. */
tree marked = NULL_TREE;
if (deduping)
for (tree next, probe = fns; probe; probe = next)
{
tree fn = probe;
next = NULL_TREE;

for (tree probe = fns; probe; probe = OVL_CHAIN (probe))
if (LOOKUP_SEEN_P (probe))
if (TREE_CODE (probe) == OVERLOAD)
{
marked = probe;
break;
fn = OVL_FUNCTION (probe);
next = OVL_CHAIN (probe);
}
else if (TREE_CODE (probe) != OVERLOAD)
break;

if (marked)
{
/* The tail of this overload is already in the lookup
set. Stitch out the tail case, which might involve
copying. */
bool rewrite = false;

LOOKUP_SEEN_P (marked) = false;
for (tree *prev = &lookup, probe = *prev;
; prev = &OVL_CHAIN (probe), probe = *prev)
{
if (probe == marked)
{
*prev = NULL_TREE;
break;
}
gcc_checking_assert (OVL_LOOKUP_P (probe));
if (marked == OVL_FUNCTION (probe))
{
*prev = OVL_CHAIN (probe);
break;
}
if (!LOOKUP_SEEN_P (fn))
LOOKUP_SEEN_P (fn) = true;
else
{
/* This function was already seen. Insert all the
predecessors onto the lookup. */
for (; fns != probe; fns = OVL_CHAIN (fns))
{
lookup = lookup_add (OVL_FUNCTION (fns), lookup);
/* Propagate OVL_USING, but OVL_HIDDEN doesn't matter. */
if (OVL_USING_P (fns))
OVL_USING_P (lookup) = true;
}

/* If we're in a used part of the lookup set, copy the
node, so as to not disturb stored uses. */
gcc_checking_assert (!rewrite || OVL_USED_P (probe));
if (OVL_USED_P (probe))
{
rewrite = true;
probe = ovl_copy (probe);
OVL_LOOKUP_P (probe) = true;
*prev = probe;
}
}
}
}
/* And now skip this function. */
fns = next;
}
}

/* Finally mark the new overload and prepend it to the current
lookup. */
LOOKUP_SEEN_P (fns) = true;
if (fns)
/* We ended in a set of new functions. Add them all in one go. */
lookup = lookup_add (fns, lookup);

return lookup_add (fns, lookup);
return lookup;
}

/* Regular overload OVL is part of a kept lookup. Mark the nodes on
Expand Down
5 changes: 5 additions & 0 deletions gcc/testsuite/ChangeLog
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
2017-05-29 Nathan Sidwell <nathan@acm.org>

PR c++/80891 (#5)
* g++.dg/lookup/pr80891-5.C: New.

2017-05-29 Jerry DeLisle <jvdelisle@gcc.gnu.org>

PR libgfortran/53029
Expand Down
Loading

0 comments on commit 6fbf0ce

Please sign in to comment.