Bug 489526 - ASTVisitor does not have enough visiting methods
Summary: ASTVisitor does not have enough visiting methods
Status: NEW
Alias: None
Product: CDT
Classification: Tools
Component: cdt-core (show other bugs)
Version: 8.8.1   Edit
Hardware: PC Linux
: P3 normal (vote)
Target Milestone: ---   Edit
Assignee: Project Inbox CLA
QA Contact: Jonah Graham CLA
URL:
Whiteboard:
Keywords:
Depends on:
Blocks:
 
Reported: 2016-03-14 06:14 EDT by Nicolas Anquetil CLA
Modified: 2020-09-04 15:20 EDT (History)
2 users (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Nicolas Anquetil CLA 2016-03-14 06:14:56 EDT
I am trying to use CDT and its visitor infrastructure to parse a C++ project.

It seems that the visitor methods are too few and too "high level"

For example the same visit(...) method is called whether it is a type declaration/class/declaration/function declaration:
visit(IASTDeclSpecifier)

This forces the user of the visitor to do a lot of additional checks (instanceof) in these too generic methods.

A possible cause would be that too few accept(Visitor) methods have been implemented in the different ASTNode subclasses ... ?


I would be willing to help improve this part (if my diagnosis is correct) as it is really painful to use the visitor as it is

nicolas
Comment 1 Sergey Prigogin CLA 2016-03-14 15:02:45 EDT
It's not clear what is being proposed in comment #0. ASTVisitor.visit methods are distinguished by the type of the argument. Is the proposal is to somehow change that?
Comment 2 Nicolas Anquetil CLA 2016-03-15 08:32:51 EDT
thanks for looking into it.

The issue is that currently, the visitor does not seem to be able to receive visit(CPPASTNamedTypeSpecifier) or visit(IASTFunctionDeclarator) for example.
I was wondering why?

The problem is that it forces one to put instanceof the visit method to check what subtype of the visit method one is actually dealing with.

A possible solution/proposal was to put more accept(aVisitor) in all classes AST classes so that the visitor could receive such calls.
Comment 3 Sergey Prigogin CLA 2016-03-15 13:53:19 EDT
(In reply to Nicolas Anquetil from comment #2)

The current minimalistic approach used by ASTVisitor has a significant advantage that it is always easy to anticipate what method will be called for any AST node. Specializing visit and leave methods by of the parameter would introduce additional confusion and complexity. Consider, for example, visit(IASTStatement). This method would have to be replaced by more than 20 separate methods. It wouldn't be immediately obvious what method is called for a compound statement since there would be three candidates to choose from. Using leaf nodes in the node type hierarchy for visit methods may be convenient for code that needs to distinguish between all different statement types but very inconvenient for code that does not.
Comment 4 Nicolas Anquetil CLA 2016-03-15 14:27:15 EDT
Yes I thought about this point.

My proposal would be to add in the default visitor (ASTVisitor I guess, or maybe ASTGenericVisitor?) specialized visit methods that would "point it back" to the generic method.

Thus, considering your example, one could define in the default visitor:

public int visit(IASTWhileStatement node) { return this.visit( (IASTStatement)node); }

That way code inheriting from the default visitor would not see any change, but code needing more specialized behaviour could override visit(IASTWhileStatement node) ...

nicolas
Comment 5 Sergey Prigogin CLA 2016-03-15 15:27:05 EDT
(In reply to Nicolas Anquetil from comment #4)

Somebody who writes a specialized visitor would still have to be acutely aware of node type hierarchy, e.g. ICPPASTCompoundStatement -> IASTCompoundStatement -> IASTStatement. Moreover, new branches of the type hierarchy may be introduced in future and break previously valid assumptions. You may say that the same is true for the chained "if" statements with instanceof checks, but at least ASTVisitor is not responsible for that.
Comment 6 Nicolas Anquetil CLA 2016-03-17 04:43:23 EDT
I am afraid I must disagree with you:

"Somebody who writes a specialized visitor would still have to be acutely aware of node type hierarchy"

1- Anybody writing a visitor must already know more or less what the type hierarchy is.
2- And same thing goes for the current situation because one needs to know to what super type a given node is "mapped" to know where to put the right instanceof

moreover, the example you give (ICPPASTCompoundStatement -> IASTCompoundStatement -> IASTStatement) show that anyway, the hierarchy is quite natural and intuitive. So it does not seem it would be a real issue.

"new branches of the type hierarchy may be introduced in future and break previously valid assumptions".
1- one may assume that not so many new AST nodes will appear in C in the future (even though I agree it can happen)
2- but as you say, if that happens, anyway both solution will have to deal with the problem.


Anyway, I think it defeats the very purpose of the Visitor pattern not to have all the visiting methods available (at least for all the available Java interfaces).
Comment 7 Nathan Ridge CLA 2017-01-21 01:35:08 EST
(In reply to Nicolas Anquetil from comment #4)
> My proposal would be to add in the default visitor (ASTVisitor I guess, or
> maybe ASTGenericVisitor?) specialized visit methods that would "point it
> back" to the generic method.

Interestingly, we have a class called ASTGenericVisitor, which is similar to what you describe, just a lot more generic: it "points back" all of the existing visit() methods to a genericVisit(IASTNode) method.

Anyways, I would expect that we would consider patches that enhance our AST visitors subject to the following constraints:

  - They do not break the behaviour of public API classes like 
    ASTVisitor.

  - They do not make existing uses of AST visitors inside CDT
    code significantly more verbose or complex.