public inbox for gcc@gcc.gnu.org
 help / color / mirror / Atom feed
* RFC: New C++ Attribute: final
@ 2004-02-29  5:50 Kevin Atkinson
  2004-02-29  6:51 ` Mark Mielke
                   ` (2 more replies)
  0 siblings, 3 replies; 55+ messages in thread
From: Kevin Atkinson @ 2004-02-29  5:50 UTC (permalink / raw)
  To: gcc


I have an idea for a new C++ attribute, and would like to know what others 
think of it:

'final'

Marks a virtual function of an entire class as being "final", in the sense 
that it can not be overridden in a derived class.  If it is applied to a 
type then every virtual function in the class will be considered final.  
It will not, however, keep a class from being inherited from.  
Gcc will produce a warning (or maybe an error) if a "final" method is 
overridden.

The primary purpose of this attribute is to serve as an optimization hint 
so that a virtual function call does not need to be used.  It will also 
allow gcc to perhaps inline the function call if it is appropriate.

An example:

class A {
public:
  virtual void f();
};

class B {
public:
  __attribute__((final)) void f();
};

int main()
{
  B * b = new B;
  A * a = b;
  a->f(); // virtual call
  b->f(); // non virtual call since the function in type B is marked
          // as final
  b->B::f(); // equalvent to the above
}

I have not written any code for GCC yet, but if people think it is a good 
idea I may just implement this, as it seams like something that can't be 
too difficult to do.

-- 
http://kevin.atkinson.dhs.org

^ permalink raw reply	[flat|nested] 55+ messages in thread
* RE: RFC: New C++ Attribute: final
@ 2004-03-01 19:03 Cheng, Cheuk
  0 siblings, 0 replies; 55+ messages in thread
From: Cheng, Cheuk @ 2004-03-01 19:03 UTC (permalink / raw)
  To: gcc

Hi, I am using sparc-elf-gcc 2.9.5.3.  Does the virtual destructor in
the 00000000 <__._1B>: portion below actually call the destructor for
the parent class A?  Or is it calling ___builtin_delete() when it says
something like this?

00000004 <.LM10>:
   4:	11 00 00 00 	sethi  %hi(0), %o0
			4: R_SPARC_HI22	___vt_1A
   8:	90 12 20 00 	mov  %o0, %o0	! 0 <__._1B>
			8: R_SPARC_LO10	___vt_1A
   c:	d0 26 20 08 	st  %o0, [ %i0 + 8 ]
  10:	40 00 00 00 	call  10 <.LM10+0xc>
			10: R_SPARC_WDISP30	___builtin_delete
  ...............
  <full version follows source code below>

When I compile the same source code without any optimizations (-O1, -O2,
etc.) , then I can see a call to A::~A instead of ___builtin_delete().
Since I do not know sparc assembly language, I do not know whether the
object code generated is right or not.

Thanks.



class A
{
public:
	A() { X = 0; Z = new(char); }
	A(int temp) : X(temp) { Z = new(char); }
	virtual ~A() { delete Z; }

private:
	int	 X;
	char *Z;
};

class B: public A
{
public:
	B() : A(1), Y(2) {}
	~B() {}

private:
	int	Y;
};

int main(int argc, char **argv)
{
	B	*obj = new B();

	delete obj;

	return 0;
}

====================================================

dstr.o:     file format elf32-sparc

SYMBOL TABLE:
00000000 l    df *ABS*	00000000 dstr.cpp
00000000 l    d  .text	00000000 
00000000 l    d  .data	00000000 
00000000 l    d  .bss	00000000 
00000000 l    d  .gnu.linkonce.d.__vt_1B	00000000 
00000000 l    d  .gnu.linkonce.d.__vt_1A	00000000 
00000000 l    d  .gnu.linkonce.t._._1B	00000000 
00000000 l       .gnu.linkonce.t._._1B	00000000 .LM9
00000004 l       .gnu.linkonce.t._._1B	00000000 .LM10
0000002c l       .gnu.linkonce.t._._1B	00000000 .LM11
00000000 l    d  .gnu.linkonce.t._._1A	00000000 
00000000 l       .gnu.linkonce.t._._1A	00000000 .LM12
00000000 l       *ABS*	00000000 *ABS*
00000000 l    d  .stab	00000000 
00000000 l    d  .stabstr	00000000 
00000000 l    d  .comment	00000000 
00000000 g     F .text	00000064 _main
00000000         *UND*	00000000 ___builtin_new
00000000  w    O .gnu.linkonce.d.__vt_1A	0000000c ___vt_1A
00000000  w    O .gnu.linkonce.d.__vt_1B	0000000c ___vt_1B
00000000  w    F .gnu.linkonce.t._._1B	00000034 __._1B
00000000  w    F .gnu.linkonce.t._._1A	00000034 __._1A
00000000         *UND*	00000000 ___builtin_delete


Disassembly of section .text:

00000000 <_main>:
   0:	9d e3 bf 98 	save  %sp, -104, %sp
   4:	40 00 00 00 	call  4 <_main+0x4>
			4: R_SPARC_WDISP30	___builtin_new
   8:	90 10 20 10 	mov  0x10, %o0
   c:	b0 10 00 08 	mov  %o0, %i0
  10:	92 10 20 01 	mov  1, %o1
  14:	11 00 00 00 	sethi  %hi(0), %o0
			14: R_SPARC_HI22	___vt_1A
  18:	90 12 20 00 	mov  %o0, %o0	! 0 <_main>
			18: R_SPARC_LO10	___vt_1A
  1c:	d0 26 20 08 	st  %o0, [ %i0 + 8 ]
  20:	d2 26 00 00 	st  %o1, [ %i0 ]
  24:	40 00 00 00 	call  24 <_main+0x24>
			24: R_SPARC_WDISP30	___builtin_new
  28:	90 10 20 01 	mov  1, %o0
  2c:	d0 26 20 04 	st  %o0, [ %i0 + 4 ]
  30:	11 00 00 00 	sethi  %hi(0), %o0
			30: R_SPARC_HI22	___vt_1B
  34:	92 12 20 00 	mov  %o0, %o1	! 0 <_main>
			34: R_SPARC_LO10	___vt_1B
  38:	d2 26 20 08 	st  %o1, [ %i0 + 8 ]
  3c:	90 10 20 02 	mov  2, %o0
  40:	80 a6 20 00 	cmp  %i0, 0
  44:	02 80 00 06 	be  5c <_main+0x5c>
  48:	d0 26 20 0c 	st  %o0, [ %i0 + 0xc ]
  4c:	d4 02 60 08 	ld  [ %o1 + 8 ], %o2
  50:	90 10 00 18 	mov  %i0, %o0
  54:	9f c2 80 00 	call  %o2
  58:	92 10 20 03 	mov  3, %o1
  5c:	81 c7 e0 08 	ret 
  60:	91 e8 20 00 	restore  %g0, 0, %o0
Disassembly of section .data:
Disassembly of section .gnu.linkonce.d.__vt_1B:

00000000 <___vt_1B>:
	...
			8: R_SPARC_32	__._1B
Disassembly of section .gnu.linkonce.d.__vt_1A:

00000000 <___vt_1A>:
	...
			8: R_SPARC_32	__._1A
Disassembly of section .gnu.linkonce.t._._1B:

00000000 <__._1B>:
   0:	9d e3 bf 98 	save  %sp, -104, %sp

00000004 <.LM10>:
   4:	11 00 00 00 	sethi  %hi(0), %o0
			4: R_SPARC_HI22	___vt_1A
   8:	90 12 20 00 	mov  %o0, %o0	! 0 <__._1B>
			8: R_SPARC_LO10	___vt_1A
   c:	d0 26 20 08 	st  %o0, [ %i0 + 8 ]
  10:	40 00 00 00 	call  10 <.LM10+0xc>
			10: R_SPARC_WDISP30	___builtin_delete
  14:	d0 06 20 04 	ld  [ %i0 + 4 ], %o0
  18:	80 8e 60 01 	btst  1, %i1
  1c:	02 80 00 04 	be  2c <.LM11>
  20:	01 00 00 00 	nop 
  24:	40 00 00 00 	call  24 <.LM10+0x20>
			24: R_SPARC_WDISP30	___builtin_delete
  28:	90 10 00 18 	mov  %i0, %o0

0000002c <.LM11>:
  2c:	81 c7 e0 08 	ret 
  30:	81 e8 00 00 	restore 
Disassembly of section .gnu.linkonce.t._._1A:

00000000 <__._1A>:
   0:	9d e3 bf 98 	save  %sp, -104, %sp
   4:	11 00 00 00 	sethi  %hi(0), %o0
			4: R_SPARC_HI22	___vt_1A
   8:	90 12 20 00 	mov  %o0, %o0	! 0 <__._1A>
			8: R_SPARC_LO10	___vt_1A
   c:	d0 26 20 08 	st  %o0, [ %i0 + 8 ]
  10:	40 00 00 00 	call  10 <__._1A+0x10>
			10: R_SPARC_WDISP30	___builtin_delete
  14:	d0 06 20 04 	ld  [ %i0 + 4 ], %o0
  18:	80 8e 60 01 	btst  1, %i1
  1c:	02 80 00 04 	be  2c <__._1A+0x2c>
  20:	01 00 00 00 	nop 
  24:	40 00 00 00 	call  24 <__._1A+0x24>
			24: R_SPARC_WDISP30	___builtin_delete
  28:	90 10 00 18 	mov  %i0, %o0
  2c:	81 c7 e0 08 	ret 
  30:	81 e8 00 00 	restore 


- - - - - - - Appended by PowerTV, Inc. - - - - - - - 
This e-mail and any attachments may contain information that is confidential, proprietary, privileged or otherwise protected by law. The information is solely intended for the named addressee (or a person responsible for delivering it to the addressee). If you are not the intended recipient of this message, you are not authorized to read, print, retain, copy or disseminate this message or any part of it. If you have received this e-mail in error, please notify the sender immediately by return e-mail and delete it from your computer.

^ permalink raw reply	[flat|nested] 55+ messages in thread
* Re: RFC: New C++ Attribute: final
@ 2004-03-01 19:34 Chris Lattner
  0 siblings, 0 replies; 55+ messages in thread
From: Chris Lattner @ 2004-03-01 19:34 UTC (permalink / raw)
  To: Joe Buck
  Cc: Gabriel Dos Reis, Nathan Sidwell, Kevin Atkinson, Mark Mielke, gcc


> Every problem in computer science can be solved by an extra level of
> indirection.  Or rather, every problem other than inadequate
> performance. Such claims usually come from those who follow rigid
> ideological approaches to object-oriented design.

Speaking from the perspective of a developer that writes a lot of C++
code, I totally agree with your viewpoint.  There _are_ workarounds that
don't cause code duplication (in the derived class, have a non-virtual
method do the work, and have the virtual one just call it), but these are
really gross and seemingly against the spirit of C++.

Also, it's not feasible to make all of the clients know about this
distinction, and if it changes over time, it's not scalable to modify tons
of client code where we could just modify one declaration in the header.
Also, if the client code is really a template using std::mem_fun or
something, it's not *possible* to change the client (without even more
gross hacks I suppose).

In my situation, I have complete control over the class hierarchy and if a
new subclass needs to be added, it's ok to remove the attribute and force
clients to recompile.  Though not everyone is in this situation, many are,
and it seems very reasonable to support it directly: even as a GCC
extension.

-Chris

-- 
http://llvm.cs.uiuc.edu/
http://www.nondot.org/~sabre/Projects/

^ permalink raw reply	[flat|nested] 55+ messages in thread
* Re: RFC: New C++ Attribute: final
@ 2004-03-03  0:33 Robert Dewar
  0 siblings, 0 replies; 55+ messages in thread
From: Robert Dewar @ 2004-03-03  0:33 UTC (permalink / raw)
  To: Joe.Buck, aoliva; +Cc: austern, gcc, kevina

> How about: experimental extensions must only be enabled if a certain
> flag is given in the command line?  Like that of signatures, just to
> mention an example of an extension that didn't go forward.  Then we
> still serve as a testbed, but we don't have backward-compatibility
> issues to worry about.

The switch -gnatX acts this way for Ada, and is currently being used
to enable tentatively approved extensions for Ada0Y (the next version
of Ada).

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

end of thread, other threads:[~2004-03-03 19:04 UTC | newest]

Thread overview: 55+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2004-02-29  5:50 RFC: New C++ Attribute: final Kevin Atkinson
2004-02-29  6:51 ` Mark Mielke
2004-02-29  6:56   ` Kevin Atkinson
2004-03-01 10:20     ` Nathan Sidwell
2004-03-01 13:51       ` Per Abrahamsen
2004-03-01 20:58         ` Kevin Atkinson
2004-03-01 23:05         ` Robert Dewar
2004-03-01 14:51       ` Jeff Sturm
2004-03-01 15:02         ` Gabriel Dos Reis
2004-03-01 15:04         ` Nathan Sidwell
2004-03-01 18:53           ` Joe Buck
2004-03-02 10:01             ` Nathan Sidwell
2004-03-01 19:39           ` Jeff Sturm
2004-03-01 19:57             ` Gabriel Dos Reis
2004-03-01 20:18               ` Joe Buck
2004-03-01 20:57                 ` Gabriel Dos Reis
2004-03-01 21:22                   ` Joe Buck
2004-03-01 21:40                     ` Gabriel Dos Reis
2004-03-02 16:59               ` Alexandre Oliva
2004-03-02 17:33                 ` Gabriel Dos Reis
2004-03-02 22:54                   ` Alexandre Oliva
2004-03-02 23:24                     ` Daniel Berlin
2004-03-03  0:09                       ` Richard Henderson
2004-03-03  0:23                         ` Dale Johannesen
2004-03-03  0:33                           ` Richard Henderson
2004-03-03  0:39                             ` Dale Johannesen
2004-03-03  0:27                       ` Alexandre Oliva
2004-03-03  0:57                         ` Gabriel Dos Reis
2004-03-03  3:53                           ` Alexandre Oliva
2004-03-03 11:08                             ` Gabriel Dos Reis
2004-03-03 16:49                               ` Alexandre Oliva
2004-03-03 17:34                                 ` Gabriel Dos Reis
2004-03-03 18:12                                   ` Joe Buck
2004-03-03 19:04                             ` Gabriel Dos Reis
2004-03-03  0:52                     ` Gabriel Dos Reis
2004-03-03  1:51                       ` Joe Buck
2004-03-03 11:37                         ` Gabriel Dos Reis
2004-03-01 18:45       ` Joe Buck
2004-03-01 18:56         ` Gabriel Dos Reis
2004-03-01 19:11           ` Joe Buck
2004-03-01 19:48             ` Gabriel Dos Reis
2004-03-01 20:09               ` Joe Buck
2004-03-01 20:37                 ` Gabriel Dos Reis
2004-03-01 18:34   ` Joe Buck
2004-02-29  7:18 ` Phil Edwards
2004-03-01  6:15   ` Kevin Atkinson
2004-03-01  8:04     ` Kevin Atkinson
2004-03-02 20:58 ` Matt Austern
2004-03-02 21:24   ` George Garvey
2004-03-03  1:21     ` Gabriel Dos Reis
2004-03-02 23:10   ` Joe Buck
2004-03-03  0:30     ` Alexandre Oliva
2004-03-01 19:03 Cheng, Cheuk
2004-03-01 19:34 Chris Lattner
2004-03-03  0:33 Robert Dewar

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