Skip to content

Commit 7913824

Browse files
committed
SymbolID rewrite
1 parent ebdcf07 commit 7913824

23 files changed

+142
-116
lines changed

include/mrdox/Metadata/Record.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ struct BaseInfo
5151
bool IsVirtual;
5252

5353
BaseInfo(
54-
SymbolID const& id_ = EmptySID,
54+
SymbolID const& id_ = SymbolID::zero,
5555
std::string_view Name_ = "",
5656
Access access_ = Access::Public,
5757
bool IsVirtual_ = false)

include/mrdox/Metadata/Reference.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ struct Reference
4444
// string as a valid input for the global namespace (it will have
4545
// "GlobalNamespace" as the name, but an empty QualName).
4646
Reference(
47-
SymbolID id_ = EmptySID,
47+
SymbolID id_ = SymbolID::zero,
4848
llvm::StringRef Name = llvm::StringRef(),
4949
InfoType IT = InfoType::IT_default)
5050
: id(id_)

include/mrdox/Metadata/Symbols.hpp

Lines changed: 60 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
66
//
77
// Copyright (c) 2023 Vinnie Falco ([email protected])
8+
// Copyright (c) 2023 Krystian Stasiowski ([email protected])
89
//
910
// Official repository: https://github.com/cppalliance/mrdox
1011
//
@@ -14,7 +15,9 @@
1415

1516
#include <mrdox/Platform.hpp>
1617
#include <llvm/ADT/StringRef.h>
17-
#include <array>
18+
#include <cstdint>
19+
#include <cstring>
20+
#include <compare>
1821

1922
namespace clang {
2023
namespace mrdox {
@@ -25,22 +28,72 @@ namespace mrdox {
2528
USR. A USRs is a string that provide an
2629
unambiguous reference to a symbol.
2730
*/
28-
struct SymbolID :
29-
std::array<std::uint8_t, 20>
31+
class SymbolID
3032
{
33+
public:
34+
static const SymbolID zero;
35+
36+
using value_type = std::uint8_t;
37+
3138
constexpr SymbolID() = default;
32-
constexpr SymbolID(const std::array<std::uint8_t, 20>& arr)
33-
: array(arr)
39+
40+
template<typename Elem>
41+
SymbolID(const Elem* src)
42+
{
43+
for(auto& c : data_)
44+
c = *src++;
45+
}
46+
47+
constexpr auto data() const noexcept
48+
{
49+
return data_;
50+
}
51+
52+
constexpr std::size_t size() const noexcept
53+
{
54+
return 20;
55+
}
56+
57+
constexpr auto begin() const noexcept
3458
{
59+
return data_;
3560
}
61+
62+
constexpr auto end() const noexcept
63+
{
64+
return data_ + size();
65+
}
66+
67+
operator llvm::StringRef() const noexcept
68+
{
69+
return llvm::StringRef(reinterpret_cast<
70+
const char*>(data()), size());
71+
}
72+
73+
auto operator<=>(
74+
const SymbolID& other) const noexcept
75+
{
76+
return std::memcmp(
77+
data(),
78+
other.data(),
79+
size()) <=> 0;
80+
}
81+
82+
bool operator==(
83+
const SymbolID& other) const noexcept = default;
84+
85+
private:
86+
value_type data_[20];
3687
};
3788

3889
/** The empty Symbol ID.
3990
4091
This is used to avoid unnecessary constructions,
4192
and to identify the global namespace.
4293
*/
43-
constexpr SymbolID const EmptySID = SymbolID();
94+
// KRYSTIAN NOTE: msvc requires inline as it doesn't consider SymbolID::zero
95+
// to be an inline variable without it (it should; see [dcl.constexpr])
96+
constexpr inline SymbolID SymbolID::zero = SymbolID();
4497

4598
/** Like optional<SymbolID>
4699
*/
@@ -86,7 +139,7 @@ class OptionalSymbolID
86139

87140
constexpr bool has_value() const noexcept
88141
{
89-
return ID_ != EmptySID;
142+
return ID_ != SymbolID::zero;
90143
}
91144

92145
template<typename... Args>
@@ -97,10 +150,6 @@ class OptionalSymbolID
97150
}
98151
};
99152

100-
/** The ID of the global namespace.
101-
*/
102-
constexpr SymbolID const globalNamespaceID = EmptySID;
103-
104153
/** Info variant discriminator
105154
*/
106155
enum class InfoType

include/mrdox/Metadata/Type.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ struct TypeInfo
5858
TypeInfo(
5959
llvm::StringRef Name)
6060
: Type(
61-
EmptySID, Name, InfoType::IT_default)
61+
SymbolID::zero, Name, InfoType::IT_default)
6262
{
6363
}
6464

source/-XML/XMLTags.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ struct Attribute
101101
SymbolID id)
102102
: name("id")
103103
, value(toString(id))
104-
, pred(id != EmptySID)
104+
, pred(id != SymbolID::zero)
105105
{
106106
}
107107

source/-XML/XMLWriter.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,7 @@ build()
149149
if(options_.index || options_.safe_names)
150150
writeIndex();
151151

152-
if(! corpus_.traverse(*this, globalNamespaceID))
152+
if(! corpus_.traverse(*this, SymbolID::zero))
153153
return makeErr("visit failed");
154154

155155
if(options_.prolog)
@@ -555,7 +555,7 @@ writeTemplate(
555555
break;
556556
}
557557
const SymbolID& id = I->Primary ?
558-
*I->Primary : EmptySID;
558+
*I->Primary : SymbolID::zero;
559559

560560
tags_.open(templateTagName, {
561561
{"class", spec, !! spec},

source/-adoc/AdocPagesBuilder.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ Err
2424
AdocPagesBuilder::
2525
build()
2626
{
27-
corpus_.traverse(*this, globalNamespaceID);
27+
corpus_.traverse(*this, SymbolID::zero);
2828
wg_.wait();
2929
return {};
3030
}

source/-adoc/AdocSinglePageWriter.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ build()
3838
os_ <<
3939
"= Reference\n"
4040
":role: mrdox\n";
41-
corpus_.traverse(*this, globalNamespaceID);
41+
corpus_.traverse(*this, SymbolID::zero);
4242
endSection();
4343
return {};
4444
}
@@ -93,7 +93,7 @@ visit(
9393
! I.Children.Vars.empty())
9494
{
9595
std::string s;
96-
if(I.id == EmptySID)
96+
if(I.id == SymbolID::zero)
9797
{
9898
s = "global namespace";
9999
}

source/-adoc/AdocWriter.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,7 @@ struct AdocWriter::TypeName
134134
llvm::raw_ostream& os,
135135
TypeName const& t)
136136
{
137-
if(t.I.Type.id == globalNamespaceID)
137+
if(t.I.Type.id == SymbolID::zero)
138138
{
139139
os << t.I.Type.Name;
140140
return os;

source/-bitcode/BitcodeGenerator.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ class MultiFileBuilder : public Corpus::Visitor
4343
Err
4444
build()
4545
{
46-
corpus_.traverse(*this, globalNamespaceID);
46+
corpus_.traverse(*this, SymbolID::zero);
4747
wg_.wait();
4848
return Err();
4949
}
@@ -128,7 +128,7 @@ class SingleFileBuilder : public Corpus::Visitor
128128
Err
129129
build()
130130
{
131-
corpus_.traverse(*this, globalNamespaceID);
131+
corpus_.traverse(*this, SymbolID::zero);
132132
return Err();
133133
}
134134

0 commit comments

Comments
 (0)