Skip to main content

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [List Home]
[cdt-dev] refactoring that replaces multiple nodes in the same branch of a subtree

Hi all,

 

It should be something frequently hit, but I can’t find an elegant solution. I need to do a refactoring that involves replacing two nodes located in one branch of a subtree. For example, I need to replace:

 

a = a + 1;

 

with:

 

a.write(a.read() + 1);

 

So I need to rewrite the assignment operator (“a = …“) as well as the subexpression (“a”) in the subtree. I cannot do it with two replace rewrites, because when I rewrite the assignment operator, I would use the “copy” method on the subtree but it would not include any rewrites happened below the tree as far as I can tell. So what I can do is to detect if the subtree has any nodes that need rewrites, and then traverse it by recreating the tree and substituting the node that needs a rewrite with a new node, but this solution seems to need code for recreating all types of ASTNodes! Do you see how I can do it another way that is more elegant? Something like a shallow copy for ASTNodes would help in the solution I mentioned, but I couldn’t find such a method either.

 

Thanks,

Andrey


Back to the top