public inbox for gdb-patches@sourceware.org
 help / color / mirror / Atom feed
From: Hannes Domani <ssbssa@yahoo.de>
To: gdb-patches@sourceware.org
Subject: [PATCH] Allow calling of user-defined function call operators
Date: Sun, 21 Apr 2024 14:49:54 +0200	[thread overview]
Message-ID: <20240421124954.3285-1-ssbssa@yahoo.de> (raw)
In-Reply-To: <20240421124954.3285-1-ssbssa.ref@yahoo.de>

Currently it's not possible to call user-defined function call
operators, at least not without specifying operator() directly:
```
(gdb) l 1
1       struct S {
2         int operator() (int x) { return x + 5; }
3       };
4
5       int main () {
6         S s;
7
8         return s(23);
9       }
(gdb) p s(10)
Invalid data type for function to be called.
(gdb) p s.operator()(10)
$1 = 15
```

This now looks if an user-defined call operator is available when
trying to 'call' a struct value, and calls it instead, making this
possible:
```
(gdb) p s(10)
$1 = 15
```

Bug: https://sourceware.org/bugzilla/show_bug.cgi?id=12213
---
 gdb/eval.c                       | 46 +++++++++++++++++++++++++++-----
 gdb/testsuite/gdb.cp/userdef.cc  | 18 +++++++++++++
 gdb/testsuite/gdb.cp/userdef.exp |  4 +++
 3 files changed, 62 insertions(+), 6 deletions(-)

diff --git a/gdb/eval.c b/gdb/eval.c
index 6b752e70635..e737774ca28 100644
--- a/gdb/eval.c
+++ b/gdb/eval.c
@@ -664,14 +664,34 @@ operation::evaluate_funcall (struct type *expect_type,
 
   value *callee = evaluate_with_coercion (exp, noside);
   struct type *type = callee->type ();
-  if (type->code () == TYPE_CODE_PTR)
-    type = type->target_type ();
-  for (int i = 0; i < args.size (); ++i)
+
+  /* If the callee is a struct, there might be a user-defined function call
+     operator that should be used instead.  */
+  if (overload_resolution
+      && exp->language_defn->la_language == language_cplus
+      && check_typedef (type)->code () == TYPE_CODE_STRUCT)
     {
-      if (i < type->num_fields ())
-	vals[i] = args[i]->evaluate (type->field (i).type (), exp, noside);
-      else
+      for (int i = 0; i < args.size (); ++i)
 	vals[i] = args[i]->evaluate_with_coercion (exp, noside);
+
+      vals.insert (vals.begin(), value_addr (callee));
+      int static_memfuncp;
+      find_overload_match (vals, "operator()", METHOD, &vals[0], nullptr,
+			   &callee, nullptr, &static_memfuncp, 0, noside);
+      if (static_memfuncp)
+	vals.erase (vals.begin ());
+    }
+  else
+    {
+      if (type->code () == TYPE_CODE_PTR)
+	type = type->target_type ();
+      for (int i = 0; i < args.size (); ++i)
+	{
+	  if (i < type->num_fields ())
+	    vals[i] = args[i]->evaluate (type->field (i).type (), exp, noside);
+	  else
+	    vals[i] = args[i]->evaluate_with_coercion (exp, noside);
+	}
     }
 
   return evaluate_subexp_do_call (exp, noside, callee, vals,
@@ -702,6 +722,20 @@ var_value_operation::evaluate_funcall (struct type *expect_type,
   value *callee = evaluate_var_value (noside, std::get<0> (m_storage).block,
 				      symp);
 
+  /* If the callee is a struct, there might be a user-defined function call
+     operator that should be used instead.  */
+  if (overload_resolution
+      && exp->language_defn->la_language == language_cplus
+      && check_typedef (callee->type ())->code () == TYPE_CODE_STRUCT)
+    {
+      argvec.insert (argvec.begin(), value_addr (callee));
+      int static_memfuncp;
+      find_overload_match (argvec, "operator()", METHOD, &argvec[0], nullptr,
+			   &callee, nullptr, &static_memfuncp, 0, noside);
+      if (static_memfuncp)
+	argvec.erase (argvec.begin ());
+    }
+
   return evaluate_subexp_do_call (exp, noside, callee, argvec,
 				  nullptr, expect_type);
 }
diff --git a/gdb/testsuite/gdb.cp/userdef.cc b/gdb/testsuite/gdb.cp/userdef.cc
index 774191726f3..48507551079 100644
--- a/gdb/testsuite/gdb.cp/userdef.cc
+++ b/gdb/testsuite/gdb.cp/userdef.cc
@@ -68,6 +68,9 @@ A1 operator++(int);
 A1 operator--(); 
 A1 operator--(int);
 
+int operator()();
+int operator()(int);
+
 };
 
 
@@ -293,6 +296,16 @@ ostream& operator<<(ostream& outs, A1 one)
  return (outs << endl << "x = " << one.x << endl << "y = " << one.y << endl << "-------" << endl); 
 }
 
+int A1::operator()()
+{
+  return x + y;
+}
+
+int A1::operator()(int value)
+{
+  return value * (x + y);
+}
+
 class A2 {
   public:
 A2 operator+();
@@ -404,6 +417,11 @@ int main (void)
  ++three;
  cout << "preinc " << three;
 
+ val = two();
+ cout << "funcall " << val << endl;
+ val = two(10);
+ cout << "funcall 2 " << val << endl;
+
  (*c).z = 1;
 
  return 0;
diff --git a/gdb/testsuite/gdb.cp/userdef.exp b/gdb/testsuite/gdb.cp/userdef.exp
index e96636bef0c..667bded6b83 100644
--- a/gdb/testsuite/gdb.cp/userdef.exp
+++ b/gdb/testsuite/gdb.cp/userdef.exp
@@ -119,6 +119,10 @@ gdb_test "print one += 7" "\\\$\[0-9\]* = {x = 9, y = 10}"
 
 gdb_test "print two = one" "\\\$\[0-9\]* = {x = 9, y = 10}"
 
+gdb_test "print two()" " = 19"
+gdb_test "print two(10)" " = 190"
+gdb_test "print (*&two)(2)" " = 38"
+
 # Check that GDB tolerates whitespace in operator names.
 gdb_test "break A2::operator+" ".*Breakpoint $decimal at.*"
 gdb_test "break A2::operator +" ".*Breakpoint $decimal at.*"
-- 
2.35.1


       reply	other threads:[~2024-04-21 12:50 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
     [not found] <20240421124954.3285-1-ssbssa.ref@yahoo.de>
2024-04-21 12:49 ` Hannes Domani [this message]
2024-04-25 18:34   ` Guinevere Larsen
2024-04-27 16:43     ` Hannes Domani

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=20240421124954.3285-1-ssbssa@yahoo.de \
    --to=ssbssa@yahoo.de \
    --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).