Skip to content

Commit

Permalink
[clang][ASTImporter] Add import of a few type related nodes
Browse files Browse the repository at this point in the history
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
  • Loading branch information
danix800 committed Aug 31, 2023
1 parent 0a3519d commit e7bd436
Show file tree
Hide file tree
Showing 3 changed files with 231 additions and 52 deletions.
177 changes: 125 additions & 52 deletions clang/lib/AST/ASTImporter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -368,58 +368,9 @@ namespace clang {

// Importing types
ExpectedType VisitType(const Type *T);
ExpectedType VisitAtomicType(const AtomicType *T);
ExpectedType VisitBuiltinType(const BuiltinType *T);
ExpectedType VisitDecayedType(const DecayedType *T);
ExpectedType VisitComplexType(const ComplexType *T);
ExpectedType VisitPointerType(const PointerType *T);
ExpectedType VisitBlockPointerType(const BlockPointerType *T);
ExpectedType VisitLValueReferenceType(const LValueReferenceType *T);
ExpectedType VisitRValueReferenceType(const RValueReferenceType *T);
ExpectedType VisitMemberPointerType(const MemberPointerType *T);
ExpectedType VisitConstantArrayType(const ConstantArrayType *T);
ExpectedType VisitIncompleteArrayType(const IncompleteArrayType *T);
ExpectedType VisitVariableArrayType(const VariableArrayType *T);
ExpectedType VisitDependentSizedArrayType(const DependentSizedArrayType *T);
ExpectedType
VisitDependentSizedExtVectorType(const DependentSizedExtVectorType *T);
ExpectedType VisitVectorType(const VectorType *T);
ExpectedType VisitExtVectorType(const ExtVectorType *T);
ExpectedType VisitFunctionNoProtoType(const FunctionNoProtoType *T);
ExpectedType VisitFunctionProtoType(const FunctionProtoType *T);
ExpectedType VisitUnresolvedUsingType(const UnresolvedUsingType *T);
ExpectedType VisitParenType(const ParenType *T);
ExpectedType VisitTypedefType(const TypedefType *T);
ExpectedType VisitTypeOfExprType(const TypeOfExprType *T);
// FIXME: DependentTypeOfExprType
ExpectedType VisitTypeOfType(const TypeOfType *T);
ExpectedType VisitUsingType(const UsingType *T);
ExpectedType VisitDecltypeType(const DecltypeType *T);
ExpectedType VisitUnaryTransformType(const UnaryTransformType *T);
ExpectedType VisitAutoType(const AutoType *T);
ExpectedType VisitDeducedTemplateSpecializationType(
const DeducedTemplateSpecializationType *T);
ExpectedType VisitInjectedClassNameType(const InjectedClassNameType *T);
// FIXME: DependentDecltypeType
ExpectedType VisitRecordType(const RecordType *T);
ExpectedType VisitEnumType(const EnumType *T);
ExpectedType VisitAttributedType(const AttributedType *T);
ExpectedType VisitTemplateTypeParmType(const TemplateTypeParmType *T);
ExpectedType VisitSubstTemplateTypeParmType(
const SubstTemplateTypeParmType *T);
ExpectedType
VisitSubstTemplateTypeParmPackType(const SubstTemplateTypeParmPackType *T);
ExpectedType VisitTemplateSpecializationType(
const TemplateSpecializationType *T);
ExpectedType VisitElaboratedType(const ElaboratedType *T);
ExpectedType VisitDependentNameType(const DependentNameType *T);
ExpectedType VisitPackExpansionType(const PackExpansionType *T);
ExpectedType VisitDependentTemplateSpecializationType(
const DependentTemplateSpecializationType *T);
ExpectedType VisitObjCInterfaceType(const ObjCInterfaceType *T);
ExpectedType VisitObjCObjectType(const ObjCObjectType *T);
ExpectedType VisitObjCObjectPointerType(const ObjCObjectPointerType *T);
ExpectedType VisitMacroQualifiedType(const MacroQualifiedType *T);
#define TYPE(Class, Base) \
ExpectedType Visit##Class##Type(const Class##Type *T);
#include "clang/AST/TypeNodes.inc"

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

ExpectedType clang::ASTNodeImporter::VisitAdjustedType(const AdjustedType *T) {
Error Err = Error::success();
QualType ToOriginalType = importChecked(Err, T->getOriginalType());
QualType ToAdjustedType = importChecked(Err, T->getAdjustedType());
if (Err)
return std::move(Err);

return Importer.getToContext().getAdjustedType(ToOriginalType,
ToAdjustedType);
}

ExpectedType clang::ASTNodeImporter::VisitBitIntType(const BitIntType *T) {
return Importer.getToContext().getBitIntType(T->isUnsigned(),
T->getNumBits());
}

ExpectedType clang::ASTNodeImporter::VisitBTFTagAttributedType(
const clang::BTFTagAttributedType *T) {
Error Err = Error::success();
const BTFTypeTagAttr *ToBTFAttr = importChecked(Err, T->getAttr());
QualType ToWrappedType = importChecked(Err, T->getWrappedType());
if (Err)
return std::move(Err);

return Importer.getToContext().getBTFTagAttributedType(ToBTFAttr,
ToWrappedType);
}

ExpectedType clang::ASTNodeImporter::VisitConstantMatrixType(
const clang::ConstantMatrixType *T) {
ExpectedType ToElementTypeOrErr = import(T->getElementType());
if (!ToElementTypeOrErr)
return ToElementTypeOrErr.takeError();

return Importer.getToContext().getConstantMatrixType(
*ToElementTypeOrErr, T->getNumRows(), T->getNumColumns());
}

ExpectedType clang::ASTNodeImporter::VisitDependentAddressSpaceType(
const clang::DependentAddressSpaceType *T) {
Error Err = Error::success();
QualType ToPointeeType = importChecked(Err, T->getPointeeType());
Expr *ToAddrSpaceExpr = importChecked(Err, T->getAddrSpaceExpr());
SourceLocation ToAttrLoc = importChecked(Err, T->getAttributeLoc());
if (Err)
return std::move(Err);

return Importer.getToContext().getDependentAddressSpaceType(
ToPointeeType, ToAddrSpaceExpr, ToAttrLoc);
}

ExpectedType clang::ASTNodeImporter::VisitDependentBitIntType(
const clang::DependentBitIntType *T) {
ExpectedExpr ToNumBitsExprOrErr = import(T->getNumBitsExpr());
if (!ToNumBitsExprOrErr)
return ToNumBitsExprOrErr.takeError();
return Importer.getToContext().getDependentBitIntType(T->isUnsigned(),
*ToNumBitsExprOrErr);
}

ExpectedType clang::ASTNodeImporter::VisitDependentSizedMatrixType(
const clang::DependentSizedMatrixType *T) {
Error Err = Error::success();
QualType ToElementType = importChecked(Err, T->getElementType());
Expr *ToRowExpr = importChecked(Err, T->getRowExpr());
Expr *ToColumnExpr = importChecked(Err, T->getColumnExpr());
SourceLocation ToAttrLoc = importChecked(Err, T->getAttributeLoc());
if (Err)
return std::move(Err);

return Importer.getToContext().getDependentSizedMatrixType(
ToElementType, ToRowExpr, ToColumnExpr, ToAttrLoc);
}

ExpectedType clang::ASTNodeImporter::VisitDependentVectorType(
const clang::DependentVectorType *T) {
Error Err = Error::success();
QualType ToElementType = importChecked(Err, T->getElementType());
Expr *ToSizeExpr = importChecked(Err, T->getSizeExpr());
SourceLocation ToAttrLoc = importChecked(Err, T->getAttributeLoc());
if (Err)
return std::move(Err);

return Importer.getToContext().getDependentVectorType(
ToElementType, ToSizeExpr, ToAttrLoc, T->getVectorKind());
}

ExpectedType clang::ASTNodeImporter::VisitObjCTypeParamType(
const clang::ObjCTypeParamType *T) {
Expected<ObjCTypeParamDecl *> ToDeclOrErr = import(T->getDecl());
if (!ToDeclOrErr)
return ToDeclOrErr.takeError();

SmallVector<ObjCProtocolDecl *, 4> ToProtocols;
for (ObjCProtocolDecl *FromProtocol : T->getProtocols()) {
Expected<ObjCProtocolDecl *> ToProtocolOrErr = import(FromProtocol);
if (!ToProtocolOrErr)
return ToProtocolOrErr.takeError();
ToProtocols.push_back(*ToProtocolOrErr);
}

return Importer.getToContext().getObjCTypeParamType(*ToDeclOrErr,
ToProtocols);
}

ExpectedType clang::ASTNodeImporter::VisitPipeType(const clang::PipeType *T) {
ExpectedType ToElementTypeOrErr = import(T->getElementType());
if (!ToElementTypeOrErr)
return ToElementTypeOrErr.takeError();

ASTContext &ToCtx = Importer.getToContext();
if (T->isReadOnly())
return ToCtx.getReadPipeType(*ToElementTypeOrErr);
else
return ToCtx.getWritePipeType(*ToElementTypeOrErr);
}

//----------------------------------------------------------------------------
// Import Declarations
//----------------------------------------------------------------------------
Expand Down Expand Up @@ -4681,6 +4749,11 @@ ExpectedDecl ASTNodeImporter::VisitObjCTypeParamDecl(ObjCTypeParamDecl *D) {
ToColonLoc, ToTypeSourceInfo))
return Result;

// Only import 'ObjCTypeParamType' after the decl is created.
auto ToTypeForDecl = importChecked(Err, D->getTypeForDecl());
if (Err)
return std::move(Err);
Result->setTypeForDecl(ToTypeForDecl);
Result->setLexicalDeclContext(LexicalDC);
return Result;
}
Expand Down
16 changes: 16 additions & 0 deletions clang/unittests/AST/ASTImporterObjCTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,22 @@ TEST_P(ImportObjCDecl, ObjPropertyNameConflict) {
}
}

TEST_P(ImportObjCDecl, ImportObjCTypeParamDecl) {
Decl *FromTU = getTuDecl(
R"(
@interface X <FirstParam: id>
@end
)",
Lang_OBJCXX, "input.mm");
auto *FromInterfaceDecl = FirstDeclMatcher<ObjCInterfaceDecl>().match(
FromTU, namedDecl(hasName("X")));
auto *FromTypeParamDecl =
FromInterfaceDecl->getTypeParamListAsWritten()->front();

auto *ToTypeParamDeclImported = Import(FromTypeParamDecl, Lang_OBJCXX);
ASSERT_TRUE(ToTypeParamDeclImported);
}

static const auto ObjCTestArrayForRunOptions =
std::array<std::vector<std::string>, 2>{
{std::vector<std::string>{"-fno-objc-arc"},
Expand Down
90 changes: 90 additions & 0 deletions clang/unittests/AST/ASTImporterTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -583,6 +583,96 @@ TEST_P(ImportType, ImportAtomicType) {
functionDecl(hasDescendant(typedefDecl(has(atomicType())))));
}

TEST_P(ImportType, ImportBitIntType) {
const AstTypeMatcher<BitIntType> bitIntType;
MatchVerifier<Decl> Verifier;
testImport("_BitInt(10) declToImport;", Lang_CXX11, "", Lang_CXX11, Verifier,
varDecl(hasType(bitIntType())));
}

TEST_P(ImportType, ImportDependentBitIntType) {
const AstTypeMatcher<DependentBitIntType> dependentBitIntType;
MatchVerifier<Decl> Verifier;
testImport("template<int Width> using declToImport = _BitInt(Width);",
Lang_CXX11, "", Lang_CXX11, Verifier,
typeAliasTemplateDecl(
has(typeAliasDecl(hasType(dependentBitIntType())))));
}

TEST_P(ImportType, ImportDependentAddressSpaceType) {
const AstTypeMatcher<DependentAddressSpaceType> dependentAddressSpaceType;
MatchVerifier<Decl> Verifier;
testImport(
R"(
template<typename T, int AddrSpace>
using declToImport = T __attribute__((address_space(AddrSpace)));
)",
Lang_CXX11, "", Lang_CXX11, Verifier,
typeAliasTemplateDecl(
has(typeAliasDecl(hasType(dependentAddressSpaceType())))));
}

TEST_P(ImportType, ImportVectorType) {
const AstTypeMatcher<VectorType> vectorType;
MatchVerifier<Decl> Verifier;
testImport("typedef int __attribute__((vector_size(12))) declToImport;",
Lang_CXX11, "", Lang_CXX11, Verifier,
typedefDecl(hasType(vectorType())));
}

TEST_P(ImportType, ImportDependentVectorType) {
const AstTypeMatcher<DependentVectorType> dependentVectorType;
MatchVerifier<Decl> Verifier;
testImport(
R"(
template<typename T, int Size>
using declToImport = T __attribute__((vector_size(Size)));
)",
Lang_CXX11, "", Lang_CXX11, Verifier,
typeAliasTemplateDecl(
has(typeAliasDecl(hasType(dependentVectorType())))));
}

struct ImportOpenCLPipe : ImportType {
std::vector<std::string> getExtraArgs() const override {
return {"-x", "cl", "-cl-no-stdinc", "-cl-std=CL2.0"};
}
};

TEST_P(ImportOpenCLPipe, ImportPipeType) {
const AstTypeMatcher<PipeType> pipeType;
MatchVerifier<Decl> Verifier;
testImport("typedef pipe int declToImport;", Lang_OpenCL, "", Lang_OpenCL,
Verifier, typedefDecl(hasType(pipeType())));
}

struct ImportMatrixType : ImportType {
std::vector<std::string> getExtraArgs() const override {
return {"-fenable-matrix"};
}
};

TEST_P(ImportMatrixType, ImportConstantMatrixType) {
const AstTypeMatcher<ConstantMatrixType> constantMatrixType;
MatchVerifier<Decl> Verifier;
testImport("typedef int __attribute__((matrix_type(5, 5))) declToImport;",
Lang_CXX11, "", Lang_CXX11, Verifier,
typedefDecl(hasType(constantMatrixType())));
}

TEST_P(ImportMatrixType, ImportDependentSizedMatrixType) {
const AstTypeMatcher<DependentSizedMatrixType> dependentSizedMatrixType;
MatchVerifier<Decl> Verifier;
testImport(
R"(
template<typename T, int Rows, int Cols>
using declToImport = T __attribute__((matrix_type(Rows, Cols)));
)",
Lang_CXX11, "", Lang_CXX11, Verifier,
typeAliasTemplateDecl(
has(typeAliasDecl(hasType(dependentSizedMatrixType())))));
}

TEST_P(ImportType, ImportUsingType) {
MatchVerifier<Decl> Verifier;
testImport("struct C {};"
Expand Down

0 comments on commit e7bd436

Please sign in to comment.