public inbox for gdb-patches@sourceware.org
 help / color / mirror / Atom feed
From: Siva Chandra <sivachandra@google.com>
To: gdb-patches <gdb-patches@sourceware.org>
Subject: Re: [PATCH] Enable invoking overloaded operator() method on class and union values.
Date: Mon, 01 Dec 2014 08:32:00 -0000	[thread overview]
Message-ID: <CAGyQ6gxfgW_urK0BC4o0NVZJ9OT5P4oFGHRKYG-tpXS4wZn7Lw@mail.gmail.com> (raw)
In-Reply-To: <CAGyQ6gxeuscNqvqgrt=Secw5C+dqyLyXebm_GuNgpjZZYddRgQ@mail.gmail.com>

[-- Attachment #1: Type: text/plain, Size: 893 bytes --]

On Thu, Nov 20, 2014 at 8:56 AM, Siva Chandra <sivachandra@google.com> wrote:
> Hello,
>
> The attached patch enables invoking overloaded operator() method on
> class and union values when evaluating expressions.
>
> gdb/ChangeLog:
>
> 2014-11-20  Siva Chandra Reddy  <sivachandra@google.com>
>
>         * eval.c (evaluate_subexp_standard): Extend handling of
>         OP_FUNCALL operation to enable invoking overloaded
>         operator() methods.
>
> gdb/testsuite/ChangeLog:
>
> 2014-11-20  Siva Chandra Reddy  <sivachandra@google.com>
>
>         * gdb.cp/member-ptr.exp: Modify expected pattern of
>         "print diamond.*diamond_pfunc_ptr (20)" and
>         "print diamond.*left_vpmf ()".
>         * gdb.cp/paren-op.cc: New file.
>         * gdb.cp/paren-op.exp: New file.
>
> Regression tested on x86_64 GNU/Linux.
>
> Thank you,
> Siva Chandra

Ping. Rebased patch is attached.

[-- Attachment #2: paren_op_v1.txt --]
[-- Type: text/plain, Size: 7035 bytes --]

diff --git a/gdb/eval.c b/gdb/eval.c
index c2ab879..fb9ed75 100644
--- a/gdb/eval.c
+++ b/gdb/eval.c
@@ -707,6 +707,7 @@ evaluate_subexp_standard (struct type *expect_type,
   struct type *type;
   int nargs;
   struct value **argvec;
+  struct type *callable_type = NULL;
   int code;
   int ix;
   long mem_offset;
@@ -1524,21 +1525,48 @@ evaluate_subexp_standard (struct type *expect_type,
         }
       else
 	{
-	  /* Non-method function call.  */
+	  /* It could either be a normal function call, or an overloaded
+	     operator() call.  */
+	  struct value *callable_value;
+	  int fptr = 0, struct_or_union = 0, internal_func = 0;
 	  save_pos1 = *pos;
 	  tem = 1;
 
-	  /* If this is a C++ function wait until overload resolution.  */
-	  if (op == OP_VAR_VALUE
-	      && overload_resolution
-	      && (exp->language_defn->la_language == language_cplus))
+	  callable_value = evaluate_subexp_with_coercion (exp, pos, noside);
+	  callable_type = check_typedef (value_type (callable_value));
+	  if (TYPE_CODE (callable_type) == TYPE_CODE_PTR)
 	    {
-	      (*pos) += 4; /* Skip the evaluation of the symbol.  */
+	      callable_type = check_typedef (TYPE_TARGET_TYPE (callable_type));
+	      if (TYPE_CODE (callable_type) != TYPE_CODE_FUNC)
+		error (_("Operand to '()' operator is not a callable."));
+	      fptr = 1;
+	    }
+	  else if (exp->language_defn->la_language == language_cplus
+		   && class_or_union_p (callable_type))
+	    struct_or_union = 1;  /* There could be an operator() method.  */
+	  else if (TYPE_CODE (callable_type) == TYPE_CODE_INTERNAL_FUNCTION)
+	    internal_func = 1;
+	  else if (TYPE_CODE (callable_type) == TYPE_CODE_FUNC)
+	    ;  /* Normal function call.  */
+	  else
+	    error (_("Operand to '()' operator is not a callable."));
+
+	  /* If this is C++, wait until overload resolution.  */
+	  if (!fptr && !internal_func
+	      && exp->language_defn->la_language == language_cplus
+	      && (overload_resolution || struct_or_union))
+	    {
+	      if (struct_or_union)
+		{
+		  arg2 = value_addr (callable_value);
+		  nargs++;
+		  tem = 2;
+		}
 	      argvec[0] = NULL;
 	    }
 	  else
 	    {
-	      argvec[0] = evaluate_subexp_with_coercion (exp, pos, noside);
+	      argvec[0] = callable_value;
 	      type = value_type (argvec[0]);
 	      if (type && TYPE_CODE (type) == TYPE_CODE_PTR)
 		type = TYPE_TARGET_TYPE (type);
@@ -1593,10 +1621,11 @@ evaluate_subexp_standard (struct type *expect_type,
         }
 
       if (op == STRUCTOP_STRUCT || op == STRUCTOP_PTR
-	  || (op == OP_SCOPE && function_name != NULL))
+	  || (op == OP_SCOPE && function_name != NULL)
+	  || (callable_type != NULL && class_or_union_p (callable_type)))
 	{
 	  int static_memfuncp;
-	  char *tstr;
+	  const char *tstr;
 
 	  /* Method invocation: stuff "this" as first parameter.
 	     If the method turns out to be static we undo this below.  */
@@ -1604,8 +1633,13 @@ evaluate_subexp_standard (struct type *expect_type,
 
 	  if (op != OP_SCOPE)
 	    {
-	      /* Name of method from expression.  */
-	      tstr = &exp->elts[pc2 + 2].string;
+	      if (callable_type == NULL)
+		{
+		  /* Name of method from expression.  */
+		  tstr = &exp->elts[pc2 + 2].string;
+		}
+	      else
+		tstr = "operator()";
 	    }
 	  else
 	    tstr = function_name;
diff --git a/gdb/testsuite/gdb.cp/member-ptr.exp b/gdb/testsuite/gdb.cp/member-ptr.exp
index c13b852..649fe89 100644
--- a/gdb/testsuite/gdb.cp/member-ptr.exp
+++ b/gdb/testsuite/gdb.cp/member-ptr.exp
@@ -389,7 +389,7 @@ gdb_test "ptype diamond.*diamond_pfunc_ptr" \
 # call the member pointer as a normal pointer-to-function.
 
 gdb_test "print diamond.*diamond_pfunc_ptr (20)" \
-    "Invalid data type for function to be called."
+    "Operand to '\\(\\)' operator is not a callable\\."
 
 # With parentheses, it is valid.
 
@@ -665,7 +665,7 @@ gdb_test "print base_vpmf" \
 
 # Make sure we parse this correctly; it's invalid.
 gdb_test "print diamond.*left_vpmf ()" \
-    "Invalid data type for function to be called\\."
+    "Operand to '\\(\\)' operator is not a callable\\."
 
 # NULL pointer to member tests.
 gdb_test "print null_pmi" "$vhn = NULL"
diff --git a/gdb/testsuite/gdb.cp/paren-op.cc b/gdb/testsuite/gdb.cp/paren-op.cc
new file mode 100644
index 0000000..89a085a
--- /dev/null
+++ b/gdb/testsuite/gdb.cp/paren-op.cc
@@ -0,0 +1,60 @@
+/* This testcase is part of GDB, the GNU debugger.
+
+   Copyright 2014 Free Software Foundation, Inc.
+
+   This program is free software; you can redistribute it and/or modify
+   it under the terms of the GNU General Public License as published by
+   the Free Software Foundation; either version 3 of the License, or
+   (at your option) any later version.
+
+   This program is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+   GNU General Public License for more details.
+
+   You should have received a copy of the GNU General Public License
+   along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
+
+struct S
+{
+  int operator() (int x);
+};
+
+int
+S::operator() (int x)
+{
+  return x + 5;
+}
+
+int
+s (int a, int b)
+{
+  return a + b;
+}
+
+int (*s1_ptr) (int, int) = &s;
+
+int
+s (int a)
+{
+  return a * a;
+}
+
+union U
+{
+  int operator () (int x);
+};
+
+int
+U::operator() (int x)
+{
+  return x + 10;
+}
+
+int main () {
+  S s;
+  U u;
+  int i = 10;
+
+  return 0;  /* Break here  */
+}
diff --git a/gdb/testsuite/gdb.cp/paren-op.exp b/gdb/testsuite/gdb.cp/paren-op.exp
new file mode 100644
index 0000000..152b8c9
--- /dev/null
+++ b/gdb/testsuite/gdb.cp/paren-op.exp
@@ -0,0 +1,37 @@
+# Copyright 2014 Free Software Foundation, Inc.
+
+# This program is free software; you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation; either version 3 of the License, or
+# (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program.  If not, see <http://www.gnu.org/licenses/>.
+
+# This file is part of the gdb testsuite
+
+if {[skip_cplus_tests]} { continue }
+
+standard_testfile .cc
+
+if {[prepare_for_testing $testfile.exp $testfile $srcfile \
+    {debug c++ additional_flags=-std=c++11}]} {
+    return -1
+}
+
+if {![runto_main]} {
+    return -1
+}
+
+gdb_breakpoint [gdb_get_line_number "Break here"]
+gdb_continue_to_breakpoint "Break here"
+
+gdb_test "p s(12340)" ".* = 12345" "p s()"
+gdb_test "p u(446)" ".* = 456" "p u()"
+gdb_test "p i(789)" "Operand to '\\(\\)' operator is not a callable\\." "p i()"
+gdb_test "p s1_ptr(500, 67)" ".* = 567" "p s1_ptr()"

  parent reply	other threads:[~2014-12-01  8:32 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2014-11-20 16:56 Siva Chandra
2014-11-20 17:02 ` Siva Chandra
2014-12-01  8:32 ` Siva Chandra [this message]
2015-01-07  6:26 ` Siva Chandra

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=CAGyQ6gxfgW_urK0BC4o0NVZJ9OT5P4oFGHRKYG-tpXS4wZn7Lw@mail.gmail.com \
    --to=sivachandra@google.com \
    --cc=gdb-patches@sourceware.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).