<br><br><div class="gmail_quote">On Fri, Apr 22, 2011 at 12:38 PM, Douglas Gregor <span dir="ltr"><<a href="mailto:dgregor@apple.com">dgregor@apple.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin: 0pt 0pt 0pt 0.8ex; border-left: 1px solid rgb(204, 204, 204); padding-left: 1ex;">
<div style="word-wrap: break-word;"><br><div><div><div></div><div class="h5"><div>On Apr 22, 2011, at 9:27 AM, Larisse Voufo wrote:</div><br><blockquote type="cite"><br><br><div class="gmail_quote">On Fri, Apr 22, 2011 at 11:51 AM, Douglas Gregor <span dir="ltr"><<a href="mailto:dgregor@apple.com" target="_blank">dgregor@apple.com</a>></span> wrote:<br>
<blockquote class="gmail_quote" style="margin: 0pt 0pt 0pt 0.8ex; border-left: 1px solid rgb(204, 204, 204); padding-left: 1ex;">
<div style="word-wrap: break-word;"><br><div><div><div></div><div><div>On Apr 22, 2011, at 2:05 AM, Larisse Voufo wrote:</div><br><blockquote type="cite">Here's a basic example I've been trying to get to work:<br>

<br>explicit concept A<typename T> { <br>    typename AType;  <br><br>    AType h(AType a) { <br>        return a;<br>    }<br><br>    AType f(AType a) { <br>
        AType b;<br>
        return b;<br>
    }<br>}<br><br>concept_map A<int> {<br>    typedef char AType;<br>} <br><br>template<typename T><br>requires (A<T>) <br>void func(T a, AType b) {<br>    AType x = h(b);         // <-- Instantiation DOES NOT WORK<br>



    AType y = f(b);          // <-- Instantiation WORKS<br>}<br><br>int main(int argc, char **argv) <br>{ <br>    func<int>(10, 'a');      // Fails with declaration for 'x', for passes with declaration of 'y'.            <br>


}<br><br><br>It appears that during the instantiation of <b>func<int>()</b>, the value for input parameter '<b>b</b>' for '<b>func()</b>'<br>can be looked up no problem. However, that of '<b>a</b>' for associated function '<b>h()</b>' cannot. <br>


Instead, a call to <b>clang::LocalInstantiationScope::getInstantiationOf()</b> fails at runtime on the assertion:<br><br><b>(D->isInvalidDecl() && "declaration was not instantiated in this scope!")</b>.<br>


<br>I have been tracing through the procedures for building, transforming, and rebuilding Call Expressions -- as well as instantiating functions, noting each <br>execution of <b>clang::LocalInstantiationScope::getInstantiationOf()</b> and <b>clang::LocalInstantiationScope::InstantiatedLocal()</b>...<br>


<br>Still, I can't see what needs to be done differently for calls to <b>h()</b> from calls to <b>func<int>()</b>. Hence...<br><br>Question: <br>When/how exactly are input arguments bound to functions?<br></blockquote>

<div><br></div></div></div><div>That's a runtime issue. All the front-end does is enforce the calling convention on both sides of the call.</div></div></div></blockquote><div><br>And how does it do that? Exactly, when is <b>func<int></b>'s '<b>b</b>' supposed to meet <b>h</b>'s '<b>a</b>'? <br>

Just as... When do the <b>main</b>'s <b>10</b> and '<b>a</b>' meet <b>func<int></b>'s '<b>a</b>' and '<b>b</b>' ?<br></div></div></blockquote><div><br></div></div></div><div>At run time, the caller puts the call arguments into registers and on the stack, and executes a call instruction. The Clang front-end never does this; optimization passes may do it at the IR level.</div>
<div class="im"><br><blockquote type="cite"><div class="gmail_quote"><blockquote class="gmail_quote" style="margin: 0pt 0pt 0pt 0.8ex; border-left: 1px solid rgb(204, 204, 204); padding-left: 1ex;">
<div style="word-wrap: break-word;"><div><div><br><blockquote type="cite">Why would the current procedure fail to bind the value of '<b>b</b>' from the context of <b>func<int></b> to the <br>
parameter '<b>a</b>' of <b>h</b> ?<br></blockquote></div></div><br><div>They are in completely different contexts. func<int>'s b is completely disjoint from h's a. And, in fact, they might not be the same thing, since passing 'b' as an argument to h() implies the creation of a temporary.</div>

<div><br></div><div>The only way I could see this issue coming up is if you're doing some kind of inlining of A<AType>::h into func<int> as part of instantiation. If so, I recommend against doing that: inlining will be handled by later optimization passes, and tangling the func<int> instantiation with the A<AType>::h instantiation is going to cause more problems in the front end than it solves.</div>

</div></blockquote><div><br>Hmm... How would the call be handled if <b>h</b> was a global function -- a specialization of some template, not associated with any concept?<br></div></div></blockquote><div><br></div></div>Exactly the same here.</div>
<div class="im"><div><br><blockquote type="cite"><div class="gmail_quote"><div>I am not trying to achieve any inlining here, I just want to pass in the arguments as I would any inner function call (though thanks for the tip)...<br>
</div></div></blockquote></div><br></div><div>You don't need to do any argument passing. Let Clang do that for you. All you need to do is trigger the right instantiation.</div></div></blockquote><div><br>I believe I'm doing exactly what should be done with any function call, 
i.e. rebuilding the call expressions et al. -- after hijacking the 
callee pointer so it points to the appropriate concept's associated 
function implementation. <br>
Apparently, in the way that I'm currently using Clang, it is not doing a
 very good job at taking care of things for me, and I am not sure which 
"parameters" I may have overlooked.<br><br>I notice that the transformation of a call expression (via TreeTransform<>::<div>TransformCallExpr)  transforms both the callee and the arguments. I am doing just that after redefining the callee...<br>

Somehow, this looses the temporaries instantiation that happens for normal function calls... <br>Note that when I run examples where h is global, it works... But when I put h in a concept, it doesn't.<br>The local instantiation scope for the instantiation of h somehow can pick up any local variable, but not it's parameters...<br>

and this only happens in cases where h is in a concept context.<br> <br>Is there something that ActOnCallExpr does that is dependent on the calling context that I am overlooking?<br>I'm really not sure how to diagnose this issue anymore... :-/ <br>

<br>A quick hack that i could do, is explicitly inject an instantiation 
of function parameters after/while transforming the call expression. But
 I'm not sure (don't think) this is a good idea, as -- like you said -- 
Clang should automatically take care of this for me.<br>
<br>What do you think? I'll keep thinking about this, but thanks for your help already.<br><br></div> </div><blockquote class="gmail_quote" style="margin: 0pt 0pt 0pt 0.8ex; border-left: 1px solid rgb(204, 204, 204); padding-left: 1ex;">
<div style="word-wrap: break-word;"><div><br></div><div><span style="white-space: pre-wrap;">   </span>- Doug</div></div>
</blockquote></div><br>