Skip to content

Commit 3948a8c

Browse files
mizvekovK-ballo
andauthored
getSFINAEControlParams fix
Co-authored-by: Agustin Berge <[email protected]>
1 parent cf169e6 commit 3948a8c

File tree

9 files changed

+694
-20
lines changed

9 files changed

+694
-20
lines changed

src/lib/AST/ASTVisitor.cpp

Lines changed: 41 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -2382,26 +2382,16 @@ getSFINAEControlParams(
23822382
// template argument in a list of template arguments. It is used
23832383
// to find the index of the controlling parameter in the list of
23842384
// template arguments of the template declaration.
2385-
auto FindParam = [this](
2386-
llvm::ArrayRef<clang::TemplateArgument> Arguments,
2387-
clang::TemplateArgument const& Arg) -> std::size_t
2388-
{
2389-
if (Arg.getKind() != clang::TemplateArgument::Type)
2390-
{
2391-
return -1;
2392-
}
2393-
auto const It = std::ranges::find_if(
2394-
Arguments,
2395-
[&](clang::TemplateArgument const& Other)
2396-
{
2397-
if (Other.getKind() != clang::TemplateArgument::Type)
2398-
{
2399-
return false;
2400-
}
2401-
return context_.hasSameType(Other.getAsType(), Arg.getAsType());
2402-
});
2385+
auto FindParam =
2386+
[this](
2387+
llvm::ArrayRef<clang::TemplateArgument> Arguments,
2388+
clang::TemplateArgument const& Arg) -> std::size_t {
2389+
auto const It = std::ranges::
2390+
find_if(Arguments, [&](clang::TemplateArgument const& Other) {
2391+
return context_.isSameTemplateArgument(Arg, Other);
2392+
});
24032393
bool const found = It != Arguments.end();
2404-
return found ? It - Arguments.data() : static_cast<std::size_t>(-1);
2394+
return found ? It - Arguments.begin() : static_cast<std::size_t>(-1);
24052395
};
24062396

24072397
if(auto* ATD = dyn_cast<clang::TypeAliasTemplateDecl>(TD))
@@ -2441,9 +2431,40 @@ getSFINAEControlParams(
24412431
// the primary template arguments
24422432
clang::TemplateParameterList* primaryTemplParams = ATD->getTemplateParameters();
24432433
MRDOCS_SYMBOL_TRACE(primaryTemplParams, context_);
2434+
2435+
llvm::SmallBitVector primaryControllingParams(
2436+
primaryTemplParams->size());
2437+
for (std::size_t i = 0; i < sfinaeControl->ControllingParams.size();
2438+
++i)
2439+
{
2440+
if (sfinaeControl->ControllingParams[i])
2441+
{
2442+
// Find the index of the parameter that represents the SFINAE
2443+
// result in the underlying template arguments
2444+
auto resultType = tryGetTemplateArgument(
2445+
sfinaeControl->Parameters,
2446+
underlyingTemplateInfo->Arguments,
2447+
i);
2448+
MRDOCS_CHECK_OR_CONTINUE(resultType);
2449+
MRDOCS_SYMBOL_TRACE(*resultType, context_);
2450+
2451+
// Find the index of the parameter that represents the param
2452+
// in the primary template arguments
2453+
auto ParamIdx = FindParam(
2454+
ATD->getInjectedTemplateArgs(context_),
2455+
*resultType);
2456+
if (ParamIdx == static_cast<std::size_t>(-1))
2457+
{
2458+
continue;
2459+
}
2460+
2461+
primaryControllingParams.set(ParamIdx);
2462+
}
2463+
}
2464+
24442465
return SFINAEControlParams(
24452466
primaryTemplParams,
2446-
std::move(sfinaeControl->ControllingParams),
2467+
std::move(primaryControllingParams),
24472468
ParamIdx);
24482469
}
24492470

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
= Reference
2+
:mrdocs:
3+
4+
[#index]
5+
== Global namespace
6+
7+
=== Types
8+
9+
[cols=1]
10+
|===
11+
| Name
12+
| link:#enable_if_t[`enable&lowbar;if&lowbar;t`]
13+
| link:#if_enable_t[`if&lowbar;enable&lowbar;t`]
14+
|===
15+
16+
=== Functions
17+
18+
[cols=1]
19+
|===
20+
| Name
21+
| link:#f1[`f1`]
22+
| link:#f2[`f2`]
23+
|===
24+
25+
[#enable_if_t]
26+
== enable&lowbar;if&lowbar;t
27+
28+
=== Synopsis
29+
30+
Declared in `&lt;alias&period;cpp&gt;`
31+
32+
[source,cpp,subs="verbatim,replacements,macros,-callouts"]
33+
----
34+
template&lt;
35+
bool C,
36+
typename T&gt;
37+
using enable&lowbar;if&lowbar;t = T;
38+
----
39+
40+
[#if_enable_t]
41+
== if&lowbar;enable&lowbar;t
42+
43+
=== Synopsis
44+
45+
Declared in `&lt;alias&period;cpp&gt;`
46+
47+
[source,cpp,subs="verbatim,replacements,macros,-callouts"]
48+
----
49+
template&lt;
50+
typename T,
51+
bool C&gt;
52+
using if&lowbar;enable&lowbar;t = T;
53+
----
54+
55+
[#f1]
56+
== f1
57+
58+
=== Synopsis
59+
60+
Declared in `&lt;alias&period;cpp&gt;`
61+
62+
[source,cpp,subs="verbatim,replacements,macros,-callouts"]
63+
----
64+
template&lt;typename T&gt;
65+
T
66+
f1()
67+
requires std&colon;&colon;is&lowbar;class&lowbar;v&lt;T&gt;;
68+
----
69+
70+
[#f2]
71+
== f2
72+
73+
=== Synopsis
74+
75+
Declared in `&lt;alias&period;cpp&gt;`
76+
77+
[source,cpp,subs="verbatim,replacements,macros,-callouts"]
78+
----
79+
template&lt;typename T&gt;
80+
T
81+
f2()
82+
requires std&colon;&colon;is&lowbar;class&lowbar;v&lt;T&gt;;
83+
----
84+
85+
86+
[.small]#Created with https://www.mrdocs.com[MrDocs]#
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
#include <type_traits>
2+
3+
// just like std::enable_if_t
4+
template <bool C, typename T>
5+
using enable_if_t = typename std::enable_if<C, T>::type;
6+
7+
template <typename T>
8+
enable_if_t<std::is_class_v<T>, T>
9+
f1();
10+
11+
// reversed param order
12+
template <typename T, bool C>
13+
using if_enable_t = typename std::enable_if<C, T>::type;
14+
15+
template <typename T>
16+
if_enable_t<T, std::is_class_v<T>>
17+
f2();
Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
<html lang="en">
2+
<head>
3+
<title>Reference</title>
4+
</head>
5+
<body>
6+
<div>
7+
<h1>Reference</h1>
8+
<div>
9+
<div>
10+
<h2 id="index"></h2>
11+
</div>
12+
<h2>Types</h2>
13+
<table style="table-layout: fixed; width: 100%;">
14+
<thead>
15+
<tr>
16+
<th>Name</th>
17+
</tr>
18+
</thead>
19+
<tbody>
20+
<tr>
21+
<td><a href="#enable_if_t"><code>enable_if_t</code></a> </td></tr><tr>
22+
<td><a href="#if_enable_t"><code>if_enable_t</code></a> </td></tr>
23+
</tbody>
24+
</table>
25+
26+
<h2>Functions</h2>
27+
<table style="table-layout: fixed; width: 100%;">
28+
<thead>
29+
<tr>
30+
<th>Name</th>
31+
</tr>
32+
</thead>
33+
<tbody>
34+
<tr>
35+
<td><a href="#f1"><code>f1</code></a> </td></tr><tr>
36+
<td><a href="#f2"><code>f2</code></a> </td></tr>
37+
</tbody>
38+
</table>
39+
40+
</div>
41+
<div>
42+
<div>
43+
<h2 id="enable_if_t">enable_if_t</h2>
44+
</div>
45+
<div>
46+
<h3>Synopsis</h3>
47+
<div>
48+
Declared in <code>&lt;alias.cpp&gt;</code></div>
49+
<pre>
50+
<code class="source-code cpp">template&lt;
51+
bool C,
52+
typename T&gt;
53+
using enable_if_t = T;</code>
54+
</pre>
55+
</div>
56+
</div>
57+
<div>
58+
<div>
59+
<h2 id="if_enable_t">if_enable_t</h2>
60+
</div>
61+
<div>
62+
<h3>Synopsis</h3>
63+
<div>
64+
Declared in <code>&lt;alias.cpp&gt;</code></div>
65+
<pre>
66+
<code class="source-code cpp">template&lt;
67+
typename T,
68+
bool C&gt;
69+
using if_enable_t = T;</code>
70+
</pre>
71+
</div>
72+
</div>
73+
<div>
74+
<div>
75+
<h2 id="f1">f1</h2>
76+
</div>
77+
<div>
78+
<h3>Synopsis</h3>
79+
<div>
80+
Declared in <code>&lt;alias.cpp&gt;</code></div>
81+
<pre>
82+
<code class="source-code cpp">template&lt;typename T&gt;
83+
T
84+
f1()
85+
requires std::is_class_v&lt;T&gt;;</code>
86+
</pre>
87+
</div>
88+
</div>
89+
<div>
90+
<div>
91+
<h2 id="f2">f2</h2>
92+
</div>
93+
<div>
94+
<h3>Synopsis</h3>
95+
<div>
96+
Declared in <code>&lt;alias.cpp&gt;</code></div>
97+
<pre>
98+
<code class="source-code cpp">template&lt;typename T&gt;
99+
T
100+
f2()
101+
requires std::is_class_v&lt;T&gt;;</code>
102+
</pre>
103+
</div>
104+
</div>
105+
106+
</div>
107+
<div>
108+
<h4>Created with <a href="https://www.mrdocs.com">MrDocs</a></h4>
109+
</div>
110+
</body>
111+
</html>
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<mrdocs xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3+
xsi:noNamespaceSchemaLocation="https://github.com/cppalliance/mrdocs/raw/develop/mrdocs.rnc">
4+
<namespace id="//////////////////////////8=">
5+
<template>
6+
<tparam name="C" class="constant" type="bool"/>
7+
<tparam name="T" class="type"/>
8+
<namespace-alias name="enable_if_t" id="G6KvcosPT2J4Mp72/Jj/t+hpu7w=">
9+
<file short-path="alias.cpp" source-path="alias.cpp" line="4"/>
10+
<type name="T"/>
11+
</namespace-alias>
12+
</template>
13+
<template>
14+
<tparam name="T" class="type"/>
15+
<tparam name="C" class="constant" type="bool"/>
16+
<namespace-alias name="if_enable_t" id="3mL2o5cGgdygMfQf6RJVMv4YrLg=">
17+
<file short-path="alias.cpp" source-path="alias.cpp" line="12"/>
18+
<type name="T"/>
19+
</namespace-alias>
20+
</template>
21+
<template>
22+
<tparam name="T" class="type"/>
23+
<function name="f1" requires="std::is_class_v&lt;T&gt;" id="wxSwXFUt9arsW/YBeWhVf1gINhw=">
24+
<file short-path="alias.cpp" source-path="alias.cpp" line="7"/>
25+
<return>
26+
<type name="T"/>
27+
</return>
28+
</function>
29+
</template>
30+
<template>
31+
<tparam name="T" class="type"/>
32+
<function name="f2" requires="std::is_class_v&lt;T&gt;" id="koj7SBKjX4SV47aOs0homZpcPWQ=">
33+
<file short-path="alias.cpp" source-path="alias.cpp" line="15"/>
34+
<return>
35+
<type name="T"/>
36+
</return>
37+
</function>
38+
</template>
39+
</namespace>
40+
</mrdocs>

0 commit comments

Comments
 (0)