[cfe-dev] Function Instantiation: Where? Or is it?

Larisse Voufo lvoufo at cs.indiana.edu
Tue Feb 1 11:45:52 PST 2011


Let me see if I can rephrase this problem...
At this point, instantiation is no longer an issue. The issue is managing
new DeclContext's.

Take the following definition of a concept:

concept A<typename T> {
    void f(int) { }
}

This definition is parsed into a ConceptDecl  object *ContextA* where
ConceptDecl is a DeclContext as well as a TemplateDecl.
When the concept is used on a generic function such as:

template<typename T>
requires A<T>
void func(T a) {
    f(a);
}

at the call f(a),  the lookup process finds the definition of f() in the
DeclContext *ContextA*.
Hence, the definition of func() is accepted.
Let's refer to the found definition as *Fn*.

Now, when func() is used in the main, the lookup and instantiation-related
processes are still finding and using that same definition of f(): Fn.

However, when compilation is over, and things are being linked together, Fn
(of f()) is lost, and can't be found?
It seems like a fatal assumption is being made about the way that the
various lookup* methods in SemaLookup.cpp work and should be used. I just
don't know what those assumptions are...
For one thing, could the use of LookupQualifiedName(...) perhaps entail
something else that would affect link time resolution?


On Mon, Jan 31, 2011 at 6:19 PM, Douglas Gregor <dgregor at apple.com> wrote:

>
> On Jan 31, 2011, at 3:18 PM, Larisse Voufo wrote:
>
>
>
> On Mon, Jan 31, 2011 at 5:50 PM, Douglas Gregor <dgregor at apple.com> wrote:
>
>>
>> On Jan 31, 2011, at 1:43 PM, Larisse Voufo wrote:
>>
>> Thanks. That was a lot helpful. However, I do have another question.
>> Consider the following two case scenari:
>>
>> Case 1:
>> ------------
>> The previous one, with
>> (1) f() defined as a concept member, and
>> (2) func() a restricted template
>>
>>
>> Case 2:
>> ------------
>> (1) f() is a global function, and
>> (2) func() is a simple template, as in
>>
>> void f() { }
>>
>> template<typename T>
>> void func(T a) {
>>     f(0);
>> }
>>
>> int main(int argc, char **argv)
>> {
>>     int i=0;
>>     func<int>(i);
>> }
>>
>>
>> Observation:
>> ----------------------
>> In both cases, f() never seems to be "marked for instantiation", at least
>> not through Sema::
>> MarkDeclarationReferenced. However, f() does seem to eventually get
>> instantiated in the "Case 2" somehow, whereas it never does in "Case 1". Any
>> idea?
>>
>> 1) When does f() get instantiated in "Case 2"?
>>
>>
>> Case 2 is ill-formed, but the general rule is that
>> MarkDeclarationReferenced will get called when we use that function in a
>> potentially evaluated context, either in non-template code or during
>> template instantiation.
>>
>> Sorry about this. I meant for the definition of f() to take an integer in
> as argument...
>
>
>> 2) is this connected to lookup somehow?
>>      I have made some very minor changes in Sema::CppLookupName(), and I'm
>> not sure how this could affect instantiation...
>>
>>
>> It's more likely that you haven't added any logic to permit the
>> instantiation of functions defined within concept maps.
>>
>
> Any rule of thumb on how to add such logic? Say, for example how is this
> currently setup for  "(corrected) Case 2"?
>
>
> Please see the handling of function templates in
> Sema::MarkDeclarationReferenced and the actual instantiation in
> Sema::InstantiateFunctionDefinition. It probably needs a small tweak for
> concept maps, but there's no more advice anyone can give without looking
> specifically at your representation of concept maps.
>
> - Doug
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/cfe-dev/attachments/20110201/94d75a84/attachment.html>


More information about the cfe-dev mailing list