Skip to content

Commit 42cc6ec

Browse files
committed
feat: extract and render initializers for variables and fields
1 parent bf60a45 commit 42cc6ec

File tree

12 files changed

+68
-10
lines changed

12 files changed

+68
-10
lines changed

include/mrdocs/Metadata/Field.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ struct FieldInfo
4848

4949
/** The default member initializer, if any.
5050
*/
51-
std::string Default;
51+
ExprInfo Default;
5252

5353
// attributes (maybe_unused, no_unique_address, deprecated)
5454
FieldFlags specs;

include/mrdocs/Metadata/Variable.hpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414

1515
#include <mrdocs/Platform.hpp>
1616
#include <mrdocs/ADT/BitField.hpp>
17+
#include <mrdocs/Metadata/Expression.hpp>
1718
#include <mrdocs/Metadata/Source.hpp>
1819
#include <mrdocs/Metadata/Template.hpp>
1920
#include <mrdocs/Metadata/Type.hpp>
@@ -48,6 +49,8 @@ struct VariableInfo
4849

4950
VariableFlags0 specs{.raw={0}};
5051

52+
ExprInfo Initializer;
53+
5154
//--------------------------------------------
5255

5356
explicit VariableInfo(SymbolID ID) noexcept

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,5 @@
22
{{/if~}}
33
{{>declarator-before symbol.type}} {{>declarator-id symbol~}}
44
{{#if symbol.isBitfield}} : {{symbol.bitfieldWidth}}{{/if~}}
5+
{{#if symbol.default}} = {{symbol.default}}{{/if~}}
56
{{>declarator-after symbol.type}}

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,5 @@
77
{{#if symbol.isThreadLocal}}thread_local
88
{{/if~}}
99
{{>declarator-before symbol.type}} {{>declarator-id symbol link=symbol.template.primary~}}
10-
{{>declarator-after symbol.type}}
10+
{{>declarator-after symbol.type~}}
11+
{{#if symbol.initializer}} = {{symbol.initializer}}{{/if}}

src/lib/AST/ASTVisitor.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1682,6 +1682,9 @@ class ASTVisitor
16821682
if(D->isConstexpr())
16831683
I.specs.constexprKind = ConstexprKind::Constexpr;
16841684

1685+
if(const Expr* E = D->getInit())
1686+
buildExprInfo(I.Initializer, E);
1687+
16851688
if(! created)
16861689
return;
16871690

@@ -1714,6 +1717,9 @@ class ASTVisitor
17141717

17151718
I.IsMutable = D->isMutable();
17161719

1720+
if(const Expr* E = D->getInClassInitializer())
1721+
buildExprInfo(I.Default, E);
1722+
17171723
if(D->isBitField())
17181724
{
17191725
I.IsBitfield = true;

src/lib/AST/AnyBlock.hpp

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -778,6 +778,22 @@ class ExprBlock
778778
return AnyBlock::parseRecord(R, ID, Blob);
779779
}
780780
}
781+
782+
Error
783+
readSubBlock(unsigned ID) override
784+
{
785+
switch(ID)
786+
{
787+
// if this has a BI_EXPR_BLOCK_ID child, it means
788+
// that this is the child of some block which is
789+
// a proxy for an EXPR_BLOCK. in such cases, just
790+
// forward the result to the caller
791+
case BI_EXPR_BLOCK_ID:
792+
return br_.readBlock(*this, ID);
793+
default:
794+
return AnyBlock::readSubBlock(ID);
795+
};
796+
}
781797
};
782798

783799
//------------------------------------------------
@@ -1694,6 +1710,11 @@ class VarBlock
16941710
{
16951711
switch(ID)
16961712
{
1713+
case BI_EXPR_BLOCK_ID:
1714+
{
1715+
ExprBlock B(I->Initializer, br_);
1716+
return br_.readBlock(B, ID);
1717+
}
16971718
case BI_TYPEINFO_BLOCK_ID:
16981719
{
16991720
TypeInfoBlock B(I->Type, br_);
@@ -1727,8 +1748,6 @@ class FieldBlock
17271748
{
17281749
switch(ID)
17291750
{
1730-
case FIELD_DEFAULT:
1731-
return decodeRecord(R, I->Default, Blob);
17321751
case FIELD_ATTRIBUTES:
17331752
return decodeRecord(R, {&I->specs.raw}, Blob);
17341753
case FIELD_IS_MUTABLE:
@@ -1752,6 +1771,11 @@ class FieldBlock
17521771
return br_.readBlock(B, ID);
17531772
}
17541773
case BI_EXPR_BLOCK_ID:
1774+
{
1775+
ExprBlock B(I->Default, br_);
1776+
return br_.readBlock(B, ID);
1777+
}
1778+
case BI_BITFIELD_WIDTH_BLOCK_ID:
17551779
{
17561780
ExprBlock B(I->BitfieldWidth, br_);
17571781
return br_.readBlock(B, ID);

src/lib/AST/BitcodeIDs.hpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ enum BlockID
5959
BI_BASE_BLOCK_ID,
6060
BI_ENUM_BLOCK_ID,
6161
BI_EXPR_BLOCK_ID,
62+
BI_BITFIELD_WIDTH_BLOCK_ID,
6263
BI_FIELD_BLOCK_ID,
6364
BI_FUNCTION_BLOCK_ID,
6465
BI_FUNCTION_PARAM_BLOCK_ID,

src/lib/AST/BitcodeWriter.cpp

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -236,6 +236,7 @@ BlockIdNameMap = []()
236236
{BI_NAMESPACE_BLOCK_ID, "NamespaceBlock"},
237237
{BI_ENUM_BLOCK_ID, "EnumBlock"},
238238
{BI_EXPR_BLOCK_ID, "ExprBlock"},
239+
{BI_BITFIELD_WIDTH_BLOCK_ID, "BitfieldWidthBlock"},
239240
{BI_TYPEDEF_BLOCK_ID, "TypedefBlock"},
240241
{BI_TYPEINFO_BLOCK_ID, "TypeInfoBlock"},
241242
{BI_TYPEINFO_PARENT_BLOCK_ID, "TypeInfoParentBlock"},
@@ -374,6 +375,8 @@ RecordsByBlock{
374375
// ExprInfo and ConstantExprInfo
375376
{BI_EXPR_BLOCK_ID,
376377
{EXPR_WRITTEN, EXPR_VALUE}},
378+
{BI_BITFIELD_WIDTH_BLOCK_ID,
379+
{}},
377380
// FieldInfo
378381
{BI_FIELD_BLOCK_ID,
379382
{FIELD_DEFAULT, FIELD_ATTRIBUTES,
@@ -854,11 +857,11 @@ emitBlock(
854857
emitInfoPart(F);
855858
emitSourceInfo(F);
856859
emitBlock(F.Type);
857-
emitRecord(F.Default, FIELD_DEFAULT);
860+
emitBlock(F.Default);
858861
emitRecord({F.specs.raw}, FIELD_ATTRIBUTES);
859862
emitRecord(F.IsMutable, FIELD_IS_MUTABLE);
860863
emitRecord(F.IsBitfield, FIELD_IS_BITFIELD);
861-
emitBlock(F.BitfieldWidth);
864+
emitBlock(F.BitfieldWidth, BI_BITFIELD_WIDTH_BLOCK_ID);
862865
}
863866

864867
void
@@ -968,6 +971,18 @@ emitBlock(
968971
}
969972
}
970973

974+
template<typename ExprInfoTy>
975+
requires std::derived_from<ExprInfoTy, ExprInfo>
976+
void
977+
BitcodeWriter::
978+
emitBlock(
979+
ExprInfoTy const& E,
980+
BlockID ID)
981+
{
982+
StreamSubBlockGuard Block(Stream, ID);
983+
emitBlock(E);
984+
}
985+
971986
void
972987
BitcodeWriter::
973988
emitBlock(
@@ -1226,6 +1241,7 @@ emitBlock(
12261241
if(I.Template)
12271242
emitBlock(*I.Template);
12281243
emitBlock(I.Type);
1244+
emitBlock(I.Initializer);
12291245
emitRecord({I.specs.raw}, VARIABLE_BITS);
12301246
}
12311247

src/lib/AST/BitcodeWriter.hpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,9 @@ class BitcodeWriter
138138
template<typename ExprInfoTy>
139139
requires std::derived_from<ExprInfoTy, ExprInfo>
140140
void emitBlock(ExprInfoTy const& E);
141+
template<typename ExprInfoTy>
142+
requires std::derived_from<ExprInfoTy, ExprInfo>
143+
void emitBlock(ExprInfoTy const& E, BlockID ID);
141144

142145
//--------------------------------------------
143146

src/lib/Gen/xml/XMLWriter.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -468,7 +468,7 @@ writeField(
468468
{ I.Access },
469469
{ I.id },
470470
{ "width", bit_width, I.IsBitfield },
471-
{ "default", I.Default, ! I.Default.empty() }
471+
{ "default", I.Default.Written, ! I.Default.Written.empty() }
472472
});
473473

474474
writeSourceInfo(I);

0 commit comments

Comments
 (0)