From patchwork Thu Dec 10 21:43:47 2015 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Patrick Palka X-Patchwork-Id: 555342 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from sourceware.org (server1.sourceware.org [209.132.180.131]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id A20161401DE for ; Fri, 11 Dec 2015 08:44:11 +1100 (AEDT) Authentication-Results: ozlabs.org; dkim=pass (1024-bit key; unprotected) header.d=gcc.gnu.org header.i=@gcc.gnu.org header.b=duh3FIH+; dkim-atps=neutral DomainKey-Signature: a=rsa-sha1; c=nofws; d=gcc.gnu.org; h=list-id :list-unsubscribe:list-archive:list-post:list-help:sender:from :to:cc:subject:date:message-id; q=dns; s=default; b=M7v3ygYmPz9r UssTYaqoq0dkBe496+0Ft+C9Y8oSVG6hBdqbY2W/0FVrkH64lmD25Ic69vSFhiRL IJqqcGhfrE21vymSn4FrJmPDO9MuzRRTxofPxNb1kN+Wex+EfpbxGYUXFMsN+hNw ef8+glXgqo6oddrnfsJ9lPzM8ST3MrA= DKIM-Signature: v=1; a=rsa-sha1; c=relaxed; d=gcc.gnu.org; h=list-id :list-unsubscribe:list-archive:list-post:list-help:sender:from :to:cc:subject:date:message-id; s=default; bh=34SrWsOzYnu8nnTmoY HqaOCUeFM=; b=duh3FIH+bOwSpR/8A2Woz4Vra7aeexi+rGx1fSKClE0JsXV8uH ky9Sf24Ee3SB+v+neFpe2srm8XEfEP47Ko8or7b49xHHSikYSCa39ZoL4+wYIXmS bdfdQv5fr0gCqv97UM9dkwD/JPJkg6mtf9of4oRRL0IxhOxqgqmRTa+RE= Received: (qmail 121765 invoked by alias); 10 Dec 2015 21:43:59 -0000 Mailing-List: contact gcc-patches-help@gcc.gnu.org; run by ezmlm Precedence: bulk List-Id: List-Unsubscribe: List-Archive: List-Post: List-Help: Sender: gcc-patches-owner@gcc.gnu.org Delivered-To: mailing list gcc-patches@gcc.gnu.org Received: (qmail 121746 invoked by uid 89); 10 Dec 2015 21:43:58 -0000 Authentication-Results: sourceware.org; auth=none X-Virus-Found: No X-Spam-SWARE-Status: No, score=-1.1 required=5.0 tests=AWL, BAYES_20, RCVD_IN_DNSWL_LOW autolearn=ham version=3.3.2 X-HELO: mail-qg0-f54.google.com Received: from mail-qg0-f54.google.com (HELO mail-qg0-f54.google.com) (209.85.192.54) by sourceware.org (qpsmtpd/0.93/v0.84-503-g423c35a) with (AES128-GCM-SHA256 encrypted) ESMTPS; Thu, 10 Dec 2015 21:43:56 +0000 Received: by qgeb1 with SMTP id b1so165281528qge.1 for ; Thu, 10 Dec 2015 13:43:53 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:from:to:cc:subject:date:message-id; bh=kZFxp/5nZtvitsdbzYWMNpUTNIIRRWwrI7CvQCZRkkE=; b=dPNpd3dGJwRLbMHazB+yRyzRQN1/Rw9LanGNtxaupG5ZjBsF3UWBO7tLOP0uwKjoMW zDiR9avnqiNcXskMfVBh2uCjn8haQiRuJPNrDeW+RFvQtWdQ74/EHDs1Ro8FpRh4YSkf Rgl8rNVoHiKS6ZzL9BVoaU1d8uEllWG9lp9k8kTLIoP8lNdJyTByhigmY9uTQZCRJdVy qjscvYih5ouIRXiUD8qfDd5sS1eEMhC7C/gdR96a/q18knC1CZBGsPiZx9mhJ+eBmOEN xIpyWxzqW7mj7XrwBWRe88MfEzFQkz2EsJiDECgawziLidi0K2PDsVqo2exxFG6MRDmD W1kA== X-Gm-Message-State: ALoCoQn7EJuYM0DYaecQt42fJoheb7AwEGMO7GDz7QuvJxFqGqoKhOTkLRqbvic8WsALWZij0HfR7oF70S7V6Fhzxz9JHADyWA== X-Received: by 10.55.217.149 with SMTP id q21mr18799038qkl.55.1449783833478; Thu, 10 Dec 2015 13:43:53 -0800 (PST) Received: from localhost.localdomain (ool-4353a8be.dyn.optonline.net. [67.83.168.190]) by smtp.gmail.com with ESMTPSA id 81sm6856769qhx.15.2015.12.10.13.43.52 (version=TLS1_2 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Thu, 10 Dec 2015 13:43:52 -0800 (PST) From: Patrick Palka To: gcc-patches@gcc.gnu.org Cc: jason@redhat.com, Patrick Palka Subject: [PATCH] Fix PR c++/21802 (two-stage name lookup fails for operators) Date: Thu, 10 Dec 2015 16:43:47 -0500 Message-Id: <1449783827-27797-1-git-send-email-patrick@parcs.ath.cx> This patch fixes name-lookup of operators in template definitions whose operands are non-dependent expressions, i.e. PR c++/21802 (and incidentally 53223). The approach that this patch takes is to detect when build_new_op() returns a call to an overloaded function and to store a call to this overload intothe template AST instead of storing the raw operator (an operator would be erroneously subject to overload resolution during instantiation). The new function build_min_non_dep_op_overload is the workhorse of the patch. It reconstructs the CALL_EXPR that would have been built had an explicit operator+, operator* etc call been used, i.e. had the overload gone through finish_call_expr() / build_new_method_call() instead of through build_new_op(). The parameter OVERLOAD of this new function is probably not strictly necessary -- one can probably just look at the CALL_EXPR_FN of the parameter NON_DEP to figure out the overload to use -- but since the requisite plumbing from build_new_op() already existed to conveniently get at the overload information I thought I might as well use it. I have also created a test case that hopefully exercises all the changes that were made and to verify that these operator calls are being built correctly. Does this approach seem adequate? Bootstrap and regtesting in progress on x86_64, OK to commit if testing succeeds? gcc/cp/ChangeLog: PR c++/21802 PR c++/53223 * cp-tree.h (build_min_non_dep_op_overload): Declare. * tree.c (build_min_non_dep_op_overload): Define. * typeck.c (build_x_indirect_ref): Use build_min_non_dep_op_overload when the given expression has been resolved to an operator overload. (build_x_binary_op): Likewise. (build_x_array_ref): Likewise. (build_x_unary_op): Likewise. (build_x_compound_expr): Likewise. (build_x_modify_expr): Likewise. gcc/testsuite/ChangeLog: PR c++/21802 PR c++/53223 * g++.dg/cpp0x/pr53223.C: New test. * g++.dg/lookup/pr21802.C: New test. * g++.dg/lookup/two-stage4.C: Remove XFAIL. --- gcc/cp/cp-tree.h | 1 + gcc/cp/tree.c | 64 ++++++++ gcc/cp/typeck.c | 100 +++++++++--- gcc/testsuite/g++.dg/cpp0x/pr53223.C | 35 ++++ gcc/testsuite/g++.dg/lookup/pr21802.C | 271 +++++++++++++++++++++++++++++++ gcc/testsuite/g++.dg/lookup/two-stage4.C | 2 +- 6 files changed, 453 insertions(+), 20 deletions(-) create mode 100644 gcc/testsuite/g++.dg/cpp0x/pr53223.C create mode 100644 gcc/testsuite/g++.dg/lookup/pr21802.C diff --git a/gcc/cp/cp-tree.h b/gcc/cp/cp-tree.h index 6190f4e..3487d77 100644 --- a/gcc/cp/cp-tree.h +++ b/gcc/cp/cp-tree.h @@ -6513,6 +6513,7 @@ extern tree build_min (enum tree_code, tree, ...); extern tree build_min_nt_loc (location_t, enum tree_code, ...); extern tree build_min_non_dep (enum tree_code, tree, ...); +extern tree build_min_non_dep_op_overload (enum tree_code, tree, tree, ...); extern tree build_min_non_dep_call_vec (tree, tree, vec *); extern tree build_cplus_new (tree, tree, tsubst_flags_t); extern tree build_aggr_init_expr (tree, tree); diff --git a/gcc/cp/tree.c b/gcc/cp/tree.c index 5dad0a7..2635736 100644 --- a/gcc/cp/tree.c +++ b/gcc/cp/tree.c @@ -2744,6 +2744,70 @@ build_min_non_dep_call_vec (tree non_dep, tree fn, vec *argvec) return convert_from_reference (t); } +/* Similar to build_min_non_dep, but for expressions that have been resolved to + a call to an operator overload. OP is the operator that has been + overloaded. NON_DEP is the non-dependent expression that's been built, + which should be a CALL_EXPR or an INDIRECT_REF to a CALL_EXPR. OVERLOAD is + the overload that NON_DEP is calling. */ + +tree +build_min_non_dep_op_overload (enum tree_code op, + tree non_dep, + tree overload, ...) +{ + va_list p; + int nargs; + tree fn, call; + vec *args; + + if (REFERENCE_REF_P (non_dep)) + non_dep = TREE_OPERAND (non_dep, 0); + + nargs = call_expr_nargs (non_dep); + + if (op == PREINCREMENT_EXPR + || op == PREDECREMENT_EXPR) + gcc_assert (nargs == 1); + else if (op == MODOP_EXPR) + gcc_assert (nargs == 2); + else + gcc_assert (nargs == TREE_CODE_LENGTH (op)); + + args = make_tree_vector (); + va_start (p, overload); + + if (TREE_CODE (TREE_TYPE (overload)) == FUNCTION_TYPE) + { + fn = overload; + for (int i = 0; i < nargs; i++) + { + tree arg = va_arg (p, tree); + vec_safe_push (args, arg); + } + } + else if (TREE_CODE (TREE_TYPE (overload)) == METHOD_TYPE) + { + tree object = va_arg (p, tree); + tree binfo = TYPE_BINFO (TREE_TYPE (object)); + tree method = build_baselink (binfo, binfo, overload, NULL_TREE); + fn = build_min (COMPONENT_REF, TREE_TYPE (overload), + object, method, NULL_TREE); + for (int i = 1; i < nargs; i++) + { + tree arg = va_arg (p, tree); + vec_safe_push (args, arg); + } + } + else + gcc_unreachable (); + + va_end (p); + call = build_min_non_dep_call_vec (non_dep, fn, args); + release_tree_vector (args); + + return call; +} + tree get_type_decl (tree t) { diff --git a/gcc/cp/typeck.c b/gcc/cp/typeck.c index 17671ee..2e5e46e 100644 --- a/gcc/cp/typeck.c +++ b/gcc/cp/typeck.c @@ -2905,6 +2905,7 @@ build_x_indirect_ref (location_t loc, tree expr, ref_operator errorstring, { tree orig_expr = expr; tree rval; + tree overload = NULL_TREE; if (processing_template_decl) { @@ -2917,12 +2918,18 @@ build_x_indirect_ref (location_t loc, tree expr, ref_operator errorstring, } rval = build_new_op (loc, INDIRECT_REF, LOOKUP_NORMAL, expr, - NULL_TREE, NULL_TREE, /*overload=*/NULL, complain); + NULL_TREE, NULL_TREE, &overload, complain); if (!rval) rval = cp_build_indirect_ref (expr, errorstring, complain); if (processing_template_decl && rval != error_mark_node) - return build_min_non_dep (INDIRECT_REF, rval, orig_expr); + { + if (overload != NULL_TREE) + return (build_min_non_dep_op_overload + (INDIRECT_REF, rval, overload, orig_expr)); + + return build_min_non_dep (INDIRECT_REF, rval, orig_expr); + } else return rval; } @@ -3814,12 +3821,13 @@ convert_arguments (tree typelist, vec **values, tree fndecl, tree build_x_binary_op (location_t loc, enum tree_code code, tree arg1, enum tree_code arg1_code, tree arg2, - enum tree_code arg2_code, tree *overload, + enum tree_code arg2_code, tree *overload_p, tsubst_flags_t complain) { tree orig_arg1; tree orig_arg2; tree expr; + tree overload = NULL_TREE; orig_arg1 = arg1; orig_arg2 = arg2; @@ -3837,7 +3845,10 @@ build_x_binary_op (location_t loc, enum tree_code code, tree arg1, expr = build_m_component_ref (arg1, arg2, complain); else expr = build_new_op (loc, code, LOOKUP_NORMAL, arg1, arg2, NULL_TREE, - overload, complain); + &overload, complain); + + if (overload_p != NULL) + *overload_p = overload; /* Check for cases such as x+y< +void func(T t) +{ + A x; + auto &&g1 = x.good(); + auto &&g2 = x.operator*(); + auto &&error1 = *x; // { dg-bogus "invalid initialization" } + auto &&error2 = ++x; // { dg-bogus "invalid initialization" } + auto &&error3 = --x; // { dg-bogus "invalid initialization" } +} + +void func2(int) +{ + A x; + auto &&g = *x; +} + +int main() +{ + func(0); + func2(0); +} + diff --git a/gcc/testsuite/g++.dg/lookup/pr21802.C b/gcc/testsuite/g++.dg/lookup/pr21802.C new file mode 100644 index 0000000..4909bad --- /dev/null +++ b/gcc/testsuite/g++.dg/lookup/pr21802.C @@ -0,0 +1,271 @@ +// PR c++/21802 +// { dg-do run } +#include + +struct X; +int I = 6; + +/* A mostly exhaustive and ad-hoc assortment of operator overloads and calls + thereof, to stress-test two-stage name lookup of operators inside template + definitions and then to verify that the calls get built correctly. */ + +inline int operator+(const X &, int x) { return x; } +inline int operator-(const X &, int x) { return x; } +inline int operator*(const X &, int x) { return x; } +inline int operator/(const X &, int x) { return x; } +inline int operator+=(const X &, int x) { return x; } + +struct X +{ + X () : m (1) { } + int operator%(int x) { return m + x; } + virtual int operator>>(int x) { return m + x; } + int operator<<(int x) { return m + x; } + int operator&(int x) { return m + x; } + int operator|(int x) { return m + x; } + int operator^(int x) { return m + x; } + int operator&&(int x) { return m + x; } + int operator||(int x) { return m + x; } + int operator==(int x) { return m + x; } + int operator!=(int x) { return m + x; } + int operator<(int x) { return m + x; } + int operator<=(int x) { return m + x; } + int operator>(int x) { return m + x; } + int operator>=(int x) { return m + x; } + int operator*() { return m + I; } + int operator!() { return m + I; } + int operator~() { return m + I; } + int operator++() { return m + I + 100; } + int operator--() { return m + I + 100; } + int operator++(int) { return m + I; } + int operator--(int) { return m + I; } + int operator()() { return m + I; } + int operator,(int x) { return m + x; } + int operator[](int x) { return m + x; } + int operator*=(int x) { return m + x; } + int operator-=(int x) { return m + x; } + int operator/=(int x) { return m + x; } + virtual int operator& () { return m + I; } + int m; +}; +struct Y : virtual X +{ + /* Virtual override. */ + int operator>>(int x) { return m + x + 1; } + int operator& () { return m + I + 1; } + + /* Not virtual. */ + int operator&(int x) { return m + x + 1; } +}; + +/* The folloiwng "FooN" functions each contain a different way to call and to + resolve these operator overloads. */ + +template +void +Foo1 (T) +{ + Y x; + { int t = x + I; assert (t == 6); } + { int t = x - I; assert (t == 6); } + { int t = x * I; assert (t == 6); } + { int t = x / I; assert (t == 6); } + { int t = (x+=I); assert (t == 6); } + + { int t = x % I; assert (t == 7); } + { int t = x << I; assert (t == 7); } + { int t = x | I; assert (t == 7); } + { int t = x && I; assert (t == 7); } + { int t = x || I; assert (t == 7); } + { int t = x == I; assert (t == 7); } + { int t = x != I; assert (t == 7); } + { int t = x < I; assert (t == 7); } + { int t = x <= I; assert (t == 7); } + { int t = x > I; assert (t == 7); } + { int t = x >= I; assert (t == 7); } + { int t = *x; assert (t == 7); } + { int t = !x; assert (t == 7); } + { int t = ~x; assert (t == 7); } + { int t = x++; assert (t == 7); } + { int t = x--; assert (t == 7); } + { int t = ++x; assert (t == 107); } + { int t = --x; assert (t == 107); } + { int t = x (); assert (t == 7); } + { int t = (x, I); assert (t == 7); } + { int t = x[I]; assert (t == 7); } + { int t = (x-=I); assert (t == 7); } + { int t = (x/=I); assert (t == 7); } + { int t = (x*=I); assert (t == 7); } + + { int t = x >> I; assert (t == 8); } + { int t = x & I; assert (t == 8); } + { int t = &x; assert (t == 8); } +} + +template +void +Foo2 (T) +{ + X x; + { int t = x + I; assert (t == 6); } + { int t = x - I; assert (t == 6); } + { int t = x * I; assert (t == 6); } + { int t = x / I; assert (t == 6); } + { int t = (x+=I); assert (t == 6); } + + { int t = x % I; assert (t == 7); } + { int t = x >> I; assert (t == 7); } + { int t = x << I; assert (t == 7); } + { int t = x | I; assert (t == 7); } + { int t = x && I; assert (t == 7); } + { int t = x || I; assert (t == 7); } + { int t = x == I; assert (t == 7); } + { int t = x != I; assert (t == 7); } + { int t = x < I; assert (t == 7); } + { int t = x <= I; assert (t == 7); } + { int t = x > I; assert (t == 7); } + { int t = x >= I; assert (t == 7); } + { int t = *x; assert (t == 7); } + { int t = !x; assert (t == 7); } + { int t = ~x; assert (t == 7); } + { int t = x++; assert (t == 7); } + { int t = x--; assert (t == 7); } + { int t = ++x; assert (t == 107); } + { int t = --x; assert (t == 107); } + { int t = x (); assert (t == 7); } + { int t = (x, I); assert (t == 7); } + { int t = x[I]; assert (t == 7); } + { int t = &x; assert (t == 7); } + { int t = (x-=I); assert (t == 7); } + { int t = (x/=I); assert (t == 7); } + { int t = (x*=I); assert (t == 7); } + { int t = x & I; assert (t == 7); } +} + +template +void +Foo3 (T) +{ + Y o; + X &x = o; + { int t = x + I; assert (t == 6); } + { int t = x - I; assert (t == 6); } + { int t = x * I; assert (t == 6); } + { int t = x / I; assert (t == 6); } + { int t = (x+=I); assert (t == 6); } + + { int t = x % I; assert (t == 7); } + { int t = x << I; assert (t == 7); } + { int t = x | I; assert (t == 7); } + { int t = x && I; assert (t == 7); } + { int t = x || I; assert (t == 7); } + { int t = x == I; assert (t == 7); } + { int t = x != I; assert (t == 7); } + { int t = x < I; assert (t == 7); } + { int t = x <= I; assert (t == 7); } + { int t = x > I; assert (t == 7); } + { int t = x >= I; assert (t == 7); } + { int t = *x; assert (t == 7); } + { int t = !x; assert (t == 7); } + { int t = ~x; assert (t == 7); } + { int t = x++; assert (t == 7); } + { int t = x--; assert (t == 7); } + { int t = ++x; assert (t == 107); } + { int t = --x; assert (t == 107); } + { int t = x (); assert (t == 7); } + { int t = (x, I); assert (t == 7); } + { int t = x[I]; assert (t == 7); } + { int t = (x-=I); assert (t == 7); } + { int t = (x/=I); assert (t == 7); } + { int t = (x*=I); assert (t == 7); } + + { int t = x & I; assert (t == 7); } + { int t = x >> I; assert (t == 8); } + { int t = &x; assert (t == 8); } +} + +template +void +Foo4 (T) +{ + Y x; + { int t = operator+ (x, I); assert (t == 6); } + { int t = operator- (x, I); assert (t == 6); } + { int t = operator* (x, I); assert (t == 6); } + { int t = operator/ (x, I); assert (t == 6); } + { int t = operator+= (x, I); assert (t == 6); } + + { int t = x.operator% (I); assert (t == 7); } + { int t = x.operator<< (I); assert (t == 7); } + { int t = x.operator| (I); assert (t == 7); } + { int t = x.operator&& (I); assert (t == 7); } + { int t = x.operator|| (I); assert (t == 7); } + { int t = x.operator== (I); assert (t == 7); } + { int t = x.operator!= (I); assert (t == 7); } + { int t = x.operator< (I); assert (t == 7); } + { int t = x.operator<= (I); assert (t == 7); } + { int t = x.operator> (I); assert (t == 7); } + { int t = x.operator>= (I); assert (t == 7); } + { int t = x.operator* (); assert (t == 7); } + { int t = x.operator! (); assert (t == 7); } + { int t = x.operator~ (); assert (t == 7); } + { int t = x.operator++ (0); assert (t == 7); } + { int t = x.operator-- (0); assert (t == 7); } + { int t = x.operator++ (); assert (t == 107); } + { int t = x.operator-- (); assert (t == 107); } + { int t = x.operator() (); assert (t == 7); } + { int t = x.operator, (I); assert (t == 7); } + { int t = x.operator[] (I); assert (t == 7); } + { int t = x.operator-= (I); assert (t == 7); } + { int t = x.operator/= (I); assert (t == 7); } + { int t = x.operator*= (I); assert (t == 7); } + + { int t = x.operator>> (I); assert (t == 8); } + { int t = x.operator& (); assert (t == 8); } + { int t = x.operator& (I); assert (t == 8); } +} + + +/* These definitions should be irrelevant to operator lookup of non-dependent + expressions inside the above templates since they are not in scope at + template-definition time (but are in scope at instantiation time). */ +inline int operator+(const Y&, int) { return 11; } +inline int operator-(const Y&, int) { return 11; } +inline int operator*(const Y&, int) { return 11; } +inline int operator/(const Y&, int) { return 11; } +inline int operator%(const Y&, int) { return 11; } +inline int operator>>(const Y&, int) { return 11; } +inline int operator<<(const Y&, int) { return 11; } +inline int operator&(const Y&, int) { return 11; } +inline int operator|(const Y&, int) { return 11; } +inline int operator^(const Y&, int) { return 11; } +inline int operator&&(const Y&, int) { return 11; } +inline int operator||(const Y&, int) { return 11; } +inline int operator==(const Y&, int) { return 11; } +inline int operator!=(const Y&, int) { return 11; } +inline int operator<(const Y&, int) { return 11; } +inline int operator<=(const Y&, int) { return 11; } +inline int operator>(const Y&, int) { return 11; } +inline int operator>=(const Y&, int) { return 11; } +inline int operator*(const Y&) { return 11; } +inline int operator!(const Y&) { return 11; } +inline int operator~(const Y&) { return 11; } +inline int operator++(const Y&) { return 11; } +inline int operator--(const Y&) { return 11; } +inline int operator++(const Y&, int) { return 11; } +inline int operator--(const Y&, int) { return 11; } +inline int operator,(const Y&, int) { return 11; } +inline int operator&(const Y&) { return 11; } +inline int operator+=(const Y&, int x) { return 11; } +inline int operator*=(const Y&, int x) { return 11; } +inline int operator-=(const Y&, int x) { return 11; } +inline int operator/=(const Y&, int x) { return 11; } + +int +main () +{ + Foo1 (0); + Foo2 (0); + Foo3 (0); + Foo4 (0); +} diff --git a/gcc/testsuite/g++.dg/lookup/two-stage4.C b/gcc/testsuite/g++.dg/lookup/two-stage4.C index 7d97109..a89e618 100644 --- a/gcc/testsuite/g++.dg/lookup/two-stage4.C +++ b/gcc/testsuite/g++.dg/lookup/two-stage4.C @@ -8,7 +8,7 @@ template bool operator==(wrap, wrap); template void g(T, wrap > x) { - bool b = x == x; // { dg-bogus "" "" { xfail *-*-* } } + bool b = x == x; // { dg-bogus "" "" } } template void operator==(wrap >, wrap >);