Skip to content

Commit

Permalink
[FOLD]
Browse files Browse the repository at this point in the history
  • Loading branch information
sdkrystian committed Dec 11, 2023
1 parent 1092a8d commit 2adb9dc
Show file tree
Hide file tree
Showing 7 changed files with 124 additions and 5 deletions.
29 changes: 29 additions & 0 deletions include/mrdocs/Metadata/Name.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,10 @@ enum class NameKind
Specialization
};

MRDOCS_DECL
dom::String
toString(NameKind kind) noexcept;

/** Represents a (possibly qualified) symbol name.
*/
struct NameInfo
Expand Down Expand Up @@ -77,6 +81,31 @@ struct SpecializationNameInfo
}
};

template<
class NameInfoTy,
class Fn,
class... Args>
requires std::derived_from<NameInfoTy, NameInfo>
decltype(auto)
visit(
NameInfoTy& info,
Fn&& fn,
Args&&... args)
{
auto visitor = makeVisitor<NameInfo>(
info, std::forward<Fn>(fn),
std::forward<Args>(args)...);
switch(info.Kind)
{
case NameKind::Identifier:
return visitor.template visit<NameInfo>();
case NameKind::Specialization:
return visitor.template visit<SpecializationNameInfo>();
default:
MRDOCS_UNREACHABLE();
}
}

} // mrdocs
} // clang

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@
{{#if cv-qualifiers~}}
{{#if pointee-type}} {{cv-qualifiers}}{{else}}{{cv-qualifiers}} {{/if~}}
{{/if~}}
{{#if (and symbol (not parent-type))}}{{>qualified-path symbol=symbol.parent nolink=nolink}}{{/if~}}
{{#if prefix~}}{{>name-info prefix nolink=nolink}}::{{/if~}}
{{!-- {{#if (and symbol (not parent-type))}}{{>qualified-path symbol=symbol.parent nolink=nolink}}{{/if~}} --~}}
{{#if (and symbol (not nolink))~}}
xref:{{symbol.ref}}[{{name}}]
{{~else if name}}{{name~}}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
{{#if prefix}}{{>name-info prefix nolink=nolink}}::{{/if~}}
{{#if (and symbol.ref (not nolink))}}xref:{{symbol.ref}}[{{name}}]{{else}}{{name}}{{/if}}{{#if args}}{{>template-args args=args nolink=nolink}}{{/if}}
20 changes: 16 additions & 4 deletions src/lib/AST/ASTVisitor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -493,7 +493,8 @@ class ASTVisitor
while((DC = DC->getParent()));

// add the adjusted declaration to the set of dependencies
dependencies_.insert(Outer);
if(! isa<NamespaceDecl, TranslationUnitDecl>(Outer))
dependencies_.insert(Outer);
}

//------------------------------------------------
Expand Down Expand Up @@ -3623,6 +3624,7 @@ class ASTVisitor::TypeInfoBuilder
auto I = std::make_unique<BuiltinTypeInfo>();
I->CVQualifiers = convertToQualifierKind(quals);
I->Name = std::move(name);
I->Name_ = getASTVisitor().buildNameInfo(NNS);
*Inner = std::move(I);
Result->IsPackExpansion = pack;
}
Expand All @@ -3640,6 +3642,7 @@ class ASTVisitor::TypeInfoBuilder
auto I = std::make_unique<SpecializationTypeInfo>();
I->CVQualifiers = convertToQualifierKind(quals);
I->Name = II->getName();
I->Name_ = getASTVisitor().buildNameInfo(NNS);
getASTVisitor().buildTemplateArgs(
I->TemplateArgs, *TArgs);
*Inner = std::move(I);
Expand All @@ -3649,6 +3652,7 @@ class ASTVisitor::TypeInfoBuilder
auto I = std::make_unique<TagTypeInfo>();
I->CVQualifiers = convertToQualifierKind(quals);
I->Name = II->getName();
I->Name_ = getASTVisitor().buildNameInfo(NNS);
*Inner = std::move(I);
}
Result->IsPackExpansion = pack;
Expand All @@ -3669,6 +3673,10 @@ class ASTVisitor::TypeInfoBuilder
I->CVQualifiers = convertToQualifierKind(quals);
I->Name = II->getName();
getASTVisitor().getDependencyID(D, I->id);
if(NNS)
I->Name_ = getASTVisitor().buildNameInfo(NNS);
else
I->Name_ = getASTVisitor().buildNameInfo(D);
getASTVisitor().buildTemplateArgs(
I->TemplateArgs, *TArgs);
*Inner = std::move(I);
Expand All @@ -3679,6 +3687,10 @@ class ASTVisitor::TypeInfoBuilder
I->CVQualifiers = convertToQualifierKind(quals);
I->Name = II->getName();
getASTVisitor().getDependencyID(D, I->id);
if(NNS)
I->Name_ = getASTVisitor().buildNameInfo(NNS);
else
I->Name_ = getASTVisitor().buildNameInfo(D);
*Inner = std::move(I);
}
Result->IsPackExpansion = pack;
Expand Down Expand Up @@ -3831,14 +3843,14 @@ buildNameInfo(
else if(const NamespaceDecl* ND = NNS->getAsNamespace())
{
I = std::make_unique<NameInfo>();
I->Name = II->getName();
I->Name = ND->getIdentifier()->getName();
getDependencyID(ND, I->id);
I->Prefix = buildNameInfo(ND, extract_mode);
}
else if(const NamespaceAliasDecl* ND = NNS->getAsNamespaceAlias())
{
I = std::make_unique<NameInfo>();
I->Name = II->getName();
I->Name = ND->getIdentifier()->getName();
getDependencyID(ND->getNamespace(), I->id);
I->Prefix = buildNameInfo(ND->getNamespace(), extract_mode);
}
Expand All @@ -3860,7 +3872,7 @@ buildNameInfo(
auto I = std::make_unique<NameInfo>();
if(const IdentifierInfo* II = PD->getIdentifier())
I->Name = II->getName();
getDependencyID(ND, I->id);
getDependencyID(PD, I->id);
I->Prefix = buildNameInfo(PD, extract_mode);
return I;
}
Expand Down
29 changes: 29 additions & 0 deletions src/lib/Metadata/DomMetadata.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -421,6 +421,31 @@ domCreate(

//------------------------------------------------

static
dom::Value
domCreate(
std::unique_ptr<NameInfo> const& I,
DomCorpus const& domCorpus)
{
if(! I)
return nullptr;
dom::Object::storage_type entries = {
{ "kind", toString(I->Kind) }
};
visit(*I, [&]<typename T>(const T& t)
{
entries.emplace_back("name", t.Name);
entries.emplace_back("symbol", domCorpus.get(t.id));

if constexpr(requires { t.TemplateArgs; })
entries.emplace_back("args",
dom::newArray<DomTArgArray>(t.TemplateArgs, domCorpus));

entries.emplace_back("prefix", domCreate(t.Prefix, domCorpus));
});
return dom::Object(std::move(entries));
}

static
dom::Value
domCreate(
Expand All @@ -438,6 +463,10 @@ domCreate(
if constexpr(requires { t.Name; })
entries.emplace_back("name", t.Name);

if constexpr(requires { t.Name_; })
entries.emplace_back("prefix",
domCreate(t.Name_, domCorpus));

if constexpr(requires { t.id; })
entries.emplace_back("symbol", domCorpus.get(t.id));

Expand Down
16 changes: 16 additions & 0 deletions src/lib/Metadata/Finalize.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -144,11 +144,27 @@ class Finalizer
if constexpr(requires { T.id; })
finalize(T.id);

if constexpr(requires { T.Name_; })
finalize(T.Name_);

if constexpr(Ty::isSpecialization())
finalize(T.TemplateArgs);
});
}

void finalize(NameInfo& name)
{
visit(name, [this]<typename Ty>(Ty& T)
{
finalize(T.Prefix);

if constexpr(requires { T.TemplateArgs; })
finalize(T.TemplateArgs);

finalize(T.id);
});
}

void finalize(doc::Node& node)
{
visit(node, [&]<typename NodeTy>(NodeTy& N)
Expand Down
30 changes: 30 additions & 0 deletions src/lib/Metadata/Name.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
//
// Licensed under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
// Copyright (c) 2023 Krystian Stasiowski ([email protected])
//
// Official repository: https://github.com/cppalliance/mrdocs
//

#include <mrdocs/Metadata/Name.hpp>

namespace clang {
namespace mrdocs {

dom::String toString(NameKind kind) noexcept
{
switch(kind)
{
case NameKind::Identifier:
return "identifier";
case NameKind::Specialization:
return "specialization";
default:
MRDOCS_UNREACHABLE();
}
}

} // mrdocs
} // clang

0 comments on commit 2adb9dc

Please sign in to comment.