Skip to content

Commit c4441d4

Browse files
committed
refactor: don't represent using-directives as Info
1 parent 995c8ac commit c4441d4

File tree

19 files changed

+61
-89
lines changed

19 files changed

+61
-89
lines changed

include/mrdocs/Metadata/Info.hpp

+3-3
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ enum class InfoKind
6969
Guide,
7070
/// The symbol is a namespace alias
7171
Alias,
72-
/// The symbol is a using declaration and using directive
72+
/// The symbol is a using declaration
7373
Using,
7474
};
7575

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

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

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

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

251251
protected:

include/mrdocs/Metadata/Namespace.hpp

+4
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,10 @@ struct NamespaceInfo
3737
{
3838
NamespaceFlags specs;
3939

40+
/** Namespaces nominated by using-directives.
41+
*/
42+
std::vector<SymbolID> UsingDirectives;
43+
4044
//--------------------------------------------
4145

4246
explicit NamespaceInfo(SymbolID ID) noexcept

include/mrdocs/Metadata/Using.hpp

+7-6
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@ enum class UsingClass
2626
Normal = 0, // using
2727
Typename, // using typename
2828
Enum, // using enum
29-
Namespace // using namespace (using directive)
3029
};
3130

3231
static constexpr
@@ -38,24 +37,26 @@ toString(UsingClass const& value)
3837
case UsingClass::Normal: return "normal";
3938
case UsingClass::Typename: return "typename";
4039
case UsingClass::Enum: return "enum";
41-
case UsingClass::Namespace: return "namespace";
4240
}
4341
return "unknown";
4442
}
4543

46-
/** Info for using declarations and directives.
44+
/** Info for using declarations.
4745
*/
4846
struct UsingInfo
4947
: IsInfo<InfoKind::Using>,
5048
SourceInfo
5149
{
52-
/** The kind of using declaration/directive. */
50+
/** The kind of using declaration.
51+
*/
5352
UsingClass Class = UsingClass::Normal;
5453

55-
/** The symbols being "used". */
54+
/** The symbols being "used".
55+
*/
5656
std::vector<SymbolID> UsingSymbols;
5757

58-
/** The qualifier for a using declaration/directive. */
58+
/** The qualifier for a using declaration.
59+
*/
5960
std::unique_ptr<NameInfo> Qualifier;
6061

6162
//--------------------------------------------

mrdocs.rnc

+2-1
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ grammar
3535
attribute is-inline { "1" }?,
3636
attribute is-anonymous { "1" }?,
3737
Javadoc?,
38+
element using-directive { ID } *,
3839
Scope
3940
}
4041

@@ -238,7 +239,7 @@ grammar
238239
ID,
239240
Location *,
240241
Javadoc ?,
241-
attribute class { "using" | "using typename" | "using enum" | "using namespace" },
242+
attribute class { "using" | "using typename" | "using enum" },
242243
attribute qualifier { text } ?,
243244
element named { ID } *
244245
}
Original file line numberDiff line numberDiff line change
@@ -1,5 +1 @@
1-
{{#if (eq symbol.class "namespace")}}
2-
using namespace {{#if symbol.qualifier}}{{>name-info symbol.qualifier}}::{{/if}}{{symbol.name}}
3-
{{else}}
4-
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}}
5-
{{/if}}
1+
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~}}

share/mrdocs/addons/generator/asciidoc/partials/symbols/namespace.adoc.hbs

+2
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,5 @@
22
= {{#if symbol.name}}Namespace {{>nested-name-specifier symbol=symbol.parent}}{{symbol.name}}{{else if symbol.parent}}Unnamed namespace{{else}}Global namespace{{/if}}
33

44
{{> tranche tranche=symbol.interface label="" is-namespace=true}}
5+
6+
{{>info-list members=symbol.usingDirectives title="Using Directives"}}

share/mrdocs/addons/generator/asciidoc/partials/symbols/using.adoc.hbs

+5-4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{{!-- symbols/using.adoc.hbs --}}
2-
= Using {{#if (eq symbol.class "namespace")}}Directive: {{symbol.qualifier.name}}{{else}}Declaration: {{symbol.name}}{{/if}}
2+
= Using Declaration {{symbol.name}}
33

44
{{symbol.doc.brief}}
55

@@ -19,13 +19,14 @@
1919
2020
{{/if}}
2121
22-
{{#unless (eq symbol.class "namespace")}}
22+
{{#if symbol.shadows}}
2323
== Introduced Symbols
2424
2525
|===
2626
| Name
27-
{{#each symbol.symbols}}
27+
{{#each symbol.shadows}}
2828
| {{name}}
2929
{{/each}}
3030
|===
31-
{{/unless}}
31+
32+
{{/if}}

share/mrdocs/addons/generator/asciidoc/partials/tranche.adoc.hbs

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,6 @@
1111
{{>info-list members=tranche.variables title=(concat label " " "Static Data Members")}}
1212
{{>info-list members=tranche.friends title=(concat label " " "Friends")}}
1313
{{>info-list members=tranche.aliases title=(concat label " " "Aliases")}}
14-
{{>info-list members=tranche.usings title=(concat label " " "Usings Declarations/Directives")}}
14+
{{>info-list members=tranche.usings title=(concat label " " "Usings Declarations")}}
1515
{{/if}}
1616
{{>info-list members=tranche.guides title=(concat label " " "Deduction Guides")}}

src/lib/AST/ASTVisitor.cpp

+18-49
Original file line numberDiff line numberDiff line change
@@ -436,6 +436,8 @@ class ASTVisitor
436436
std::pair<InfoTy&, bool>
437437
getOrCreateInfo(const SymbolID& id)
438438
{
439+
assert(id != SymbolID::invalid &&
440+
"creating symbol with invalid SymbolID?");
439441
Info* info = getInfo(id);
440442
bool created = ! info;
441443
if(! info)
@@ -535,17 +537,6 @@ class ASTVisitor
535537
return false;
536538
}
537539

538-
// Handling UsingDirectiveDecl
539-
if (const auto* UDD = dyn_cast<UsingDirectiveDecl>(D))
540-
{
541-
if (index::generateUSRForDecl(UDD->getNominatedNamespace(), usr_)) {
542-
return true;
543-
}
544-
usr_.append("@UD");
545-
usr_.append(UDD->getQualifiedNameAsString());
546-
return false;
547-
}
548-
549540
// Handling UsingDecl
550541
if (const auto* UD = dyn_cast<UsingDecl>(D))
551542
{
@@ -2194,39 +2185,6 @@ class ASTVisitor
21942185
getParentNamespaces(I, D);
21952186
}
21962187

2197-
2198-
//------------------------------------------------
2199-
2200-
void
2201-
buildUsingDirective(
2202-
UsingInfo& I,
2203-
bool created,
2204-
UsingDirectiveDecl* D)
2205-
{
2206-
bool documented = parseRawComment(I.javadoc, D);
2207-
addSourceLocation(I, D->getBeginLoc(), true, documented);
2208-
2209-
if(! created)
2210-
return;
2211-
2212-
I.Class = UsingClass::Namespace;
2213-
2214-
if (D->getQualifier())
2215-
{
2216-
I.Qualifier = buildNameInfo(D->getQualifier());
2217-
}
2218-
2219-
if (NamedDecl* ND = D->getNominatedNamespace())
2220-
{
2221-
I.Name = extractName(ND);
2222-
SymbolID id;
2223-
getDependencyID(ND, id);
2224-
I.UsingSymbols.emplace_back(id);
2225-
}
2226-
getParentNamespaces(I, D);
2227-
}
2228-
2229-
22302188
//------------------------------------------------
22312189

22322190
void
@@ -2601,12 +2559,23 @@ void
26012559
ASTVisitor::
26022560
traverse(UsingDirectiveDecl* D)
26032561
{
2604-
auto exp = getAsMrDocsInfo(D);
2605-
if(! exp) { return; }
2606-
auto [I, created] = *exp;
2607-
buildUsingDirective(I, created, D);
2608-
}
2562+
if(! shouldExtract(D, getAccess(D)))
2563+
return;
26092564

2565+
Decl* PD = getParentDecl(D);
2566+
// only extract using-directives in namespace scope
2567+
if(! cast<DeclContext>(PD)->isFileContext())
2568+
return;
2569+
2570+
if(Info* PI = getInfo(extractSymbolID(PD)))
2571+
{
2572+
assert(PI->isNamespace());
2573+
NamespaceInfo* NI = static_cast<NamespaceInfo*>(PI);
2574+
getDependencyID(
2575+
D->getNominatedNamespaceAsWritten(),
2576+
NI->UsingDirectives.emplace_back());
2577+
}
2578+
}
26102579

26112580
//------------------------------------------------
26122581
// UsingDecl

src/lib/AST/ASTVisitorHelpers.hpp

-3
Original file line numberDiff line numberDiff line change
@@ -58,9 +58,6 @@ struct MrDocsType<CXXDeductionGuideDecl> : std::type_identity<GuideInfo> {};
5858
template <>
5959
struct MrDocsType<NamespaceAliasDecl> : std::type_identity<AliasInfo> {};
6060

61-
template <>
62-
struct MrDocsType<UsingDirectiveDecl> : std::type_identity<UsingInfo> {};
63-
6461
template <>
6562
struct MrDocsType<UsingDecl> : std::type_identity<UsingInfo> {};
6663

src/lib/AST/AnyBlock.hpp

+2
Original file line numberDiff line numberDiff line change
@@ -1467,6 +1467,8 @@ class NamespaceBlock
14671467
{
14681468
case NAMESPACE_BITS:
14691469
return decodeRecord(R, {&I->specs.raw}, Blob);
1470+
case NAMESPACE_USING_DIRECTIVES:
1471+
return decodeRecord(R, I->UsingDirectives, Blob);
14701472
default:
14711473
return TopLevelBlock::parseRecord(R, ID, Blob);
14721474
}

src/lib/AST/BitcodeIDs.hpp

+1
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,7 @@ enum RecordID
109109
NAME_INFO_NAME,
110110

111111
NAMESPACE_BITS,
112+
NAMESPACE_USING_DIRECTIVES,
112113
TYPEINFO_KIND,
113114
TYPEINFO_IS_PACK,
114115
TYPEINFO_CVQUAL,

src/lib/AST/BitcodeWriter.cpp

+3-1
Original file line numberDiff line numberDiff line change
@@ -329,6 +329,7 @@ RecordIDNameMap = []()
329329
{JAVADOC_NODE_SYMBOLREF, {"JavadocNodeSymbol", &SymbolIDAbbrev}},
330330
{JAVADOC_PARAM_DIRECTION, {"JavadocParamDirection", &Integer32Abbrev}},
331331
{NAMESPACE_BITS, {"NamespaceBits", &Integer32ArrayAbbrev}},
332+
{NAMESPACE_USING_DIRECTIVES, {"NamespaceUsingDirectives", &SymbolIDsAbbrev}},
332333
{NAME_INFO_KIND, {"NameKind", &Integer32Abbrev}},
333334
{NAME_INFO_ID, {"NameID", &SymbolIDAbbrev}},
334335
{NAME_INFO_NAME, {"NameName", &StringAbbrev}},
@@ -426,7 +427,7 @@ RecordsByBlock{
426427
JAVADOC_NODE_PART, JAVADOC_NODE_SYMBOLREF}},
427428
// NamespaceInfo
428429
{BI_NAMESPACE_BLOCK_ID,
429-
{NAMESPACE_BITS}},
430+
{NAMESPACE_BITS, NAMESPACE_USING_DIRECTIVES}},
430431
// RecordInfo
431432
{BI_RECORD_BLOCK_ID,
432433
{RECORD_KEY_KIND, RECORD_IS_TYPE_DEF, RECORD_BITS}},
@@ -1108,6 +1109,7 @@ emitBlock(
11081109
emitInfoPart(I);
11091110
emitScopeInfo(I);
11101111
emitRecord({I.specs.raw}, NAMESPACE_BITS);
1112+
emitRecord(I.UsingDirectives, NAMESPACE_USING_DIRECTIVES);
11111113
}
11121114

11131115
void

src/lib/Gen/xml/XMLWriter.cpp

+2-3
Original file line numberDiff line numberDiff line change
@@ -209,6 +209,8 @@ operator()(
209209
{ "is-inline", "1", I.specs.isInline}
210210
});
211211
writeJavadoc(I.javadoc);
212+
for(const SymbolID& id : I.UsingDirectives)
213+
tags_.write("using-directive", {}, { { id } });
212214
corpus_.traverse(I, *this);
213215
tags_.close(namespaceTagName);
214216
}
@@ -425,9 +427,6 @@ XMLWriter::
425427
case UsingClass::Enum:
426428
classStr = "using enum";
427429
break;
428-
case UsingClass::Namespace:
429-
classStr = "using namespace";
430-
break;
431430
default:
432431
MRDOCS_UNREACHABLE();
433432
}

src/lib/Metadata/DomMetadata.cpp

+4-5
Original file line numberDiff line numberDiff line change
@@ -737,6 +737,8 @@ DomInfo<T>::construct() const
737737
std::make_shared<Tranche>(
738738
makeTranche(I_, *domCorpus_)),
739739
domCorpus_));
740+
entries.emplace_back("usingDirectives", dom::newArray<DomSymbolArray>(
741+
I_.UsingDirectives, domCorpus_));
740742
}
741743
if constexpr(T::isRecord())
742744
{
@@ -875,11 +877,8 @@ DomInfo<T>::construct() const
875877
if constexpr(T::isUsing())
876878
{
877879
entries.emplace_back("class", toString(I_.Class));
878-
entries.emplace_back("symbols", dom::newArray<DomSymbolArray>(I_.UsingSymbols, domCorpus_));
879-
if (I_.Qualifier)
880-
{
881-
entries.emplace_back("qualifier", domCreate(I_.Qualifier, domCorpus_));
882-
}
880+
entries.emplace_back("shadows", dom::newArray<DomSymbolArray>(I_.UsingSymbols, domCorpus_));
881+
entries.emplace_back("qualifier", domCreate(I_.Qualifier, domCorpus_));
883882
}
884883
if constexpr(T::isEnumerator())
885884
{

src/lib/Metadata/Finalize.cpp

+1
Original file line numberDiff line numberDiff line change
@@ -274,6 +274,7 @@ class Finalizer
274274
check(I.Namespace);
275275
check(I.Members);
276276
finalize(I.javadoc);
277+
finalize(I.UsingDirectives);
277278
// finalize(I.Specializations);
278279
}
279280

src/lib/Metadata/Reduce.cpp

+2
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,8 @@ void merge(NamespaceInfo& I, NamespaceInfo&& Other)
163163
I.specs.raw.value |= Other.specs.raw.value;
164164
mergeScopeInfo(I, std::move(Other));
165165
mergeInfo(I, std::move(Other));
166+
reduceSymbolIDs(I.UsingDirectives,
167+
std::move(Other.UsingDirectives));
166168
}
167169

168170
void merge(RecordInfo& I, RecordInfo&& Other)

src/lib/Support/SafeNames.cpp

+2-4
Original file line numberDiff line numberDiff line change
@@ -208,11 +208,9 @@ class SafeNames::Impl
208208
return t.Name;
209209
}
210210

211-
if constexpr(T::isUsing()) {
211+
if constexpr(T::isUsing())
212+
{
212213
MRDOCS_ASSERT(! t.Name.empty());
213-
if (t.Class == UsingClass::Namespace) {
214-
return getReserved(t);
215-
}
216214
return t.Name;
217215
}
218216

test-files/old-tests/using-1.xml

+1-4
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,8 @@
22
<mrdocs xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
33
xsi:noNamespaceSchemaLocation="https://github.com/cppalliance/mrdocs/raw/develop/mrdocs.rnc">
44
<namespace id="//////////////////////////8=">
5+
<using-directive id="IjtkITsyqdEWPMXZF3lD/2/XH0s="/>
56
<namespace name="LongName" id="IjtkITsyqdEWPMXZF3lD/2/XH0s=">
67
</namespace>
7-
<using id="0dkQs6+jYNOCUA7aOGsT92PFOfk=" class="using namespace">
8-
<file path="using-1.cpp" line="3" class="def"/>
9-
<named id="IjtkITsyqdEWPMXZF3lD/2/XH0s="/>
10-
</using>
118
</namespace>
129
</mrdocs>

0 commit comments

Comments
 (0)