[cfe-dev] TreeTransform and Clang

Douglas Gregor dgregor at apple.com
Mon Aug 9 09:02:24 PDT 2010


On Aug 9, 2010, at 7:26 AM, Vassil Vassilev wrote:

> On Sat, 2010-08-07 at 21:58 +0200, Douglas Gregor wrote:
>> On Aug 7, 2010, at 6:19 PM, Vassil Vassilev wrote:
>> 
>>> On Sat, 2010-08-07 at 13:47 +0200, Douglas Gregor wrote:
>>>> On Aug 7, 2010, at 9:28 AM, Vassil Vassilev wrote:
>>>> 
>>>>> Hello,
>>>>> Hm, Is the error handling in the semantic analysis, too?
>>>> 
>>>> Yes.
>>>> 
>>>>> I need the following
>>>>> For example (I am skipping the pointers and other ugly things that
>>>>> made it less readable):
>>>>> 
>>>>> funtion F(int x, float y,string z) {
>>>>>   int i = objX.GetValue(x);  //compile time object and method
>>>>> invocation
>>>>>   TFile t = new TFile("MyFile.hhh");  //here goes the interpreter,but
>>>>> it needs addresses of the variables from the compiler
>>>>>   i = t.DoSomething(i, x, y, z);
>>>>>   Console.WriteLine(i.ToString());
>>>>> }
>>>>> 
>>>>> Here the semantic analyzer should say undeclared variable and so on. 
>>>> 
>>>> Yes, it will.
>>>> 
>>>>> Before that happens I want to change the AST and to convert it in something like:
>>>>> 
>>>>> funtion F(int x, float y,string z) {      
>>>>>   int i = objX->GetValue(x);  //compile time object and method invocation      
>>>>>   TFile t = new TFile("MyFile.hhh");  //here goes the interpreter,but it needs addresses of the variables from the compiler      
>>>>>   Context c = new Context();
>>>>>   c.AddVariable(i.GetDeclaration()) ; //here we should use some kind of  reflection or we can insert just the variable address and the proper mapping.   
>>>>>   c.AddArgument(x.GetDeclaration());
>>>>>   ...
>>>>>   Interpreter.Interpret("t->DoSomething(i,x,y,z)", c);
>>>>>   i = t.DoSomething(i, x, y, z);
>>>>>   Console.WriteLine(i.ToString());   
>>>>> } 
>>>>> 
>>>>> I know I can modify the Sema and/or put something in the symbol table
>>>>> to achieve the goal but I am looking for more elegant solution.
>>>> 
>>>> The right way to do this would be to augment Sema's name-lookup facilities, so that your application gets a chance to provide symbols when no other symbols of the same name work. One way to do this is to create your own ExternalASTSource, and override FindExternalVisibleDeclsByName to add those symbols. If I remember correctly, LLDB does this.
>>> I understand. But I want only to replace one node of the AST with
>>> another. I don't need to exchange external symbol names and so on (at
>>> least for now). Is it possible to hook somewhere before the error
>>> handling and substitute the ExpressionStatement (i = t.DoSomething(i, x,
>>> y, z);) With new BlockStatement or several ExpressionStatements.
>> 
>> You might be able to hack Sema to do this, but I don't see any clean way to do it.
> I am thinking about 2 passes:
> 1-st pass I can only put "TFile t = new TFile("MyFile.hhh");", which is
> valid construction and it will be marker for the next pass.
> 2-nd pass I will use the TreeTransform to extend the block with the
> custom invocations of the interpreter. 
> I need to use some kind of preprocessors to achieve that, because at the
> first pass I need to escape the "unknown" for the sema symbols, right?

I don't know what you plan to do with "unknown" symbols, but I can't think of any approach that is likely to work. The parser needs to know what every symbol is so that it can inform the semantic analysis module, which then builds the ASTs. An entity with "unknown" type won't pass semantic analysis and therefore we won't get an AST for it. There's not really any way around with problem, since C++ is so ridiculously context-sensitive.

Your best bet is to intercept name lookup and provide a proper, fully-formed symbol at the time when the parser needs it. Then, you'll get well-formed ASTs from well-formed code.

> After that I need somehow to send them to TreeTransformer. Can you give
> me basic directions or better approach to achieve my goal?


Perhaps we should step back a bit, because I don't think TreeTransform is what you need: what are you trying to accomplish with Clang?

	- Doug



More information about the cfe-dev mailing list