Skip to content

Commit e7bd436

Browse files
committed
[clang][ASTImporter] Add import of a few type related nodes
Add import of type-related nodes: - bitIntType - constantMatrixType - dependentAddressSpaceType - dependentBitIntType - dependentSizedMatrixType - dependentVectorType - objcTypeParamDecl - objcTypeParamType - pipeType - vectorType Reviewed By: balazske Differential Revision: https://reviews.llvm.org/D158948
1 parent 0a3519d commit e7bd436

File tree

3 files changed

+231
-52
lines changed

3 files changed

+231
-52
lines changed

clang/lib/AST/ASTImporter.cpp

+125-52
Original file line numberDiff line numberDiff line change
@@ -368,58 +368,9 @@ namespace clang {
368368

369369
// Importing types
370370
ExpectedType VisitType(const Type *T);
371-
ExpectedType VisitAtomicType(const AtomicType *T);
372-
ExpectedType VisitBuiltinType(const BuiltinType *T);
373-
ExpectedType VisitDecayedType(const DecayedType *T);
374-
ExpectedType VisitComplexType(const ComplexType *T);
375-
ExpectedType VisitPointerType(const PointerType *T);
376-
ExpectedType VisitBlockPointerType(const BlockPointerType *T);
377-
ExpectedType VisitLValueReferenceType(const LValueReferenceType *T);
378-
ExpectedType VisitRValueReferenceType(const RValueReferenceType *T);
379-
ExpectedType VisitMemberPointerType(const MemberPointerType *T);
380-
ExpectedType VisitConstantArrayType(const ConstantArrayType *T);
381-
ExpectedType VisitIncompleteArrayType(const IncompleteArrayType *T);
382-
ExpectedType VisitVariableArrayType(const VariableArrayType *T);
383-
ExpectedType VisitDependentSizedArrayType(const DependentSizedArrayType *T);
384-
ExpectedType
385-
VisitDependentSizedExtVectorType(const DependentSizedExtVectorType *T);
386-
ExpectedType VisitVectorType(const VectorType *T);
387-
ExpectedType VisitExtVectorType(const ExtVectorType *T);
388-
ExpectedType VisitFunctionNoProtoType(const FunctionNoProtoType *T);
389-
ExpectedType VisitFunctionProtoType(const FunctionProtoType *T);
390-
ExpectedType VisitUnresolvedUsingType(const UnresolvedUsingType *T);
391-
ExpectedType VisitParenType(const ParenType *T);
392-
ExpectedType VisitTypedefType(const TypedefType *T);
393-
ExpectedType VisitTypeOfExprType(const TypeOfExprType *T);
394-
// FIXME: DependentTypeOfExprType
395-
ExpectedType VisitTypeOfType(const TypeOfType *T);
396-
ExpectedType VisitUsingType(const UsingType *T);
397-
ExpectedType VisitDecltypeType(const DecltypeType *T);
398-
ExpectedType VisitUnaryTransformType(const UnaryTransformType *T);
399-
ExpectedType VisitAutoType(const AutoType *T);
400-
ExpectedType VisitDeducedTemplateSpecializationType(
401-
const DeducedTemplateSpecializationType *T);
402-
ExpectedType VisitInjectedClassNameType(const InjectedClassNameType *T);
403-
// FIXME: DependentDecltypeType
404-
ExpectedType VisitRecordType(const RecordType *T);
405-
ExpectedType VisitEnumType(const EnumType *T);
406-
ExpectedType VisitAttributedType(const AttributedType *T);
407-
ExpectedType VisitTemplateTypeParmType(const TemplateTypeParmType *T);
408-
ExpectedType VisitSubstTemplateTypeParmType(
409-
const SubstTemplateTypeParmType *T);
410-
ExpectedType
411-
VisitSubstTemplateTypeParmPackType(const SubstTemplateTypeParmPackType *T);
412-
ExpectedType VisitTemplateSpecializationType(
413-
const TemplateSpecializationType *T);
414-
ExpectedType VisitElaboratedType(const ElaboratedType *T);
415-
ExpectedType VisitDependentNameType(const DependentNameType *T);
416-
ExpectedType VisitPackExpansionType(const PackExpansionType *T);
417-
ExpectedType VisitDependentTemplateSpecializationType(
418-
const DependentTemplateSpecializationType *T);
419-
ExpectedType VisitObjCInterfaceType(const ObjCInterfaceType *T);
420-
ExpectedType VisitObjCObjectType(const ObjCObjectType *T);
421-
ExpectedType VisitObjCObjectPointerType(const ObjCObjectPointerType *T);
422-
ExpectedType VisitMacroQualifiedType(const MacroQualifiedType *T);
371+
#define TYPE(Class, Base) \
372+
ExpectedType Visit##Class##Type(const Class##Type *T);
373+
#include "clang/AST/TypeNodes.inc"
423374

424375
// Importing declarations
425376
Error ImportDeclParts(NamedDecl *D, DeclarationName &Name, NamedDecl *&ToD,
@@ -1741,6 +1692,123 @@ ASTNodeImporter::VisitMacroQualifiedType(const MacroQualifiedType *T) {
17411692
ToIdentifier);
17421693
}
17431694

1695+
ExpectedType clang::ASTNodeImporter::VisitAdjustedType(const AdjustedType *T) {
1696+
Error Err = Error::success();
1697+
QualType ToOriginalType = importChecked(Err, T->getOriginalType());
1698+
QualType ToAdjustedType = importChecked(Err, T->getAdjustedType());
1699+
if (Err)
1700+
return std::move(Err);
1701+
1702+
return Importer.getToContext().getAdjustedType(ToOriginalType,
1703+
ToAdjustedType);
1704+
}
1705+
1706+
ExpectedType clang::ASTNodeImporter::VisitBitIntType(const BitIntType *T) {
1707+
return Importer.getToContext().getBitIntType(T->isUnsigned(),
1708+
T->getNumBits());
1709+
}
1710+
1711+
ExpectedType clang::ASTNodeImporter::VisitBTFTagAttributedType(
1712+
const clang::BTFTagAttributedType *T) {
1713+
Error Err = Error::success();
1714+
const BTFTypeTagAttr *ToBTFAttr = importChecked(Err, T->getAttr());
1715+
QualType ToWrappedType = importChecked(Err, T->getWrappedType());
1716+
if (Err)
1717+
return std::move(Err);
1718+
1719+
return Importer.getToContext().getBTFTagAttributedType(ToBTFAttr,
1720+
ToWrappedType);
1721+
}
1722+
1723+
ExpectedType clang::ASTNodeImporter::VisitConstantMatrixType(
1724+
const clang::ConstantMatrixType *T) {
1725+
ExpectedType ToElementTypeOrErr = import(T->getElementType());
1726+
if (!ToElementTypeOrErr)
1727+
return ToElementTypeOrErr.takeError();
1728+
1729+
return Importer.getToContext().getConstantMatrixType(
1730+
*ToElementTypeOrErr, T->getNumRows(), T->getNumColumns());
1731+
}
1732+
1733+
ExpectedType clang::ASTNodeImporter::VisitDependentAddressSpaceType(
1734+
const clang::DependentAddressSpaceType *T) {
1735+
Error Err = Error::success();
1736+
QualType ToPointeeType = importChecked(Err, T->getPointeeType());
1737+
Expr *ToAddrSpaceExpr = importChecked(Err, T->getAddrSpaceExpr());
1738+
SourceLocation ToAttrLoc = importChecked(Err, T->getAttributeLoc());
1739+
if (Err)
1740+
return std::move(Err);
1741+
1742+
return Importer.getToContext().getDependentAddressSpaceType(
1743+
ToPointeeType, ToAddrSpaceExpr, ToAttrLoc);
1744+
}
1745+
1746+
ExpectedType clang::ASTNodeImporter::VisitDependentBitIntType(
1747+
const clang::DependentBitIntType *T) {
1748+
ExpectedExpr ToNumBitsExprOrErr = import(T->getNumBitsExpr());
1749+
if (!ToNumBitsExprOrErr)
1750+
return ToNumBitsExprOrErr.takeError();
1751+
return Importer.getToContext().getDependentBitIntType(T->isUnsigned(),
1752+
*ToNumBitsExprOrErr);
1753+
}
1754+
1755+
ExpectedType clang::ASTNodeImporter::VisitDependentSizedMatrixType(
1756+
const clang::DependentSizedMatrixType *T) {
1757+
Error Err = Error::success();
1758+
QualType ToElementType = importChecked(Err, T->getElementType());
1759+
Expr *ToRowExpr = importChecked(Err, T->getRowExpr());
1760+
Expr *ToColumnExpr = importChecked(Err, T->getColumnExpr());
1761+
SourceLocation ToAttrLoc = importChecked(Err, T->getAttributeLoc());
1762+
if (Err)
1763+
return std::move(Err);
1764+
1765+
return Importer.getToContext().getDependentSizedMatrixType(
1766+
ToElementType, ToRowExpr, ToColumnExpr, ToAttrLoc);
1767+
}
1768+
1769+
ExpectedType clang::ASTNodeImporter::VisitDependentVectorType(
1770+
const clang::DependentVectorType *T) {
1771+
Error Err = Error::success();
1772+
QualType ToElementType = importChecked(Err, T->getElementType());
1773+
Expr *ToSizeExpr = importChecked(Err, T->getSizeExpr());
1774+
SourceLocation ToAttrLoc = importChecked(Err, T->getAttributeLoc());
1775+
if (Err)
1776+
return std::move(Err);
1777+
1778+
return Importer.getToContext().getDependentVectorType(
1779+
ToElementType, ToSizeExpr, ToAttrLoc, T->getVectorKind());
1780+
}
1781+
1782+
ExpectedType clang::ASTNodeImporter::VisitObjCTypeParamType(
1783+
const clang::ObjCTypeParamType *T) {
1784+
Expected<ObjCTypeParamDecl *> ToDeclOrErr = import(T->getDecl());
1785+
if (!ToDeclOrErr)
1786+
return ToDeclOrErr.takeError();
1787+
1788+
SmallVector<ObjCProtocolDecl *, 4> ToProtocols;
1789+
for (ObjCProtocolDecl *FromProtocol : T->getProtocols()) {
1790+
Expected<ObjCProtocolDecl *> ToProtocolOrErr = import(FromProtocol);
1791+
if (!ToProtocolOrErr)
1792+
return ToProtocolOrErr.takeError();
1793+
ToProtocols.push_back(*ToProtocolOrErr);
1794+
}
1795+
1796+
return Importer.getToContext().getObjCTypeParamType(*ToDeclOrErr,
1797+
ToProtocols);
1798+
}
1799+
1800+
ExpectedType clang::ASTNodeImporter::VisitPipeType(const clang::PipeType *T) {
1801+
ExpectedType ToElementTypeOrErr = import(T->getElementType());
1802+
if (!ToElementTypeOrErr)
1803+
return ToElementTypeOrErr.takeError();
1804+
1805+
ASTContext &ToCtx = Importer.getToContext();
1806+
if (T->isReadOnly())
1807+
return ToCtx.getReadPipeType(*ToElementTypeOrErr);
1808+
else
1809+
return ToCtx.getWritePipeType(*ToElementTypeOrErr);
1810+
}
1811+
17441812
//----------------------------------------------------------------------------
17451813
// Import Declarations
17461814
//----------------------------------------------------------------------------
@@ -4681,6 +4749,11 @@ ExpectedDecl ASTNodeImporter::VisitObjCTypeParamDecl(ObjCTypeParamDecl *D) {
46814749
ToColonLoc, ToTypeSourceInfo))
46824750
return Result;
46834751

4752+
// Only import 'ObjCTypeParamType' after the decl is created.
4753+
auto ToTypeForDecl = importChecked(Err, D->getTypeForDecl());
4754+
if (Err)
4755+
return std::move(Err);
4756+
Result->setTypeForDecl(ToTypeForDecl);
46844757
Result->setLexicalDeclContext(LexicalDC);
46854758
return Result;
46864759
}

clang/unittests/AST/ASTImporterObjCTest.cpp

+16
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,22 @@ TEST_P(ImportObjCDecl, ObjPropertyNameConflict) {
7777
}
7878
}
7979

80+
TEST_P(ImportObjCDecl, ImportObjCTypeParamDecl) {
81+
Decl *FromTU = getTuDecl(
82+
R"(
83+
@interface X <FirstParam: id>
84+
@end
85+
)",
86+
Lang_OBJCXX, "input.mm");
87+
auto *FromInterfaceDecl = FirstDeclMatcher<ObjCInterfaceDecl>().match(
88+
FromTU, namedDecl(hasName("X")));
89+
auto *FromTypeParamDecl =
90+
FromInterfaceDecl->getTypeParamListAsWritten()->front();
91+
92+
auto *ToTypeParamDeclImported = Import(FromTypeParamDecl, Lang_OBJCXX);
93+
ASSERT_TRUE(ToTypeParamDeclImported);
94+
}
95+
8096
static const auto ObjCTestArrayForRunOptions =
8197
std::array<std::vector<std::string>, 2>{
8298
{std::vector<std::string>{"-fno-objc-arc"},

clang/unittests/AST/ASTImporterTest.cpp

+90
Original file line numberDiff line numberDiff line change
@@ -583,6 +583,96 @@ TEST_P(ImportType, ImportAtomicType) {
583583
functionDecl(hasDescendant(typedefDecl(has(atomicType())))));
584584
}
585585

586+
TEST_P(ImportType, ImportBitIntType) {
587+
const AstTypeMatcher<BitIntType> bitIntType;
588+
MatchVerifier<Decl> Verifier;
589+
testImport("_BitInt(10) declToImport;", Lang_CXX11, "", Lang_CXX11, Verifier,
590+
varDecl(hasType(bitIntType())));
591+
}
592+
593+
TEST_P(ImportType, ImportDependentBitIntType) {
594+
const AstTypeMatcher<DependentBitIntType> dependentBitIntType;
595+
MatchVerifier<Decl> Verifier;
596+
testImport("template<int Width> using declToImport = _BitInt(Width);",
597+
Lang_CXX11, "", Lang_CXX11, Verifier,
598+
typeAliasTemplateDecl(
599+
has(typeAliasDecl(hasType(dependentBitIntType())))));
600+
}
601+
602+
TEST_P(ImportType, ImportDependentAddressSpaceType) {
603+
const AstTypeMatcher<DependentAddressSpaceType> dependentAddressSpaceType;
604+
MatchVerifier<Decl> Verifier;
605+
testImport(
606+
R"(
607+
template<typename T, int AddrSpace>
608+
using declToImport = T __attribute__((address_space(AddrSpace)));
609+
)",
610+
Lang_CXX11, "", Lang_CXX11, Verifier,
611+
typeAliasTemplateDecl(
612+
has(typeAliasDecl(hasType(dependentAddressSpaceType())))));
613+
}
614+
615+
TEST_P(ImportType, ImportVectorType) {
616+
const AstTypeMatcher<VectorType> vectorType;
617+
MatchVerifier<Decl> Verifier;
618+
testImport("typedef int __attribute__((vector_size(12))) declToImport;",
619+
Lang_CXX11, "", Lang_CXX11, Verifier,
620+
typedefDecl(hasType(vectorType())));
621+
}
622+
623+
TEST_P(ImportType, ImportDependentVectorType) {
624+
const AstTypeMatcher<DependentVectorType> dependentVectorType;
625+
MatchVerifier<Decl> Verifier;
626+
testImport(
627+
R"(
628+
template<typename T, int Size>
629+
using declToImport = T __attribute__((vector_size(Size)));
630+
)",
631+
Lang_CXX11, "", Lang_CXX11, Verifier,
632+
typeAliasTemplateDecl(
633+
has(typeAliasDecl(hasType(dependentVectorType())))));
634+
}
635+
636+
struct ImportOpenCLPipe : ImportType {
637+
std::vector<std::string> getExtraArgs() const override {
638+
return {"-x", "cl", "-cl-no-stdinc", "-cl-std=CL2.0"};
639+
}
640+
};
641+
642+
TEST_P(ImportOpenCLPipe, ImportPipeType) {
643+
const AstTypeMatcher<PipeType> pipeType;
644+
MatchVerifier<Decl> Verifier;
645+
testImport("typedef pipe int declToImport;", Lang_OpenCL, "", Lang_OpenCL,
646+
Verifier, typedefDecl(hasType(pipeType())));
647+
}
648+
649+
struct ImportMatrixType : ImportType {
650+
std::vector<std::string> getExtraArgs() const override {
651+
return {"-fenable-matrix"};
652+
}
653+
};
654+
655+
TEST_P(ImportMatrixType, ImportConstantMatrixType) {
656+
const AstTypeMatcher<ConstantMatrixType> constantMatrixType;
657+
MatchVerifier<Decl> Verifier;
658+
testImport("typedef int __attribute__((matrix_type(5, 5))) declToImport;",
659+
Lang_CXX11, "", Lang_CXX11, Verifier,
660+
typedefDecl(hasType(constantMatrixType())));
661+
}
662+
663+
TEST_P(ImportMatrixType, ImportDependentSizedMatrixType) {
664+
const AstTypeMatcher<DependentSizedMatrixType> dependentSizedMatrixType;
665+
MatchVerifier<Decl> Verifier;
666+
testImport(
667+
R"(
668+
template<typename T, int Rows, int Cols>
669+
using declToImport = T __attribute__((matrix_type(Rows, Cols)));
670+
)",
671+
Lang_CXX11, "", Lang_CXX11, Verifier,
672+
typeAliasTemplateDecl(
673+
has(typeAliasDecl(hasType(dependentSizedMatrixType())))));
674+
}
675+
586676
TEST_P(ImportType, ImportUsingType) {
587677
MatchVerifier<Decl> Verifier;
588678
testImport("struct C {};"

0 commit comments

Comments
 (0)