Hi,

On 27/06/19 23:19, Jason Merrill wrote:
Ah, thanks.  Then perhaps we want to change the CLASSTYPE_FINAL in build_over_call to resolves_to_fixed_type_p (arg), to also handle the other reasons we might know the dynamic type of the argument, and remove the related code from build_new_method_call_1?

You could avoid needing to move the conditional lower by comparing DECL_CONTEXT (fn) and BINFO_TYPE (cand->conversion_path) rather than parmtype and TREE_TYPE (converted_arg).

Sorry for late replying, a few days off.

Anyway, great, it looks like we are reaching a nice synthesis. I must admit that until yesterday I hadn't noticed that Fabien dealt precisely with using declarations in order to fix c++/11750, thus the existing check in build_new_method_call_1 is exactly what we need. The below does that and passes testing, in it I didn't keep the checks of DECL_VINDEX (fn) && ! (flags & LOOKUP_NONVIRTUAL) which don't seem necessary, might avoid function calls, though. Let me know...

Thanks, Paolo.

////////////////////


Index: cp/call.c
===================================================================
--- cp/call.c   (revision 273076)
+++ cp/call.c   (working copy)
@@ -8241,10 +8241,17 @@ build_over_call (struct z_candidate *cand, int fla
            return error_mark_node;
        }
 
-      /* See if the function member or the whole class type is declared
-        final and the call can be devirtualized.  */
+      /* Optimize away vtable lookup if we know that this
+        function can't be overridden.  We need to check if
+        the context and the type where we found fn are the same,
+        actually FN might be defined in a different class
+        type because of a using-declaration. In this case, we
+        do not want to perform a non-virtual call.  Note that
+        resolves_to_fixed_type_p checks CLASSTYPE_FINAL too.  */
       if (DECL_FINAL_P (fn)
-         || CLASSTYPE_FINAL (TYPE_METHOD_BASETYPE (TREE_TYPE (fn))))
+         || (resolves_to_fixed_type_p (arg, 0)
+             && same_type_ignoring_top_level_qualifiers_p
+             (DECL_CONTEXT (fn), BINFO_TYPE (cand->conversion_path)))) 
        flags |= LOOKUP_NONVIRTUAL;
 
       /* [class.mfct.nonstatic]: If a nonstatic member function of a class
@@ -9845,17 +9852,6 @@ build_new_method_call_1 (tree instance, tree fns,
 
          if (call != error_mark_node)
            {
-             /* Optimize away vtable lookup if we know that this
-                function can't be overridden.  We need to check if
-                the context and the type where we found fn are the same,
-                actually FN might be defined in a different class
-                type because of a using-declaration. In this case, we
-                do not want to perform a non-virtual call.  */
-             if (DECL_VINDEX (fn) && ! (flags & LOOKUP_NONVIRTUAL)
-                 && same_type_ignoring_top_level_qualifiers_p
-                 (DECL_CONTEXT (fn), BINFO_TYPE (binfo))
-                 && resolves_to_fixed_type_p (instance, 0))
-               flags |= LOOKUP_NONVIRTUAL;
               if (explicit_targs)
                 flags |= LOOKUP_EXPLICIT_TMPL_ARGS;
              /* Now we know what function is being called.  */
Index: testsuite/g++.dg/other/final4.C
===================================================================
--- testsuite/g++.dg/other/final4.C     (nonexistent)
+++ testsuite/g++.dg/other/final4.C     (working copy)
@@ -0,0 +1,16 @@
+// PR c++/67184
+// { dg-do compile { target c++11 } }
+// { dg-options "-fdump-tree-original"  }
+
+struct B
+{
+  virtual void operator()();
+  virtual operator int();
+  virtual int operator++();
+};
+
+struct D final : B { };
+
+void foo(D& d) { d(); int t = d; ++d; }
+
+// { dg-final { scan-tree-dump-times "OBJ_TYPE_REF" 0 "original" } }

Reply via email to