summaryrefslogtreecommitdiff
path: root/Userland/Libraries/LibCpp
diff options
context:
space:
mode:
authorItamar <itamar8910@gmail.com>2021-05-17 10:40:19 +0300
committerAndreas Kling <kling@serenityos.org>2021-05-19 23:19:07 +0200
commitf28d944122f48e18ef30c4ebb855e6659a0b50e9 (patch)
tree2c9f115d6b5314abd26f6b4b985630956aaf0c8a /Userland/Libraries/LibCpp
parenteeb98335d541ea018efd45dca7d4fff2999b438b (diff)
downloadserenity-f28d944122f48e18ef30c4ebb855e6659a0b50e9.zip
LibCpp: Support 'auto' Type
Diffstat (limited to 'Userland/Libraries/LibCpp')
-rw-r--r--Userland/Libraries/LibCpp/AST.cpp9
-rw-r--r--Userland/Libraries/LibCpp/AST.h1
-rw-r--r--Userland/Libraries/LibCpp/Parser.cpp26
3 files changed, 27 insertions, 9 deletions
diff --git a/Userland/Libraries/LibCpp/AST.cpp b/Userland/Libraries/LibCpp/AST.cpp
index 855f4b28af..6d67cbf5d5 100644
--- a/Userland/Libraries/LibCpp/AST.cpp
+++ b/Userland/Libraries/LibCpp/AST.cpp
@@ -76,7 +76,14 @@ String Type::to_string() const
String qualifiers_string;
if (!m_qualifiers.is_empty())
qualifiers_string = String::formatted("[{}] ", String::join(" ", m_qualifiers));
- return String::formatted("{}{}", qualifiers_string, m_name.is_null() ? "" : m_name->full_name());
+
+ String name;
+ if (m_is_auto)
+ name = "auto";
+ else
+ name = m_name.is_null() ? "" : m_name->full_name();
+
+ return String::formatted("{}{}", qualifiers_string, name);
}
String Pointer::to_string() const
diff --git a/Userland/Libraries/LibCpp/AST.h b/Userland/Libraries/LibCpp/AST.h
index fb265a1016..b0afb4a5b5 100644
--- a/Userland/Libraries/LibCpp/AST.h
+++ b/Userland/Libraries/LibCpp/AST.h
@@ -206,6 +206,7 @@ public:
{
}
+ bool m_is_auto { false };
RefPtr<Name> m_name;
Vector<StringView> m_qualifiers;
};
diff --git a/Userland/Libraries/LibCpp/Parser.cpp b/Userland/Libraries/LibCpp/Parser.cpp
index 62ae77e2a3..f75993489a 100644
--- a/Userland/Libraries/LibCpp/Parser.cpp
+++ b/Userland/Libraries/LibCpp/Parser.cpp
@@ -235,6 +235,10 @@ bool Parser::match_type()
ScopeGuard state_guard = [this] { load_state(); };
parse_type_qualifiers();
+ if (match_keyword("auto")) {
+ return true;
+ }
+
if (match_keyword("struct")) {
consume(Token::Type::Keyword); // Consume struct prefix
}
@@ -1104,16 +1108,22 @@ NonnullRefPtr<Type> Parser::parse_type(ASTNode& parent)
auto qualifiers = parse_type_qualifiers();
type->m_qualifiers = move(qualifiers);
- if (match_keyword("struct")) {
- consume(Token::Type::Keyword); // Consume struct prefix
- }
+ if (match_keyword("auto")) {
+ consume(Token::Type::Keyword);
+ type->m_is_auto = true;
+ } else {
- if (!match_name()) {
- type->set_end(position());
- error(String::formatted("expected name instead of: {}", peek().text()));
- return type;
+ 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);
}
- type->m_name = parse_name(*type);
while (!eof() && peek().type() == Token::Type::Asterisk) {
type->set_end(position());