Skip to content

Commit abf42ae

Browse files
authored
feat: support concepts and constraints (#649)
1 parent e460d86 commit abf42ae

37 files changed

+793
-64
lines changed

include/mrdocs/Metadata.hpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
// metadata extracted from AST
1919

2020
#include <mrdocs/Metadata/Alias.hpp>
21+
#include <mrdocs/Metadata/Concept.hpp>
2122
#include <mrdocs/Metadata/Enum.hpp>
2223
#include <mrdocs/Metadata/Enumerator.hpp>
2324
#include <mrdocs/Metadata/Expression.hpp>
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
//
2+
// Licensed under the Apache License v2.0 with LLVM Exceptions.
3+
// See https://llvm.org/LICENSE.txt for license information.
4+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
5+
//
6+
// Copyright (c) 2024 Krystian Stasiowski ([email protected])
7+
//
8+
// Official repository: https://github.com/cppalliance/mrdocs
9+
//
10+
11+
#ifndef MRDOCS_API_METADATA_CONCEPT_HPP
12+
#define MRDOCS_API_METADATA_CONCEPT_HPP
13+
14+
#include <mrdocs/Platform.hpp>
15+
#include <mrdocs/Metadata/Info.hpp>
16+
#include <mrdocs/Metadata/Expression.hpp>
17+
#include <mrdocs/Metadata/Source.hpp>
18+
19+
namespace clang {
20+
namespace mrdocs {
21+
22+
/** Info for concepts.
23+
*/
24+
struct ConceptInfo
25+
: InfoCommonBase<InfoKind::Concept>
26+
, SourceInfo
27+
{
28+
/** The concepts template parameters
29+
*/
30+
std::unique_ptr<TemplateInfo> Template;
31+
32+
/** The concepts constraint-expression
33+
*/
34+
ExprInfo Constraint;
35+
36+
//--------------------------------------------
37+
38+
explicit ConceptInfo(SymbolID ID) noexcept
39+
: InfoCommonBase(ID)
40+
{
41+
}
42+
};
43+
44+
} // mrdocs
45+
} // clang
46+
47+
#endif

include/mrdocs/Metadata/Function.hpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,8 @@ struct FunctionInfo
159159

160160
ExplicitInfo Explicit;
161161

162+
ExprInfo Requires;
163+
162164
//--------------------------------------------
163165

164166
explicit FunctionInfo(SymbolID ID) noexcept

include/mrdocs/Metadata/InfoNodes.inc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@ INFO(Enumerator, Enumerators, ENUMERATOR, enumerator, enumerator
7777
INFO(Guide, Guides, GUIDE, guide, guides, The symbol is a deduction guide)
7878
INFO(Alias, Aliases, ALIAS, alias, aliases, The symbol is a namespace alias)
7979
INFO(Using, Usings, USING, using, usings, The symbol is a using declaration)
80+
INFO(Concept, Concepts, CONCEPT, concept, concepts, The symbol is a concept)
8081

8182
#ifdef INFO_PASCAL
8283
#undef INFO_PASCAL

include/mrdocs/Metadata/Template.hpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -207,6 +207,9 @@ struct TypeTParam
207207
{
208208
/** Keyword (class/typename) the parameter uses */
209209
TParamKeyKind KeyKind = TParamKeyKind::Class;
210+
211+
/** The type-constraint for the parameter, if any. */
212+
std::unique_ptr<NameInfo> Constraint;
210213
};
211214

212215
struct NonTypeTParam
@@ -284,6 +287,10 @@ struct TemplateInfo
284287
std::vector<std::unique_ptr<TParam>> Params;
285288
std::vector<std::unique_ptr<TArg>> Args;
286289

290+
/** The requires-clause for the template parameter list, if any.
291+
*/
292+
ExprInfo Requires;
293+
287294
/** Primary template ID for partial and explicit specializations.
288295
*/
289296
SymbolID Primary = SymbolID::invalid;

include/mrdocs/Metadata/Type.hpp

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
namespace clang {
2727
namespace mrdocs {
2828

29-
enum QualifierKind : int
29+
enum QualifierKind
3030
{
3131
None,
3232
Const,
@@ -39,6 +39,7 @@ enum class TypeKind
3939
{
4040
Named = 1, // for bitstream
4141
Decltype,
42+
Auto,
4243
LValueReference,
4344
RValueReference,
4445
Pointer,
@@ -49,6 +50,14 @@ enum class TypeKind
4950

5051
MRDOCS_DECL dom::String toString(TypeKind kind) noexcept;
5152

53+
enum class AutoKind
54+
{
55+
Auto,
56+
DecltypeAuto
57+
};
58+
59+
MRDOCS_DECL dom::String toString(AutoKind kind) noexcept;
60+
5261
struct TypeInfo
5362
{
5463
/** The kind of TypeInfo this is
@@ -63,6 +72,7 @@ struct TypeInfo
6372

6473
constexpr bool isNamed() const noexcept { return Kind == TypeKind::Named; }
6574
constexpr bool isDecltype() const noexcept { return Kind == TypeKind::Decltype; }
75+
constexpr bool isAuto() const noexcept { return Kind == TypeKind::Auto; }
6676
constexpr bool isLValueReference() const noexcept { return Kind == TypeKind::LValueReference; }
6777
constexpr bool isRValueReference() const noexcept { return Kind == TypeKind::RValueReference; }
6878
constexpr bool isPointer() const noexcept { return Kind == TypeKind::Pointer; }
@@ -100,6 +110,7 @@ struct IsType : TypeInfo
100110

101111
static constexpr bool isNamed() noexcept { return K == TypeKind::Named; }
102112
static constexpr bool isDecltype() noexcept { return K == TypeKind::Decltype; }
113+
static constexpr bool isAuto() noexcept { return K == TypeKind::Auto; }
103114
static constexpr bool isLValueReference() noexcept { return K == TypeKind::LValueReference; }
104115
static constexpr bool isRValueReference() noexcept { return K == TypeKind::RValueReference; }
105116
static constexpr bool isPointer() noexcept { return K == TypeKind::Pointer; }
@@ -129,6 +140,14 @@ struct DecltypeTypeInfo
129140
ExprInfo Operand;
130141
};
131142

143+
struct AutoTypeInfo
144+
: IsType<TypeKind::Auto>
145+
{
146+
QualifierKind CVQualifiers = QualifierKind::None;
147+
AutoKind Keyword = AutoKind::Auto;
148+
std::unique_ptr<NameInfo> Constraint;
149+
};
150+
132151
struct LValueReferenceTypeInfo
133152
: IsType<TypeKind::LValueReference>
134153
{
@@ -226,6 +245,10 @@ visit(
226245
return f(static_cast<add_cv_from_t<
227246
TypeTy, DecltypeTypeInfo>&>(II),
228247
std::forward<Args>(args)...);
248+
case TypeKind::Auto:
249+
return f(static_cast<add_cv_from_t<
250+
TypeTy, AutoTypeInfo>&>(II),
251+
std::forward<Args>(args)...);
229252
case TypeKind::LValueReference:
230253
return f(static_cast<add_cv_from_t<
231254
TypeTy, LValueReferenceTypeInfo>&>(II),

mrdocs.rnc

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@ grammar
7979
attribute class { "constructor"|"destructor"|"conversion" } ?,
8080
attribute exception-spec { text } ?,
8181
attribute explicit-spec { text } ?,
82+
attribute requires { text } ?,
8283
Location *,
8384
(
8485
Attr * |
@@ -246,6 +247,19 @@ grammar
246247

247248
#---------------------------------------------
248249

250+
Concept =
251+
element concept
252+
{
253+
Name,
254+
Access ?,
255+
ID,
256+
Location *,
257+
Javadoc ?,
258+
attribute constraint { text }
259+
}
260+
261+
#---------------------------------------------
262+
249263
Symbol =
250264
(
251265
attribute Tag { text },
@@ -263,6 +277,7 @@ grammar
263277
element template
264278
{
265279
attribute class { "explicit"|"partial" } ?,
280+
attribute requires { text } ?,
266281
ID ?,
267282
TemplateParam *,
268283
TemplateArg *,
@@ -272,6 +287,7 @@ grammar
272287
Typedef |
273288
Variable |
274289
Guide |
290+
Concept |
275291
Template
276292
)
277293
}
@@ -337,6 +353,7 @@ grammar
337353
Template |
338354
Alias |
339355
Using |
356+
Concept |
340357
Specialization
341358
)*
342359

@@ -436,6 +453,7 @@ grammar
436453
{
437454
"named" |
438455
"decltype" |
456+
"auto" |
439457
"lvalue-reference" |
440458
"rvalue-reference" |
441459
"pointer" |
@@ -453,6 +471,8 @@ grammar
453471
attribute exception-spec { text } ?,
454472
attribute bounds { text } ?,
455473
attribute operand { text } ?,
474+
attribute constraint { text } ?,
475+
attribute keyword { text } ?,
456476
TypeInfo *
457477
}
458478
)

share/mrdocs/addons/generator/asciidoc/partials/declarator-before.adoc.hbs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
{{#if element-type~}}{{~>declarator-before element-type nolink=nolink~}}{{/if~}}
66
{{#if return-type~}}{{~>declarator-before return-type nolink=nolink~}}{{/if~}}
77
{{#if (eq kind "named")}}{{>name-info name nolink=nolink}}{{/if~}}
8+
{{#if (eq kind "auto")}}{{#if constraint}}{{>name-info constraint nolink=nolink}} {{/if~}}{{keyword}}{{/if~}}
89
{{#if cv-qualifiers~}}
910
{{#if pointee-type}} {{cv-qualifiers}}{{else}} {{cv-qualifiers}}{{/if~}}
1011
{{/if~}}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{{>template-head symbol.template}}
2+
3+
concept {{>declarator-id symbol}} = {{symbol.constraint}}

share/mrdocs/addons/generator/asciidoc/partials/signature/function.adoc.hbs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
{{#if symbol.refQualifier}} {{symbol.refQualifier}}{{/if~}}
2424
{{#if symbol.exceptionSpec}} {{symbol.exceptionSpec}}{{/if~}}
2525
{{#if (eq symbol.class "normal")}}{{>declarator-after symbol.return}}{{/if~}}
26+
{{#if symbol.requires}} requires {{symbol.requires}}{{/if~}}
2627
{{#if symbol.hasOverrideAttr}} override{{/if~}}
2728
{{#if symbol.isFinal}} final{{/if~}}
2829
{{#if symbol.isPure}} = 0{{/if~}}

0 commit comments

Comments
 (0)