[clang] 1630e50 - [Syntax] Tablegen literal expressions.

Sam McCall via cfe-commits cfe-commits at lists.llvm.org
Wed Nov 11 16:26:54 PST 2020


Author: Sam McCall
Date: 2020-11-12T01:26:02+01:00
New Revision: 1630e50874a9ab6dae778bbdbb30d7dff6ade164

URL: https://github.com/llvm/llvm-project/commit/1630e50874a9ab6dae778bbdbb30d7dff6ade164
DIFF: https://github.com/llvm/llvm-project/commit/1630e50874a9ab6dae778bbdbb30d7dff6ade164.diff

LOG: [Syntax] Tablegen literal expressions.

Non-mechanical changes:
 - Added FIXME to StringLiteral to cover multi-token string literals.
 - LiteralExpression::getLiteralToken() is gone. (It was never called)
   This is because we don't codegen methods in Alternatives
   It's conceptually suspect if we consider multi-token string literals, though.

Differential Revision: https://reviews.llvm.org/D91277

Added: 
    

Modified: 
    clang/include/clang/Tooling/Syntax/Nodes.h
    clang/include/clang/Tooling/Syntax/Nodes.td
    clang/lib/Tooling/Syntax/Nodes.cpp

Removed: 
    


################################################################################
diff  --git a/clang/include/clang/Tooling/Syntax/Nodes.h b/clang/include/clang/Tooling/Syntax/Nodes.h
index 6f3436691cc8..edb6d4d4381d 100644
--- a/clang/include/clang/Tooling/Syntax/Nodes.h
+++ b/clang/include/clang/Tooling/Syntax/Nodes.h
@@ -156,113 +156,6 @@ class CallArguments final : public List {
   std::vector<List::ElementAndDelimiter<Expression>> getArgumentsAndCommas();
 };
 
-/// Expression for literals. C++ [lex.literal]
-class LiteralExpression : public Expression {
-public:
-  LiteralExpression(NodeKind K) : Expression(K) {}
-  static bool classof(const Node *N);
-  Leaf *getLiteralToken();
-};
-
-/// Expression for integer literals. C++ [lex.icon]
-class IntegerLiteralExpression final : public LiteralExpression {
-public:
-  IntegerLiteralExpression()
-      : LiteralExpression(NodeKind::IntegerLiteralExpression) {}
-  static bool classof(const Node *N);
-};
-
-/// Expression for character literals. C++ [lex.ccon]
-class CharacterLiteralExpression final : public LiteralExpression {
-public:
-  CharacterLiteralExpression()
-      : LiteralExpression(NodeKind::CharacterLiteralExpression) {}
-  static bool classof(const Node *N);
-};
-
-/// Expression for floating-point literals. C++ [lex.fcon]
-class FloatingLiteralExpression final : public LiteralExpression {
-public:
-  FloatingLiteralExpression()
-      : LiteralExpression(NodeKind::FloatingLiteralExpression) {}
-  static bool classof(const Node *N);
-};
-
-/// Expression for string-literals. C++ [lex.string]
-class StringLiteralExpression final : public LiteralExpression {
-public:
-  StringLiteralExpression()
-      : LiteralExpression(NodeKind::StringLiteralExpression) {}
-  static bool classof(const Node *N);
-};
-
-/// Expression for boolean literals. C++ [lex.bool]
-class BoolLiteralExpression final : public LiteralExpression {
-public:
-  BoolLiteralExpression()
-      : LiteralExpression(NodeKind::BoolLiteralExpression) {}
-  static bool classof(const Node *N);
-};
-
-/// Expression for the `nullptr` literal. C++ [lex.nullptr]
-class CxxNullPtrExpression final : public LiteralExpression {
-public:
-  CxxNullPtrExpression() : LiteralExpression(NodeKind::CxxNullPtrExpression) {}
-  static bool classof(const Node *N);
-};
-
-/// Expression for user-defined literal. C++ [lex.ext]
-/// user-defined-literal:
-///   user-defined-integer-literal
-///   user-defined-floating-point-literal
-///   user-defined-string-literal
-///   user-defined-character-literal
-class UserDefinedLiteralExpression : public LiteralExpression {
-public:
-  UserDefinedLiteralExpression(NodeKind K) : LiteralExpression(K) {}
-  static bool classof(const Node *N);
-};
-
-/// Expression for user-defined-integer-literal. C++ [lex.ext]
-class IntegerUserDefinedLiteralExpression final
-    : public UserDefinedLiteralExpression {
-public:
-  IntegerUserDefinedLiteralExpression()
-      : UserDefinedLiteralExpression(
-            NodeKind::IntegerUserDefinedLiteralExpression) {}
-  static bool classof(const Node *N);
-};
-
-/// Expression for user-defined-floating-point-literal. C++ [lex.ext]
-class FloatUserDefinedLiteralExpression final
-    : public UserDefinedLiteralExpression {
-public:
-  FloatUserDefinedLiteralExpression()
-      : UserDefinedLiteralExpression(
-            NodeKind::FloatUserDefinedLiteralExpression) {}
-  static bool classof(const Node *N);
-};
-
-/// Expression for user-defined-character-literal. C++ [lex.ext]
-class CharUserDefinedLiteralExpression final
-    : public UserDefinedLiteralExpression {
-public:
-  CharUserDefinedLiteralExpression()
-      : UserDefinedLiteralExpression(
-            NodeKind::CharUserDefinedLiteralExpression) {}
-  static bool classof(const Node *N);
-};
-
-/// Expression for user-defined-string-literal. C++ [lex.ext]
-class StringUserDefinedLiteralExpression final
-    : public UserDefinedLiteralExpression {
-public:
-  StringUserDefinedLiteralExpression()
-      : UserDefinedLiteralExpression(
-            NodeKind::StringUserDefinedLiteralExpression) {}
-  static bool classof(const Node *N);
-};
-
 /// An abstract class for prefix and postfix unary operators.
 class UnaryOperatorExpression : public Expression {
 public:

diff  --git a/clang/include/clang/Tooling/Syntax/Nodes.td b/clang/include/clang/Tooling/Syntax/Nodes.td
index 8f3d1e8f182f..765710e3e72e 100644
--- a/clang/include/clang/Tooling/Syntax/Nodes.td
+++ b/clang/include/clang/Tooling/Syntax/Nodes.td
@@ -54,18 +54,112 @@ def ParenExpression : Sequence<Expression> {
     Role<"CloseParen", Token<"r_paren">>,
   ];
 }
-def LiteralExpression : External<Expression> {}
-def IntegerLiteralExpression : External<LiteralExpression> {}
-def CharacterLiteralExpression : External<LiteralExpression> {}
-def FloatingLiteralExpression : External<LiteralExpression> {}
-def StringLiteralExpression : External<LiteralExpression> {}
-def BoolLiteralExpression : External<LiteralExpression> {}
-def CxxNullPtrExpression : External<LiteralExpression> {}
-def UserDefinedLiteralExpression : External<LiteralExpression> {}
-def IntegerUserDefinedLiteralExpression : External<UserDefinedLiteralExpression> {}
-def FloatUserDefinedLiteralExpression : External<UserDefinedLiteralExpression> {}
-def CharUserDefinedLiteralExpression : External<UserDefinedLiteralExpression> {}
-def StringUserDefinedLiteralExpression : External<UserDefinedLiteralExpression> {}
+def LiteralExpression : Alternatives<Expression> {
+  let documentation = [{
+    Expression for literals. C++ [lex.literal]
+  }];
+}
+def IntegerLiteralExpression : Sequence<LiteralExpression> {
+  let documentation = [{
+    Expression for integer literals. C++ [lex.icon]
+  }];
+  let children = [
+    Role<"LiteralToken", Token<"numeric_constant">>,
+  ];
+}
+defvar AnyCharacterLiteral = AnyToken<[
+  "char_constant", "wide_char_constant", "utf8_char_constant",
+  "utf16_char_constant", "utf32_char_constant"
+]>;
+def CharacterLiteralExpression : Sequence<LiteralExpression> {
+  let documentation = [{
+    Expression for character literals. C++ [lex.ccon]
+  }];
+  let children = [
+    Role<"LiteralToken", AnyCharacterLiteral>,
+  ];
+}
+def FloatingLiteralExpression : Sequence<LiteralExpression> {
+  let documentation = [{
+    Expression for floating-point literals. C++ [lex.fcon]
+  }];
+  let children = [
+    Role<"LiteralToken", Token<"numeric_constant">>,
+  ];
+}
+defvar AnyStringLiteral = AnyToken<[
+  "string_literal", "wide_string_literal", "utf8_string_literal",
+  "utf16_string_literal", "utf32_string_literal"
+]>;
+def StringLiteralExpression : Sequence<LiteralExpression> {
+  let documentation = [{
+    Expression for string-literals. C++ [lex.string]
+  }];
+  // FIXME: string literals may consist of multiple tokens.
+  // These are merged in phase 6, but tokens are captured after phase 4.
+  // The child here should be a list of literal tokens instead.
+  let children = [
+    Role<"LiteralToken", AnyStringLiteral>,
+  ];
+}
+def BoolLiteralExpression : Sequence<LiteralExpression> {
+  let documentation = [{
+    Expression for boolean literals. C++ [lex.bool]
+  }];
+  let children = [
+    Role<"LiteralToken", AnyToken<["kw_false","kw_true"]>>,
+  ];
+}
+def CxxNullPtrExpression : Sequence<LiteralExpression> {
+  let documentation = [{
+    Expression for the `nullptr` literal. C++ [lex.nullptr]
+  }];
+  let children = [
+    Role<"LiteralToken", Keyword<"nullptr">>,
+  ];
+}
+def UserDefinedLiteralExpression : Alternatives<LiteralExpression> {
+  let documentation = [{
+    Expression for user-defined literal. C++ [lex.ext]
+    user-defined-literal:
+      user-defined-integer-literal
+      user-defined-floating-point-literal
+      user-defined-string-literal
+      user-defined-character-literal
+  }];
+}
+def IntegerUserDefinedLiteralExpression : Sequence<UserDefinedLiteralExpression> {
+  let documentation = [{
+    Expression for user-defined-integer-literal. C++ [lex.ext]
+  }];
+  let children = [
+    Role<"LiteralToken", Keyword<"numeric_constant">>,
+  ];
+}
+def FloatUserDefinedLiteralExpression : Sequence<UserDefinedLiteralExpression> {
+  let documentation = [{
+    Expression for user-defined-floating-point-literal. C++ [lex.ext]
+  }];
+  let children = [
+    Role<"LiteralToken", Keyword<"numeric_constant">>,
+  ];
+}
+def CharUserDefinedLiteralExpression : Sequence<UserDefinedLiteralExpression> {
+  let documentation = [{
+    Expression for user-defined-character-literal. C++ [lex.ext]
+  }];
+  let children = [
+    Role<"LiteralToken", AnyCharacterLiteral>,
+  ];
+}
+def StringUserDefinedLiteralExpression : Sequence<UserDefinedLiteralExpression> {
+  let documentation = [{
+    Expression for user-defined-string-literal. C++ [lex.ext]
+  }];
+  let children = [
+    Role<"LiteralToken", AnyStringLiteral>,
+  ];
+}
 def IdExpression : Sequence<Expression> {
   let documentation = [{
     Models an `id-expression`, e.g. `std::vector<int>::size`.

diff  --git a/clang/lib/Tooling/Syntax/Nodes.cpp b/clang/lib/Tooling/Syntax/Nodes.cpp
index bbf880c8f4ea..fc6f8ef1a82c 100644
--- a/clang/lib/Tooling/Syntax/Nodes.cpp
+++ b/clang/lib/Tooling/Syntax/Nodes.cpp
@@ -196,10 +196,6 @@ syntax::DeclaratorList::getDeclaratorsAndCommas() {
   return Children;
 }
 
-syntax::Leaf *syntax::LiteralExpression::getLiteralToken() {
-  return cast_or_null<syntax::Leaf>(findChild(syntax::NodeRole::LiteralToken));
-}
-
 syntax::Expression *syntax::BinaryOperatorExpression::getLhs() {
   return cast_or_null<syntax::Expression>(
       findChild(syntax::NodeRole::LeftHandSide));


        


More information about the cfe-commits mailing list