Skip to content

Commit

Permalink
chore: generate symbol IDs for type by looking through pointers/refer…
Browse files Browse the repository at this point in the history
…ences etc
  • Loading branch information
sdkrystian committed Jun 19, 2023
1 parent 75cd2dd commit 072f43c
Show file tree
Hide file tree
Showing 9 changed files with 420 additions and 26 deletions.
138 changes: 128 additions & 10 deletions source/AST/ASTVisitor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -168,18 +168,139 @@ getCXXRecordDeclForType(
return nullptr;
}

TypeInfo
SymbolID
ASTVisitor::
getTypeInfoForType(
getSymbolIDForType(
QualType T)
{
SymbolID id = SymbolID::zero;
if(const TagDecl* TD = getTagDeclForType(T))
QualType inner = T;
while(true)
{
extractSymbolID(TD, id);
return TypeInfo(id, TD->getNameAsString());
inner = inner.getLocalUnqualifiedType();
const Type* type = inner.getTypePtr();
switch(inner->getTypeClass())
{
// parenthesized types
case Type::Paren:
{
auto* PT = cast<ParenType>(type);
inner = PT->getInnerType();
continue;
}
// pointers
case Type::Pointer:
{
auto* PT = cast<PointerType>(type);
inner = PT->getPointeeType();
continue;
}
// references
case Type::LValueReference:
case Type::RValueReference:
{
auto* RT = cast<ReferenceType>(type);
inner = RT->getPointeeType();
continue;
}
// pointer to members
case Type::MemberPointer:
{
auto* MPT = cast<MemberPointerType>(type);
inner = MPT->getPointeeType();
continue;
}
// arrays
case Type::ConstantArray:
case Type::IncompleteArray:
case Type::VariableArray:
case Type::DependentSizedArray:
{
auto* AT = cast<ArrayType>(type);
inner = AT->getElementType();
continue;
}
// elaborated type specifier
case Type::Elaborated:
{
auto* ET = cast<ElaboratedType>(type);
inner = ET->getNamedType();
continue;
}
// type with __atribute__
case Type::Attributed:
{
auto* AT = cast<AttributedType>(type);
inner = AT->getModifiedType();
continue;
}
// adjusted and decayed types
case Type::Decayed:
case Type::Adjusted:
{
auto* AT = cast<AdjustedType>(type);
inner = AT->getOriginalType();
continue;
}
// using declarations
case Type::Using:
{
auto* UT = cast<UsingType>(type);
// look through the using declaration and
// use the the type from the referenced declaration
inner = UT->getUnderlyingType();
continue;
}
// specialization of a class/alias template or
// template template parameter
case Type::TemplateSpecialization:
{
auto* TST = cast<TemplateSpecializationType>(type);
// use the SymbolID of the corresponding template if it is known
if(auto* TD = TST->getTemplateName().getAsTemplateDecl())
return extractSymbolID(TD);
return SymbolID::zero;
}
// pack expansion
case Type::PackExpansion:
{
auto* PET = cast<PackExpansionType>(type);
inner = PET->getPattern();
continue;
}
// record type
case Type::Record:
{
auto* RT = cast<RecordType>(type);
return extractSymbolID(RT->getDecl());
}
// enum type
case Type::Enum:
{
auto* ET = cast<EnumType>(type);
return extractSymbolID(ET->getDecl());
}
// typedef/alias type
case Type::Typedef:
{
auto* TT = cast<TypedefType>(type);
return extractSymbolID(TT->getDecl());
}
default:
return SymbolID::zero;
}
}
return TypeInfo(id, getTypeAsString(T));

}

TypeInfo
ASTVisitor::
getTypeInfoForType(
QualType T)
{
return TypeInfo(
getSymbolIDForType(T),
getTypeAsString(T));
}

void
Expand All @@ -190,11 +311,8 @@ parseParameters(
{
for(const ParmVarDecl* P : D->parameters())
{
// KRYSTIAN NOTE: call getOriginalType instead
// of getType if we want to preserve top-level
// cv-qualfiers/array types/function types
I.Params.emplace_back(
getTypeInfoForType(P->getType()),
getTypeInfoForType(P->getOriginalType()),
P->getNameAsString(),
getSourceCode(D, P->getDefaultArgRange()));
}
Expand Down
4 changes: 4 additions & 0 deletions source/AST/ASTVisitor.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,10 @@ class ASTVisitor
getCXXRecordDeclForType(
QualType T);

SymbolID
getSymbolIDForType(
QualType T);

TypeInfo
getTypeInfoForType(
QualType T);
Expand Down
4 changes: 2 additions & 2 deletions test-files/old-tests/alias-template.xml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
<tparam name="T" class="type"/>
<alias name="C" id="f75RIL8qKOwbyH6ffvrv59F0nCA=">
<file path="alias-template.cpp" line="8" class="def"/>
<type name="A&lt;T&gt;"/>
<type name="A&lt;T&gt;" id="5tUSuMtQqtYE49jBjSYSWp0DJAM="/>
</alias>
</template>
<template>
Expand All @@ -30,7 +30,7 @@
<tparam name="U" class="type"/>
<alias name="E" id="STM3CgeaOEakPrQJdlRWWM4MMFo=">
<file path="alias-template.cpp" line="14" class="def"/>
<type name="B&lt;T, U&gt;"/>
<type name="B&lt;T, U&gt;" id="swBEqeWAJoaWVKty+1lCoYUK04A="/>
</alias>
</template>
</struct>
Expand Down
12 changes: 6 additions & 6 deletions test-files/old-tests/explicit-ctor.xml
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,13 @@
<function name="Explicit" id="CuzDbFSxtCdVcGerq2UySnHF7po=">
<file path="explicit-ctor.cpp" line="5"/>
<attr id="explicit-spec" name="explicit" value="1"/>
<param type="const Explicit &amp;"/>
<param type="const Explicit &amp;" id="bonobNKGOblPVcRjxpiPan4nYnc="/>
</function>
<function name="Explicit" id="saOH3agrCzNHcRhbXHOM0FYFxGc=">
<file path="explicit-ctor.cpp" line="6"/>
<attr id="exception-spec" name="noexcept" value="5"/>
<attr id="explicit-spec" name="explicit" value="1"/>
<param type="Explicit &amp;&amp;"/>
<param type="Explicit &amp;&amp;" id="bonobNKGOblPVcRjxpiPan4nYnc="/>
</function>
<function name="Explicit" id="Oj6cGCOaarVfZXkgsr/pOPOOiFY=">
<file path="explicit-ctor.cpp" line="7"/>
Expand All @@ -35,13 +35,13 @@
<function name="ExplicitTrue" id="Tj9uY+jY7QBYQC8IsjplHuoDgQo=">
<file path="explicit-ctor.cpp" line="12"/>
<attr id="explicit-spec" name="explicit(true)" value="3"/>
<param type="const ExplicitTrue &amp;"/>
<param type="const ExplicitTrue &amp;" id="htdCGapMsdazR1XkXoanrOCkvDE="/>
</function>
<function name="ExplicitTrue" id="iQOO8pEh0iNSnpupVwR9tCVIf8U=">
<file path="explicit-ctor.cpp" line="13"/>
<attr id="exception-spec" name="noexcept" value="5"/>
<attr id="explicit-spec" name="explicit(true)" value="3"/>
<param type="ExplicitTrue &amp;&amp;"/>
<param type="ExplicitTrue &amp;&amp;" id="htdCGapMsdazR1XkXoanrOCkvDE="/>
</function>
<function name="ExplicitTrue" id="XCoHYDLlYJYpV6Ut7Ms0GjcQKjM=">
<file path="explicit-ctor.cpp" line="14"/>
Expand All @@ -59,13 +59,13 @@
<function name="ExplicitFalse" id="gsl+2i8v8v4Lifq3SZ5sFhSfseQ=">
<file path="explicit-ctor.cpp" line="19"/>
<attr id="explicit-spec" name="explicit(false)" value="2"/>
<param type="const ExplicitFalse &amp;"/>
<param type="const ExplicitFalse &amp;" id="Lt5u3k14Z7EBxY7Ike3h9qkJ0fo="/>
</function>
<function name="ExplicitFalse" id="qXjKOO0FQ3p2vTehkEQl2kIKpFs=">
<file path="explicit-ctor.cpp" line="20"/>
<attr id="exception-spec" name="noexcept" value="5"/>
<attr id="explicit-spec" name="explicit(false)" value="2"/>
<param type="ExplicitFalse &amp;&amp;"/>
<param type="ExplicitFalse &amp;&amp;" id="Lt5u3k14Z7EBxY7Ike3h9qkJ0fo="/>
</function>
<function name="ExplicitFalse" id="T0aRgSzz7xtFbPWKv3q57ueIYlI=">
<file path="explicit-ctor.cpp" line="21"/>
Expand Down
8 changes: 4 additions & 4 deletions test-files/old-tests/explicit-deduct-guide.xml
Original file line number Diff line number Diff line change
Expand Up @@ -12,21 +12,21 @@
<file path="explicit-deduct-guide.cpp" line="6"/>
<attr id="has-trailing-return"/>
<attr id="explicit-spec" name="explicit" value="1"/>
<return type="X&lt;0&gt;"/>
<return type="X&lt;0&gt;" id="7wllQWhGGsbbeoZVa3gh4d7bcq8="/>
<param type="bool"/>
</function>
<function name="&lt;deduction guide for X&gt;" id="AtQyLakD3CjcM24GabAbHMNgVU4=">
<file path="explicit-deduct-guide.cpp" line="8"/>
<attr id="has-trailing-return"/>
<attr id="explicit-spec" name="explicit(true)" value="3"/>
<return type="X&lt;0&gt;"/>
<return type="X&lt;0&gt;" id="7wllQWhGGsbbeoZVa3gh4d7bcq8="/>
<param type="char"/>
</function>
<function name="&lt;deduction guide for X&gt;" id="tkYkUU6H8cbFpCU7yBuvBLkT6dg=">
<file path="explicit-deduct-guide.cpp" line="10"/>
<attr id="has-trailing-return"/>
<attr id="explicit-spec" name="explicit(false)" value="2"/>
<return type="X&lt;0&gt;"/>
<return type="X&lt;0&gt;" id="7wllQWhGGsbbeoZVa3gh4d7bcq8="/>
<param type="int"/>
</function>
<template>
Expand All @@ -35,7 +35,7 @@
<file path="explicit-deduct-guide.cpp" line="13"/>
<attr id="has-trailing-return"/>
<attr id="explicit-spec" name="explicit(expr)" value="4"/>
<return type="X&lt;0&gt;"/>
<return type="X&lt;0&gt;" id="7wllQWhGGsbbeoZVa3gh4d7bcq8="/>
<param type="long"/>
</function>
</template>
Expand Down
6 changes: 3 additions & 3 deletions test-files/old-tests/function-parm-decay.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
<file path="function-parm-decay.cpp" line="4"/>
<file path="function-parm-decay.cpp" line="5"/>
<file path="function-parm-decay.cpp" line="6"/>
<param name="x" type="int"/>
<param name="x" type="const int"/>
</function>
<function name="g" id="CJhykN8TDmEo0Pl8UC2D9+rotIs=">
<file path="function-parm-decay.cpp" line="8"/>
Expand All @@ -21,7 +21,7 @@
<file path="function-parm-decay.cpp" line="14"/>
<file path="function-parm-decay.cpp" line="15"/>
<file path="function-parm-decay.cpp" line="16"/>
<param name="x" type="int (*)(bool)"/>
<param name="x" type="int (bool)"/>
</function>
<alias name="T" id="PvNOREc+JWxdkVgvSz+jC45IbGQ=">
<file path="function-parm-decay.cpp" line="18" class="def"/>
Expand All @@ -36,7 +36,7 @@
<file path="function-parm-decay.cpp" line="22"/>
<file path="function-parm-decay.cpp" line="23"/>
<file path="function-parm-decay.cpp" line="24"/>
<param type="T"/>
<param type="T" id="PvNOREc+JWxdkVgvSz+jC45IbGQ="/>
</function>
</namespace>
</mrdox>
2 changes: 1 addition & 1 deletion test-files/old-tests/static-data-def-constexpr.xml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
<file path="static-data-def-constexpr.cpp" line="5" class="def"/>
<file path="static-data-def-constexpr.cpp" line="3"/>
<attr id="storage-class" name="static" value="2"/>
<type name="S" id="pOYGF6pLJlICuiGO0Xj0daDb/to="/>
<type name="const S" id="pOYGF6pLJlICuiGO0Xj0daDb/to="/>
</variable>
</struct>
</namespace>
Expand Down
61 changes: 61 additions & 0 deletions test-files/old-tests/type-resolution.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
struct A { };

template<typename T, typename U>
struct B { };

using C = A;

using D = B<short, long>;

template<typename T>
using E = B<T, long>;

void f0(A);
void f1(const A);
void f2(A&);
void f3(const A&);
void f4(A*);
void f5(const A*);
void f6(A**);
void f7(const A**);
void f8(const A* const*);

void g0(C);
void g1(const C);
void g2(C&);
void g3(const C&);
void g4(C*);
void g5(const C*);
void g6(C**);
void g7(const C**);
void g8(const C* const*);

void h0(B<short, long>);
void h1(const B<short, long>);
void h2(B<short, long>&);
void h3(const B<short, long>&);
void h4(B<short, long>*);
void h5(const B<short, long>*);
void h6(B<short, long>**);
void h7(const B<short, long>**);
void h8(const B<short, long>* const*);

void i0(D);
void i1(const D);
void i2(D&);
void i3(const D&);
void i4(D*);
void i5(const D*);
void i6(D**);
void i7(const D**);
void i8(const D* const*);

void j0(E<short>);
void j1(const E<short>);
void j2(E<short>&);
void j3(const E<short>&);
void j4(E<short>*);
void j5(const E<short>*);
void j6(E<short>**);
void j7(const E<short>**);
void j8(const E<short>* const*);
Loading

0 comments on commit 072f43c

Please sign in to comment.