From 87d07523d8b563813043f169d166143bbcb36c34 Mon Sep 17 00:00:00 2001 From: Vinnie Falco Date: Thu, 15 Jun 2023 05:46:49 -0700 Subject: [PATCH] chore: better bitcoding for doc nodes --- include/mrdox/Metadata/Javadoc.hpp | 147 +++++++++++++++++------------ source/-XML/XMLWriter.cpp | 4 +- source/-XML/XMLWriter.hpp | 2 +- source/-adoc/AdocWriter.cpp | 4 +- source/-adoc/AdocWriter.hpp | 2 +- source/AST/AnyBlock.hpp | 50 +++------- source/AST/BitcodeWriter.cpp | 110 ++++++++------------- source/AST/ParseJavadoc.cpp | 2 +- 8 files changed, 148 insertions(+), 173 deletions(-) diff --git a/include/mrdox/Metadata/Javadoc.hpp b/include/mrdox/Metadata/Javadoc.hpp index 23a11c822..1ce692667 100644 --- a/include/mrdox/Metadata/Javadoc.hpp +++ b/include/mrdox/Metadata/Javadoc.hpp @@ -97,17 +97,24 @@ struct Node virtual ~Node() = default; - bool operator==(const Node&) - const noexcept = default; - - virtual bool equals( - const Node& other) const noexcept + bool operator==(const Node&)const noexcept = default; + virtual bool equals(const Node& other) const noexcept { return kind == other.kind; } }; -/** A string of plain text. +//------------------------------------------------ +// +// Text nodes +// +//------------------------------------------------ + +/** A Node containing a string of text. + + There will be no newlines in the text. Otherwise, + this would be represented as multiple text nodes + within a Paragraph node. */ struct Text : Node { @@ -117,15 +124,13 @@ struct Text : Node explicit Text( - String string_ = String()) + String string_ = String()) noexcept : Node(Kind::text) , string(std::move(string_)) { } - bool operator==(const Text&) - const noexcept = default; - + bool operator==(const Text&) const noexcept = default; bool equals(const Node& other) const noexcept override { return kind == other.kind && @@ -144,31 +149,35 @@ struct Text : Node /** A piece of style text. */ -struct StyledText : Text +struct Styled : Text { Style style; static constexpr Kind static_kind = Kind::styled; - StyledText( + Styled( String string_ = String(), - Style style_ = Style::none) + Style style_ = Style::none) noexcept : Text(std::move(string_), Kind::styled) , style(style_) { } - bool operator==(const StyledText&) - const noexcept = default; - - bool equals(const Node& other) const noexcept override + bool operator==(Styled const&) const noexcept = default; + bool equals(Node const& other) const noexcept override { return kind == other.kind && - *this == static_cast(other); + *this == static_cast(other); } }; +//------------------------------------------------ +// +// Block nodes +// +//------------------------------------------------ + /** A piece of block content The top level is a list of blocks. @@ -220,15 +229,13 @@ struct Heading : Block String string; Heading( - String string_ = String()) + String string_ = String()) noexcept : Block(Kind::heading) , string(std::move(string_)) { } - bool operator==(const Heading&) - const noexcept = default; - + bool operator==(Heading const&) const noexcept = default; bool equals(const Node& other) const noexcept override { return kind == other.kind && @@ -247,9 +254,7 @@ struct Paragraph : Block { } - bool operator==(const Paragraph&) - const noexcept = default; - + bool operator==(const Paragraph&) const noexcept = default; bool equals(const Node& other) const noexcept override { return kind == other.kind && @@ -260,7 +265,7 @@ struct Paragraph : Block explicit Paragraph( Kind kind, - List children_ = {}) + List children_ = {}) noexcept : Block(kind, std::move(children_)) { } @@ -277,9 +282,7 @@ struct Brief : Paragraph { } - bool operator==(const Brief&) - const noexcept = default; - + bool operator==(const Brief&) const noexcept = default; bool equals(const Node& other) const noexcept override { return kind == other.kind && @@ -287,7 +290,7 @@ struct Brief : Paragraph } }; -/** Documentation for an admonition +/** An admonition. */ struct Admonition : Paragraph { @@ -295,15 +298,13 @@ struct Admonition : Paragraph explicit Admonition( - Admonish style_ = Admonish::none) + Admonish style_ = Admonish::none) noexcept : Paragraph(Kind::admonition) , style(style_) { } - bool operator==(const Admonition&) - const noexcept = default; - + bool operator==(const Admonition&) const noexcept = default; bool equals(const Node& other) const noexcept override { return kind == other.kind && @@ -320,14 +321,12 @@ struct Code : Paragraph static constexpr Kind static_kind = Kind::code; - Code() + Code() noexcept : Paragraph(Kind::code) { } - bool operator==(const Code&) - const noexcept = default; - + bool operator==(const Code&) const noexcept = default; bool equals(const Node& other) const noexcept override { return kind == other.kind && @@ -366,52 +365,84 @@ struct Param : Paragraph } }; -/** Documentation for a template parameter + +/** Documentation for a function return type */ -struct TParam : Paragraph +struct Returns : Paragraph { - String name; - - static constexpr Kind static_kind = Kind::tparam; + static constexpr Kind static_kind = Kind::returns; - TParam() - : Paragraph(Kind::tparam) + Returns() + : Paragraph(Kind::returns) { } - bool operator==(const TParam&) + bool operator==(const Returns&) const noexcept = default; bool equals(const Node& other) const noexcept override { return kind == other.kind && - *this == static_cast(other); + *this == static_cast(other); } }; - -/** Documentation for a function return type +/** Documentation for a template parameter */ -struct Returns : Paragraph +struct TParam : Paragraph { - static constexpr Kind static_kind = Kind::returns; + String name; - Returns() - : Paragraph(Kind::returns) + static constexpr Kind static_kind = Kind::tparam; + + TParam() + : Paragraph(Kind::tparam) { } - bool operator==(const Returns&) - const noexcept = default; - + bool operator==(const TParam&) const noexcept = default; bool equals(const Node& other) const noexcept override { return kind == other.kind && - *this == static_cast(other); + *this == static_cast(other); } }; //------------------------------------------------ +template +constexpr +auto +visit( + Kind kind, + F&& f, Args&&... args) +{ + switch(kind) + { + case Kind::admonition: + return f.template operator()(std::forward(args)...); + case Kind::brief: + return f.template operator()(std::forward(args)...); + case Kind::code: + return f.template operator()(std::forward(args)...); + case Kind::heading: + return f.template operator()(std::forward(args)...); + case Kind::paragraph: + return f.template operator()(std::forward(args)...); + case Kind::param: + return f.template operator()(std::forward(args)...); + case Kind::returns: + return f.template operator()(std::forward(args)...); + case Kind::styled: + return f.template operator()(std::forward(args)...); + case Kind::text: + return f.template operator()(std::forward(args)...); + case Kind::tparam: + return f.template operator()(std::forward(args)...); + default: + return f.template operator()(std::forward(args)...); + } +} + template constexpr auto @@ -443,7 +474,7 @@ visit( return f(static_cast(node), std::forward(args)...); case Kind::styled: - return f(static_cast(node), + return f(static_cast(node), std::forward(args)...); case Kind::text: return f(static_cast(node), diff --git a/source/-XML/XMLWriter.cpp b/source/-XML/XMLWriter.cpp index b4e9631f8..a2d105235 100644 --- a/source/-XML/XMLWriter.cpp +++ b/source/-XML/XMLWriter.cpp @@ -605,7 +605,7 @@ writeNode( writeText(static_cast(node)); break; case doc::Kind::styled: - writeStyledText(static_cast(node)); + writeStyledText(static_cast(node)); break; case doc::Kind::heading: writeHeading(static_cast(node)); @@ -661,7 +661,7 @@ writeText( void XMLWriter:: writeStyledText( - doc::StyledText const& node) + doc::Styled const& node) { tags_.write(toString(node.style), node.string); } diff --git a/source/-XML/XMLWriter.hpp b/source/-XML/XMLWriter.hpp index 8b0b10c70..229b25884 100644 --- a/source/-XML/XMLWriter.hpp +++ b/source/-XML/XMLWriter.hpp @@ -86,7 +86,7 @@ class XMLWriter void writeNode(doc::Node const& node); void writeBrief(doc::Paragraph const& node); void writeText(doc::Text const& node); - void writeStyledText(doc::StyledText const& node); + void writeStyledText(doc::Styled const& node); void writeHeading(doc::Heading const& node); void writeParagraph(doc::Paragraph const& node, llvm::StringRef tag = ""); void writeAdmonition(doc::Admonition const& node); diff --git a/source/-adoc/AdocWriter.cpp b/source/-adoc/AdocWriter.cpp index f9c087b5a..718b74371 100644 --- a/source/-adoc/AdocWriter.cpp +++ b/source/-adoc/AdocWriter.cpp @@ -633,7 +633,7 @@ writeNode( writeNode(static_cast(node)); return; case doc::Kind::styled: - writeNode(static_cast(node)); + writeNode(static_cast(node)); return; #if 0 case doc::Node::block: @@ -687,7 +687,7 @@ writeNode( void AdocWriter:: writeNode( - doc::StyledText const& node) + doc::Styled const& node) { switch(node.style) { diff --git a/source/-adoc/AdocWriter.hpp b/source/-adoc/AdocWriter.hpp index 83ac3dc24..a0bc20e6c 100644 --- a/source/-adoc/AdocWriter.hpp +++ b/source/-adoc/AdocWriter.hpp @@ -114,7 +114,7 @@ class AdocWriter void writeNode(doc::Node const& node); void writeNode(doc::Block const& node); void writeNode(doc::Text const& node); - void writeNode(doc::StyledText const& node); + void writeNode(doc::Styled const& node); void writeNode(doc::Paragraph const& node); void writeNode(doc::Admonition const& node); void writeNode(doc::Code const& node); diff --git a/source/AST/AnyBlock.hpp b/source/AST/AnyBlock.hpp index 7b5804f7b..9949dc6b4 100644 --- a/source/AST/AnyBlock.hpp +++ b/source/AST/AnyBlock.hpp @@ -103,41 +103,19 @@ class JavadocNodesBlock doc::Kind kind{}; if(auto err = decodeRecord(R, kind, Blob)) return err; - switch(kind) - { - case doc::Kind::text: - nodes.emplace_back(std::make_unique()); - return Error::success(); - case doc::Kind::styled: - nodes.emplace_back(std::make_unique()); - return Error::success(); - case doc::Kind::heading: - nodes.emplace_back(std::make_unique()); - return Error::success(); - case doc::Kind::paragraph: - nodes.emplace_back(std::make_unique()); - return Error::success(); - case doc::Kind::brief: - nodes.emplace_back(std::make_unique()); - return Error::success(); - case doc::Kind::admonition: - nodes.emplace_back(std::make_unique()); - return Error::success(); - case doc::Kind::code: - nodes.emplace_back(std::make_unique()); - return Error::success(); - case doc::Kind::returns: - nodes.emplace_back(std::make_unique()); - return Error::success(); - case doc::Kind::param: - nodes.emplace_back(std::make_unique()); - return Error::success(); - case doc::Kind::tparam: - nodes.emplace_back(std::make_unique()); - return Error::success(); - default: - return Error("invalid kind"); - } + return visit(kind, + [&]() + { + if constexpr(! std::is_same_v) + { + nodes.emplace_back(std::make_unique()); + return Error::success(); + } + else + { + return Error("unknown doc::Kind"); + } + }); } case JAVADOC_PARAM_DIRECTION: { @@ -186,7 +164,7 @@ class JavadocNodesBlock auto node = nodes.back().get(); if(node->kind != doc::Kind::styled) return Error("style on wrong kind"); - static_cast( + static_cast( node)->style = style; return Error::success(); diff --git a/source/AST/BitcodeWriter.cpp b/source/AST/BitcodeWriter.cpp index 0e3243abe..177b688f1 100644 --- a/source/AST/BitcodeWriter.cpp +++ b/source/AST/BitcodeWriter.cpp @@ -853,78 +853,44 @@ emitBlock( { StreamSubBlockGuard Block(Stream, BI_JAVADOC_NODE_BLOCK_ID); emitRecord(I.kind, JAVADOC_NODE_KIND); - switch(I.kind) - { - case doc::Kind::text: - { - auto const& J = static_cast(I); - emitRecord(J.string, JAVADOC_NODE_STRING); - break; - } - case doc::Kind::styled: - { - auto const& J = static_cast(I); - emitRecord(J.style, JAVADOC_NODE_STYLE); - emitRecord(J.string, JAVADOC_NODE_STRING); - break; - } - case doc::Kind::heading: - { - auto const& J = static_cast(I); - emitRecord(J.string, JAVADOC_NODE_STRING); - break; - } - case doc::Kind::paragraph: - { - auto const& J = static_cast(I); - emitBlock(J.children); - break; - } - case doc::Kind::brief: - { - auto const& J = static_cast(I); - emitBlock(J.children); - break; - } - case doc::Kind::admonition: - { - auto const& J = static_cast(I); - emitRecord(J.style, JAVADOC_NODE_ADMONISH); - emitBlock(J.children); - break; - } - case doc::Kind::code: - { - auto const& J = static_cast(I); - emitBlock(J.children); - break; - } - case doc::Kind::returns: - { - auto const& J = static_cast(I); - emitBlock(J.children); - break; - } - case doc::Kind::param: - { - auto const& J = static_cast(I); - emitRecord(J.direction, JAVADOC_PARAM_DIRECTION); - emitRecord(J.name, JAVADOC_NODE_STRING); - emitBlock(J.children); - break; - } - case doc::Kind::tparam: - { - auto const& J = static_cast(I); - emitRecord(J.name, JAVADOC_NODE_STRING); - emitBlock(J.children); - break; - } - default: - // unknown kind - MRDOX_UNREACHABLE(); - break; - } + doc::visit(I.kind, + [&]() + { + if constexpr(! std::is_void_v) + { + auto const& J = static_cast(I); + + if constexpr( + std::derived_from || + std::derived_from) + emitRecord(J.string, JAVADOC_NODE_STRING); + + if constexpr(std::derived_from) + emitRecord(J.style, JAVADOC_NODE_ADMONISH); + + if constexpr(std::derived_from) + emitRecord(J.style, JAVADOC_NODE_STYLE); + + if constexpr( + std::derived_from) + { + emitRecord(J.name, JAVADOC_NODE_STRING); + emitRecord(J.direction, JAVADOC_PARAM_DIRECTION); + } + + if constexpr(std::derived_from) + { + emitRecord(J.name, JAVADOC_NODE_STRING); + } + + if constexpr(std::derived_from) + emitBlock(J.children); + } + else + { + MRDOX_UNREACHABLE(); + } + }); } void diff --git a/source/AST/ParseJavadoc.cpp b/source/AST/ParseJavadoc.cpp index acea7b439..c439fb685 100644 --- a/source/AST/ParseJavadoc.cpp +++ b/source/AST/ParseJavadoc.cpp @@ -248,7 +248,7 @@ class JavadocVisitor if(style != doc::Style::none) Javadoc::append(*paragraph_, std::make_unique< - doc::StyledText>(std::move(s), style)); + doc::Styled>(std::move(s), style)); else Javadoc::append(*paragraph_, std::make_unique< doc::Text>(std::move(s)));