<div dir="ltr">On Tue, Apr 16, 2013 at 8:47 PM, Manuel Klimek <span dir="ltr"><<a href="mailto:klimek@google.com" target="_blank">klimek@google.com</a>></span> wrote:<br><div class="gmail_extra"><div class="gmail_quote">
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir="ltr">+djasper, alexfh<div><div class="h5"><div class="gmail_extra"><br></div><div class="gmail_extra"><div class="gmail_quote">
On Tue, Apr 16, 2013 at 7:15 PM, Vane, Edwin <span dir="ltr"><<a href="mailto:edwin.vane@intel.com" target="_blank">edwin.vane@intel.com</a>></span> wrote:<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">Hi all,<br>
<br>
I've been looking at implementing sub-expression references in AST Matchers. I'd like to propose an implementation and get feedback, especially if I've overlooked something.<br>
<br>
======<br>
Quick motivation first:<br>
The goal of sub-expression references is to write matchers that can refer to nodes tagged by the matcher and use these nodes in more tests. Consider the following simple example that wants to find variable declarations whose type exactly matches the initializer type (for simplicity, lets ignore all the implicit cast stuff that will exist around the initializer):<br>


<br>
varDecl(hasType(qualType(...).bind("declType")),<br>
               hasInitializer(hasType(sameAs("declType")))<br></blockquote></div></div></div></div></div></blockquote><div><br></div><div style>Minor point: I'd guess we'll need to specify the type here explicitly, like sameAs<QualType>("declType"). Also, I'm not very happy with the color of the bike shed called "sameAs" yet :)</div>
<div style><br></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir="ltr"><div><div class="h5"><div class="gmail_extra"><div class="gmail_quote"><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">

)<br>
<br>
There's potential for other sub-expression reference matchers beyond the "sameAs()" matcher proposed by this example. Name comparisons for example.<br>
======<br>
<br>
Proposed changes to implement sub-expression references:<br>
- In a sub-ex matcher, look-up in the provided BoundNodesTreeBuilder for the given id.<br>
  - If the id exists perform matcher logic, return truth value.<br>
  - If an id doesn't exist, record an unresolved sub-expression reference in the Builder and return true.<br>
    - Info recorded: id we're looking for and some sort of matcher "future": a callback or functor for executing matcher logic at a later time along with all necessary data matcher needs.<br>
- When control returns to the top level finder logic (MatchASTVisitor::match()) we tend to unresolved sub-ex refs. For each recorded unresolved sub-ex ref, do a search through the BoundNodesTree for a matching id. If one is found, execute matcher future. If the future returns false, the match fails and the Callback that would normally get called will not be. If no id is found, match also fails. If the matcher future returns true, proceed to calling Callback for the match. The handling of unresolved sub-ex refs could be handled by modifying MatchVisitor.<br>
</blockquote></div></div></div></div></div></blockquote><div><br></div><div style>I'm currently opposed to unresolved subexpression matcher handling (but happy to be convinced otherwise).</div><div style>Currently, we have:</div>
<div style>a) a defined order of execution</div><div style>b) a well-defined cut-off for logical expressions</div><div style><br></div><div style>That is, if you say anyOf(a(), b()), b() will not be matched if a() matches. Now a() could contain a sub-expression matcher referring to something that might be bound in b(); how would you propose to solve this?</div>
<div style><br></div><div style>And on a more principled note:</div><div style>Which things can we solve with unresolved subexpression matchers that cannot also be solved by re-arranging the matcher so that the bind() to the id must always happen before the sameAs match, otherwise sameAs will not match?</div>
<div> </div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir="ltr"><div><div class="h5"><div class="gmail_extra"><div class="gmail_quote"><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">

Outstanding questions:<br>
- Where should unresolved sub-ex refs be stored? Top-most level of the BoundNodesTree or just in the level at which the ref happens. Does it matter? Sub-ex refs should be able to refer to nodes anywhere else in the matcher and not have specific scope. Even if refs are stored at the current level, they should be collected and handled all at once when matching is complete.<br>


<br>
--<br>
Edwin Vane<br>
  Software Developer<br>
  Intel of Canada, Inc.<br>
</blockquote></div><br></div></div></div></div>
</blockquote></div><br></div></div>