diff options
-rw-r--r-- | Userland/Libraries/LibCpp/AST.cpp | 13 | ||||
-rw-r--r-- | Userland/Libraries/LibCpp/AST.h | 4 | ||||
-rw-r--r-- | Userland/Libraries/LibCpp/Parser.cpp | 6 | ||||
-rw-r--r-- | Userland/Libraries/LibCpp/Tests/parser/class.ast | 1 | ||||
-rw-r--r-- | Userland/Libraries/LibCpp/Tests/parser/inheritance.ast | 14 | ||||
-rw-r--r-- | Userland/Libraries/LibCpp/Tests/parser/inheritance.cpp | 4 | ||||
-rw-r--r-- | Userland/Libraries/LibCpp/Tests/parser/out-of-line.ast | 1 | ||||
-rw-r--r-- | Userland/Libraries/LibCpp/Tests/parser/struct.ast | 1 |
8 files changed, 43 insertions, 1 deletions
diff --git a/Userland/Libraries/LibCpp/AST.cpp b/Userland/Libraries/LibCpp/AST.cpp index 53ff3cfe2d..65483a5426 100644 --- a/Userland/Libraries/LibCpp/AST.cpp +++ b/Userland/Libraries/LibCpp/AST.cpp @@ -333,6 +333,19 @@ void StructOrClassDeclaration::dump(FILE* output, size_t indent) const ASTNode::dump(output, indent); print_indent(output, indent); outln(output, "{}", full_name()); + if (!m_baseclasses.is_empty()) { + print_indent(output, indent + 1); + outln(output, ":"); + for (size_t i = 0; i < m_baseclasses.size(); ++i) { + auto& baseclass = m_baseclasses[i]; + baseclass.dump(output, indent + 1); + if (i < m_baseclasses.size() - 1) { + print_indent(output, indent + 1); + outln(output, ","); + } + } + } + outln(output, ""); for (auto& member : m_members) { member.dump(output, indent + 1); } diff --git a/Userland/Libraries/LibCpp/AST.h b/Userland/Libraries/LibCpp/AST.h index c2c772aa33..bdcfd4aec4 100644 --- a/Userland/Libraries/LibCpp/AST.h +++ b/Userland/Libraries/LibCpp/AST.h @@ -705,9 +705,13 @@ public: NonnullRefPtrVector<Declaration> const& members() const { return m_members; } void set_members(NonnullRefPtrVector<Declaration>&& members) { m_members = move(members); } + NonnullRefPtrVector<Name> const& baseclasses() const { return m_baseclasses; } + void set_baseclasses(NonnullRefPtrVector<Name>&& baseclasses) { m_baseclasses = move(baseclasses); } + private: StructOrClassDeclaration::Type m_type; NonnullRefPtrVector<Declaration> m_members; + NonnullRefPtrVector<Name> m_baseclasses; }; enum class UnaryOp { diff --git a/Userland/Libraries/LibCpp/Parser.cpp b/Userland/Libraries/LibCpp/Parser.cpp index 9cb3e65de5..cbefa93f78 100644 --- a/Userland/Libraries/LibCpp/Parser.cpp +++ b/Userland/Libraries/LibCpp/Parser.cpp @@ -1151,6 +1151,8 @@ NonnullRefPtr<StructOrClassDeclaration> Parser::parse_class_declaration(ASTNode& auto has_final = match_keyword("final"); + NonnullRefPtrVector<Name> baseclasses; + // FIXME: Don't ignore this. if (peek(has_final ? 1 : 0).type() == Token::Type::Colon) { if (has_final) @@ -1162,10 +1164,12 @@ NonnullRefPtr<StructOrClassDeclaration> Parser::parse_class_declaration(ASTNode& while (match_keyword("private") || match_keyword("public") || match_keyword("protected") || match_keyword("virtual")) consume(); - (void)parse_name(get_dummy_node()); + baseclasses.append(parse_name(*decl)); } while (peek().type() == Token::Type::Comma); } + decl->set_baseclasses(move(baseclasses)); + consume(Token::Type::LeftCurly); while (!eof() && peek().type() != Token::Type::RightCurly) { diff --git a/Userland/Libraries/LibCpp/Tests/parser/class.ast b/Userland/Libraries/LibCpp/Tests/parser/class.ast index 98c5333e56..c53eddc1b7 100644 --- a/Userland/Libraries/LibCpp/Tests/parser/class.ast +++ b/Userland/Libraries/LibCpp/Tests/parser/class.ast @@ -1,6 +1,7 @@ TranslationUnit[0:0->10:1] StructOrClassDeclaration[0:6->10:1] A + C'tor ( Parameter[1:6->1:10] diff --git a/Userland/Libraries/LibCpp/Tests/parser/inheritance.ast b/Userland/Libraries/LibCpp/Tests/parser/inheritance.ast new file mode 100644 index 0000000000..eb67cb13e9 --- /dev/null +++ b/Userland/Libraries/LibCpp/Tests/parser/inheritance.ast @@ -0,0 +1,14 @@ +TranslationUnit[0:0->3:1] + StructOrClassDeclaration[0:6->3:1] + A + : + Name[0:17->0:32] + SomeNamespace::B + , + Name[0:43->0:43] + C + + VariableDeclaration[2:4->3:0] + NamedType[2:4->2:6] + int + x diff --git a/Userland/Libraries/LibCpp/Tests/parser/inheritance.cpp b/Userland/Libraries/LibCpp/Tests/parser/inheritance.cpp new file mode 100644 index 0000000000..84d3fd0c52 --- /dev/null +++ b/Userland/Libraries/LibCpp/Tests/parser/inheritance.cpp @@ -0,0 +1,4 @@ +class A : public SomeNamespace::B, private C +{ + int x; +}; diff --git a/Userland/Libraries/LibCpp/Tests/parser/out-of-line.ast b/Userland/Libraries/LibCpp/Tests/parser/out-of-line.ast index 08119aad33..d8e08c34f0 100644 --- a/Userland/Libraries/LibCpp/Tests/parser/out-of-line.ast +++ b/Userland/Libraries/LibCpp/Tests/parser/out-of-line.ast @@ -1,6 +1,7 @@ TranslationUnit[2:0->9:0] StructOrClassDeclaration[2:6->7:0] A + FunctionDeclaration[4:4->4:14] NamedType[4:4->4:7] bool diff --git a/Userland/Libraries/LibCpp/Tests/parser/struct.ast b/Userland/Libraries/LibCpp/Tests/parser/struct.ast index f58f4c3f8b..112dffe263 100644 --- a/Userland/Libraries/LibCpp/Tests/parser/struct.ast +++ b/Userland/Libraries/LibCpp/Tests/parser/struct.ast @@ -1,6 +1,7 @@ TranslationUnit[1:0->12:0] StructOrClassDeclaration[1:7->7:0] MyStruct + VariableDeclaration[3:4->4:4] NamedType[3:4->3:6] int |