Skip to content

Commit

Permalink
feat: extract and render initializers for variables and fields
Browse files Browse the repository at this point in the history
  • Loading branch information
sdkrystian committed Dec 18, 2023
1 parent bf60a45 commit 42cc6ec
Show file tree
Hide file tree
Showing 12 changed files with 68 additions and 10 deletions.
2 changes: 1 addition & 1 deletion include/mrdocs/Metadata/Field.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ struct FieldInfo

/** The default member initializer, if any.
*/
std::string Default;
ExprInfo Default;

// attributes (maybe_unused, no_unique_address, deprecated)
FieldFlags specs;
Expand Down
3 changes: 3 additions & 0 deletions include/mrdocs/Metadata/Variable.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

#include <mrdocs/Platform.hpp>
#include <mrdocs/ADT/BitField.hpp>
#include <mrdocs/Metadata/Expression.hpp>
#include <mrdocs/Metadata/Source.hpp>
#include <mrdocs/Metadata/Template.hpp>
#include <mrdocs/Metadata/Type.hpp>
Expand Down Expand Up @@ -48,6 +49,8 @@ struct VariableInfo

VariableFlags0 specs{.raw={0}};

ExprInfo Initializer;

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

explicit VariableInfo(SymbolID ID) noexcept
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@
{{/if~}}
{{>declarator-before symbol.type}} {{>declarator-id symbol~}}
{{#if symbol.isBitfield}} : {{symbol.bitfieldWidth}}{{/if~}}
{{#if symbol.default}} = {{symbol.default}}{{/if~}}
{{>declarator-after symbol.type}}
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,5 @@
{{#if symbol.isThreadLocal}}thread_local
{{/if~}}
{{>declarator-before symbol.type}} {{>declarator-id symbol link=symbol.template.primary~}}
{{>declarator-after symbol.type}}
{{>declarator-after symbol.type~}}
{{#if symbol.initializer}} = {{symbol.initializer}}{{/if}}
6 changes: 6 additions & 0 deletions src/lib/AST/ASTVisitor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1682,6 +1682,9 @@ class ASTVisitor
if(D->isConstexpr())
I.specs.constexprKind = ConstexprKind::Constexpr;

if(const Expr* E = D->getInit())
buildExprInfo(I.Initializer, E);

if(! created)
return;

Expand Down Expand Up @@ -1714,6 +1717,9 @@ class ASTVisitor

I.IsMutable = D->isMutable();

if(const Expr* E = D->getInClassInitializer())
buildExprInfo(I.Default, E);

if(D->isBitField())
{
I.IsBitfield = true;
Expand Down
28 changes: 26 additions & 2 deletions src/lib/AST/AnyBlock.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -778,6 +778,22 @@ class ExprBlock
return AnyBlock::parseRecord(R, ID, Blob);
}
}

Error
readSubBlock(unsigned ID) override
{
switch(ID)
{
// if this has a BI_EXPR_BLOCK_ID child, it means
// that this is the child of some block which is
// a proxy for an EXPR_BLOCK. in such cases, just
// forward the result to the caller
case BI_EXPR_BLOCK_ID:
return br_.readBlock(*this, ID);
default:
return AnyBlock::readSubBlock(ID);
};
}
};

//------------------------------------------------
Expand Down Expand Up @@ -1694,6 +1710,11 @@ class VarBlock
{
switch(ID)
{
case BI_EXPR_BLOCK_ID:
{
ExprBlock B(I->Initializer, br_);
return br_.readBlock(B, ID);
}
case BI_TYPEINFO_BLOCK_ID:
{
TypeInfoBlock B(I->Type, br_);
Expand Down Expand Up @@ -1727,8 +1748,6 @@ class FieldBlock
{
switch(ID)
{
case FIELD_DEFAULT:
return decodeRecord(R, I->Default, Blob);
case FIELD_ATTRIBUTES:
return decodeRecord(R, {&I->specs.raw}, Blob);
case FIELD_IS_MUTABLE:
Expand All @@ -1752,6 +1771,11 @@ class FieldBlock
return br_.readBlock(B, ID);
}
case BI_EXPR_BLOCK_ID:
{
ExprBlock B(I->Default, br_);
return br_.readBlock(B, ID);
}
case BI_BITFIELD_WIDTH_BLOCK_ID:
{
ExprBlock B(I->BitfieldWidth, br_);
return br_.readBlock(B, ID);
Expand Down
1 change: 1 addition & 0 deletions src/lib/AST/BitcodeIDs.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ enum BlockID
BI_BASE_BLOCK_ID,
BI_ENUM_BLOCK_ID,
BI_EXPR_BLOCK_ID,
BI_BITFIELD_WIDTH_BLOCK_ID,
BI_FIELD_BLOCK_ID,
BI_FUNCTION_BLOCK_ID,
BI_FUNCTION_PARAM_BLOCK_ID,
Expand Down
20 changes: 18 additions & 2 deletions src/lib/AST/BitcodeWriter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,7 @@ BlockIdNameMap = []()
{BI_NAMESPACE_BLOCK_ID, "NamespaceBlock"},
{BI_ENUM_BLOCK_ID, "EnumBlock"},
{BI_EXPR_BLOCK_ID, "ExprBlock"},
{BI_BITFIELD_WIDTH_BLOCK_ID, "BitfieldWidthBlock"},
{BI_TYPEDEF_BLOCK_ID, "TypedefBlock"},
{BI_TYPEINFO_BLOCK_ID, "TypeInfoBlock"},
{BI_TYPEINFO_PARENT_BLOCK_ID, "TypeInfoParentBlock"},
Expand Down Expand Up @@ -374,6 +375,8 @@ RecordsByBlock{
// ExprInfo and ConstantExprInfo
{BI_EXPR_BLOCK_ID,
{EXPR_WRITTEN, EXPR_VALUE}},
{BI_BITFIELD_WIDTH_BLOCK_ID,
{}},
// FieldInfo
{BI_FIELD_BLOCK_ID,
{FIELD_DEFAULT, FIELD_ATTRIBUTES,
Expand Down Expand Up @@ -854,11 +857,11 @@ emitBlock(
emitInfoPart(F);
emitSourceInfo(F);
emitBlock(F.Type);
emitRecord(F.Default, FIELD_DEFAULT);
emitBlock(F.Default);
emitRecord({F.specs.raw}, FIELD_ATTRIBUTES);
emitRecord(F.IsMutable, FIELD_IS_MUTABLE);
emitRecord(F.IsBitfield, FIELD_IS_BITFIELD);
emitBlock(F.BitfieldWidth);
emitBlock(F.BitfieldWidth, BI_BITFIELD_WIDTH_BLOCK_ID);
}

void
Expand Down Expand Up @@ -968,6 +971,18 @@ emitBlock(
}
}

template<typename ExprInfoTy>
requires std::derived_from<ExprInfoTy, ExprInfo>
void
BitcodeWriter::
emitBlock(
ExprInfoTy const& E,
BlockID ID)
{
StreamSubBlockGuard Block(Stream, ID);
emitBlock(E);
}

void
BitcodeWriter::
emitBlock(
Expand Down Expand Up @@ -1226,6 +1241,7 @@ emitBlock(
if(I.Template)
emitBlock(*I.Template);
emitBlock(I.Type);
emitBlock(I.Initializer);
emitRecord({I.specs.raw}, VARIABLE_BITS);
}

Expand Down
3 changes: 3 additions & 0 deletions src/lib/AST/BitcodeWriter.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,9 @@ class BitcodeWriter
template<typename ExprInfoTy>
requires std::derived_from<ExprInfoTy, ExprInfo>
void emitBlock(ExprInfoTy const& E);
template<typename ExprInfoTy>
requires std::derived_from<ExprInfoTy, ExprInfo>
void emitBlock(ExprInfoTy const& E, BlockID ID);

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

Expand Down
2 changes: 1 addition & 1 deletion src/lib/Gen/xml/XMLWriter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -468,7 +468,7 @@ writeField(
{ I.Access },
{ I.id },
{ "width", bit_width, I.IsBitfield },
{ "default", I.Default, ! I.Default.empty() }
{ "default", I.Default.Written, ! I.Default.Written.empty() }
});

writeSourceInfo(I);
Expand Down
5 changes: 3 additions & 2 deletions src/lib/Metadata/DomMetadata.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -825,14 +825,15 @@ DomInfo<T>::construct() const
{ "constexprKind", toString(I_.specs.constexprKind.get()) },
{ "storageClass", toString(I_.specs.storageClass.get()) },
{ "isConstinit", I_.specs.isConstinit.get() },
{ "isThreadLocal", I_.specs.isThreadLocal.get() }
{ "isThreadLocal", I_.specs.isThreadLocal.get() },
{ "initializer", dom::stringOrNull(I_.Initializer.Written) },
});
}
if constexpr(T::isField())
{
entries.insert(entries.end(), {
{ "type", domCreate(I_.Type, domCorpus_) },
{ "default", dom::stringOrNull(I_.Default) },
{ "default", dom::stringOrNull(I_.Default.Written) },
{ "isMaybeUnused", I_.specs.isMaybeUnused.get() },
{ "isDeprecated", I_.specs.isDeprecated.get() },
{ "isMutable", I_.IsMutable },
Expand Down
4 changes: 3 additions & 1 deletion src/lib/Metadata/Reduce.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -254,7 +254,7 @@ void merge(FieldInfo& I, FieldInfo&& Other)
mergeInfo(I, std::move(Other));
I.specs.raw.value |= Other.specs.raw.value;
I.IsMutable |= Other.IsMutable;
if(I.Default.empty())
if(I.Default.Written.empty())
I.Default = std::move(Other.Default);

I.IsBitfield |= Other.IsBitfield;
Expand All @@ -269,6 +269,8 @@ void merge(VariableInfo& I, VariableInfo&& Other)
I.Type = std::move(Other.Type);
if(! I.Template)
I.Template = std::move(Other.Template);
if(I.Initializer.Written.empty())
I.Initializer = std::move(Other.Initializer);
mergeSourceInfo(I, std::move(Other));
mergeInfo(I, std::move(Other));
I.specs.raw.value |= Other.specs.raw.value;
Expand Down

0 comments on commit 42cc6ec

Please sign in to comment.