From 860cbbdd6b84017e6d37e1752b0358a05da6b115 Mon Sep 17 00:00:00 2001 From: Eduardo Caldas Date: Thu, 23 Jul 2020 09:20:06 +0000 Subject: [PATCH] [SyntaxTree] Add support for `LiteralExpression` We use inheritance to model the grammar's disjunction rule: literal: integer-literal character-literal floating-point-literal string-literal boolean-literal pointer-literal user-defined-literal Differential Revision: https://reviews.llvm.org/D85186 --- clang/include/clang/Tooling/Syntax/Nodes.h | 57 +++++++++++++++++++----------- clang/lib/Tooling/Syntax/Nodes.cpp | 32 +---------------- 2 files changed, 37 insertions(+), 52 deletions(-) diff --git a/clang/include/clang/Tooling/Syntax/Nodes.h b/clang/include/clang/Tooling/Syntax/Nodes.h index d97b127..8a873f9 100644 --- a/clang/include/clang/Tooling/Syntax/Nodes.h +++ b/clang/include/clang/Tooling/Syntax/Nodes.h @@ -267,66 +267,82 @@ public: syntax::Leaf *closeParen(); }; +/// Expression for literals. C++ [lex.literal] +class LiteralExpression : public Expression { +public: + LiteralExpression(NodeKind K) : Expression(K) {} + static bool classof(const Node *N) { + return N->kind() == NodeKind::IntegerLiteralExpression || + N->kind() == NodeKind::CharacterLiteralExpression || + N->kind() == NodeKind::FloatingLiteralExpression || + N->kind() == NodeKind::StringLiteralExpression || + N->kind() == NodeKind::BoolLiteralExpression || + N->kind() == NodeKind::CxxNullPtrExpression || + N->kind() == NodeKind::IntegerUserDefinedLiteralExpression || + N->kind() == NodeKind::FloatUserDefinedLiteralExpression || + N->kind() == NodeKind::CharUserDefinedLiteralExpression || + N->kind() == NodeKind::StringUserDefinedLiteralExpression; + } + syntax::Leaf *literalToken(); +}; + /// Expression for integer literals. C++ [lex.icon] -class IntegerLiteralExpression final : public Expression { +class IntegerLiteralExpression final : public LiteralExpression { public: - IntegerLiteralExpression() : Expression(NodeKind::IntegerLiteralExpression) {} + IntegerLiteralExpression() + : LiteralExpression(NodeKind::IntegerLiteralExpression) {} static bool classof(const Node *N) { return N->kind() == NodeKind::IntegerLiteralExpression; } - syntax::Leaf *literalToken(); }; /// Expression for character literals. C++ [lex.ccon] -class CharacterLiteralExpression final : public Expression { +class CharacterLiteralExpression final : public LiteralExpression { public: CharacterLiteralExpression() - : Expression(NodeKind::CharacterLiteralExpression) {} + : LiteralExpression(NodeKind::CharacterLiteralExpression) {} static bool classof(const Node *N) { return N->kind() == NodeKind::CharacterLiteralExpression; } - syntax::Leaf *literalToken(); }; /// Expression for floating-point literals. C++ [lex.fcon] -class FloatingLiteralExpression final : public Expression { +class FloatingLiteralExpression final : public LiteralExpression { public: FloatingLiteralExpression() - : Expression(NodeKind::FloatingLiteralExpression) {} + : LiteralExpression(NodeKind::FloatingLiteralExpression) {} static bool classof(const Node *N) { return N->kind() == NodeKind::FloatingLiteralExpression; } - syntax::Leaf *literalToken(); }; /// Expression for string-literals. C++ [lex.string] -class StringLiteralExpression final : public Expression { +class StringLiteralExpression final : public LiteralExpression { public: - StringLiteralExpression() : Expression(NodeKind::StringLiteralExpression) {} + StringLiteralExpression() + : LiteralExpression(NodeKind::StringLiteralExpression) {} static bool classof(const Node *N) { return N->kind() == NodeKind::StringLiteralExpression; } - syntax::Leaf *literalToken(); }; /// Expression for boolean literals. C++ [lex.bool] -class BoolLiteralExpression final : public Expression { +class BoolLiteralExpression final : public LiteralExpression { public: - BoolLiteralExpression() : Expression(NodeKind::BoolLiteralExpression) {} + BoolLiteralExpression() + : LiteralExpression(NodeKind::BoolLiteralExpression) {} static bool classof(const Node *N) { return N->kind() == NodeKind::BoolLiteralExpression; } - syntax::Leaf *literalToken(); }; /// Expression for the `nullptr` literal. C++ [lex.nullptr] -class CxxNullPtrExpression final : public Expression { +class CxxNullPtrExpression final : public LiteralExpression { public: - CxxNullPtrExpression() : Expression(NodeKind::CxxNullPtrExpression) {} + CxxNullPtrExpression() : LiteralExpression(NodeKind::CxxNullPtrExpression) {} static bool classof(const Node *N) { return N->kind() == NodeKind::CxxNullPtrExpression; } - syntax::Leaf *nullPtrKeyword(); }; /// Expression for user-defined literal. C++ [lex.ext] @@ -335,16 +351,15 @@ public: /// user-defined-floating-point-literal /// user-defined-string-literal /// user-defined-character-literal -class UserDefinedLiteralExpression : public Expression { +class UserDefinedLiteralExpression : public LiteralExpression { public: - UserDefinedLiteralExpression(NodeKind K) : Expression(K) {} + UserDefinedLiteralExpression(NodeKind K) : LiteralExpression(K) {} static bool classof(const Node *N) { return N->kind() == NodeKind::IntegerUserDefinedLiteralExpression || N->kind() == NodeKind::FloatUserDefinedLiteralExpression || N->kind() == NodeKind::CharUserDefinedLiteralExpression || N->kind() == NodeKind::StringUserDefinedLiteralExpression; } - syntax::Leaf *literalToken(); }; /// Expression for user-defined-integer-literal. C++ [lex.ext] diff --git a/clang/lib/Tooling/Syntax/Nodes.cpp b/clang/lib/Tooling/Syntax/Nodes.cpp index 2435ae0..eced68f 100644 --- a/clang/lib/Tooling/Syntax/Nodes.cpp +++ b/clang/lib/Tooling/Syntax/Nodes.cpp @@ -230,37 +230,7 @@ syntax::Leaf *syntax::ParenExpression::closeParen() { findChild(syntax::NodeRole::CloseParen)); } -syntax::Leaf *syntax::IntegerLiteralExpression::literalToken() { - return llvm::cast_or_null( - findChild(syntax::NodeRole::LiteralToken)); -} - -syntax::Leaf *syntax::CharacterLiteralExpression::literalToken() { - return llvm::cast_or_null( - findChild(syntax::NodeRole::LiteralToken)); -} - -syntax::Leaf *syntax::FloatingLiteralExpression::literalToken() { - return llvm::cast_or_null( - findChild(syntax::NodeRole::LiteralToken)); -} - -syntax::Leaf *syntax::StringLiteralExpression::literalToken() { - return llvm::cast_or_null( - findChild(syntax::NodeRole::LiteralToken)); -} - -syntax::Leaf *syntax::BoolLiteralExpression::literalToken() { - return llvm::cast_or_null( - findChild(syntax::NodeRole::LiteralToken)); -} - -syntax::Leaf *syntax::CxxNullPtrExpression::nullPtrKeyword() { - return llvm::cast_or_null( - findChild(syntax::NodeRole::LiteralToken)); -} - -syntax::Leaf *syntax::UserDefinedLiteralExpression::literalToken() { +syntax::Leaf *syntax::LiteralExpression::literalToken() { return llvm::cast_or_null( findChild(syntax::NodeRole::LiteralToken)); } -- 2.7.4