Skip to content

Commit

Permalink
feat: replace (Builtin/Tag/Specialization)TypeInfo with NamedTypeInfo
Browse files Browse the repository at this point in the history
  • Loading branch information
sdkrystian committed Dec 13, 2023
1 parent 3d128cc commit 2ff3579
Show file tree
Hide file tree
Showing 16 changed files with 199 additions and 276 deletions.
15 changes: 11 additions & 4 deletions include/mrdocs/Metadata/Name.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ enum class NameKind
Specialization
};

MRDOCS_DECL
dom::String
MRDOCS_DECL
dom::String
toString(NameKind kind) noexcept;

/** Represents a (possibly qualified) symbol name.
Expand All @@ -41,7 +41,7 @@ struct NameInfo
/** The SymbolID of the named symbol, if it exists.
*/
SymbolID id = SymbolID::invalid;

/** The unqualified name.
*/
std::string Name;
Expand All @@ -50,6 +50,9 @@ struct NameInfo
*/
std::unique_ptr<NameInfo> Prefix;

constexpr bool isIdentifier() const noexcept { return Kind == NameKind::Identifier; }
constexpr bool isSpecialization() const noexcept { return Kind == NameKind::Specialization; }

constexpr
NameInfo() noexcept
: NameInfo(NameKind::Identifier)
Expand All @@ -67,7 +70,7 @@ struct NameInfo

/** Represents a (possibly qualified) symbol name with template arguments.
*/
struct SpecializationNameInfo
struct SpecializationNameInfo
: NameInfo
{
/** The template arguments.
Expand Down Expand Up @@ -106,6 +109,10 @@ visit(
}
}

MRDOCS_DECL
std::string
toString(const NameInfo& N);

} // mrdocs
} // clang

Expand Down
59 changes: 12 additions & 47 deletions include/mrdocs/Metadata/Type.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,7 @@ MRDOCS_DECL dom::String toString(QualifierKind kind) noexcept;

enum class TypeKind
{
Builtin = 1, // for bitstream
Tag,
Specialization,
Named = 1, // for bitstream
Decltype,
LValueReference,
RValueReference,
Expand All @@ -63,9 +61,7 @@ struct TypeInfo

constexpr virtual ~TypeInfo() = default;

constexpr bool isBuiltin() const noexcept { return Kind == TypeKind::Builtin ; }
constexpr bool isTag() const noexcept { return Kind == TypeKind::Tag; }
constexpr bool isSpecialization() const noexcept { return Kind == TypeKind::Specialization; }
constexpr bool isNamed() const noexcept { return Kind == TypeKind::Named; }
constexpr bool isDecltype() const noexcept { return Kind == TypeKind::Decltype; }
constexpr bool isLValueReference() const noexcept { return Kind == TypeKind::LValueReference; }
constexpr bool isRValueReference() const noexcept { return Kind == TypeKind::RValueReference; }
Expand All @@ -84,6 +80,10 @@ struct TypeInfo
return nullptr;
}

/** Return the symbol named by this type.
*/
SymbolID namedSymbol() const noexcept;

protected:
constexpr
TypeInfo(
Expand All @@ -98,9 +98,7 @@ struct IsType : TypeInfo
{
static constexpr TypeKind kind_id = K;

static constexpr bool isBuiltin() noexcept { return K == TypeKind::Builtin; }
static constexpr bool isTag() noexcept { return K == TypeKind::Tag; }
static constexpr bool isSpecialization() noexcept { return K == TypeKind::Specialization; }
static constexpr bool isNamed() noexcept { return K == TypeKind::Named; }
static constexpr bool isDecltype() noexcept { return K == TypeKind::Decltype; }
static constexpr bool isLValueReference() noexcept { return K == TypeKind::LValueReference; }
static constexpr bool isRValueReference() noexcept { return K == TypeKind::RValueReference; }
Expand All @@ -117,36 +115,11 @@ struct IsType : TypeInfo
}
};

struct BuiltinTypeInfo
: IsType<TypeKind::Builtin>
{
QualifierKind CVQualifiers = QualifierKind::None;
std::string Name;

std::unique_ptr<NameInfo> Name_;
};

struct TagTypeInfo
: IsType<TypeKind::Tag>
struct NamedTypeInfo
: IsType<TypeKind::Named>
{
QualifierKind CVQualifiers = QualifierKind::None;
std::unique_ptr<TypeInfo> ParentType;
std::string Name;
SymbolID id = SymbolID::invalid;

std::unique_ptr<NameInfo> Name_;
};

struct SpecializationTypeInfo
: IsType<TypeKind::Specialization>
{
QualifierKind CVQualifiers = QualifierKind::None;
std::unique_ptr<TypeInfo> ParentType;
std::string Name;
SymbolID id = SymbolID::invalid;
std::vector<std::unique_ptr<TArg>> TemplateArgs;

std::unique_ptr<NameInfo> Name_;
std::unique_ptr<NameInfo> Name;
};

struct DecltypeTypeInfo
Expand Down Expand Up @@ -244,17 +217,9 @@ visit(
add_cv_from_t<TypeTy, TypeInfo>& II = I;
switch(I.Kind)
{
case TypeKind::Builtin:
return f(static_cast<add_cv_from_t<
TypeTy, BuiltinTypeInfo>&>(II),
std::forward<Args>(args)...);
case TypeKind::Tag:
return f(static_cast<add_cv_from_t<
TypeTy, TagTypeInfo>&>(II),
std::forward<Args>(args)...);
case TypeKind::Specialization:
case TypeKind::Named:
return f(static_cast<add_cv_from_t<
TypeTy, SpecializationTypeInfo>&>(II),
TypeTy, NamedTypeInfo>&>(II),
std::forward<Args>(args)...);
case TypeKind::Decltype:
return f(static_cast<add_cv_from_t<
Expand Down
1 change: 1 addition & 0 deletions include/mrdocs/MetadataFwd.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ struct NameInfo;
struct RecordInfo;
struct Param;
struct SpecializationInfo;
struct SpecializationNameInfo;
struct SourceInfo;
struct TypeInfo;
struct TypedefInfo;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,13 @@
{{~/if~}}
{{#if element-type~}}{{~>declarator-before element-type nolink=nolink~}}{{/if~}}
{{#if return-type~}}{{~>declarator-before return-type nolink=nolink~}}{{/if~}}
{{#if parent-type~}}{{>declarator parent-type nolink=nolink}}::{{/if~}}
{{#if (eq kind "lvalue-reference")}}&{{/if~}}
{{#if (eq kind "rvalue-reference")}}&&{{/if~}}
{{#if (eq kind "pointer")}}*{{/if~}}
{{#if (eq kind "member-pointer")}}*{{/if~}}
{{#if (eq kind "member-pointer")}}{{>declarator parent-type nolink=nolink}}::*{{/if~}}
{{#if cv-qualifiers~}}
{{#if pointee-type}} {{cv-qualifiers}}{{else}}{{cv-qualifiers}} {{/if~}}
{{/if~}}
{{#if prefix~}}{{>name-info prefix nolink=nolink}}{{/if~}}
{{!-- {{#if (and symbol (not parent-type))}}{{>qualified-path symbol=symbol.parent nolink=nolink}}{{/if~}} --~}}
{{#if (and symbol (not nolink))~}}
xref:{{symbol.ref}}[{{name}}]
{{~else if name}}{{name~}}
{{/if~}}
{{#if (eq kind "decltype")}}decltype({{operand}}){{/if~}}
{{#if (eq kind "specialization")}}{{>template-args args=args nolink=nolink}}{{/if~}}
{{#if (eq kind "named")}}{{>name-info name nolink=nolink}}{{/if~}}
{{#if is-pack~}}...{{/if}}
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
{{#unless (or (contains @root.symbol.namespace symbol) (eq @root.symbol symbol))~}}
{{#if prefix~}}
{{>name-info prefix nolink=nolink~}}
{{else~}}
{{#unless (or (contains @root.symbol.namespace prefix.symbol) (eq @root.symbol prefix.symbol))~}}
{{>name-info prefix nolink=nolink~}}::
{{~/unless~}}
{{/if~}}
{{#if (and symbol.ref (not nolink))}}xref:{{symbol.ref}}[{{name}}]{{else~}}
{{name}}{{/if}}{{#if args}}{{>template-args args=args nolink=nolink}}{{/if}}::
{{~/unless}}
{{name}}{{/if}}{{#if args}}{{>template-args args=args nolink=nolink}}{{/if~}}
80 changes: 48 additions & 32 deletions src/lib/AST/ASTVisitor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2725,6 +2725,12 @@ class TerminalTypeVisitor
bool VisitAutoType(
const AutoType* T)
{
// KRYSTIAN TODO: we should probably add a TypeInfo
// to represent deduced types that also stores what
// it was deduced as.
// KRYSTIAN NOTE: we don't use isDeduced because it will
// return true if the type is dependent
// if the type has been deduced, use the deduced type
if(QualType DT = T->getDeducedType(); ! DT.isNull())
return Visit(DT);
getDerived().buildTerminal(NNS_, T, Quals_, IsPack_);
Expand All @@ -2734,6 +2740,9 @@ class TerminalTypeVisitor
bool VisitDeducedTemplateSpecializationType(
const DeducedTemplateSpecializationType* T)
{
// KRYSTIAN TODO: we should probably add a TypeInfo
// to represent deduced types that also stores what
// it was deduced as.
if(QualType DT = T->getDeducedType(); ! DT.isNull())
return Visit(DT);
TemplateName TN = T->getTemplateName();
Expand Down Expand Up @@ -3047,10 +3056,13 @@ class TypeInfoBuilder
unsigned quals,
bool pack)
{
auto I = std::make_unique<BuiltinTypeInfo>();
auto I = std::make_unique<NamedTypeInfo>();
I->CVQualifiers = convertToQualifierKind(quals);
I->Name = getASTVisitor().getTypeAsString(T);
I->Name_ = getASTVisitor().buildNameInfo(NNS);

auto Name = std::make_unique<NameInfo>();
Name->Name = getASTVisitor().getTypeAsString(T);
Name->Prefix = getASTVisitor().buildNameInfo(NNS);
I->Name = std::move(Name);
*Inner = std::move(I);
Result->IsPackExpansion = pack;
}
Expand All @@ -3064,25 +3076,27 @@ class TypeInfoBuilder
bool pack)
{
ASTVisitor& V = getASTVisitor();
auto I = std::make_unique<NamedTypeInfo>();
I->CVQualifiers = convertToQualifierKind(quals);

if(TArgs)
{
auto I = std::make_unique<SpecializationTypeInfo>();
I->CVQualifiers = convertToQualifierKind(quals);
auto Name = std::make_unique<SpecializationNameInfo>();
if(II)
I->Name = II->getName();
I->Name_ = V.buildNameInfo(NNS);
V.buildTemplateArgs(I->TemplateArgs, *TArgs);
*Inner = std::move(I);
Name->Name = II->getName();
Name->Prefix = getASTVisitor().buildNameInfo(NNS);
V.buildTemplateArgs(Name->TemplateArgs, *TArgs);
I->Name = std::move(Name);
}
else
{
auto I = std::make_unique<TagTypeInfo>();
I->CVQualifiers = convertToQualifierKind(quals);
auto Name = std::make_unique<NameInfo>();
if(II)
I->Name = II->getName();
I->Name_ = V.buildNameInfo(NNS);
*Inner = std::move(I);
Name->Name = II->getName();
Name->Prefix = getASTVisitor().buildNameInfo(NNS);
I->Name = std::move(Name);
}
*Inner = std::move(I);
Result->IsPackExpansion = pack;
}

Expand All @@ -3095,34 +3109,36 @@ class TypeInfoBuilder
bool pack)
{
ASTVisitor& V = getASTVisitor();
const IdentifierInfo* II = D->getIdentifier();
auto I = std::make_unique<NamedTypeInfo>();
I->CVQualifiers = convertToQualifierKind(quals);

if(TArgs)
{
auto I = std::make_unique<SpecializationTypeInfo>();
I->CVQualifiers = convertToQualifierKind(quals);
if(II)
I->Name = II->getName();
V.getDependencyID(V.getInstantiatedFrom(D), I->id);
auto Name = std::make_unique<SpecializationNameInfo>();
if(const IdentifierInfo* II = D->getIdentifier())
Name->Name = II->getName();
V.getDependencyID(V.getInstantiatedFrom(D), Name->id);
if(NNS)
I->Name_ = V.buildNameInfo(NNS);
Name->Prefix = V.buildNameInfo(NNS);
else
I->Name_ = V.buildNameInfo(D);
V.buildTemplateArgs(I->TemplateArgs, *TArgs);
*Inner = std::move(I);
Name->Prefix = V.buildNameInfo(D);

V.buildTemplateArgs(Name->TemplateArgs, *TArgs);
I->Name = std::move(Name);
}
else
{
auto I = std::make_unique<TagTypeInfo>();
I->CVQualifiers = convertToQualifierKind(quals);
if(II)
I->Name = II->getName();
V.getDependencyID(V.getInstantiatedFrom(D), I->id);
auto Name = std::make_unique<NameInfo>();
if(const IdentifierInfo* II = D->getIdentifier())
Name->Name = II->getName();
V.getDependencyID(V.getInstantiatedFrom(D), Name->id);
if(NNS)
I->Name_ = V.buildNameInfo(NNS);
Name->Prefix = V.buildNameInfo(NNS);
else
I->Name_ = V.buildNameInfo(D);
*Inner = std::move(I);
Name->Prefix = V.buildNameInfo(D);
I->Name = std::move(Name);
}
*Inner = std::move(I);
Result->IsPackExpansion = pack;
}
};
Expand Down
Loading

0 comments on commit 2ff3579

Please sign in to comment.