public inbox for gcc@gcc.gnu.org
 help / color / mirror / Atom feed
* When to emit the vtable for a class template
@ 2002-10-10 13:50 Matt Austern
  2002-10-10 15:15 ` Matt Austern
  0 siblings, 1 reply; 5+ messages in thread
From: Matt Austern @ 2002-10-10 13:50 UTC (permalink / raw)
  To: gcc

I'm not sure whether the compiler is following what the ABI
specification (http://www.codesourcery.com/cxx-abi/abi.html)
says.  Entirely possible that I'm misunderstanding what the
ABI document is saying, of course.

The question: if you've got a polymorphic class template,
which translation units do you emit the vtable in?

5.2.3 says that you emit a vtable in the object file where you've
got the definition of the key function, or in every object file if
no key function exists.  5.2.3 may or may not apply to a class
template, of course.

5.2.7 seems to say that you emit a vtable for a class template
in every object file where the class template is instantiated,
irrespective of key functions.

What I'm not completely clear of is what exactly it means to
say that a class template is instantiated.  The most reasonable
interpretation is that the ABI specification is using this language
in the same sense of the C++ Standard, meaning that a class
template is instantiated "if the class type is used in a context that
requires a completely-defined object type or if the completeness
of the class type affects the semantics of the program".  Was that
the intention of the ABI specification and of the implementation?

			--Matt

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

* Re: When to emit the vtable for a class template
  2002-10-10 13:50 When to emit the vtable for a class template Matt Austern
@ 2002-10-10 15:15 ` Matt Austern
  2002-10-10 15:24   ` Gabriel Dos Reis
  0 siblings, 1 reply; 5+ messages in thread
From: Matt Austern @ 2002-10-10 15:15 UTC (permalink / raw)
  To: Matt Austern; +Cc: gcc

On Thursday, October 10, 2002, at 12:53 PM, Matt Austern wrote:

> I'm not sure whether the compiler is following what the ABI
> specification (http://www.codesourcery.com/cxx-abi/abi.html)
> says.  Entirely possible that I'm misunderstanding what the
> ABI document is saying, of course.
>
> The question: if you've got a polymorphic class template,
> which translation units do you emit the vtable in?
>
> 5.2.3 says that you emit a vtable in the object file where you've
> got the definition of the key function, or in every object file if
> no key function exists.  5.2.3 may or may not apply to a class
> template, of course.
>
> 5.2.7 seems to say that you emit a vtable for a class template
> in every object file where the class template is instantiated,
> irrespective of key functions.
>
> What I'm not completely clear of is what exactly it means to
> say that a class template is instantiated.  The most reasonable
> interpretation is that the ABI specification is using this language
> in the same sense of the C++ Standard, meaning that a class
> template is instantiated "if the class type is used in a context that
> requires a completely-defined object type or if the completeness
> of the class type affects the semantics of the program".  Was that
> the intention of the ABI specification and of the implementation?
>

Oh, and to follow up to myself...  If indeed (a) a vtable is to be
emitted in every translation unit where a polymorphic class
template is instantiated; and (b) "instantiated" is used in the
same sense as in clause 14 of the C++ Standard, then I've
found a case where it looks like that's not what's happening.
Consider the following translation unit:

     template<class T> struct X {
       virtual void foo();
       virtual ~X();
     };

     template <class T> void X<T>::foo() { }


     X<int>* xyzzy(X<int>* p) {
       return p + 1;
     }

Clearly X<int> must be instantiated.  However, compiling this
with -S and looking at the assembly code, I'm not seeing
X<int>'s vtable.

So, my question stands: under what circumstances does the
compiler emit a vtable for a polymorphic class template?  Is
the answer the same as what the ABI specification says it
should be?

			--Matt

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

* Re: When to emit the vtable for a class template
  2002-10-10 15:15 ` Matt Austern
@ 2002-10-10 15:24   ` Gabriel Dos Reis
  2002-10-10 15:30     ` Matt Austern
  0 siblings, 1 reply; 5+ messages in thread
From: Gabriel Dos Reis @ 2002-10-10 15:24 UTC (permalink / raw)
  To: Matt Austern; +Cc: gcc

Matt Austern <austern@apple.com> writes:

[...]

| Consider the following translation unit:
| 
|      template<class T> struct X {
|        virtual void foo();
|        virtual ~X();
|      };
| 
|      template <class T> void X<T>::foo() { }
| 
| 
|      X<int>* xyzzy(X<int>* p) {
|        return p + 1;
|      }
| 
| Clearly X<int> must be instantiated.

Hmm, if "instantiated" is used as the C++ standard uses it, then I'm
not clear why X<int> ought to be instantiated.  AFAIK, only the
template-id 'X<int>' needs to be "instantiated". 

Consider

   struct A;

   A* foo(X* p) { return p + 1; }

That is a valid construct that does not need completeness of type A and
the situation is not different from the X<int> case.

| However, compiling this
| with -S and looking at the assembly code, I'm not seeing
| X<int>'s vtable.

I fear the vtable for X<int> does not need to be emitted.

| So, my question stands: under what circumstances does the
| compiler emit a vtable for a polymorphic class template? 

I'm however unable to give an informed answer to this question.

| Is
| the answer the same as what the ABI specification says it
| should be?

I would expect so.

-- Gaby

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

* Re: When to emit the vtable for a class template
  2002-10-10 15:24   ` Gabriel Dos Reis
@ 2002-10-10 15:30     ` Matt Austern
  2002-10-10 19:42       ` Gabriel Dos Reis
  0 siblings, 1 reply; 5+ messages in thread
From: Matt Austern @ 2002-10-10 15:30 UTC (permalink / raw)
  To: Gabriel Dos Reis; +Cc: gcc

On Thursday, October 10, 2002, at 02:22 PM, Gabriel Dos Reis wrote:

> Matt Austern <austern@apple.com> writes:
>
> [...]
>
> | Consider the following translation unit:
> |
> |      template<class T> struct X {
> |        virtual void foo();
> |        virtual ~X();
> |      };
> |
> |      template <class T> void X<T>::foo() { }
> |
> |
> |      X<int>* xyzzy(X<int>* p) {
> |        return p + 1;
> |      }
> |
> | Clearly X<int> must be instantiated.
>
> Hmm, if "instantiated" is used as the C++ standard uses it, then I'm
> not clear why X<int> ought to be instantiated.  AFAIK, only the
> template-id 'X<int>' needs to be "instantiated".

5.7, paragraph 1: "For addition ... one operand shall be a pointer to
a completely defined object type and the other shall have integral
or enumeration type".  (Intuitively, X<int> needs to be complete for
pointer arithmetic to work because we need to know X<int>'s size.)

Since X<int> has to be a complete type, it follows from 14.7.1,
paragraphs 1 and 4, that this is an implicit instantiation of X<int>.

My (possibly incorrect) reading of the ABI specification is that an
implicit instantiation of a polymorphic class template results in
having its vtable emitted.

			--Matt

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

* Re: When to emit the vtable for a class template
  2002-10-10 15:30     ` Matt Austern
@ 2002-10-10 19:42       ` Gabriel Dos Reis
  0 siblings, 0 replies; 5+ messages in thread
From: Gabriel Dos Reis @ 2002-10-10 19:42 UTC (permalink / raw)
  To: Matt Austern; +Cc: gcc

Matt Austern <austern@apple.com> writes:

| On Thursday, October 10, 2002, at 02:22 PM, Gabriel Dos Reis wrote:
| 
| > Matt Austern <austern@apple.com> writes:
| >
| > [...]
| >
| > | Consider the following translation unit:
| > |
| > |      template<class T> struct X {
| > |        virtual void foo();
| > |        virtual ~X();
| > |      };
| > |
| > |      template <class T> void X<T>::foo() { }
| > |
| > |
| > |      X<int>* xyzzy(X<int>* p) {
| > |        return p + 1;
| > |      }
| > |
| > | Clearly X<int> must be instantiated.
| >
| > Hmm, if "instantiated" is used as the C++ standard uses it, then I'm
| > not clear why X<int> ought to be instantiated.  AFAIK, only the
| > template-id 'X<int>' needs to be "instantiated".
| 
| 5.7, paragraph 1: "For addition ... one operand shall be a pointer to
| a completely defined object type and the other shall have integral
| or enumeration type".

Oops, I spoke non-sense.  Please, ignore my previous message -- I
should have rethought the issue before pressing the send key.

[...]

| My (possibly incorrect) reading of the ABI specification is that an
| implicit instantiation of a polymorphic class template results in
| having its vtable emitted.

I think your interpretation makes sense and you have a valid point.

-- Gaby

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

end of thread, other threads:[~2002-10-10 21:43 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2002-10-10 13:50 When to emit the vtable for a class template Matt Austern
2002-10-10 15:15 ` Matt Austern
2002-10-10 15:24   ` Gabriel Dos Reis
2002-10-10 15:30     ` Matt Austern
2002-10-10 19:42       ` Gabriel Dos Reis

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