This is the mail archive of the gcc@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: [G++] templates not instantiated early enough


On 4 Jun 2004, Gabriel Dos Reis wrote:

> Chris Lattner <sabre@nondot.org> writes:
>
> | I've been tracking down a G++ bug when using my target, and I think that
> | the problem needs to be fixed in the generic portion of the C++ front-end
> | (which I know very little about).
> |
> | In particular, consider this testcase:
> |
> | template<typename Ty>
> | struct some_template {};
> |
> | void foo(some_template<int>);
> |
> | void bar() {
> |   foo;
> | }
> |
> | Now it appears that G++ is delaying instantiating the some_template type
> | until it actually needs it.
>
> When the declaration of foo is seen, the completeness of the class
> type some_template<int> is not needed, only its declaration -- which
> the compiler synthetizes.

Agreed.

> |  In particular, the prototype for foo doesn't
> | get emitted to the backend until foo is referenced in "bar".
>
> I don't understand this.  When foo is declared, it gets assigned a
> type, which is FUNCTION_TYPE (void, some_template<int>).

Yes, of course you're right.  The problem is that the function prototype
is being mark_used and sent to the backend.  At this point the template is
not instantiated yet, so the backend can't look into the structure
argument to see what it contains.  Andrew pointed out an example of how
this can actually result in an erroneous program not being flagged as
such.

> |  This happens
> | when the parser sees the reference to foo, and decides that it really
> | is referring to the foo function in finish_id_expression.
> |
> | The problem is that, in this testcase (which is reduced from a
> | non-pointless larger testcase), the some_template is never instantiated
> | with an int type.
>
> It does not need to.  Its completeness is not required.  It is only
> referenced in a context of parameter-type-list, in a declaration
> of foo and no call if made to that function.

That's fine, and I understand how it is valuable to avoid instantiation
tons of templates that aren't used.  However, the foo function is
referenced in the program, despite the fact that it is not called.
Because it is being referenced, I believe the template ought to be
instantiated (even if it's not a call).

> |  This causes problems for my backend, which needs to
> | look at the types for arguments for foo.
>
> If your backend unconditionally assumes that any type referenced in a
> function declaration ought to be a complete type, then it contains a bug.

It's not that it unconditionally assumes it: that would be easy to fix.
The problem is that it needs to know the elements and layout information
about structures passed by values when it emits the prototypes to the
backend.

-Chris

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


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]