Skip to content

Commit

Permalink
[FOLD] add ConceptInfo
Browse files Browse the repository at this point in the history
  • Loading branch information
sdkrystian committed Jul 24, 2024
1 parent 8d2a465 commit 0456abe
Show file tree
Hide file tree
Showing 21 changed files with 262 additions and 6 deletions.
1 change: 1 addition & 0 deletions include/mrdocs/Metadata.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
// metadata extracted from AST

#include <mrdocs/Metadata/Alias.hpp>
#include <mrdocs/Metadata/Concept.hpp>
#include <mrdocs/Metadata/Enum.hpp>
#include <mrdocs/Metadata/Enumerator.hpp>
#include <mrdocs/Metadata/Expression.hpp>
Expand Down
47 changes: 47 additions & 0 deletions include/mrdocs/Metadata/Concept.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
//
// 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) 2024 Krystian Stasiowski ([email protected])
//
// Official repository: https://github.com/cppalliance/mrdocs
//

#ifndef MRDOCS_API_METADATA_CONCEPT_HPP
#define MRDOCS_API_METADATA_CONCEPT_HPP

#include <mrdocs/Platform.hpp>
#include <mrdocs/Metadata/Info.hpp>
#include <mrdocs/Metadata/Expression.hpp>
#include <mrdocs/Metadata/Source.hpp>

namespace clang {
namespace mrdocs {

/** Info for concepts.
*/
struct ConceptInfo
: InfoCommonBase<InfoKind::Concept>
, SourceInfo
{
/** The concepts template parameters
*/
std::unique_ptr<TemplateInfo> Template;

/** The concepts constraint-expression
*/
ExprInfo Constraint;

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

explicit ConceptInfo(SymbolID ID) noexcept
: InfoCommonBase(ID)
{
}
};

} // mrdocs
} // clang

#endif
1 change: 1 addition & 0 deletions include/mrdocs/Metadata/InfoNodes.inc
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ INFO(Enumerator, Enumerators, ENUMERATOR, enumerator, enumerator
INFO(Guide, Guides, GUIDE, guide, guides, The symbol is a deduction guide)
INFO(Alias, Aliases, ALIAS, alias, aliases, The symbol is a namespace alias)
INFO(Using, Usings, USING, using, usings, The symbol is a using declaration)
INFO(Concept, Concepts, CONCEPT, concept, concepts, The symbol is a concept)

#ifdef INFO_PASCAL
#undef INFO_PASCAL
Expand Down
15 changes: 15 additions & 0 deletions mrdocs.rnc
Original file line number Diff line number Diff line change
Expand Up @@ -246,6 +246,19 @@ grammar

#---------------------------------------------

Concept =
element concept
{
Access ?,
ID,
Location *,
Javadoc ?,
attribute constraint { text },
element named { ID } *
}

#---------------------------------------------

Symbol =
(
attribute Tag { text },
Expand All @@ -272,6 +285,7 @@ grammar
Typedef |
Variable |
Guide |
Concept |
Template
)
}
Expand Down Expand Up @@ -337,6 +351,7 @@ grammar
Template |
Alias |
Using |
Concept |
Specialization
)*

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{{>template-head symbol.template}}

concept {{>declarator-id symbol}} = {{symbol.constraint}}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
{{!-- concept --}}
= {{>nested-name-specifier symbol=symbol.parent includeNamespace=true}}{{symbol.name}}

{{symbol.doc.brief}}

== Synopsis

{{>source dcl=(primary_location symbol)}}

[source,cpp,subs="verbatim,macros,-callouts"]
----
{{>signature/concept symbol=symbol}};
----
{{#if symbol.doc.description}}
== Description
{{symbol.doc.description}}
{{/if}}
{{#if symbol.doc.see}}
== See Also
{{#each symbol.doc.see}}
{{.}}
{{/each}}
{{/if}}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
template<{{#each params}}{{#unless (and @first @last)}}
{{/unless}}{{>template-param~}}
{{#unless @last}},{{/unless~}}
{{/each~}}>{{#if requires}} requires {{requires}}{{/if}}
{{/each}}>{{#if requires}} requires {{requires}}{{/if}}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
{{#if is-namespace}}
{{>info-list members=tranche.overloads title="Functions"}}
{{>info-list members=tranche.variables title="Variables"}}
{{>info-list members=tranche.concepts title="Concepts"}}
{{else}}
{{>info-list members=tranche.overloads title=(concat label " " "Member Functions")}}
{{>info-list members=tranche.staticoverloads title=(concat label " " "Static Member Functions")}}
Expand Down
53 changes: 49 additions & 4 deletions src/lib/AST/ASTVisitor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -622,10 +622,13 @@ class ASTVisitor
return true;

const auto* Described = dyn_cast_if_present<TemplateDecl>(D);
if(auto* TD = D->getDescribedTemplate())
Described = TD;
const auto* Templated =
Described ? Described->getTemplatedDecl() : D;
const auto* Templated = D;
if(auto* DT = D->getDescribedTemplate())
{
Described = DT;
if (auto* TD = DT->getTemplatedDecl())
Templated = TD;
}

if(Described)
{
Expand Down Expand Up @@ -2570,6 +2573,24 @@ class ASTVisitor
getParentNamespaces(I, D);
}

void
buildConcept(
ConceptInfo& I,
bool created,
ConceptDecl* D)
{
bool documented = parseRawComment(I.javadoc, D);
addSourceLocation(I, D->getBeginLoc(), true, documented);

if(! created)
return;

I.Name = extractName(D);
buildExprInfo(I.Constraint, D->getConstraintExpr());

getParentNamespaces(I, D);
}

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

/** Get the DeclType as a MrDocs Info object
Expand Down Expand Up @@ -2692,6 +2713,14 @@ class ASTVisitor
void
traverse(FieldDecl*);

/** Traverse a concept definition
This function is called by traverseDecl to traverse a
C++ concept definition.
*/
void
traverse(ConceptDecl*);

/** Traverse a deduction guide
This function is called by traverseDecl to traverse a
Expand Down Expand Up @@ -2952,6 +2981,22 @@ traverse(FieldDecl* D)
auto [I, created] = *exp;
buildField(I, created, D);
}
//------------------------------------------------
// ConceptDecl

void
ASTVisitor::
traverse(ConceptDecl* D)
{
auto exp = upsertMrDocsInfoFor(D);
if(! exp) { return; }
auto [I, created] = *exp;

if(! I.Template)
I.Template = std::make_unique<TemplateInfo>();
buildTemplateParams(*I.Template, D->getTemplateParameters());
buildConcept(I, created, D);
}

//------------------------------------------------
// EnumConstantDecl
Expand Down
3 changes: 3 additions & 0 deletions src/lib/AST/ASTVisitorHelpers.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,9 @@ struct MrDocsType<UsingDecl> : std::type_identity<UsingInfo> {};
template <>
struct MrDocsType<NamespaceDecl> : std::type_identity<NamespaceInfo>{};

template <>
struct MrDocsType<ConceptDecl> : std::type_identity<ConceptInfo>{};

/// @copydoc MrDocsType
template <class DeclType>
using MrDocsType_t = typename MrDocsType<DeclType>::type;
Expand Down
31 changes: 31 additions & 0 deletions src/lib/AST/AnyBlock.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2060,6 +2060,37 @@ readSubBlock(
return AnyBlock::readSubBlock(ID);
}

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

class ConceptBlock
: public TopLevelBlock<ConceptInfo>
{
public:
using TopLevelBlock::TopLevelBlock;

Error
readSubBlock(
unsigned ID) override
{
switch(ID)
{
case BI_EXPR_BLOCK_ID:
{
ExprBlock E(I->Constraint, br_);
return br_.readBlock(E, ID);
}
case BI_TEMPLATE_BLOCK_ID:
{
I->Template = std::make_unique<TemplateInfo>();
TemplateBlock T(*I->Template, br_);
return br_.readBlock(T, ID);
}
default:
return TopLevelBlock::readSubBlock(ID);
}
}
};

} // mrdocs
} // clang

Expand Down
15 changes: 15 additions & 0 deletions src/lib/AST/BitcodeWriter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -453,6 +453,9 @@ RecordsByBlock{
// EnumeratorInfo
{BI_ENUMERATOR_BLOCK_ID,
{}},
// ConceptInfo
{BI_CONCEPT_BLOCK_ID,
{}},
// TypeInfo
{BI_TYPEINFO_BLOCK_ID,
{TYPEINFO_KIND, TYPEINFO_IS_PACK, TYPEINFO_CVQUAL,
Expand Down Expand Up @@ -1203,6 +1206,18 @@ emitBlock(
emitBlock(I.Initializer);
}

void
BitcodeWriter::
emitBlock(
const ConceptInfo& I)
{
StreamSubBlockGuard Block(Stream, BI_CONCEPT_BLOCK_ID);
emitInfoPart(I);
emitSourceInfo(I);
emitBlock(*I.Template);
emitBlock(I.Constraint);
}

void
BitcodeWriter::
emitBlock(
Expand Down
1 change: 1 addition & 0 deletions src/lib/AST/BitcodeWriter.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,7 @@ class BitcodeWriter
void emitBlock(NameInfo const& I);
void emitBlock(AliasInfo const& I);
void emitBlock(UsingInfo const& I);
void emitBlock(ConceptInfo const& I);

void emitBlock(std::unique_ptr<TypeInfo> const& TI);
void emitBlock(std::unique_ptr<TypeInfo> const& TI, BlockID ID);
Expand Down
23 changes: 23 additions & 0 deletions src/lib/Gen/xml/XMLWriter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -362,6 +362,29 @@ writeGuide(
closeTemplate(I.Template);
}

void
XMLWriter::
writeConcept(
ConceptInfo const& I)
{
openTemplate(I.Template);

tags_.open(conceptTagName, {
{ "name", I.Name },
{ I.Access },
{ I.id },
{ "constraint", I.Constraint.Written },
});

writeSourceInfo(I);

writeJavadoc(I.javadoc);

tags_.close(conceptTagName);

closeTemplate(I.Template);
}

void
XMLWriter::
writeAlias(
Expand Down
7 changes: 7 additions & 0 deletions src/lib/Metadata/DomMetadata.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -891,6 +891,13 @@ DomInfo<T>::construct() const

entries.emplace_back("explicitSpec", toString(I_.Explicit));
}
if constexpr(T::isConcept())
{
entries.insert(entries.end(), {
{ "template", domCreate(I_.Template, domCorpus_) },
{ "constraint", dom::stringOrNull(I_.Constraint.Written) }
});
}
return dom::Object(std::move(entries));
}

Expand Down
7 changes: 7 additions & 0 deletions src/lib/Metadata/Finalize.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -374,6 +374,13 @@ class Finalizer
finalize(I.Deduced);
finalize(I.Params);
}

void operator()(ConceptInfo& I)
{
check(I.Namespace);
finalize(I.javadoc);
finalize(I.Template);
}
};

/** Finalizes a set of Info.
Expand Down
2 changes: 1 addition & 1 deletion src/lib/Metadata/Finalize.hpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
//
//
// 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
Expand Down
7 changes: 7 additions & 0 deletions src/lib/Metadata/Interface.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -294,6 +294,13 @@ class TrancheBuilder
return;
push(&Tranche::Guides, access, I);
}

void operator()(
ConceptInfo const& I,
AccessKind access)
{
push(&Tranche::Concepts, access, I);
}
};

void
Expand Down
Loading

0 comments on commit 0456abe

Please sign in to comment.