public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c++/46043] New: attribute to mark virtual methods that can't be further overriden so they can be devirtualized
@ 2010-10-16 12:04 zsojka at seznam dot cz
  2010-10-16 14:01 ` [Bug c++/46043] " redi at gcc dot gnu.org
                   ` (7 more replies)
  0 siblings, 8 replies; 9+ messages in thread
From: zsojka at seznam dot cz @ 2010-10-16 12:04 UTC (permalink / raw)
  To: gcc-bugs

http://gcc.gnu.org/bugzilla/show_bug.cgi?id=46043

           Summary: attribute to mark virtual methods that can't be
                    further overriden so they can be devirtualized
           Product: gcc
           Version: 4.6.0
            Status: UNCONFIRMED
          Severity: enhancement
          Priority: P3
         Component: c++
        AssignedTo: unassigned@gcc.gnu.org
        ReportedBy: zsojka@seznam.cz


In some scenarios, there is one abstract class declaring virtual methods with
several derived classes, all of them overriding those methods. (at least in
OpenTTD that happens often) The important point is that there are no further
derived classes from those. For example:

struct A {
  virtual void f1() = 0;
  virtual void f2() = 0;
  virtual void f3() { ... this->f1(); this->f2(); ... }
};

struct B : A {
  virtual void f1();
  virtual void f2() { ... this->f1(); ... }
};

struct C : A {
  virtual void f1();
  virtual void f2();
  virtual void f3() { ... this->f1(); this->f2(); ... }
};

struct D : A {
  virtual void f1();
  virtual void f2();
};

If there was an information that there are no classes derived from B,C,D, it
would allow many devirtualizations.
One idea I could come with would be omitting the "virtual" keyword causing the
method to lose its "virtual" status. But that would break a lot of existing
code.
Second idea is using an attribute for that. While searching for this issue I
found java uses "final" keyword for this purpose (or at least it seems to me
so), so I will use attribute "final" in the following example:

struct A {
    virtual void f1();
    virtual void f2();
    virtual void f3();
};

/* f1() can't be further overriden */
struct B : A {
    virtual void f1() __attribute__((final));
};

/* no virtual method can further be overriden */
struct C : A {
    virtual void f3();
} __attribute__((final));

struct D : C {
    virtual void f4();
};

struct E : D {
} __attribute__((final));

void foo(struct C *c, struct D *d, struct E *e)
{
    c->f1(); /* c->B::f1() */
    c->f2(); /* c->A::f2() */
    c->f3(); /* c->C::f3() */
    d->f1(); /* d->B::f1() */
    d->f4(); /* not known at compile-time */
    e->f1(); /* e->B::f1() */
    e->f4(); /* e->D::f4() */
}

/* The following results in compile errors */
struct F : D {
    virtual void f1(); /* error */
    virtual void f2(); /* error */
    virtual void f3(); /* error */
    virtual void f4();
};

I searched the bugzilla for this issue and found none open. Given this idea is
quite simple and the gain can be high, there might be something I overlooked.


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

* [Bug c++/46043] attribute to mark virtual methods that can't be further overriden so they can be devirtualized
  2010-10-16 12:04 [Bug c++/46043] New: attribute to mark virtual methods that can't be further overriden so they can be devirtualized zsojka at seznam dot cz
@ 2010-10-16 14:01 ` redi at gcc dot gnu.org
  2010-10-16 15:46 ` redi at gcc dot gnu.org
                   ` (6 subsequent siblings)
  7 siblings, 0 replies; 9+ messages in thread
From: redi at gcc dot gnu.org @ 2010-10-16 14:01 UTC (permalink / raw)
  To: gcc-bugs

http://gcc.gnu.org/bugzilla/show_bug.cgi?id=46043

--- Comment #1 from Jonathan Wakely <redi at gcc dot gnu.org> 2010-10-16 14:01:25 UTC ---
(In reply to comment #0)
> One idea I could come with would be omitting the "virtual" keyword causing the
> method to lose its "virtual" status. But that would break a lot of existing
> code.

This is not acceptable.

The idea is useful in general though.  You should read
http://www.open-std.org/JTC1/SC22/WG21/docs/papers/2009/n2928.htm for related
features in C++0x


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

* [Bug c++/46043] attribute to mark virtual methods that can't be further overriden so they can be devirtualized
  2010-10-16 12:04 [Bug c++/46043] New: attribute to mark virtual methods that can't be further overriden so they can be devirtualized zsojka at seznam dot cz
  2010-10-16 14:01 ` [Bug c++/46043] " redi at gcc dot gnu.org
@ 2010-10-16 15:46 ` redi at gcc dot gnu.org
  2010-10-16 15:50 ` zsojka at seznam dot cz
                   ` (5 subsequent siblings)
  7 siblings, 0 replies; 9+ messages in thread
From: redi at gcc dot gnu.org @ 2010-10-16 15:46 UTC (permalink / raw)
  To: gcc-bugs

http://gcc.gnu.org/bugzilla/show_bug.cgi?id=46043

--- Comment #2 from Jonathan Wakely <redi at gcc dot gnu.org> 2010-10-16 15:46:44 UTC ---
Oops, that's not the right paper, but C++0x already has a [[final]] attribute,
so any work on this bug should be made compatible with the C++0x semantics, to
ease transition from GNU-style attributes to C++0x ones.


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

* [Bug c++/46043] attribute to mark virtual methods that can't be further overriden so they can be devirtualized
  2010-10-16 12:04 [Bug c++/46043] New: attribute to mark virtual methods that can't be further overriden so they can be devirtualized zsojka at seznam dot cz
  2010-10-16 14:01 ` [Bug c++/46043] " redi at gcc dot gnu.org
  2010-10-16 15:46 ` redi at gcc dot gnu.org
@ 2010-10-16 15:50 ` zsojka at seznam dot cz
  2010-10-27 22:12 ` pinskia at gcc dot gnu.org
                   ` (4 subsequent siblings)
  7 siblings, 0 replies; 9+ messages in thread
From: zsojka at seznam dot cz @ 2010-10-16 15:50 UTC (permalink / raw)
  To: gcc-bugs

http://gcc.gnu.org/bugzilla/show_bug.cgi?id=46043

--- Comment #3 from Zdenek Sojka <zsojka at seznam dot cz> 2010-10-16 15:50:41 UTC ---
Thank you for answers, this is a really good news!


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

* [Bug c++/46043] attribute to mark virtual methods that can't be further overriden so they can be devirtualized
  2010-10-16 12:04 [Bug c++/46043] New: attribute to mark virtual methods that can't be further overriden so they can be devirtualized zsojka at seznam dot cz
                   ` (2 preceding siblings ...)
  2010-10-16 15:50 ` zsojka at seznam dot cz
@ 2010-10-27 22:12 ` pinskia at gcc dot gnu.org
  2010-10-28  8:50 ` redi at gcc dot gnu.org
                   ` (3 subsequent siblings)
  7 siblings, 0 replies; 9+ messages in thread
From: pinskia at gcc dot gnu.org @ 2010-10-27 22:12 UTC (permalink / raw)
  To: gcc-bugs

http://gcc.gnu.org/bugzilla/show_bug.cgi?id=46043

Andrew Pinski <pinskia at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|UNCONFIRMED                 |NEW
   Last reconfirmed|                            |2010.10.27 22:12:08
     Ever Confirmed|0                           |1

--- Comment #4 from Andrew Pinski <pinskia at gcc dot gnu.org> 2010-10-27 22:12:08 UTC ---
Confirmed, I thought I had say a bug asking for this previously.  MS Visual
Studio has such a method and IIRC it was rejected for GCC but I don't remember
why anymore.


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

* [Bug c++/46043] attribute to mark virtual methods that can't be further overriden so they can be devirtualized
  2010-10-16 12:04 [Bug c++/46043] New: attribute to mark virtual methods that can't be further overriden so they can be devirtualized zsojka at seznam dot cz
                   ` (3 preceding siblings ...)
  2010-10-27 22:12 ` pinskia at gcc dot gnu.org
@ 2010-10-28  8:50 ` redi at gcc dot gnu.org
  2011-07-07 22:02 ` jason at gcc dot gnu.org
                   ` (2 subsequent siblings)
  7 siblings, 0 replies; 9+ messages in thread
From: redi at gcc dot gnu.org @ 2010-10-28  8:50 UTC (permalink / raw)
  To: gcc-bugs

http://gcc.gnu.org/bugzilla/show_bug.cgi?id=46043

--- Comment #5 from Jonathan Wakely <redi at gcc dot gnu.org> 2010-10-28 08:50:07 UTC ---
This should be suspended, the C++0x mechanisms are not finalised (pun intended)
and might become keywords rather than attributes

http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2010/n3151.html
http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2010/n3163.pdf

PR 31397 and PR 36848 are related, the C++0x proposals would solve this report
and those ones


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

* [Bug c++/46043] attribute to mark virtual methods that can't be further overriden so they can be devirtualized
  2010-10-16 12:04 [Bug c++/46043] New: attribute to mark virtual methods that can't be further overriden so they can be devirtualized zsojka at seznam dot cz
                   ` (4 preceding siblings ...)
  2010-10-28  8:50 ` redi at gcc dot gnu.org
@ 2011-07-07 22:02 ` jason at gcc dot gnu.org
  2011-07-17 11:46 ` paolo.carlini at oracle dot com
  2011-07-17 13:12 ` jason at gcc dot gnu.org
  7 siblings, 0 replies; 9+ messages in thread
From: jason at gcc dot gnu.org @ 2011-07-07 22:02 UTC (permalink / raw)
  To: gcc-bugs

http://gcc.gnu.org/bugzilla/show_bug.cgi?id=46043

Jason Merrill <jason at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|NEW                         |WAITING
                 CC|                            |jason at gcc dot gnu.org

--- Comment #6 from Jason Merrill <jason at gcc dot gnu.org> 2011-07-07 22:01:14 UTC ---
The final keyword is now supported in C++0x mode.  Does that resolve this
issue?


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

* [Bug c++/46043] attribute to mark virtual methods that can't be further overriden so they can be devirtualized
  2010-10-16 12:04 [Bug c++/46043] New: attribute to mark virtual methods that can't be further overriden so they can be devirtualized zsojka at seznam dot cz
                   ` (5 preceding siblings ...)
  2011-07-07 22:02 ` jason at gcc dot gnu.org
@ 2011-07-17 11:46 ` paolo.carlini at oracle dot com
  2011-07-17 13:12 ` jason at gcc dot gnu.org
  7 siblings, 0 replies; 9+ messages in thread
From: paolo.carlini at oracle dot com @ 2011-07-17 11:46 UTC (permalink / raw)
  To: gcc-bugs

http://gcc.gnu.org/bugzilla/show_bug.cgi?id=46043

--- Comment #7 from Paolo Carlini <paolo.carlini at oracle dot com> 2011-07-17 11:45:53 UTC ---
Or we can mark it as duplicate of c++/49488... or viceversa.


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

* [Bug c++/46043] attribute to mark virtual methods that can't be further overriden so they can be devirtualized
  2010-10-16 12:04 [Bug c++/46043] New: attribute to mark virtual methods that can't be further overriden so they can be devirtualized zsojka at seznam dot cz
                   ` (6 preceding siblings ...)
  2011-07-17 11:46 ` paolo.carlini at oracle dot com
@ 2011-07-17 13:12 ` jason at gcc dot gnu.org
  7 siblings, 0 replies; 9+ messages in thread
From: jason at gcc dot gnu.org @ 2011-07-17 13:12 UTC (permalink / raw)
  To: gcc-bugs

http://gcc.gnu.org/bugzilla/show_bug.cgi?id=46043

Jason Merrill <jason at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|WAITING                     |RESOLVED
         Resolution|                            |DUPLICATE

--- Comment #8 from Jason Merrill <jason at gcc dot gnu.org> 2011-07-17 13:11:39 UTC ---
Sure, that makes sense.

*** This bug has been marked as a duplicate of bug 49488 ***


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

end of thread, other threads:[~2011-07-17 13:12 UTC | newest]

Thread overview: 9+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2010-10-16 12:04 [Bug c++/46043] New: attribute to mark virtual methods that can't be further overriden so they can be devirtualized zsojka at seznam dot cz
2010-10-16 14:01 ` [Bug c++/46043] " redi at gcc dot gnu.org
2010-10-16 15:46 ` redi at gcc dot gnu.org
2010-10-16 15:50 ` zsojka at seznam dot cz
2010-10-27 22:12 ` pinskia at gcc dot gnu.org
2010-10-28  8:50 ` redi at gcc dot gnu.org
2011-07-07 22:02 ` jason at gcc dot gnu.org
2011-07-17 11:46 ` paolo.carlini at oracle dot com
2011-07-17 13:12 ` jason at gcc dot gnu.org

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