Skip to content

Commit f648817

Browse files
committed
support inline constexpr variables
#feat
1 parent 6c6ffc9 commit f648817

29 files changed

+618
-141
lines changed

docs/modules/ROOT/pages/generators.adoc

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -438,14 +438,18 @@ When the symbol kind is `variable`, the symbol object has the following addition
438438
| `<<template-info-fields,Template Info Object>>`
439439
| The template information of the variable.
440440

441-
| `constexprKind`
442-
| `string`
443-
| The constexpr kind of the variable (e.g., `consteval`, `constexpr`).
444-
445441
| `storageClass`
446442
| `string`
447443
| The storage class of the variable (e.g., `static`, `extern`).
448444

445+
| `isInline`
446+
| `bool`
447+
| Whether the variable is `inline`.
448+
449+
| `isConstexpr`
450+
| `bool`
451+
| Whether the variable is `constexpr`.
452+
449453
| `isConstinit`
450454
| `bool`
451455
| Whether the variable is `constinit`.
@@ -457,6 +461,10 @@ When the symbol kind is `variable`, the symbol object has the following addition
457461
| `initializer`
458462
| `string`
459463
| The initializer of the variable.
464+
465+
| `attributes`
466+
| `string[]`
467+
| The attributes of the variable.
460468
|===
461469

462470
When the symbol kind is `field` (i.e. non-static data members), the symbol object has the following additional properties:

include/mrdocs/Metadata/Variable.hpp

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,12 +40,16 @@ struct VariableInfo
4040

4141
StorageClassKind StorageClass = StorageClassKind::None;
4242

43-
ConstexprKind Constexpr = ConstexprKind::None;
43+
bool IsInline = false;
44+
45+
bool IsConstexpr = false;
4446

4547
bool IsConstinit = false;
4648

4749
bool IsThreadLocal = false;
4850

51+
std::vector<std::string> Attributes;
52+
4953
//--------------------------------------------
5054

5155
explicit VariableInfo(SymbolID ID) noexcept

share/mrdocs/addons/generator/common/partials/symbol/signature/variable.hbs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{{#if template}}{{>template/head template}}
22
{{/if~}}
3-
{{#if constexprKind}}{{constexprKind}}
4-
{{/if~}}
3+
{{#if isInline}}inline {{/if~}}
4+
{{#if isConstexpr}}constexpr {{/if~}}
55
{{#if storageClass}}{{storageClass}}
66
{{/if~}}
77
{{#if isThreadLocal}}thread_local

src/lib/AST/ASTVisitor.cpp

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1192,15 +1192,20 @@ populate(
11921192
// it is possible to declare a static data member
11931193
// as both constexpr and constinit in separate declarations..
11941194
I.IsConstinit |= D->hasAttr<ConstInitAttr>();
1195-
if (D->isConstexpr())
1196-
{
1197-
I.Constexpr = ConstexprKind::Constexpr;
1198-
}
1195+
I.IsConstexpr |= D->isConstexpr();
1196+
I.IsInline |= D->isInline();
11991197
if (Expr const* E = D->getInit())
12001198
{
12011199
populate(I.Initializer, E);
12021200
}
1203-
I.Type = toTypeInfo(D->getType());
1201+
auto QT = D->getType();
1202+
if (D->isConstexpr()) {
1203+
// when D->isConstexpr() is true, QT contains a redundant
1204+
// `const` qualifier which we need to remove
1205+
QT.removeLocalConst();
1206+
}
1207+
I.Type = toTypeInfo(QT);
1208+
populateAttributes(I, D);
12041209
}
12051210

12061211
void

src/lib/Gen/xml/XMLWriter.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -598,7 +598,8 @@ writeVariable(
598598
writeSourceInfo(I);
599599

600600
writeAttr(I.StorageClass, "storage-class", tags_);
601-
writeAttr(I.Constexpr, "constexpr-kind", tags_);
601+
writeAttr(I.IsInline, "is-inline", tags_);
602+
writeAttr(I.IsConstexpr, "is-constexpr", tags_);
602603
writeAttr(I.IsConstinit, "is-constinit", tags_);
603604
writeAttr(I.IsThreadLocal, "is-thread-local", tags_);
604605

src/lib/Metadata/Info.cpp

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -189,22 +189,22 @@ tag_invoke(
189189
}
190190
if constexpr (T::isVariable())
191191
{
192-
io.map("type", I.Type);
193-
io.map("template", I.Template);
194-
if (I.Constexpr != ConstexprKind::None)
195-
{
196-
io.map("constexprKind", I.Constexpr);
197-
}
198-
if (I.StorageClass != StorageClassKind::None)
192+
auto const& U = static_cast<VariableInfo const&>(I);
193+
io.map("type", U.Type);
194+
io.map("template", U.Template);
195+
if (U.StorageClass != StorageClassKind::None)
199196
{
200-
io.map("storageClass", I.StorageClass);
197+
io.map("storageClass", U.StorageClass);
201198
}
202-
io.map("isConstinit", I.IsConstinit);
203-
io.map("isThreadLocal", I.IsThreadLocal);
204-
if (!I.Initializer.Written.empty())
199+
io.map("isInline", U.IsInline);
200+
io.map("isConstexpr", U.IsConstexpr);
201+
io.map("isConstinit", U.IsConstinit);
202+
io.map("isThreadLocal", U.IsThreadLocal);
203+
if (!U.Initializer.Written.empty())
205204
{
206-
io.map("initializer", I.Initializer.Written);
205+
io.map("initializer", U.Initializer.Written);
207206
}
207+
io.map("attributes", dom::LazyArray(U.Attributes));
208208
}
209209
if constexpr (T::isField())
210210
{

src/lib/Metadata/Reduce.cpp

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -325,11 +325,19 @@ void merge(VariableInfo& I, VariableInfo&& Other)
325325

326326
I.IsConstinit |= Other.IsConstinit;
327327
I.IsThreadLocal |= Other.IsThreadLocal;
328-
329-
if(I.Constexpr == ConstexprKind::None)
330-
I.Constexpr = Other.Constexpr;
331-
if(I.StorageClass == StorageClassKind::None)
328+
I.IsConstexpr |= Other.IsConstexpr;
329+
I.IsInline |= Other.IsInline;
330+
if (I.StorageClass == StorageClassKind::None)
331+
{
332332
I.StorageClass = Other.StorageClass;
333+
}
334+
for (auto& otherAttr: Other.Attributes)
335+
{
336+
if (std::ranges::find(I.Attributes, otherAttr) == I.Attributes.end())
337+
{
338+
I.Attributes.push_back(otherAttr);
339+
}
340+
}
333341
}
334342

335343
void merge(SpecializationInfo& I, SpecializationInfo&& Other)

test-files/golden-tests/filters/symbol-name/impl-defined-member.adoc

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -64,8 +64,7 @@ Declared in `&lt;impl&hyphen;defined&hyphen;member&period;cpp&gt;`
6464

6565
[source,cpp,subs="verbatim,replacements,macros,-callouts"]
6666
----
67-
constexpr
68-
&sol;&ast; implementation-defined &ast;&sol; const absolute&lowbar;uri&lowbar;rule = &lcub;&rcub;;
67+
constexpr &sol;&ast; implementation-defined &ast;&sol; absolute&lowbar;uri&lowbar;rule = &lcub;&rcub;;
6968
----
7069

7170
[#regular_absolute_uri_rule]
@@ -79,8 +78,7 @@ Declared in `&lt;impl&hyphen;defined&hyphen;member&period;cpp&gt;`
7978

8079
[source,cpp,subs="verbatim,replacements,macros,-callouts"]
8180
----
82-
constexpr
83-
<<regular,regular>>::<<regular-absolute_uri_rule_t,absolute&lowbar;uri&lowbar;rule&lowbar;t>> const regular&lowbar;absolute&lowbar;uri&lowbar;rule = &lcub;&rcub;;
81+
constexpr <<regular,regular>>::<<regular-absolute_uri_rule_t,absolute&lowbar;uri&lowbar;rule&lowbar;t>> regular&lowbar;absolute&lowbar;uri&lowbar;rule = &lcub;&rcub;;
8482
----
8583

8684

test-files/golden-tests/filters/symbol-name/impl-defined-member.html

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -79,8 +79,7 @@ <h3>Synopsis</h3>
7979
Declared in <code>&lt;impl-defined-member.cpp&gt;</code></div>
8080
<pre>
8181
<code class="source-code cpp">
82-
constexpr
83-
/* implementation-defined */ const absolute_uri_rule = {};
82+
constexpr /* implementation-defined */ absolute_uri_rule = {};
8483
</code>
8584
</pre>
8685
</div>
@@ -95,8 +94,7 @@ <h3>Synopsis</h3>
9594
Declared in <code>&lt;impl-defined-member.cpp&gt;</code></div>
9695
<pre>
9796
<code class="source-code cpp">
98-
constexpr
99-
<a href="#regular">regular</a>::<a href="#regular-absolute_uri_rule_t">absolute_uri_rule_t</a> const regular_absolute_uri_rule = {};
97+
constexpr <a href="#regular">regular</a>::<a href="#regular-absolute_uri_rule_t">absolute_uri_rule_t</a> regular_absolute_uri_rule = {};
10098
</code>
10199
</pre>
102100
</div>

test-files/golden-tests/filters/symbol-name/impl-defined-member.xml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,13 @@
1414
</namespace>
1515
<variable name="absolute_uri_rule" id="/ePshzEghZYl/D7bJ6X3M2XbKEg=">
1616
<file path="impl-defined-member.cpp" line="9" class="def"/>
17-
<attr id="constexpr-kind" name="constexpr" value="1"/>
18-
<type id="vOKKVGAcuI3CBaMkdZstuFgnnuQ=" name="detail::absolute_uri_rule_t" cv-qualifiers="const"/>
17+
<attr id="is-constexpr"/>
18+
<type id="vOKKVGAcuI3CBaMkdZstuFgnnuQ=" name="detail::absolute_uri_rule_t"/>
1919
</variable>
2020
<variable name="regular_absolute_uri_rule" id="ixmivDXFAcQVJd4G7XOqINAR858=">
2121
<file path="impl-defined-member.cpp" line="11" class="def"/>
22-
<attr id="constexpr-kind" name="constexpr" value="1"/>
23-
<type id="fA+/UQOR2lG0f1+BZ+GeGlfo5sY=" name="regular::absolute_uri_rule_t" cv-qualifiers="const"/>
22+
<attr id="is-constexpr"/>
23+
<type id="fA+/UQOR2lG0f1+BZ+GeGlfo5sY=" name="regular::absolute_uri_rule_t"/>
2424
</variable>
2525
</namespace>
2626
</mrdocs>

0 commit comments

Comments
 (0)