Skip to content

inherit base class overload sets #885

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Mar 18, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions include/mrdocs/Metadata/Info/Function.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -267,6 +267,12 @@ tag_invoke(
v = dom::LazyObject(I, domCorpus);
}

/** Determine if one function would override the other
*/
MRDOCS_DECL
bool
overrides(FunctionInfo const& base, FunctionInfo const& derived);

} // clang::mrdocs

#endif
1 change: 1 addition & 0 deletions include/mrdocs/Support/Algorithm.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,7 @@ contains_n_any(Range const& range, std::initializer_list<El> const& els, std::si
}

/** Find the last element in a range that matches an element in the specified range.

@param range The range to search.
@param els The elements to search for.
@return An iterator to the last element found, or std::ranges::end(range) if not found.
Expand Down
23 changes: 11 additions & 12 deletions src/lib/Gen/hbs/SinglePageVisitor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,18 +25,17 @@ operator()(T const& I)
{
ex_.async([this, &I, symbolIdx = numSymbols_++](Builder& builder)
{

// Output to an independent string first (async), then write to
// the shared stream (sync)
std::stringstream ss;
if(auto r = builder(ss, I))
{
writePage(ss.str(), symbolIdx);
}
else
{
r.error().Throw();
}
// Output to an independent string first (async), then write to
// the shared stream (sync)
std::stringstream ss;
if(auto r = builder(ss, I))
{
writePage(ss.str(), symbolIdx);
}
else
{
r.error().Throw();
}
});
}
Corpus::TraverseOptions opts = {.skipInherited = std::same_as<T, RecordInfo>};
Expand Down
4 changes: 1 addition & 3 deletions src/lib/Metadata/Finalizers/BaseMembersFinalizer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -129,9 +129,7 @@ inheritBaseMembers(
// are the same
auto const& otherFunc = static_cast<FunctionInfo const&>(otherInfo);
auto const& func = static_cast<FunctionInfo const&>(info);
return
std::tie(func.Name, func.Params, func.Template) ==
std::tie(otherFunc.Name, otherFunc.Params, func.Template);
return overrides(func, otherFunc);
}
// For other kinds of members, it's a shadow if the names
// are the same
Expand Down
10 changes: 4 additions & 6 deletions src/lib/Metadata/Finalizers/NamespacesFinalizer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,11 @@

namespace clang::mrdocs {

/** Finalizes a set of Info.
/** Finalizes the namespaces in corpus.

This removes any references to SymbolIDs
which do not exist.

References which should always be valid
are not checked.
Namespaces might be removed or have
their extraction mode updated depending
on its members.
*/
class NamespacesFinalizer
{
Expand Down
162 changes: 132 additions & 30 deletions src/lib/Metadata/Finalizers/OverloadsFinalizer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,54 +28,139 @@

void
OverloadsFinalizer::
foldNamespaceMembers(std::vector<SymbolID> const& ids)
foldNamespaceMembers(std::vector<SymbolID> const& namespaceIds)
{
for (SymbolID const& id: ids)
for (SymbolID const& namespaceId: namespaceIds)
{
Info* infoPtr = corpus_.find(id);
MRDOCS_CHECK_OR_CONTINUE(infoPtr);
auto* ns = dynamic_cast<NamespaceInfo*>(infoPtr);
Info* namespaceInfoPtr = corpus_.find(namespaceId);
MRDOCS_CHECK_OR_CONTINUE(namespaceInfoPtr);
auto* ns = dynamic_cast<NamespaceInfo*>(namespaceInfoPtr);
MRDOCS_CHECK_OR_CONTINUE(ns);
operator()(*ns);
}
}

namespace {
SymbolID
findBaseClassPermutation(
SymbolID const& contextId,
CorpusImpl& corpus,
ArrayRef<SymbolID> sameNameFunctionIds)
{
Info* info = corpus.find(contextId);
MRDOCS_CHECK_OR(info, SymbolID::invalid);
if (auto* record = dynamic_cast<RecordInfo*>(info))
{
bool overloadsFromBase = false;

Check warning on line 54 in src/lib/Metadata/Finalizers/OverloadsFinalizer.cpp

View workflow job for this annotation

GitHub Actions / Apple-Clang

Build Warning - clang++ - [-Wunused-variable]

clang++ - unused variable 'overloadsFromBase' ([-Wunused-variable])

Check warning on line 54 in src/lib/Metadata/Finalizers/OverloadsFinalizer.cpp

View workflow job for this annotation

GitHub Actions / Clang 18: C++20

Build Warning - clang++-18 - [-Wunused-variable]

clang++-18 - unused variable 'overloadsFromBase' ([-Wunused-variable])

Check warning on line 54 in src/lib/Metadata/Finalizers/OverloadsFinalizer.cpp

View workflow job for this annotation

GitHub Actions / GCC 14: C++20

Build Warning - g++-14 - [-Wunused-variable]

g++-14 - unused variable 'overloadsFromBase' ([-Wunused-variable])

Check warning on line 54 in src/lib/Metadata/Finalizers/OverloadsFinalizer.cpp

View workflow job for this annotation

GitHub Actions / GCC 14: C++20 (Coverage)

Build Warning - g++-14 - [-Wunused-variable]

g++-14 - unused variable 'overloadsFromBase' ([-Wunused-variable])
for (auto const& base: record->Bases)
{
auto const baseInfo = corpus.find(base.Type->namedSymbol());
MRDOCS_CHECK_OR_CONTINUE(baseInfo);
auto const baseRecord = dynamic_cast<RecordInfo*>(baseInfo);
MRDOCS_CHECK_OR_CONTINUE(baseRecord);
RecordTranche* tranchesPtrs[] = {
&baseRecord->Interface.Public,
&baseRecord->Interface.Protected,
&baseRecord->Interface.Private,
};
for (RecordTranche* tranchePtr: tranchesPtrs)
{
std::vector<SymbolID>* trancheFunctionPtrs[] = {
&tranchePtr->Functions,
&tranchePtr->StaticFunctions
};
for (std::vector<SymbolID>* trancheFunctionsPtr:
trancheFunctionPtrs)
{
for (SymbolID const& baseID: *trancheFunctionsPtr)
{
Info* baseFuncMember = corpus.find(baseID);
MRDOCS_CHECK_OR_CONTINUE(baseFuncMember);
MRDOCS_CHECK_OR_CONTINUE(baseFuncMember->isOverloads());
auto* overloads = dynamic_cast<OverloadsInfo*>(baseFuncMember);
MRDOCS_CHECK_OR_CONTINUE(overloads);
// Does this overload set have the same functions
MRDOCS_CHECK_OR_CONTINUE(
std::ranges::is_permutation(
overloads->Members,
sameNameFunctionIds));
return overloads->id;
}
}
}
}
}
return SymbolID::invalid;
}
}

void
OverloadsFinalizer::
foldOverloads(SymbolID const& parent, std::vector<SymbolID>& ids)
foldOverloads(SymbolID const& contextId, std::vector<SymbolID>& functionIds)
{
for (auto it = ids.begin(); it != ids.end(); ++it)
for (auto functionIdIt = functionIds.begin();
functionIdIt != functionIds.end();
++functionIdIt)
{
// Get the FunctionInfo for the current id
auto infoPtr = corpus_.find(*it);
MRDOCS_CHECK_OR_CONTINUE(infoPtr);
auto* function = dynamic_cast<FunctionInfo*>(infoPtr);
auto recordInfoPtr = corpus_.find(*functionIdIt);
MRDOCS_CHECK_OR_CONTINUE(recordInfoPtr);
auto* function = dynamic_cast<FunctionInfo*>(recordInfoPtr);
MRDOCS_CHECK_OR_CONTINUE(function);

// Check if the FunctionInfo is unique
auto sameNameIt =
std::find_if(
it + 1,
ids.end(),
[&](SymbolID const& otherID)
{
auto const otherInfoPtr = corpus_.find(otherID);
MRDOCS_CHECK_OR(otherInfoPtr, false);
Info& otherInfo = *otherInfoPtr;
return function->Name == otherInfo.Name;
});
if (sameNameIt == ids.end())
std::ranges::subrange otherFunctionIds(
std::next(functionIdIt),
functionIds.end());
auto isSameNameFunction = [&](SymbolID const& otherID) {
auto const otherFunctionPtr = corpus_.find(otherID);
MRDOCS_CHECK_OR(otherFunctionPtr, false);
Info const& otherInfo = *otherFunctionPtr;
return function->Name == otherInfo.Name;
};
auto sameNameIt = std::ranges::
find_if(otherFunctionIds, isSameNameFunction);
bool const isUniqueFunction = sameNameIt == otherFunctionIds.end();
MRDOCS_CHECK_OR_CONTINUE(!isUniqueFunction);

// Create a list of FunctionInfo overloads
auto sameNameFunctionIdsView =
std::ranges::subrange(functionIdIt, functionIds.end()) |
std::views::filter(isSameNameFunction);
SmallVector<SymbolID, 16> sameNameFunctionIds(
sameNameFunctionIdsView.begin(),
sameNameFunctionIdsView.end());

// Check if any of the base classes has an overload set
// with the exact same function ids. If that's the case,
// the function will create a reference.
SymbolID equivalentOverloadsID = findBaseClassPermutation(
contextId, corpus_, sameNameFunctionIds);
if (equivalentOverloadsID)
{
MRDOCS_ASSERT(corpus_.find(equivalentOverloadsID));
// This base overload set becomes the
// representation in the record
*functionIdIt = equivalentOverloadsID;
auto const offset = functionIdIt - functionIds.begin();
// Erase the other function ids with
// the same name
for (SymbolID sameNameId: sameNameFunctionIds)
{
std::erase(functionIds, sameNameId);
}
functionIdIt = functionIds.begin() + offset;
continue;
}

// FunctionInfo is not unique, so merge it with the other FunctionInfos
// into an OverloadsInfo
OverloadsInfo O(parent, function->Name);
// FunctionInfo is not unique and there's no equivalent
// overload set in base classes, so we merge it with the
// other FunctionInfos into a new OverloadsInfo
OverloadsInfo O(contextId, function->Name);
addMember(O, *function);
*it = O.id;
auto const itOffset = it - ids.begin();
for (auto otherIt = it + 1; otherIt != ids.end(); ++otherIt)
*functionIdIt = O.id;
auto const itOffset = functionIdIt - functionIds.begin();
for (auto otherIt = functionIdIt + 1; otherIt != functionIds.end(); ++otherIt)
{
Info* otherInfoPtr = corpus_.find(*otherIt);
MRDOCS_CHECK_OR_CONTINUE(otherInfoPtr);
Expand All @@ -84,10 +169,10 @@
if (function->Name == otherFunction->Name)
{
addMember(O, *otherFunction);
otherIt = std::prev(ids.erase(otherIt));
otherIt = std::prev(functionIds.erase(otherIt));
}
}
it = ids.begin() + itOffset;
functionIdIt = functionIds.begin() + itOffset;
corpus_.info_.emplace(std::make_unique<OverloadsInfo>(std::move(O)));
}
}
Expand All @@ -105,6 +190,22 @@
OverloadsFinalizer::
operator()(RecordInfo& I)
{
MRDOCS_CHECK_OR(!finalized_.contains(I.id));
for (auto& b: I.Bases)
{
auto& BT = b.Type;
MRDOCS_CHECK_OR(BT);
MRDOCS_CHECK_OR(BT->isNamed());
auto& NT = dynamic_cast<NamedTypeInfo&>(*BT);
MRDOCS_CHECK_OR(NT.Name);
auto& NI = dynamic_cast<NameInfo&>(*NT.Name);
MRDOCS_CHECK_OR(NI.id);
Info* baseInfo = corpus_.find(NI.id);
MRDOCS_CHECK_OR(baseInfo);
auto* baseRecord = dynamic_cast<RecordInfo*>(baseInfo);
MRDOCS_CHECK_OR(baseRecord);
operator()(*baseRecord);
}
foldOverloads(I.id, I.Interface.Public.Functions);
foldOverloads(I.id, I.Interface.Protected.Functions);
foldOverloads(I.id, I.Interface.Private.Functions);
Expand All @@ -114,6 +215,7 @@
foldRecordMembers(I.Interface.Public.Records);
foldRecordMembers(I.Interface.Protected.Records);
foldRecordMembers(I.Interface.Private.Records);
finalized_.emplace(I.id);
}

} // clang::mrdocs
7 changes: 5 additions & 2 deletions src/lib/Metadata/Finalizers/OverloadsFinalizer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,15 +27,18 @@ namespace clang::mrdocs {
class OverloadsFinalizer
{
CorpusImpl& corpus_;
std::set<SymbolID> finalized_;

void
foldRecordMembers(std::vector<SymbolID> const& ids);

void
foldNamespaceMembers(std::vector<SymbolID> const& ids);
foldNamespaceMembers(std::vector<SymbolID> const& namespaceIds);

void
foldOverloads(SymbolID const& parent, std::vector<SymbolID>& ids);
foldOverloads(
SymbolID const& contextId,
std::vector<SymbolID>& functionIds);

public:
OverloadsFinalizer(CorpusImpl& corpus)
Expand Down
16 changes: 16 additions & 0 deletions src/lib/Metadata/Info/Function.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -356,6 +356,22 @@ merge(FunctionInfo& I, FunctionInfo&& Other)
}
}

MRDOCS_DECL
bool
overrides(FunctionInfo const& base, FunctionInfo const& derived)
{
auto toOverrideTuple = [](FunctionInfo const& f) {
return std::forward_as_tuple(
f.Name,
f.Params,
f.Template,
f.IsVariadic,
f.IsConst,
f.RefQualifier
);
};
return toOverrideTuple(base) == toOverrideTuple(derived);
}

} // clang::mrdocs

Loading
Loading