Skip to content

Commit a864552

Browse files
committed
feat: add ConstexprKind and isConstinit to VariableInfo
closes #325, closes #205
1 parent a121fb4 commit a864552

12 files changed

+77
-19
lines changed

include/mrdox/Metadata/Specifiers.hpp

+1-3
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ enum class StorageClassKind
5555
Register
5656
};
5757

58-
/** `constexpr`/`consteval`/`constinit` specifier kinds
58+
/** `constexpr`/`consteval` specifier kinds
5959
6060
[dcl.spec.general] p2: At most one of the `constexpr`, `consteval`,
6161
and `constinit` keywords shall appear in a decl-specifier-seq
@@ -66,8 +66,6 @@ enum class ConstexprKind
6666
Constexpr,
6767
// only valid for functions
6868
Consteval,
69-
// only valid for variables
70-
Constinit
7169
};
7270

7371
/** Explicit specifier kinds

include/mrdox/Metadata/Variable.hpp

+2
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@ union VariableFlags0
2727
BitFieldFullValue raw;
2828

2929
BitField<0, 3, StorageClassKind> storageClass;
30+
BitField<3, 2, ConstexprKind> constexprKind;
31+
BitFlag<5> isConstinit;
3032
};
3133

3234
/** A variable.

source/-XML/CXXTags.hpp

+2
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,8 @@ inline void write(VariableFlags0 const& bits, XMLTags& tags)
183183
{
184184
BitFieldWriter<VariableFlags0> fw(bits, tags);
185185
fw.write(&VariableFlags0::storageClass, "storage-class");
186+
fw.write(&VariableFlags0::constexprKind, "constexpr-kind");
187+
fw.write(&VariableFlags0::isConstinit, "is-constinit");
186188
}
187189

188190
inline

source/AST/ASTVisitor.cpp

+12-10
Original file line numberDiff line numberDiff line change
@@ -1320,12 +1320,7 @@ buildField(
13201320
int line = getLine(D);
13211321
I.DefLoc.emplace(line, File_.str(), IsFileInRootDir_);
13221322

1323-
#if 0
1324-
I.Type = buildTypeInfoForType(
1325-
D->getTypeSourceInfo()->getType());
1326-
#else
13271323
I.Type = buildTypeInfoForType(D->getType());
1328-
#endif
13291324

13301325
I.IsMutable = D->isMutable();
13311326

@@ -1362,16 +1357,23 @@ buildVar(
13621357
I.DefLoc.emplace(line, File_.str(), IsFileInRootDir_);
13631358
else
13641359
I.Loc.emplace_back(line, File_.str(), IsFileInRootDir_);
1365-
#if 0
1366-
I.Type = buildTypeInfoForType(
1367-
D->getTypeSourceInfo()->getType());
1368-
#else
1360+
13691361
I.Type = buildTypeInfoForType(D->getType());
1370-
#endif
1362+
13711363
I.specs.storageClass =
13721364
convertToStorageClassKind(
13731365
D->getStorageClass());
13741366

1367+
// KRYSTIAN NOTE: VarDecl does not provide getConstexprKind,
1368+
// nor does it use getConstexprKind to store whether
1369+
// a variable is constexpr/constinit. Although
1370+
// only one is permitted in a variable declaration,
1371+
// it is possible to declare a static data member
1372+
// as both constexpr and constinit in separate declarations..
1373+
I.specs.isConstinit = D->hasAttr<ConstInitAttr>();
1374+
if(D->isConstexpr())
1375+
I.specs.constexprKind = ConstexprKind::Constexpr;
1376+
13751377
bool member_spec = getParentNamespaces(I.Namespace, D);
13761378

13771379
insertBitcode(ex_, writeBitcode(I));

source/AST/ASTVisitorHelpers.hpp

+5-1
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,11 @@ convertToConstexprKind(
6969
case OldKind::Unspecified: return NewKind::None;
7070
case OldKind::Constexpr: return NewKind::Constexpr;
7171
case OldKind::Consteval: return NewKind::Consteval;
72-
case OldKind::Constinit: return NewKind::Constinit;
72+
// KRYSTIAN NOTE: ConstexprSpecKind::Constinit exists,
73+
// but I don't think it's ever used because a variable
74+
// can be declared both constexpr and constinit
75+
// (but not both in the same declaration)
76+
case OldKind::Constinit:
7377
default:
7478
MRDOX_UNREACHABLE();
7579
}

source/Metadata/Specifiers.cpp

-1
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,6 @@ toString(ConstexprKind kind)
5050
case ConstexprKind::None: return "";
5151
case ConstexprKind::Constexpr: return "constexpr";
5252
case ConstexprKind::Consteval: return "consteval";
53-
case ConstexprKind::Constinit: return "constinit";
5453
default:
5554
MRDOX_UNREACHABLE();
5655
}

test-files/old-tests/ns-variables.cpp

+8-1
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,16 @@
11
// variables and constants
22

33
constexpr int i = 0;
4+
constinit int j = 0;
5+
6+
extern const int k;
7+
extern int l;
8+
9+
constexpr int k = 1;
10+
constinit int l = 1;
411

512
double pi = 3.14;
613

714
struct T {};
815

9-
extern T t;
16+
extern T t;

test-files/old-tests/ns-variables.xml

+23-3
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,37 @@
44
<namespace name="">
55
<variable name="i" id="X2Wd7fyQ8BJNJ0tn4nnA77ckJM8=">
66
<file path="ns-variables.cpp" line="3" class="def"/>
7+
<attr id="constexpr-kind" name="constexpr" value="1"/>
78
<type name="int" cv-qualifiers="const"/>
89
</variable>
10+
<variable name="j" id="pKvK/kHXdM/1/pu86JcSrLqVypE=">
11+
<file path="ns-variables.cpp" line="4" class="def"/>
12+
<attr id="is-constinit"/>
13+
<type name="int"/>
14+
</variable>
15+
<variable name="k" id="Fr1KYTKmyLHGW9rPSpCZK82iRqQ=">
16+
<file path="ns-variables.cpp" line="9" class="def"/>
17+
<file path="ns-variables.cpp" line="6"/>
18+
<attr id="storage-class" name="extern" value="1"/>
19+
<attr id="constexpr-kind" name="constexpr" value="1"/>
20+
<type name="int" cv-qualifiers="const"/>
21+
</variable>
22+
<variable name="l" id="h5r4fNloVAMKqMyVJI+iWTxRjhM=">
23+
<file path="ns-variables.cpp" line="10" class="def"/>
24+
<file path="ns-variables.cpp" line="7"/>
25+
<attr id="storage-class" name="extern" value="1"/>
26+
<attr id="is-constinit"/>
27+
<type name="int"/>
28+
</variable>
929
<variable name="pi" id="EgqLVTs3FIJjFIODa5OOP2hmEI0=">
10-
<file path="ns-variables.cpp" line="5" class="def"/>
30+
<file path="ns-variables.cpp" line="12" class="def"/>
1131
<type name="double"/>
1232
</variable>
1333
<struct name="T" id="CgGNdHpW5mG/i5741WPYQDw28OQ=">
14-
<file path="ns-variables.cpp" line="7" class="def"/>
34+
<file path="ns-variables.cpp" line="14" class="def"/>
1535
</struct>
1636
<variable name="t" id="eFAdMVIx9F2Zyx0LmK/nCDOhmhA=">
17-
<file path="ns-variables.cpp" line="9"/>
37+
<file path="ns-variables.cpp" line="16"/>
1838
<attr id="storage-class" name="extern" value="1"/>
1939
<type class="tag" id="CgGNdHpW5mG/i5741WPYQDw28OQ=" name="T"/>
2040
</variable>

test-files/old-tests/static-data-def-constexpr.cpp

+7
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,10 @@ struct S
33
static const S s;
44
};
55
constexpr S S::s = S{};
6+
7+
struct T
8+
{
9+
static constinit const int t = 0;
10+
};
11+
12+
constexpr int T::t;

test-files/old-tests/static-data-def-constexpr.xml

+12
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,20 @@
88
<file path="static-data-def-constexpr.cpp" line="5" class="def"/>
99
<file path="static-data-def-constexpr.cpp" line="3"/>
1010
<attr id="storage-class" name="static" value="2"/>
11+
<attr id="constexpr-kind" name="constexpr" value="1"/>
1112
<type class="tag" id="pOYGF6pLJlICuiGO0Xj0daDb/to=" name="S" cv-qualifiers="const"/>
1213
</variable>
1314
</struct>
15+
<struct name="T" id="CgGNdHpW5mG/i5741WPYQDw28OQ=">
16+
<file path="static-data-def-constexpr.cpp" line="7" class="def"/>
17+
<variable name="t" id="+nDx93NmBRbrBZ8RlBwzPk8rp8I=">
18+
<file path="static-data-def-constexpr.cpp" line="12" class="def"/>
19+
<file path="static-data-def-constexpr.cpp" line="9"/>
20+
<attr id="storage-class" name="static" value="2"/>
21+
<attr id="constexpr-kind" name="constexpr" value="1"/>
22+
<attr id="is-constinit"/>
23+
<type name="int" cv-qualifiers="const"/>
24+
</variable>
25+
</struct>
1426
</namespace>
1527
</mrdox>

test-files/old-tests/static-data-def.xml

+2
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
<file path="static-data-def.cpp" line="21" class="def"/>
2323
<file path="static-data-def.cpp" line="6"/>
2424
<attr id="storage-class" name="static" value="2"/>
25+
<attr id="constexpr-kind" name="constexpr" value="1"/>
2526
<type name="int" cv-qualifiers="const"/>
2627
</variable>
2728
<variable name="v3" id="Wx5S5oWv5o4nV7I+zFEKTvWl40g=">
@@ -49,6 +50,7 @@
4950
<variable name="v7" id="VCnmz8Cp9RtJzQQQ7yw57uzbQAY=">
5051
<file path="static-data-def.cpp" line="12" class="def"/>
5152
<attr id="storage-class" name="static" value="2"/>
53+
<attr id="constexpr-kind" name="constexpr" value="1"/>
5254
<type name="int" cv-qualifiers="const"/>
5355
</variable>
5456
</struct>

test-files/old-tests/static-data-template.xml

+3
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
<variable name="x" id="7L90lvoKPW80F2cnMs0e05abtuk=">
1313
<file path="static-data-template.cpp" line="5" class="def"/>
1414
<attr id="storage-class" name="static" value="2"/>
15+
<attr id="constexpr-kind" name="constexpr" value="1"/>
1516
<type name="T" cv-qualifiers="const"/>
1617
</variable>
1718
</template>
@@ -22,6 +23,7 @@
2223
<variable name="x" id="odTOUblgmooUozrBqW3qKzPWSO4=">
2324
<file path="static-data-template.cpp" line="7" class="def"/>
2425
<attr id="storage-class" name="static" value="2"/>
26+
<attr id="constexpr-kind" name="constexpr" value="1"/>
2527
<type name="T" cv-qualifiers="const"/>
2628
</variable>
2729
</template>
@@ -31,6 +33,7 @@
3133
<variable name="x" id="eKdgKT44WwfN7UkQXGEqVJssIPM=">
3234
<file path="static-data-template.cpp" line="10" class="def"/>
3335
<attr id="storage-class" name="static" value="2"/>
36+
<attr id="constexpr-kind" name="constexpr" value="1"/>
3437
<type name="bool" cv-qualifiers="const"/>
3538
</variable>
3639
</template>

0 commit comments

Comments
 (0)