Skip to content

Commit

Permalink
[SyntaxTree] Refactor NodeRoles
Browse files Browse the repository at this point in the history
Previously a NodeRole would generally be prefixed with the `NodeKind`,
we remove this prefix, as it we redundant and made tests more noisy.

Differential Revision: https://reviews.llvm.org/D86636
  • Loading branch information
Eduardo Caldas committed Aug 27, 2020
1 parent a45ccc9 commit 718e550
Show file tree
Hide file tree
Showing 5 changed files with 1,019 additions and 1,065 deletions.
79 changes: 38 additions & 41 deletions clang/include/clang/Tooling/Syntax/Nodes.h
Original file line number Diff line number Diff line change
Expand Up @@ -100,20 +100,22 @@ enum class NodeKind : uint16_t {
MemberPointer,
UnqualifiedId,
ParameterDeclarationList,
CallArguments,
// Nested Name Specifiers.
NestedNameSpecifier,
GlobalNameSpecifier,
DecltypeNameSpecifier,
IdentifierNameSpecifier,
SimpleTemplateNameSpecifier,
CallArguments
};
/// For debugging purposes.
raw_ostream &operator<<(raw_ostream &OS, NodeKind K);

/// A relation between a parent and child node, e.g. 'left-hand-side of
/// a binary expression'. Used for implementing accessors.
///
/// In general `NodeRole`s should be named the same as their accessors.
///
/// Some roles describe parent/child relations that occur multiple times in
/// language grammar. We define only one role to describe all instances of such
/// recurring relations. For example, grammar for both "if" and "while"
Expand All @@ -124,12 +126,6 @@ raw_ostream &operator<<(raw_ostream &OS, NodeKind K);
/// opening paren), we define a role for this token and use it across all
/// grammar rules with the same requirement. Names of such reusable roles end
/// with a ~Token or a ~Keyword suffix.
///
/// Some roles are assigned only to child nodes of one specific parent syntax
/// node type. Names of such roles start with the name of the parent syntax tree
/// node type. For example, a syntax node with a role
/// BinaryOperatorExpression_leftHandSide can only appear as a child of a
/// BinaryOperatorExpression node.
enum class NodeRole : uint8_t {
// Roles common to multiple node kinds.
/// A node without a parent
Expand All @@ -144,46 +140,45 @@ enum class NodeRole : uint8_t {
IntroducerKeyword,
/// A token that represents a literal, e.g. 'nullptr', '1', 'true', etc.
LiteralToken,
/// Tokens or Keywords
/// Tokens or Keywords.
ArrowToken,
ExternKeyword,
TemplateKeyword,
/// An inner statement for those that have only a single child of kind
/// statement, e.g. loop body for while, for, etc; inner statement for case,
/// default, etc.
BodyStatement,
List_element,
List_delimiter,
/// List API roles.
ListElement,
ListDelimiter,

// Roles specific to particular node kinds.
OperatorExpression_operatorToken,
UnaryOperatorExpression_operand,
BinaryOperatorExpression_leftHandSide,
BinaryOperatorExpression_rightHandSide,
CaseStatement_value,
IfStatement_thenStatement,
IfStatement_elseKeyword,
IfStatement_elseStatement,
ReturnStatement_value,
ExpressionStatement_expression,
CompoundStatement_statement,
StaticAssertDeclaration_condition,
StaticAssertDeclaration_message,
SimpleDeclaration_declarator,
TemplateDeclaration_declaration,
ExplicitTemplateInstantiation_declaration,
ArraySubscript_sizeExpression,
TrailingReturnType_declarator,
ParametersAndQualifiers_parameters,
ParametersAndQualifiers_trailingReturn,
IdExpression_id,
IdExpression_qualifier,
ParenExpression_subExpression,
MemberExpression_object,
MemberExpression_accessToken,
MemberExpression_member,
CallExpression_callee,
CallExpression_arguments,
OperatorToken,
Operand,
LeftHandSide,
RightHandSide,
ReturnValue,
CaseValue,
ThenStatement,
ElseKeyword,
ElseStatement,
Expression,
Statement,
Condition,
Message,
Declarator,
Declaration,
Size,
Parameters,
TrailingReturn,
UnqualifiedId,
Qualifier,
SubExpression,
Object,
AccessToken,
Member,
Callee,
Arguments,
};
/// For debugging purposes.
raw_ostream &operator<<(raw_ostream &OS, NodeRole R);
Expand Down Expand Up @@ -656,7 +651,7 @@ class CaseStatement final : public Statement {
return N->kind() == NodeKind::CaseStatement;
}
Leaf *caseKeyword();
Expression *value();
Expression *caseValue();
Statement *body();
};

Expand Down Expand Up @@ -736,7 +731,7 @@ class ReturnStatement final : public Statement {
return N->kind() == NodeKind::ReturnStatement;
}
Leaf *returnKeyword();
Expression *value();
Expression *returnValue();
};

/// for (<decl> : <init>) <body>
Expand Down Expand Up @@ -972,7 +967,7 @@ class ArraySubscript final : public Tree {
}
// TODO: add an accessor for the "static" keyword.
Leaf *lbracket();
Expression *sizeExpression();
Expression *size();
Leaf *rbracket();
};

Expand All @@ -986,6 +981,8 @@ class TrailingReturnType final : public Tree {
}
// TODO: add accessors for specifiers.
Leaf *arrowToken();
// FIXME: This should be a `type-id` following the grammar. Fix this once we
// have a representation of `type-id`s.
SimpleDeclarator *declarator();
};

Expand Down
Loading

0 comments on commit 718e550

Please sign in to comment.