public inbox for gdb-patches@sourceware.org
 help / color / mirror / Atom feed
* [PATCH v5] Allow calling of user-defined function call operators
       [not found] <20240611181851.1784-1-ssbssa.ref@yahoo.de>
@ 2024-06-11 18:18 ` Hannes Domani
  2024-06-11 19:58   ` Tom Tromey
  0 siblings, 1 reply; 3+ messages in thread
From: Hannes Domani @ 2024-06-11 18:18 UTC (permalink / raw)
  To: gdb-patches

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
```

The change in operation::evaluate_funcall is to make sure the type
fields are only used for function types, only they use them as the
argument types.

Bug: https://sourceware.org/bugzilla/show_bug.cgi?id=12213
---
v2:
- Move the logic into evaluate_subexp_do_call, to avoid duplication in
  every evaluate_funcall of each operation subclass.
  This makes it now work for some cases it didn't in v1, like if it's
  called on a class member (`print c.m(5)` in the new test).
- Added tests for other (struct member) operations.

v3:
- Additional comments.

v4:
- Fix resetting ftype after callee changes, plus some tests for this
  where noside == EVAL_AVOID_SIDE_EFFECTS.

v5:
- Add test for missing operator().
---
 gdb/eval.c                       | 34 +++++++++++++++++++++++++++++---
 gdb/testsuite/gdb.cp/userdef.cc  | 22 +++++++++++++++++++++
 gdb/testsuite/gdb.cp/userdef.exp | 10 ++++++++++
 3 files changed, 63 insertions(+), 3 deletions(-)

diff --git a/gdb/eval.c b/gdb/eval.c
index 6b752e70635..40640ae97be 100644
--- a/gdb/eval.c
+++ b/gdb/eval.c
@@ -588,14 +588,38 @@ evaluate_subexp_do_call (expression *exp, enum noside noside,
 {
   if (callee == NULL)
     error (_("Cannot evaluate function -- may be inlined"));
+
+  type *ftype = callee->type ();
+
+  /* If the callee is a struct, there might be a user-defined function call
+     operator that should be used instead.  */
+  std::vector<value *> vals;
+  if (overload_resolution
+      && exp->language_defn->la_language == language_cplus
+      && check_typedef (ftype)->code () == TYPE_CODE_STRUCT)
+    {
+      /* Include space for the `this' pointer at the start.  */
+      vals.resize (argvec.size () + 1);
+
+      vals[0] = value_addr (callee);
+      for (int i = 0; i < argvec.size (); ++i)
+	vals[i + 1] = argvec[i];
+
+      int static_memfuncp;
+      find_overload_match (vals, "operator()", METHOD, &vals[0], nullptr,
+			   &callee, nullptr, &static_memfuncp, 0, noside);
+      if (!static_memfuncp)
+	argvec = vals;
+
+      ftype = callee->type ();
+    }
+
   if (noside == EVAL_AVOID_SIDE_EFFECTS)
     {
       /* If the return type doesn't look like a function type,
 	 call an error.  This can happen if somebody tries to turn
 	 a variable into a function call.  */
 
-      type *ftype = callee->type ();
-
       if (ftype->code () == TYPE_CODE_INTERNAL_FUNCTION)
 	{
 	  /* We don't know anything about what the internal
@@ -666,9 +690,13 @@ operation::evaluate_funcall (struct type *expect_type,
   struct type *type = callee->type ();
   if (type->code () == TYPE_CODE_PTR)
     type = type->target_type ();
+  /* If type is a struct, num_fields would refer to the number of
+     members in the type, not the number of arguments.  */
+  bool type_has_arguments
+    = type->code () == TYPE_CODE_FUNC || type->code () == TYPE_CODE_METHOD;
   for (int i = 0; i < args.size (); ++i)
     {
-      if (i < type->num_fields ())
+      if (type_has_arguments && 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);
diff --git a/gdb/testsuite/gdb.cp/userdef.cc b/gdb/testsuite/gdb.cp/userdef.cc
index 774191726f3..7e045e46b3b 100644
--- a/gdb/testsuite/gdb.cp/userdef.cc
+++ b/gdb/testsuite/gdb.cp/userdef.cc
@@ -307,8 +307,21 @@ class Member
 {
 public:
   int z;
+
+  int operator() ();
+  int operator() (int);
 };
 
+int Member::operator() ()
+{
+  return z;
+}
+
+int Member::operator() (int value)
+{
+  return value * z;
+}
+
 bool operator== (const Member &m1, const Member &m2)
 {
   return m1.z == m2.z;
@@ -335,9 +348,11 @@ int main (void)
  Container c;
  Member mem1, mem2;
  int val;
+ Member Container::* mptr = &Container::m;
  
  mem1.z = 5;
  mem2.z = 7;
+ c.m.z = 8;
 
  marker1(); // marker1-returns-here
  cout << one; // marker1-returns-here
@@ -404,6 +419,13 @@ int main (void)
  ++three;
  cout << "preinc " << three;
 
+ val = mem1 ();
+ cout << "funcall " << val << endl;
+ val = mem1 (10);
+ cout << "funcall 2 " << val << endl;
+ val = (c.*mptr) (11);
+ cout << "funcall 3 " << 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..869f4bccdce 100644
--- a/gdb/testsuite/gdb.cp/userdef.exp
+++ b/gdb/testsuite/gdb.cp/userdef.exp
@@ -132,4 +132,14 @@ gdb_test "ptype &*c" "type = (struct|class) Member {(\[\r\n \]+public:)?\[\r\n \
 gdb_test "print operator== (mem1, mem2)" " = false"
 gdb_test "print operator== (mem1, mem1)" " = true"
 
+gdb_test "print mem1()" " = 5"
+gdb_test "print mem1(10)" " = 50"
+gdb_test "print (*&mem1)(2)" " = 10"
+gdb_test "print (c.*mptr)(3)" " = 24"
+gdb_test "print (&c)->m(4)" " = 32"
+gdb_test "print c.m(5)" " = 40"
+gdb_test "print sizeof(mem1(0))/sizeof(int)" " = 1"
+gdb_test "ptype mem1(0)" "type = int"
+gdb_test "print one()" "Couldn't find method A1::operator\\(\\)"
+
 gdb_exit
-- 
2.35.1


^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [PATCH v5] Allow calling of user-defined function call operators
  2024-06-11 18:18 ` [PATCH v5] Allow calling of user-defined function call operators Hannes Domani
@ 2024-06-11 19:58   ` Tom Tromey
  2024-06-12 13:26     ` Hannes Domani
  0 siblings, 1 reply; 3+ messages in thread
From: Tom Tromey @ 2024-06-11 19:58 UTC (permalink / raw)
  To: Hannes Domani; +Cc: gdb-patches

>>>>> "Hannes" == Hannes Domani <ssbssa@yahoo.de> writes:

Hannes> The change in operation::evaluate_funcall is to make sure the type
Hannes> fields are only used for function types, only they use them as the
Hannes> argument types.

Hannes> Bug: https://sourceware.org/bugzilla/show_bug.cgi?id=12213

Thank you.  This is ok.
Approved-By: Tom Tromey <tom@tromey.com>

Tom

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [PATCH v5] Allow calling of user-defined function call operators
  2024-06-11 19:58   ` Tom Tromey
@ 2024-06-12 13:26     ` Hannes Domani
  0 siblings, 0 replies; 3+ messages in thread
From: Hannes Domani @ 2024-06-12 13:26 UTC (permalink / raw)
  To: Tom Tromey; +Cc: gdb-patches

 Am Dienstag, 11. Juni 2024 um 21:58:10 MESZ hat Tom Tromey <tom@tromey.com> Folgendes geschrieben:

> >>>>> "Hannes" == Hannes Domani <ssbssa@yahoo.de> writes:
>
> Hannes> The change in operation::evaluate_funcall is to make sure the type
> Hannes> fields are only used for function types, only they use them as the
> Hannes> argument types.
>
> Hannes> Bug: https://sourceware.org/bugzilla/show_bug.cgi?id=12213
>
> Thank you.  This is ok.
> Approved-By: Tom Tromey <tom@tromey.com>

Pushed, thanks.


Hannes

^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2024-06-12 13:26 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
     [not found] <20240611181851.1784-1-ssbssa.ref@yahoo.de>
2024-06-11 18:18 ` [PATCH v5] Allow calling of user-defined function call operators Hannes Domani
2024-06-11 19:58   ` Tom Tromey
2024-06-12 13:26     ` Hannes Domani

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).