roslyn: Why doesn't Roslyn have an IdentifierExpressionSyntax AST node?
This isn’t an issue, but a question. However, I couldn’t find a discussion mailing list about Roslyn itself (only for C#/VB as a whole) or an IRC channel - please, let me know if I missed it.
Why isn’t there an “identifier expression” node in Roslyn’s AST? Compiler infrastructures I’ve worked with (and wrote) typically have an IdentifierExpressionAST
node. This allows me to immediately distinguish between ordinary names in the program and those that are proper expressions.
As an example, consider the declaration namespace Foo { }
and the statement i = 10;
. Traversal of Roslyn’s AST provides both names Foo
and i
in the form of an IdentifierNameSyntax
(which inherits from ExpressionSyntax
), but those two kinds of names are fundamentally different: (i) the former is purely for nominative organization; (ii) the latter denotes an actual value, it’s a term.
Currently, I distinguished an identifier expressions using a check such as the one below:
public override SyntaxNode VisitIdentifierName(IdentifierNameSyntax node)
{
if (node.Ancestors().OfType<ExpressionSyntax>().Any()
|| node.Parent.IsKind(SyntaxKind.ReturnStatement))
{
//...
}
return node;
}
Is this the correct way of achieving such differentiation? If so, what are the primary advantages of such design that makes every sort of name an expression?
About this issue
- Original URL
- State: closed
- Created 6 years ago
- Comments: 20 (16 by maintainers)
We made an explicit decision when designing the syntax trees to avoid trees that merely represent “chain productions” of nonterminals in the grammar. One plus of doing things this way is that syntax trees are much more compact. Your observation here is the main downside.