<br><br><div class="gmail_quote">On Fri, Jan 9, 2009 at 1:33 AM, Douglas Gregor <span dir="ltr"><<a href="mailto:dgregor@apple.com">dgregor@apple.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">
<div class="Ih2E3d"><br>
On Jan 8, 2009, at 6:17 AM, Zhongxing Xu wrote:<br>
<blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">
Currently we have two mechanisms to do name lookup: IdentifierResolver and DeclContext. In Sema::LookupDecl(), we combine the two approaches to fulfil the name lookup task. I really feel that the code in Sema::LookupDecl() logically uninituitive and error prone. Could we use DeclContext exclusively to do name lookup and eliminate the IdentifierResolver use?<br>

</blockquote>
<br></div>
Currently, no; the chains of declarations held in the IdentifierResolver contain information local declarations that are all in the same DeclContext. For example:<br>
<br>
void f(int x) { // #1<br>
  {<br>
    int x; // #2<br>
      {<br>
        int x; // #3<br>
<br>
       printf("%i", x); //  this 'x' refers to #3<br>
      }<br>
  }     <br>
}<br>
<br>
Here, the IdentifierResolver has a chain of declarations #3 -> #2 -> #1 for the name 'x'. However, all three 'x's live in the DeclContext associated with f's FunctionDecl.<br>
<br>
Now, it is possible that we want compound statements to be DeclContexts, so that the 'x's would be be in different DeclContexts. That might be a good generalization (regardless of whether we use it for name lookup), because it would mean that more of the scope structure of the program is present in the ASTs after parsing. But, that gets to your comment below...<div class="Ih2E3d">
<br>
<br>
<blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">
I think that way could make name lookup logically more clear. My only concern is if that could cause performance lost?<br>
</blockquote>
<br>
<br></div>
Yes, it would most certainly cause performance degradation if we used DeclContext for all name lookup. The beauty of IdentifierResolver is that the chain of declarations in scope for a particular name is attached to the IdentifierInfo for the name, so it's O(1) to find the first declaration with that name that's in scope. If we're using just DeclContexts, we'd have to walk the scope chain upward, checking DeclContext along the way (each check is either a search through a small array or a hash table lookup).<br>

<br>
In fact, some of the optimization comments in LookupDecl (and other optimization ideas that have yet to be written down) involve trying to skip calls to DeclContext::lookup when we know that we would have found the answer on the IdentifierResolver chain if there were an answer.<br>

<br>
So, I think the complexity of LookupDecl is inherent to solving the problem efficiently. It's also extremely important to performance: when compiling a C++ program that includes <iostream>, GCC spends 26% of its time in name lookup.<br>

<br>
        - Doug<br>
</blockquote></div><br>Thanks for the explanation, Doug.<br>