Skip to content

Commit

Permalink
refactor: don't represent using-directives as Info
Browse files Browse the repository at this point in the history
  • Loading branch information
sdkrystian committed May 30, 2024
1 parent 9b1ffc5 commit b97dcfe
Show file tree
Hide file tree
Showing 15 changed files with 49 additions and 73 deletions.
6 changes: 3 additions & 3 deletions include/mrdocs/Metadata/Info.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ enum class InfoKind
Guide,
/// The symbol is a namespace alias
Alias,
/// The symbol is a using declaration and using directive
/// The symbol is a using declaration
Using,
};

Expand Down Expand Up @@ -187,7 +187,7 @@ struct MRDOCS_VISIBLE
/// Determine if this symbol is a namespace alias.
constexpr bool isAlias() const noexcept { return Kind == InfoKind::Alias; }

/// Determine if this symbol is a using declaration or using directive.
/// Determine if this symbol is a using declaration.
constexpr bool isUsing() const noexcept { return Kind == InfoKind::Using; }
};

Expand Down Expand Up @@ -245,7 +245,7 @@ struct IsInfo : Info
/// Determine if this symbol is a namespace alias.
static constexpr bool isAlias() noexcept { return K == InfoKind::Alias; }

/// Determine if this symbol is a using declaration or using directive.
/// Determine if this symbol is a using declaration.
static constexpr bool isUsing() noexcept { return K == InfoKind::Using; }

protected:
Expand Down
4 changes: 4 additions & 0 deletions include/mrdocs/Metadata/Namespace.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,10 @@ struct NamespaceInfo
{
NamespaceFlags specs;

/** Namespaces nominated by using-directives.
*/
std::vector<SymbolID> UsingDirectives;

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

explicit NamespaceInfo(SymbolID ID) noexcept
Expand Down
13 changes: 7 additions & 6 deletions include/mrdocs/Metadata/Using.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ enum class UsingClass
Normal = 0, // using
Typename, // using typename
Enum, // using enum
Namespace // using namespace (using directive)
};

static constexpr
Expand All @@ -38,24 +37,26 @@ toString(UsingClass const& value)
case UsingClass::Normal: return "normal";
case UsingClass::Typename: return "typename";
case UsingClass::Enum: return "enum";
case UsingClass::Namespace: return "namespace";
}
return "unknown";
}

/** Info for using declarations and directives.
/** Info for using declarations.
*/
struct UsingInfo
: IsInfo<InfoKind::Using>,
SourceInfo
{
/** The kind of using declaration/directive. */
/** The kind of using declaration.
*/
UsingClass Class = UsingClass::Normal;

/** The symbols being "used". */
/** The symbols being "used".
*/
std::vector<SymbolID> UsingSymbols;

/** The qualifier for a using declaration/directive. */
/** The qualifier for a using declaration.
*/
std::unique_ptr<NameInfo> Qualifier;

//--------------------------------------------
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1 @@
{{#if (eq symbol.class "namespace")~}}
using namespace {{>name-info symbol.nominatedSymbol~}}
{{else~}}
using {{#if (eq symbol.class "typename")}}typename {{/if}}{{#if (eq symbol.class "enum")}}enum {{/if}}{{#if symbol.qualifier}}{{>name-info symbol.qualifier}}::{{/if}}{{symbol.name~}}
{{/if}}
using {{#if (eq symbol.class "typename")}}typename {{/if}}{{#if (eq symbol.class "enum")}}enum {{/if}}{{#if symbol.qualifier}}{{>name-info symbol.qualifier}}::{{/if}}{{symbol.name~}}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{{!-- symbols/using.adoc.hbs --}}
= Using {{#if (eq symbol.class "namespace")}}Directive{{else}}Declaration {{symbol.name}}{{/if}}
= Using Declaration {{symbol.name}}

{{symbol.doc.brief}}

Expand All @@ -19,7 +19,6 @@
{{/if}}
{{#unless (eq symbol.class "namespace")}}
== Introduced Symbols
|===
Expand All @@ -28,4 +27,3 @@
| {{name}}
{{/each}}
|===
{{/unless}}
59 changes: 18 additions & 41 deletions src/lib/AST/ASTVisitor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -436,6 +436,8 @@ class ASTVisitor
std::pair<InfoTy&, bool>
getOrCreateInfo(const SymbolID& id)
{
assert(id != SymbolID::invalid &&
"creating symbol with invalid SymbolID?");
Info* info = getInfo(id);
bool created = ! info;
if(! info)
Expand Down Expand Up @@ -535,17 +537,6 @@ class ASTVisitor
return false;
}

// Handling UsingDirectiveDecl
if (const auto* UDD = dyn_cast<UsingDirectiveDecl>(D))
{
if (index::generateUSRForDecl(UDD->getNominatedNamespace(), usr_)) {
return true;
}
usr_.append("@UD");
usr_.append(UDD->getQualifiedNameAsString());
return false;
}

// Handling UsingDecl
if (const auto* UD = dyn_cast<UsingDecl>(D))
{
Expand Down Expand Up @@ -2194,31 +2185,6 @@ class ASTVisitor
getParentNamespaces(I, D);
}


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

void
buildUsingDirective(
UsingInfo& I,
bool created,
UsingDirectiveDecl* D)
{
bool documented = parseRawComment(I.javadoc, D);
addSourceLocation(I, D->getBeginLoc(), true, documented);

if(! created)
return;

I.Class = UsingClass::Namespace;
// KRYSTIAN FIXME: we treat using-directives as having no name,
// and we store the full name of the nominated namespace in
// Qualifier. we should probably rename the member accordingly
I.Qualifier = buildNameInfo(D->getNominatedNamespaceAsWritten());

getParentNamespaces(I, D);
}


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

void
Expand Down Expand Up @@ -2593,12 +2559,23 @@ void
ASTVisitor::
traverse(UsingDirectiveDecl* D)
{
auto exp = getAsMrDocsInfo(D);
if(! exp) { return; }
auto [I, created] = *exp;
buildUsingDirective(I, created, D);
}
if(! shouldExtract(D, getAccess(D)))
return;

Decl* PD = getParentDecl(D);
// only extract using-directives in namespace scope
if(! cast<DeclContext>(PD)->isFileContext())
return;

if(Info* PI = getInfo(extractSymbolID(PD)))
{
assert(PI->isNamespace());
NamespaceInfo* NI = static_cast<NamespaceInfo*>(PI);
getDependencyID(
D->getNominatedNamespaceAsWritten(),
NI->UsingDirectives.emplace_back());
}
}

//------------------------------------------------
// UsingDecl
Expand Down
2 changes: 2 additions & 0 deletions src/lib/AST/ASTVisitorHelpers.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,10 @@ struct MrDocsType<CXXDeductionGuideDecl> : std::type_identity<GuideInfo> {};
template <>
struct MrDocsType<NamespaceAliasDecl> : std::type_identity<AliasInfo> {};

#if 0
template <>
struct MrDocsType<UsingDirectiveDecl> : std::type_identity<UsingInfo> {};
#endif

template <>
struct MrDocsType<UsingDecl> : std::type_identity<UsingInfo> {};
Expand Down
2 changes: 2 additions & 0 deletions src/lib/AST/AnyBlock.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -1467,6 +1467,8 @@ class NamespaceBlock
{
case NAMESPACE_BITS:
return decodeRecord(R, {&I->specs.raw}, Blob);
case NAMESPACE_USING_DIRECTIVES:
return decodeRecord(R, I->UsingDirectives, Blob);
default:
return TopLevelBlock::parseRecord(R, ID, Blob);
}
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 @@ -109,6 +109,7 @@ enum RecordID
NAME_INFO_NAME,

NAMESPACE_BITS,
NAMESPACE_USING_DIRECTIVES,
TYPEINFO_KIND,
TYPEINFO_IS_PACK,
TYPEINFO_CVQUAL,
Expand Down
4 changes: 3 additions & 1 deletion src/lib/AST/BitcodeWriter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -329,6 +329,7 @@ RecordIDNameMap = []()
{JAVADOC_NODE_SYMBOLREF, {"JavadocNodeSymbol", &SymbolIDAbbrev}},
{JAVADOC_PARAM_DIRECTION, {"JavadocParamDirection", &Integer32Abbrev}},
{NAMESPACE_BITS, {"NamespaceBits", &Integer32ArrayAbbrev}},
{NAMESPACE_USING_DIRECTIVES, {"NamespaceUsingDirectives", &SymbolIDsAbbrev}},
{NAME_INFO_KIND, {"NameKind", &Integer32Abbrev}},
{NAME_INFO_ID, {"NameID", &SymbolIDAbbrev}},
{NAME_INFO_NAME, {"NameName", &StringAbbrev}},
Expand Down Expand Up @@ -426,7 +427,7 @@ RecordsByBlock{
JAVADOC_NODE_PART, JAVADOC_NODE_SYMBOLREF}},
// NamespaceInfo
{BI_NAMESPACE_BLOCK_ID,
{NAMESPACE_BITS}},
{NAMESPACE_BITS, NAMESPACE_USING_DIRECTIVES}},
// RecordInfo
{BI_RECORD_BLOCK_ID,
{RECORD_KEY_KIND, RECORD_IS_TYPE_DEF, RECORD_BITS}},
Expand Down Expand Up @@ -1108,6 +1109,7 @@ emitBlock(
emitInfoPart(I);
emitScopeInfo(I);
emitRecord({I.specs.raw}, NAMESPACE_BITS);
emitRecord(I.UsingDirectives, NAMESPACE_USING_DIRECTIVES);
}

void
Expand Down
3 changes: 0 additions & 3 deletions src/lib/Gen/xml/XMLWriter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -425,9 +425,6 @@ XMLWriter::
case UsingClass::Enum:
classStr = "using enum";
break;
case UsingClass::Namespace:
classStr = "using namespace";
break;
default:
MRDOCS_UNREACHABLE();
}
Expand Down
13 changes: 4 additions & 9 deletions src/lib/Metadata/DomMetadata.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -737,6 +737,8 @@ DomInfo<T>::construct() const
std::make_shared<Tranche>(
makeTranche(I_, *domCorpus_)),
domCorpus_));
entries.emplace_back("usingDirectives", dom::newArray<DomSymbolArray>(
I_.UsingDirectives, domCorpus_));
}
if constexpr(T::isRecord())
{
Expand Down Expand Up @@ -875,15 +877,8 @@ DomInfo<T>::construct() const
if constexpr(T::isUsing())
{
entries.emplace_back("class", toString(I_.Class));
if (I_.Class == UsingClass::Namespace)
{
entries.emplace_back("nominatedSymbol", domCreate(I_.Qualifier, domCorpus_));
}
else
{
entries.emplace_back("symbols", dom::newArray<DomSymbolArray>(I_.UsingSymbols, domCorpus_));
entries.emplace_back("qualifier", domCreate(I_.Qualifier, domCorpus_));
}
entries.emplace_back("symbols", dom::newArray<DomSymbolArray>(I_.UsingSymbols, domCorpus_));
entries.emplace_back("qualifier", domCreate(I_.Qualifier, domCorpus_));
}
if constexpr(T::isEnumerator())
{
Expand Down
1 change: 1 addition & 0 deletions src/lib/Metadata/Finalize.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -274,6 +274,7 @@ class Finalizer
check(I.Namespace);
check(I.Members);
finalize(I.javadoc);
finalize(I.UsingDirectives);
// finalize(I.Specializations);
}

Expand Down
2 changes: 2 additions & 0 deletions src/lib/Metadata/Reduce.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,8 @@ void merge(NamespaceInfo& I, NamespaceInfo&& Other)
I.specs.raw.value |= Other.specs.raw.value;
mergeScopeInfo(I, std::move(Other));
mergeInfo(I, std::move(Other));
reduceSymbolIDs(I.UsingDirectives,
std::move(Other.UsingDirectives));
}

void merge(RecordInfo& I, RecordInfo&& Other)
Expand Down
2 changes: 0 additions & 2 deletions src/lib/Support/SafeNames.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -209,8 +209,6 @@ class SafeNames::Impl
}

if constexpr(T::isUsing()) {
if (t.Class == UsingClass::Namespace)
return getReserved(t);
MRDOCS_ASSERT(! t.Name.empty());
return t.Name;
}
Expand Down

0 comments on commit b97dcfe

Please sign in to comment.