summaryrefslogtreecommitdiff
path: root/Userland/Libraries/LibCpp/Parser.cpp
diff options
context:
space:
mode:
authorItamar <itamar8910@gmail.com>2021-06-26 15:34:23 +0300
committerAli Mohammad Pur <Ali.mpfard@gmail.com>2021-06-29 00:07:19 +0430
commitd7aa831a430800ed5ce16b00a0cdcc72e434eeba (patch)
tree0662f876f8dc199b2d6f0ce783d55232e839ca1f /Userland/Libraries/LibCpp/Parser.cpp
parent10cad8a87418a4507b5385d69f66eef349b0561d (diff)
downloadserenity-d7aa831a430800ed5ce16b00a0cdcc72e434eeba.zip
LibCpp: Differentiate between Type and NamedType
This adds a new ASTNode type called 'NamedType' which inherits from the Type node. Previously every Type node had a name field, but it was not logically accurate. For example, pointer types do not have a name (the pointed-to type may have one).
Diffstat (limited to 'Userland/Libraries/LibCpp/Parser.cpp')
-rw-r--r--Userland/Libraries/LibCpp/Parser.cpp44
1 files changed, 26 insertions, 18 deletions
diff --git a/Userland/Libraries/LibCpp/Parser.cpp b/Userland/Libraries/LibCpp/Parser.cpp
index 6e5f825a32..4ef1c3ea2b 100644
--- a/Userland/Libraries/LibCpp/Parser.cpp
+++ b/Userland/Libraries/LibCpp/Parser.cpp
@@ -230,6 +230,11 @@ NonnullRefPtr<BlockStatement> Parser::parse_block_statement(ASTNode& parent)
bool Parser::match_type()
{
+ return match_named_type();
+}
+
+bool Parser::match_named_type()
+{
save_state();
ScopeGuard state_guard = [this] { load_state(); };
@@ -258,7 +263,7 @@ bool Parser::match_template_arguments()
consume();
while (!eof() && peek().type() != Token::Type::Greater) {
- if (!match_type())
+ if (!match_named_type())
return false;
parse_type(get_dummy_node());
}
@@ -1148,35 +1153,38 @@ bool Parser::match_boolean_literal()
NonnullRefPtr<Type> Parser::parse_type(ASTNode& parent)
{
- ScopeLogger<CPP_DEBUG> logger;
+ LOG_SCOPE();
- if (!match_type()) {
+ if (!match_named_type()) {
+ error("expected named named_type");
auto token = consume();
- return create_ast_node<Type>(parent, token.start(), token.end());
+ return create_ast_node<NamedType>(parent, token.start(), token.end());
}
- auto type = create_ast_node<Type>(parent, position(), {});
+ auto named_type = create_ast_node<NamedType>(parent, position(), {});
auto qualifiers = parse_type_qualifiers();
- type->m_qualifiers = move(qualifiers);
+ named_type->m_qualifiers = move(qualifiers);
if (match_keyword("auto")) {
consume(Token::Type::Keyword);
- type->m_is_auto = true;
- } else {
+ named_type->m_is_auto = true;
+ named_type->set_end(position());
+ return named_type;
+ }
- if (match_keyword("struct")) {
- consume(Token::Type::Keyword); // Consume struct prefix
- }
+ if (match_keyword("struct")) {
+ consume(Token::Type::Keyword); // Consume struct prefix
+ }
- if (!match_name()) {
- type->set_end(position());
- error(String::formatted("expected name instead of: {}", peek().text()));
- return type;
- }
- type->m_name = parse_name(*type);
+ if (!match_name()) {
+ named_type->set_end(position());
+ error(String::formatted("expected name instead of: {}", peek().text()));
+ return named_type;
}
+ named_type->m_name = parse_name(*named_type);
+ NonnullRefPtr<Type> type = named_type;
while (!eof() && peek().type() == Token::Type::Asterisk) {
type->set_end(position());
auto asterisk = consume();
@@ -1184,7 +1192,7 @@ NonnullRefPtr<Type> Parser::parse_type(ASTNode& parent)
type->set_parent(*ptr);
ptr->m_pointee = type;
ptr->set_end(position());
- return ptr;
+ type = ptr;
}
type->set_end(position());