LibCpp: Support 'auto' Type

This commit is contained in:
Itamar 2021-05-17 10:40:19 +03:00 committed by Andreas Kling
parent eeb98335d5
commit f28d944122
Notes: sideshowbarker 2024-07-18 17:45:10 +09:00
3 changed files with 27 additions and 9 deletions

View file

@ -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

View file

@ -206,6 +206,7 @@ public:
{
}
bool m_is_auto { false };
RefPtr<Name> m_name;
Vector<StringView> m_qualifiers;
};

View file

@ -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());