Skip to content

Commit

Permalink
[Syntax] Tablegen literal expressions.
Browse files Browse the repository at this point in the history
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
  • Loading branch information
sam-mccall committed Nov 12, 2020
1 parent 0783ad9 commit 1630e50
Show file tree
Hide file tree
Showing 3 changed files with 106 additions and 123 deletions.
107 changes: 0 additions & 107 deletions clang/include/clang/Tooling/Syntax/Nodes.h
Expand Up @@ -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:
Expand Down
118 changes: 106 additions & 12 deletions clang/include/clang/Tooling/Syntax/Nodes.td
Expand Up @@ -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`.
Expand Down
4 changes: 0 additions & 4 deletions clang/lib/Tooling/Syntax/Nodes.cpp
Expand Up @@ -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));
Expand Down

0 comments on commit 1630e50

Please sign in to comment.