Skip to content

Commit

Permalink
support inline constexpr variables
Browse files Browse the repository at this point in the history
#feat
  • Loading branch information
alandefreitas committed Jan 13, 2025
1 parent 6c6ffc9 commit f648817
Show file tree
Hide file tree
Showing 29 changed files with 618 additions and 141 deletions.
16 changes: 12 additions & 4 deletions docs/modules/ROOT/pages/generators.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -438,14 +438,18 @@ When the symbol kind is `variable`, the symbol object has the following addition
| `<<template-info-fields,Template Info Object>>`
| The template information of the variable.

| `constexprKind`
| `string`
| The constexpr kind of the variable (e.g., `consteval`, `constexpr`).

| `storageClass`
| `string`
| The storage class of the variable (e.g., `static`, `extern`).

| `isInline`
| `bool`
| Whether the variable is `inline`.

| `isConstexpr`
| `bool`
| Whether the variable is `constexpr`.

| `isConstinit`
| `bool`
| Whether the variable is `constinit`.
Expand All @@ -457,6 +461,10 @@ When the symbol kind is `variable`, the symbol object has the following addition
| `initializer`
| `string`
| The initializer of the variable.

| `attributes`
| `string[]`
| The attributes of the variable.
|===

When the symbol kind is `field` (i.e. non-static data members), the symbol object has the following additional properties:
Expand Down
6 changes: 5 additions & 1 deletion include/mrdocs/Metadata/Variable.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,12 +40,16 @@ struct VariableInfo

StorageClassKind StorageClass = StorageClassKind::None;

ConstexprKind Constexpr = ConstexprKind::None;
bool IsInline = false;

bool IsConstexpr = false;

bool IsConstinit = false;

bool IsThreadLocal = false;

std::vector<std::string> Attributes;

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

explicit VariableInfo(SymbolID ID) noexcept
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{{#if template}}{{>template/head template}}
{{/if~}}
{{#if constexprKind}}{{constexprKind}}
{{/if~}}
{{#if isInline}}inline {{/if~}}
{{#if isConstexpr}}constexpr {{/if~}}
{{#if storageClass}}{{storageClass}}
{{/if~}}
{{#if isThreadLocal}}thread_local
Expand Down
15 changes: 10 additions & 5 deletions src/lib/AST/ASTVisitor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1192,15 +1192,20 @@ populate(
// it is possible to declare a static data member
// as both constexpr and constinit in separate declarations..
I.IsConstinit |= D->hasAttr<ConstInitAttr>();
if (D->isConstexpr())
{
I.Constexpr = ConstexprKind::Constexpr;
}
I.IsConstexpr |= D->isConstexpr();
I.IsInline |= D->isInline();
if (Expr const* E = D->getInit())
{
populate(I.Initializer, E);
}
I.Type = toTypeInfo(D->getType());
auto QT = D->getType();
if (D->isConstexpr()) {
// when D->isConstexpr() is true, QT contains a redundant
// `const` qualifier which we need to remove
QT.removeLocalConst();
}
I.Type = toTypeInfo(QT);
populateAttributes(I, D);
}

void
Expand Down
3 changes: 2 additions & 1 deletion src/lib/Gen/xml/XMLWriter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -598,7 +598,8 @@ writeVariable(
writeSourceInfo(I);

writeAttr(I.StorageClass, "storage-class", tags_);
writeAttr(I.Constexpr, "constexpr-kind", tags_);
writeAttr(I.IsInline, "is-inline", tags_);
writeAttr(I.IsConstexpr, "is-constexpr", tags_);
writeAttr(I.IsConstinit, "is-constinit", tags_);
writeAttr(I.IsThreadLocal, "is-thread-local", tags_);

Expand Down
24 changes: 12 additions & 12 deletions src/lib/Metadata/Info.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -189,22 +189,22 @@ tag_invoke(
}
if constexpr (T::isVariable())
{
io.map("type", I.Type);
io.map("template", I.Template);
if (I.Constexpr != ConstexprKind::None)
{
io.map("constexprKind", I.Constexpr);
}
if (I.StorageClass != StorageClassKind::None)
auto const& U = static_cast<VariableInfo const&>(I);
io.map("type", U.Type);
io.map("template", U.Template);
if (U.StorageClass != StorageClassKind::None)
{
io.map("storageClass", I.StorageClass);
io.map("storageClass", U.StorageClass);
}
io.map("isConstinit", I.IsConstinit);
io.map("isThreadLocal", I.IsThreadLocal);
if (!I.Initializer.Written.empty())
io.map("isInline", U.IsInline);
io.map("isConstexpr", U.IsConstexpr);
io.map("isConstinit", U.IsConstinit);
io.map("isThreadLocal", U.IsThreadLocal);
if (!U.Initializer.Written.empty())
{
io.map("initializer", I.Initializer.Written);
io.map("initializer", U.Initializer.Written);
}
io.map("attributes", dom::LazyArray(U.Attributes));
}
if constexpr (T::isField())
{
Expand Down
16 changes: 12 additions & 4 deletions src/lib/Metadata/Reduce.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -325,11 +325,19 @@ void merge(VariableInfo& I, VariableInfo&& Other)

I.IsConstinit |= Other.IsConstinit;
I.IsThreadLocal |= Other.IsThreadLocal;

if(I.Constexpr == ConstexprKind::None)
I.Constexpr = Other.Constexpr;
if(I.StorageClass == StorageClassKind::None)
I.IsConstexpr |= Other.IsConstexpr;
I.IsInline |= Other.IsInline;
if (I.StorageClass == StorageClassKind::None)
{
I.StorageClass = Other.StorageClass;
}
for (auto& otherAttr: Other.Attributes)
{
if (std::ranges::find(I.Attributes, otherAttr) == I.Attributes.end())
{
I.Attributes.push_back(otherAttr);
}
}
}

void merge(SpecializationInfo& I, SpecializationInfo&& Other)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,7 @@ Declared in `&lt;impl&hyphen;defined&hyphen;member&period;cpp&gt;`

[source,cpp,subs="verbatim,replacements,macros,-callouts"]
----
constexpr
&sol;&ast; implementation-defined &ast;&sol; const absolute&lowbar;uri&lowbar;rule = &lcub;&rcub;;
constexpr &sol;&ast; implementation-defined &ast;&sol; absolute&lowbar;uri&lowbar;rule = &lcub;&rcub;;
----

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

[source,cpp,subs="verbatim,replacements,macros,-callouts"]
----
constexpr
<<regular,regular>>::<<regular-absolute_uri_rule_t,absolute&lowbar;uri&lowbar;rule&lowbar;t>> const regular&lowbar;absolute&lowbar;uri&lowbar;rule = &lcub;&rcub;;
constexpr <<regular,regular>>::<<regular-absolute_uri_rule_t,absolute&lowbar;uri&lowbar;rule&lowbar;t>> regular&lowbar;absolute&lowbar;uri&lowbar;rule = &lcub;&rcub;;
----


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,8 +79,7 @@ <h3>Synopsis</h3>
Declared in <code>&lt;impl-defined-member.cpp&gt;</code></div>
<pre>
<code class="source-code cpp">
constexpr
/* implementation-defined */ const absolute_uri_rule = {};
constexpr /* implementation-defined */ absolute_uri_rule = {};
</code>
</pre>
</div>
Expand All @@ -95,8 +94,7 @@ <h3>Synopsis</h3>
Declared in <code>&lt;impl-defined-member.cpp&gt;</code></div>
<pre>
<code class="source-code cpp">
constexpr
<a href="#regular">regular</a>::<a href="#regular-absolute_uri_rule_t">absolute_uri_rule_t</a> const regular_absolute_uri_rule = {};
constexpr <a href="#regular">regular</a>::<a href="#regular-absolute_uri_rule_t">absolute_uri_rule_t</a> regular_absolute_uri_rule = {};
</code>
</pre>
</div>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,13 @@
</namespace>
<variable name="absolute_uri_rule" id="/ePshzEghZYl/D7bJ6X3M2XbKEg=">
<file path="impl-defined-member.cpp" line="9" class="def"/>
<attr id="constexpr-kind" name="constexpr" value="1"/>
<type id="vOKKVGAcuI3CBaMkdZstuFgnnuQ=" name="detail::absolute_uri_rule_t" cv-qualifiers="const"/>
<attr id="is-constexpr"/>
<type id="vOKKVGAcuI3CBaMkdZstuFgnnuQ=" name="detail::absolute_uri_rule_t"/>
</variable>
<variable name="regular_absolute_uri_rule" id="ixmivDXFAcQVJd4G7XOqINAR858=">
<file path="impl-defined-member.cpp" line="11" class="def"/>
<attr id="constexpr-kind" name="constexpr" value="1"/>
<type id="fA+/UQOR2lG0f1+BZ+GeGlfo5sY=" name="regular::absolute_uri_rule_t" cv-qualifiers="const"/>
<attr id="is-constexpr"/>
<type id="fA+/UQOR2lG0f1+BZ+GeGlfo5sY=" name="regular::absolute_uri_rule_t"/>
</variable>
</namespace>
</mrdocs>
13 changes: 5 additions & 8 deletions test-files/golden-tests/metadata/ns-variables.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,7 @@ Declared in `&lt;ns&hyphen;variables&period;cpp&gt;`

[source,cpp,subs="verbatim,replacements,macros,-callouts"]
----
constexpr
int const i = 0;
constexpr int i = 0;
----

[#j]
Expand Down Expand Up @@ -87,9 +86,8 @@ Declared in `&lt;ns&hyphen;variables&period;cpp&gt;`

[source,cpp,subs="verbatim,replacements,macros,-callouts"]
----
constexpr
extern
int const k = 1;
constexpr extern
int k = 1;
----

[#l]
Expand Down Expand Up @@ -178,10 +176,9 @@ Declared in `&lt;ns&hyphen;variables&period;cpp&gt;`

[source,cpp,subs="verbatim,replacements,macros,-callouts"]
----
constexpr
static
constexpr static
thread_local
int const x2 = 0;
int x2 = 0;
----


Expand Down
13 changes: 5 additions & 8 deletions test-files/golden-tests/metadata/ns-variables.html
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,7 @@ <h3>Synopsis</h3>
Declared in <code>&lt;ns-variables.cpp&gt;</code></div>
<pre>
<code class="source-code cpp">
constexpr
int const i = 0;
constexpr int i = 0;
</code>
</pre>
</div>
Expand Down Expand Up @@ -100,9 +99,8 @@ <h3>Synopsis</h3>
Declared in <code>&lt;ns-variables.cpp&gt;</code></div>
<pre>
<code class="source-code cpp">
constexpr
extern
int const k = 1;
constexpr extern
int k = 1;
</code>
</pre>
</div>
Expand Down Expand Up @@ -197,10 +195,9 @@ <h3>Synopsis</h3>
Declared in <code>&lt;ns-variables.cpp&gt;</code></div>
<pre>
<code class="source-code cpp">
constexpr
static
constexpr static
thread_local
int const x2 = 0;
int x2 = 0;
</code>
</pre>
</div>
Expand Down
12 changes: 6 additions & 6 deletions test-files/golden-tests/metadata/ns-variables.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
<namespace id="//////////////////////////8=">
<variable name="i" id="X2Wd7fyQ8BJNJ0tn4nnA77ckJM8=">
<file path="ns-variables.cpp" line="3" class="def"/>
<attr id="constexpr-kind" name="constexpr" value="1"/>
<type name="int" cv-qualifiers="const"/>
<attr id="is-constexpr"/>
<type name="int"/>
</variable>
<variable name="j" id="pKvK/kHXdM/1/pu86JcSrLqVypE=">
<file path="ns-variables.cpp" line="4" class="def"/>
Expand All @@ -16,8 +16,8 @@
<file path="ns-variables.cpp" line="9" class="def"/>
<file path="ns-variables.cpp" line="6"/>
<attr id="storage-class" name="extern" value="1"/>
<attr id="constexpr-kind" name="constexpr" value="1"/>
<type name="int" cv-qualifiers="const"/>
<attr id="is-constexpr"/>
<type name="int"/>
</variable>
<variable name="l" id="h5r4fNloVAMKqMyVJI+iWTxRjhM=">
<file path="ns-variables.cpp" line="10" class="def"/>
Expand Down Expand Up @@ -52,9 +52,9 @@
<variable name="x2" id="EpcDWput1W/SJ3IYEINS0W815Zw=">
<file path="ns-variables.cpp" line="20" class="def"/>
<attr id="storage-class" name="static" value="2"/>
<attr id="constexpr-kind" name="constexpr" value="1"/>
<attr id="is-constexpr"/>
<attr id="is-thread-local"/>
<type name="int" cv-qualifiers="const"/>
<type name="int"/>
</variable>
</namespace>
</mrdocs>
10 changes: 4 additions & 6 deletions test-files/golden-tests/metadata/static-data-def-constexpr.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,8 @@ Declared in `&lt;static&hyphen;data&hyphen;def&hyphen;constexpr&period;cpp&gt;`

[source,cpp,subs="verbatim,replacements,macros,-callouts"]
----
constexpr
static
<<S,S>> const s = S&lcub;&rcub;;
inline constexpr static
<<S,S>> s = S&lcub;&rcub;;
----

[#T]
Expand Down Expand Up @@ -92,9 +91,8 @@ Declared in `&lt;static&hyphen;data&hyphen;def&hyphen;constexpr&period;cpp&gt;`

[source,cpp,subs="verbatim,replacements,macros,-callouts"]
----
constexpr
static
int const t = 0;
inline constexpr static
int t = 0;
----


Expand Down
10 changes: 4 additions & 6 deletions test-files/golden-tests/metadata/static-data-def-constexpr.html
Original file line number Diff line number Diff line change
Expand Up @@ -62,9 +62,8 @@ <h3>Synopsis</h3>
Declared in <code>&lt;static-data-def-constexpr.cpp&gt;</code></div>
<pre>
<code class="source-code cpp">
constexpr
static
<a href="#S">S</a> const s = S{};
inline constexpr static
<a href="#S">S</a> s = S{};
</code>
</pre>
</div>
Expand Down Expand Up @@ -108,9 +107,8 @@ <h3>Synopsis</h3>
Declared in <code>&lt;static-data-def-constexpr.cpp&gt;</code></div>
<pre>
<code class="source-code cpp">
constexpr
static
int const t = 0;
inline constexpr static
int t = 0;
</code>
</pre>
</div>
Expand Down
10 changes: 6 additions & 4 deletions test-files/golden-tests/metadata/static-data-def-constexpr.xml
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,9 @@
<file path="static-data-def-constexpr.cpp" line="5" class="def"/>
<file path="static-data-def-constexpr.cpp" line="3"/>
<attr id="storage-class" name="static" value="2"/>
<attr id="constexpr-kind" name="constexpr" value="1"/>
<type id="pOYGF6pLJlICuiGO0Xj0daDb/to=" name="S" cv-qualifiers="const"/>
<attr id="is-inline"/>
<attr id="is-constexpr"/>
<type id="pOYGF6pLJlICuiGO0Xj0daDb/to=" name="S"/>
</variable>
</struct>
<struct name="T" id="CgGNdHpW5mG/i5741WPYQDw28OQ=">
Expand All @@ -18,9 +19,10 @@
<file path="static-data-def-constexpr.cpp" line="12" class="def"/>
<file path="static-data-def-constexpr.cpp" line="9"/>
<attr id="storage-class" name="static" value="2"/>
<attr id="constexpr-kind" name="constexpr" value="1"/>
<attr id="is-inline"/>
<attr id="is-constexpr"/>
<attr id="is-constinit"/>
<type name="int" cv-qualifiers="const"/>
<type name="int"/>
</variable>
</struct>
</namespace>
Expand Down
Loading

0 comments on commit f648817

Please sign in to comment.