summaryrefslogtreecommitdiff
path: root/Userland/Libraries/LibCpp/AST.cpp
diff options
context:
space:
mode:
authorItamar <itamar8910@gmail.com>2022-02-22 07:59:39 +0200
committerLinus Groh <mail@linusgroh.de>2022-02-23 00:48:44 +0000
commit7b42abccf29a4be57a38cc17025757fa00e8ac5e (patch)
tree365db66927a89b426ab7d3dd8c3c60c5f0d5e48c /Userland/Libraries/LibCpp/AST.cpp
parent604309327ea06837f1d33d54571e7671b9f4b584 (diff)
downloadserenity-7b42abccf29a4be57a38cc17025757fa00e8ac5e.zip
LibCpp: Allow qualified names in AST Declaration nodes
Previously, the names of declarations where stored as a simple StringView. Because of that, we couldn't parse out-of-line function definitions, which have qualified names. For example, we couldn't parse the following snippet: ``` void MyClass::foo(){} ``` To fix this, we now store the name of a declaration with a ASTNode::Name node, which represents a qualified named.
Diffstat (limited to 'Userland/Libraries/LibCpp/AST.cpp')
-rw-r--r--Userland/Libraries/LibCpp/AST.cpp44
1 files changed, 32 insertions, 12 deletions
diff --git a/Userland/Libraries/LibCpp/AST.cpp b/Userland/Libraries/LibCpp/AST.cpp
index ad0bf407b4..30087afd09 100644
--- a/Userland/Libraries/LibCpp/AST.cpp
+++ b/Userland/Libraries/LibCpp/AST.cpp
@@ -41,7 +41,7 @@ void FunctionDeclaration::dump(FILE* output, size_t indent) const
m_return_type->dump(output, indent + 1);
if (!m_name.is_null()) {
print_indent(output, indent + 1);
- outln(output, "{}", m_name);
+ outln(output, "{}", m_name->full_name());
}
print_indent(output, indent + 1);
outln(output, "(");
@@ -125,9 +125,9 @@ String FunctionType::to_string() const
else
builder.append(", ");
builder.append(parameter.type()->to_string());
- if (!parameter.name().is_empty()) {
+ if (parameter.name() && !parameter.full_name().is_empty()) {
builder.append(" ");
- builder.append(parameter.name());
+ builder.append(parameter.full_name());
}
}
builder.append(")");
@@ -143,7 +143,7 @@ void Parameter::dump(FILE* output, size_t indent) const
}
if (!m_name.is_null()) {
print_indent(output, indent);
- outln(output, "{}", m_name);
+ outln(output, "{}", m_name->full_name());
}
if (m_type)
m_type->dump(output, indent + 1);
@@ -176,7 +176,7 @@ void VariableDeclaration::dump(FILE* output, size_t indent) const
if (m_type)
m_type->dump(output, indent + 1);
print_indent(output, indent + 1);
- outln(output, "{}", m_name);
+ outln(output, "{}", full_name());
if (m_initial_value)
m_initial_value->dump(output, indent + 1);
}
@@ -318,7 +318,7 @@ void EnumDeclaration::dump(FILE* output, size_t indent) const
{
ASTNode::dump(output, indent);
print_indent(output, indent);
- outln(output, "{}", m_name);
+ outln(output, "{}", full_name());
for (auto& entry : m_entries) {
print_indent(output, indent + 1);
outln(output, "{}", entry.name);
@@ -331,7 +331,7 @@ void StructOrClassDeclaration::dump(FILE* output, size_t indent) const
{
ASTNode::dump(output, indent);
print_indent(output, indent);
- outln(output, "{}", m_name);
+ outln(output, "{}", full_name());
for (auto& member : m_members) {
member.dump(output, indent + 1);
}
@@ -510,7 +510,7 @@ void NamespaceDeclaration::dump(FILE* output, size_t indent) const
{
ASTNode::dump(output, indent);
print_indent(output, indent + 1);
- outln(output, "{}", m_name);
+ outln(output, "{}", full_name());
for (auto& decl : m_declarations)
decl.dump(output, indent + 1);
}
@@ -527,19 +527,26 @@ void Name::dump(FILE* output, size_t indent) const
outln(output, "{}", full_name());
}
-String Name::full_name() const
+StringView Name::full_name() const
{
+ if (m_full_name.has_value())
+ return *m_full_name;
+
StringBuilder builder;
if (!m_scope.is_empty()) {
for (auto& scope : m_scope) {
builder.appendff("{}::", scope.name());
}
}
- return String::formatted("{}{}", builder.to_string(), m_name.is_null() ? "" : m_name->name());
+ m_full_name = String::formatted("{}{}", builder.to_string(), m_name.is_null() ? "" : m_name->name());
+ return *m_full_name;
}
-String TemplatizedName::full_name() const
+StringView TemplatizedName::full_name() const
{
+ if (m_full_name.has_value())
+ return *m_full_name;
+
StringBuilder name;
name.append(Name::full_name());
name.append('<');
@@ -547,7 +554,8 @@ String TemplatizedName::full_name() const
name.append(type.to_string());
}
name.append('>');
- return name.to_string();
+ m_full_name = name.to_string();
+ return *m_full_name;
}
void CppCastExpression::dump(FILE* output, size_t indent) const
@@ -624,4 +632,16 @@ void Destructor::dump(FILE* output, size_t indent) const
}
}
+StringView Declaration::full_name() const
+{
+ if (!m_full_name.has_value()) {
+ if (m_name)
+ m_full_name = m_name->full_name();
+ else
+ m_full_name = String::empty();
+ }
+
+ return *m_full_name;
+}
+
}