HELLO·Android
系统源代码
IT资讯
技术文章
我的收藏
注册
登录
-
我收藏的文章
创建代码块
我的代码块
我的账号
Oreo
|
8.0.0_r4
下载
查看原文件
收藏
根目录
external
clang
lib
AST
ASTImporter.cpp
//===--- ASTImporter.cpp - Importing ASTs from other Contexts ---*- C++ -*-===// // // The LLVM Compiler Infrastructure // // This file is distributed under the University of Illinois Open Source // License. See LICENSE.TXT for details. // //===----------------------------------------------------------------------===// // // This file defines the ASTImporter class which imports AST nodes from one // context into another context. // //===----------------------------------------------------------------------===// #include "clang/AST/ASTImporter.h" #include "clang/AST/ASTContext.h" #include "clang/AST/ASTDiagnostic.h" #include "clang/AST/DeclCXX.h" #include "clang/AST/DeclObjC.h" #include "clang/AST/DeclVisitor.h" #include "clang/AST/StmtVisitor.h" #include "clang/AST/TypeVisitor.h" #include "clang/Basic/FileManager.h" #include "clang/Basic/SourceManager.h" #include "llvm/Support/MemoryBuffer.h" #include
namespace clang { class ASTNodeImporter : public TypeVisitor
, public DeclVisitor
, public StmtVisitor
{ ASTImporter &Importer; public: explicit ASTNodeImporter(ASTImporter &Importer) : Importer(Importer) { } using TypeVisitor
::Visit; using DeclVisitor
::Visit; using StmtVisitor
::Visit; // Importing types QualType VisitType(const Type *T); QualType VisitBuiltinType(const BuiltinType *T); QualType VisitComplexType(const ComplexType *T); QualType VisitPointerType(const PointerType *T); QualType VisitBlockPointerType(const BlockPointerType *T); QualType VisitLValueReferenceType(const LValueReferenceType *T); QualType VisitRValueReferenceType(const RValueReferenceType *T); QualType VisitMemberPointerType(const MemberPointerType *T); QualType VisitConstantArrayType(const ConstantArrayType *T); QualType VisitIncompleteArrayType(const IncompleteArrayType *T); QualType VisitVariableArrayType(const VariableArrayType *T); // FIXME: DependentSizedArrayType // FIXME: DependentSizedExtVectorType QualType VisitVectorType(const VectorType *T); QualType VisitExtVectorType(const ExtVectorType *T); QualType VisitFunctionNoProtoType(const FunctionNoProtoType *T); QualType VisitFunctionProtoType(const FunctionProtoType *T); // FIXME: UnresolvedUsingType QualType VisitParenType(const ParenType *T); QualType VisitTypedefType(const TypedefType *T); QualType VisitTypeOfExprType(const TypeOfExprType *T); // FIXME: DependentTypeOfExprType QualType VisitTypeOfType(const TypeOfType *T); QualType VisitDecltypeType(const DecltypeType *T); QualType VisitUnaryTransformType(const UnaryTransformType *T); QualType VisitAutoType(const AutoType *T); QualType VisitInjectedClassNameType(const InjectedClassNameType *T); // FIXME: DependentDecltypeType QualType VisitRecordType(const RecordType *T); QualType VisitEnumType(const EnumType *T); QualType VisitAttributedType(const AttributedType *T); QualType VisitTemplateTypeParmType(const TemplateTypeParmType *T); // FIXME: SubstTemplateTypeParmType QualType VisitTemplateSpecializationType(const TemplateSpecializationType *T); QualType VisitElaboratedType(const ElaboratedType *T); // FIXME: DependentNameType // FIXME: DependentTemplateSpecializationType QualType VisitObjCInterfaceType(const ObjCInterfaceType *T); QualType VisitObjCObjectType(const ObjCObjectType *T); QualType VisitObjCObjectPointerType(const ObjCObjectPointerType *T); // Importing declarations bool ImportDeclParts(NamedDecl *D, DeclContext *&DC, DeclContext *&LexicalDC, DeclarationName &Name, NamedDecl *&ToD, SourceLocation &Loc); void ImportDefinitionIfNeeded(Decl *FromD, Decl *ToD = nullptr); void ImportDeclarationNameLoc(const DeclarationNameInfo &From, DeclarationNameInfo& To); void ImportDeclContext(DeclContext *FromDC, bool ForceImport = false); typedef DesignatedInitExpr::Designator Designator; Designator ImportDesignator(const Designator &D); /// \brief What we should import from the definition. enum ImportDefinitionKind { /// \brief Import the default subset of the definition, which might be /// nothing (if minimal import is set) or might be everything (if minimal /// import is not set). IDK_Default, /// \brief Import everything. IDK_Everything, /// \brief Import only the bare bones needed to establish a valid /// DeclContext. IDK_Basic }; bool shouldForceImportDeclContext(ImportDefinitionKind IDK) { return IDK == IDK_Everything || (IDK == IDK_Default && !Importer.isMinimalImport()); } bool ImportDefinition(RecordDecl *From, RecordDecl *To, ImportDefinitionKind Kind = IDK_Default); bool ImportDefinition(VarDecl *From, VarDecl *To, ImportDefinitionKind Kind = IDK_Default); bool ImportDefinition(EnumDecl *From, EnumDecl *To, ImportDefinitionKind Kind = IDK_Default); bool ImportDefinition(ObjCInterfaceDecl *From, ObjCInterfaceDecl *To, ImportDefinitionKind Kind = IDK_Default); bool ImportDefinition(ObjCProtocolDecl *From, ObjCProtocolDecl *To, ImportDefinitionKind Kind = IDK_Default); TemplateParameterList *ImportTemplateParameterList( TemplateParameterList *Params); TemplateArgument ImportTemplateArgument(const TemplateArgument &From); bool ImportTemplateArguments(const TemplateArgument *FromArgs, unsigned NumFromArgs, SmallVectorImpl
&ToArgs); bool IsStructuralMatch(RecordDecl *FromRecord, RecordDecl *ToRecord, bool Complain = true); bool IsStructuralMatch(VarDecl *FromVar, VarDecl *ToVar, bool Complain = true); bool IsStructuralMatch(EnumDecl *FromEnum, EnumDecl *ToRecord); bool IsStructuralMatch(EnumConstantDecl *FromEC, EnumConstantDecl *ToEC); bool IsStructuralMatch(ClassTemplateDecl *From, ClassTemplateDecl *To); bool IsStructuralMatch(VarTemplateDecl *From, VarTemplateDecl *To); Decl *VisitDecl(Decl *D); Decl *VisitAccessSpecDecl(AccessSpecDecl *D); Decl *VisitTranslationUnitDecl(TranslationUnitDecl *D); Decl *VisitNamespaceDecl(NamespaceDecl *D); Decl *VisitTypedefNameDecl(TypedefNameDecl *D, bool IsAlias); Decl *VisitTypedefDecl(TypedefDecl *D); Decl *VisitTypeAliasDecl(TypeAliasDecl *D); Decl *VisitLabelDecl(LabelDecl *D); Decl *VisitEnumDecl(EnumDecl *D); Decl *VisitRecordDecl(RecordDecl *D); Decl *VisitEnumConstantDecl(EnumConstantDecl *D); Decl *VisitFunctionDecl(FunctionDecl *D); Decl *VisitCXXMethodDecl(CXXMethodDecl *D); Decl *VisitCXXConstructorDecl(CXXConstructorDecl *D); Decl *VisitCXXDestructorDecl(CXXDestructorDecl *D); Decl *VisitCXXConversionDecl(CXXConversionDecl *D); Decl *VisitFieldDecl(FieldDecl *D); Decl *VisitIndirectFieldDecl(IndirectFieldDecl *D); Decl *VisitObjCIvarDecl(ObjCIvarDecl *D); Decl *VisitVarDecl(VarDecl *D); Decl *VisitImplicitParamDecl(ImplicitParamDecl *D); Decl *VisitParmVarDecl(ParmVarDecl *D); Decl *VisitObjCMethodDecl(ObjCMethodDecl *D); Decl *VisitObjCTypeParamDecl(ObjCTypeParamDecl *D); Decl *VisitObjCCategoryDecl(ObjCCategoryDecl *D); Decl *VisitObjCProtocolDecl(ObjCProtocolDecl *D); Decl *VisitLinkageSpecDecl(LinkageSpecDecl *D); ObjCTypeParamList *ImportObjCTypeParamList(ObjCTypeParamList *list); Decl *VisitObjCInterfaceDecl(ObjCInterfaceDecl *D); Decl *VisitObjCCategoryImplDecl(ObjCCategoryImplDecl *D); Decl *VisitObjCImplementationDecl(ObjCImplementationDecl *D); Decl *VisitObjCPropertyDecl(ObjCPropertyDecl *D); Decl *VisitObjCPropertyImplDecl(ObjCPropertyImplDecl *D); Decl *VisitTemplateTypeParmDecl(TemplateTypeParmDecl *D); Decl *VisitNonTypeTemplateParmDecl(NonTypeTemplateParmDecl *D); Decl *VisitTemplateTemplateParmDecl(TemplateTemplateParmDecl *D); Decl *VisitClassTemplateDecl(ClassTemplateDecl *D); Decl *VisitClassTemplateSpecializationDecl( ClassTemplateSpecializationDecl *D); Decl *VisitVarTemplateDecl(VarTemplateDecl *D); Decl *VisitVarTemplateSpecializationDecl(VarTemplateSpecializationDecl *D); // Importing statements DeclGroupRef ImportDeclGroup(DeclGroupRef DG); Stmt *VisitStmt(Stmt *S); Stmt *VisitGCCAsmStmt(GCCAsmStmt *S); Stmt *VisitDeclStmt(DeclStmt *S); Stmt *VisitNullStmt(NullStmt *S); Stmt *VisitCompoundStmt(CompoundStmt *S); Stmt *VisitCaseStmt(CaseStmt *S); Stmt *VisitDefaultStmt(DefaultStmt *S); Stmt *VisitLabelStmt(LabelStmt *S); Stmt *VisitAttributedStmt(AttributedStmt *S); Stmt *VisitIfStmt(IfStmt *S); Stmt *VisitSwitchStmt(SwitchStmt *S); Stmt *VisitWhileStmt(WhileStmt *S); Stmt *VisitDoStmt(DoStmt *S); Stmt *VisitForStmt(ForStmt *S); Stmt *VisitGotoStmt(GotoStmt *S); Stmt *VisitIndirectGotoStmt(IndirectGotoStmt *S); Stmt *VisitContinueStmt(ContinueStmt *S); Stmt *VisitBreakStmt(BreakStmt *S); Stmt *VisitReturnStmt(ReturnStmt *S); // FIXME: MSAsmStmt // FIXME: SEHExceptStmt // FIXME: SEHFinallyStmt // FIXME: SEHTryStmt // FIXME: SEHLeaveStmt // FIXME: CapturedStmt Stmt *VisitCXXCatchStmt(CXXCatchStmt *S); Stmt *VisitCXXTryStmt(CXXTryStmt *S); Stmt *VisitCXXForRangeStmt(CXXForRangeStmt *S); // FIXME: MSDependentExistsStmt Stmt *VisitObjCForCollectionStmt(ObjCForCollectionStmt *S); Stmt *VisitObjCAtCatchStmt(ObjCAtCatchStmt *S); Stmt *VisitObjCAtFinallyStmt(ObjCAtFinallyStmt *S); Stmt *VisitObjCAtTryStmt(ObjCAtTryStmt *S); Stmt *VisitObjCAtSynchronizedStmt(ObjCAtSynchronizedStmt *S); Stmt *VisitObjCAtThrowStmt(ObjCAtThrowStmt *S); Stmt *VisitObjCAutoreleasePoolStmt(ObjCAutoreleasePoolStmt *S); // Importing expressions Expr *VisitExpr(Expr *E); Expr *VisitVAArgExpr(VAArgExpr *E); Expr *VisitGNUNullExpr(GNUNullExpr *E); Expr *VisitPredefinedExpr(PredefinedExpr *E); Expr *VisitDeclRefExpr(DeclRefExpr *E); Expr *VisitImplicitValueInitExpr(ImplicitValueInitExpr *ILE); Expr *VisitDesignatedInitExpr(DesignatedInitExpr *E); Expr *VisitCXXNullPtrLiteralExpr(CXXNullPtrLiteralExpr *E); Expr *VisitIntegerLiteral(IntegerLiteral *E); Expr *VisitFloatingLiteral(FloatingLiteral *E); Expr *VisitCharacterLiteral(CharacterLiteral *E); Expr *VisitStringLiteral(StringLiteral *E); Expr *VisitCompoundLiteralExpr(CompoundLiteralExpr *E); Expr *VisitAtomicExpr(AtomicExpr *E); Expr *VisitAddrLabelExpr(AddrLabelExpr *E); Expr *VisitParenExpr(ParenExpr *E); Expr *VisitParenListExpr(ParenListExpr *E); Expr *VisitStmtExpr(StmtExpr *E); Expr *VisitUnaryOperator(UnaryOperator *E); Expr *VisitUnaryExprOrTypeTraitExpr(UnaryExprOrTypeTraitExpr *E); Expr *VisitBinaryOperator(BinaryOperator *E); Expr *VisitConditionalOperator(ConditionalOperator *E); Expr *VisitBinaryConditionalOperator(BinaryConditionalOperator *E); Expr *VisitOpaqueValueExpr(OpaqueValueExpr *E); Expr *VisitCompoundAssignOperator(CompoundAssignOperator *E); Expr *VisitImplicitCastExpr(ImplicitCastExpr *E); Expr *VisitCStyleCastExpr(CStyleCastExpr *E); Expr *VisitCXXConstructExpr(CXXConstructExpr *E); Expr *VisitCXXMemberCallExpr(CXXMemberCallExpr *E); Expr *VisitCXXThisExpr(CXXThisExpr *E); Expr *VisitCXXBoolLiteralExpr(CXXBoolLiteralExpr *E); Expr *VisitMemberExpr(MemberExpr *E); Expr *VisitCallExpr(CallExpr *E); Expr *VisitInitListExpr(InitListExpr *E); Expr *VisitCXXDefaultInitExpr(CXXDefaultInitExpr *E); Expr *VisitCXXNamedCastExpr(CXXNamedCastExpr *E); template
void ImportArray(IIter Ibegin, IIter Iend, OIter Obegin) { typedef typename std::remove_reference
::type ItemT; ASTImporter &ImporterRef = Importer; std::transform(Ibegin, Iend, Obegin, [&ImporterRef](ItemT From) -> ItemT { return ImporterRef.Import(From); }); } template
bool ImportArrayChecked(IIter Ibegin, IIter Iend, OIter Obegin) { typedef typename std::remove_reference
::type ItemT; ASTImporter &ImporterRef = Importer; bool Failed = false; std::transform(Ibegin, Iend, Obegin, [&ImporterRef, &Failed](ItemT *From) -> ItemT * { ItemT *To = ImporterRef.Import(From); if (!To && From) Failed = true; return To; }); return Failed; } }; } using namespace clang; //---------------------------------------------------------------------------- // Structural Equivalence //---------------------------------------------------------------------------- namespace { struct StructuralEquivalenceContext { /// \brief AST contexts for which we are checking structural equivalence. ASTContext &C1, &C2; /// \brief The set of "tentative" equivalences between two canonical /// declarations, mapping from a declaration in the first context to the /// declaration in the second context that we believe to be equivalent. llvm::DenseMap
TentativeEquivalences; /// \brief Queue of declarations in the first context whose equivalence /// with a declaration in the second context still needs to be verified. std::deque
DeclsToCheck; /// \brief Declaration (from, to) pairs that are known not to be equivalent /// (which we have already complained about). llvm::DenseSet
> &NonEquivalentDecls; /// \brief Whether we're being strict about the spelling of types when /// unifying two types. bool StrictTypeSpelling; /// \brief Whether to complain about failures. bool Complain; /// \brief \c true if the last diagnostic came from C2. bool LastDiagFromC2; StructuralEquivalenceContext(ASTContext &C1, ASTContext &C2, llvm::DenseSet
> &NonEquivalentDecls, bool StrictTypeSpelling = false, bool Complain = true) : C1(C1), C2(C2), NonEquivalentDecls(NonEquivalentDecls), StrictTypeSpelling(StrictTypeSpelling), Complain(Complain), LastDiagFromC2(false) {} /// \brief Determine whether the two declarations are structurally /// equivalent. bool IsStructurallyEquivalent(Decl *D1, Decl *D2); /// \brief Determine whether the two types are structurally equivalent. bool IsStructurallyEquivalent(QualType T1, QualType T2); private: /// \brief Finish checking all of the structural equivalences. /// /// \returns true if an error occurred, false otherwise. bool Finish(); public: DiagnosticBuilder Diag1(SourceLocation Loc, unsigned DiagID) { assert(Complain && "Not allowed to complain"); if (LastDiagFromC2) C1.getDiagnostics().notePriorDiagnosticFrom(C2.getDiagnostics()); LastDiagFromC2 = false; return C1.getDiagnostics().Report(Loc, DiagID); } DiagnosticBuilder Diag2(SourceLocation Loc, unsigned DiagID) { assert(Complain && "Not allowed to complain"); if (!LastDiagFromC2) C2.getDiagnostics().notePriorDiagnosticFrom(C1.getDiagnostics()); LastDiagFromC2 = true; return C2.getDiagnostics().Report(Loc, DiagID); } }; } static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context, QualType T1, QualType T2); static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context, Decl *D1, Decl *D2); /// \brief Determine structural equivalence of two expressions. static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context, Expr *E1, Expr *E2) { if (!E1 || !E2) return E1 == E2; // FIXME: Actually perform a structural comparison! return true; } /// \brief Determine whether two identifiers are equivalent. static bool IsStructurallyEquivalent(const IdentifierInfo *Name1, const IdentifierInfo *Name2) { if (!Name1 || !Name2) return Name1 == Name2; return Name1->getName() == Name2->getName(); } /// \brief Determine whether two nested-name-specifiers are equivalent. static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context, NestedNameSpecifier *NNS1, NestedNameSpecifier *NNS2) { // FIXME: Implement! return true; } /// \brief Determine whether two template arguments are equivalent. static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context, const TemplateArgument &Arg1, const TemplateArgument &Arg2) { if (Arg1.getKind() != Arg2.getKind()) return false; switch (Arg1.getKind()) { case TemplateArgument::Null: return true; case TemplateArgument::Type: return Context.IsStructurallyEquivalent(Arg1.getAsType(), Arg2.getAsType()); case TemplateArgument::Integral: if (!Context.IsStructurallyEquivalent(Arg1.getIntegralType(), Arg2.getIntegralType())) return false; return llvm::APSInt::isSameValue(Arg1.getAsIntegral(), Arg2.getAsIntegral()); case TemplateArgument::Declaration: return Context.IsStructurallyEquivalent(Arg1.getAsDecl(), Arg2.getAsDecl()); case TemplateArgument::NullPtr: return true; // FIXME: Is this correct? case TemplateArgument::Template: return IsStructurallyEquivalent(Context, Arg1.getAsTemplate(), Arg2.getAsTemplate()); case TemplateArgument::TemplateExpansion: return IsStructurallyEquivalent(Context, Arg1.getAsTemplateOrTemplatePattern(), Arg2.getAsTemplateOrTemplatePattern()); case TemplateArgument::Expression: return IsStructurallyEquivalent(Context, Arg1.getAsExpr(), Arg2.getAsExpr()); case TemplateArgument::Pack: if (Arg1.pack_size() != Arg2.pack_size()) return false; for (unsigned I = 0, N = Arg1.pack_size(); I != N; ++I) if (!IsStructurallyEquivalent(Context, Arg1.pack_begin()[I], Arg2.pack_begin()[I])) return false; return true; } llvm_unreachable("Invalid template argument kind"); } /// \brief Determine structural equivalence for the common part of array /// types. static bool IsArrayStructurallyEquivalent(StructuralEquivalenceContext &Context, const ArrayType *Array1, const ArrayType *Array2) { if (!IsStructurallyEquivalent(Context, Array1->getElementType(), Array2->getElementType())) return false; if (Array1->getSizeModifier() != Array2->getSizeModifier()) return false; if (Array1->getIndexTypeQualifiers() != Array2->getIndexTypeQualifiers()) return false; return true; } /// \brief Determine structural equivalence of two types. static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context, QualType T1, QualType T2) { if (T1.isNull() || T2.isNull()) return T1.isNull() && T2.isNull(); if (!Context.StrictTypeSpelling) { // We aren't being strict about token-to-token equivalence of types, // so map down to the canonical type. T1 = Context.C1.getCanonicalType(T1); T2 = Context.C2.getCanonicalType(T2); } if (T1.getQualifiers() != T2.getQualifiers()) return false; Type::TypeClass TC = T1->getTypeClass(); if (T1->getTypeClass() != T2->getTypeClass()) { // Compare function types with prototypes vs. without prototypes as if // both did not have prototypes. if (T1->getTypeClass() == Type::FunctionProto && T2->getTypeClass() == Type::FunctionNoProto) TC = Type::FunctionNoProto; else if (T1->getTypeClass() == Type::FunctionNoProto && T2->getTypeClass() == Type::FunctionProto) TC = Type::FunctionNoProto; else return false; } switch (TC) { case Type::Builtin: // FIXME: Deal with Char_S/Char_U. if (cast
(T1)->getKind() != cast
(T2)->getKind()) return false; break; case Type::Complex: if (!IsStructurallyEquivalent(Context, cast
(T1)->getElementType(), cast
(T2)->getElementType())) return false; break; case Type::Adjusted: case Type::Decayed: if (!IsStructurallyEquivalent(Context, cast
(T1)->getOriginalType(), cast
(T2)->getOriginalType())) return false; break; case Type::Pointer: if (!IsStructurallyEquivalent(Context, cast
(T1)->getPointeeType(), cast
(T2)->getPointeeType())) return false; break; case Type::BlockPointer: if (!IsStructurallyEquivalent(Context, cast
(T1)->getPointeeType(), cast
(T2)->getPointeeType())) return false; break; case Type::LValueReference: case Type::RValueReference: { const ReferenceType *Ref1 = cast
(T1); const ReferenceType *Ref2 = cast
(T2); if (Ref1->isSpelledAsLValue() != Ref2->isSpelledAsLValue()) return false; if (Ref1->isInnerRef() != Ref2->isInnerRef()) return false; if (!IsStructurallyEquivalent(Context, Ref1->getPointeeTypeAsWritten(), Ref2->getPointeeTypeAsWritten())) return false; break; } case Type::MemberPointer: { const MemberPointerType *MemPtr1 = cast
(T1); const MemberPointerType *MemPtr2 = cast
(T2); if (!IsStructurallyEquivalent(Context, MemPtr1->getPointeeType(), MemPtr2->getPointeeType())) return false; if (!IsStructurallyEquivalent(Context, QualType(MemPtr1->getClass(), 0), QualType(MemPtr2->getClass(), 0))) return false; break; } case Type::ConstantArray: { const ConstantArrayType *Array1 = cast
(T1); const ConstantArrayType *Array2 = cast
(T2); if (!llvm::APInt::isSameValue(Array1->getSize(), Array2->getSize())) return false; if (!IsArrayStructurallyEquivalent(Context, Array1, Array2)) return false; break; } case Type::IncompleteArray: if (!IsArrayStructurallyEquivalent(Context, cast
(T1), cast
(T2))) return false; break; case Type::VariableArray: { const VariableArrayType *Array1 = cast
(T1); const VariableArrayType *Array2 = cast
(T2); if (!IsStructurallyEquivalent(Context, Array1->getSizeExpr(), Array2->getSizeExpr())) return false; if (!IsArrayStructurallyEquivalent(Context, Array1, Array2)) return false; break; } case Type::DependentSizedArray: { const DependentSizedArrayType *Array1 = cast
(T1); const DependentSizedArrayType *Array2 = cast
(T2); if (!IsStructurallyEquivalent(Context, Array1->getSizeExpr(), Array2->getSizeExpr())) return false; if (!IsArrayStructurallyEquivalent(Context, Array1, Array2)) return false; break; } case Type::DependentSizedExtVector: { const DependentSizedExtVectorType *Vec1 = cast
(T1); const DependentSizedExtVectorType *Vec2 = cast
(T2); if (!IsStructurallyEquivalent(Context, Vec1->getSizeExpr(), Vec2->getSizeExpr())) return false; if (!IsStructurallyEquivalent(Context, Vec1->getElementType(), Vec2->getElementType())) return false; break; } case Type::Vector: case Type::ExtVector: { const VectorType *Vec1 = cast
(T1); const VectorType *Vec2 = cast
(T2); if (!IsStructurallyEquivalent(Context, Vec1->getElementType(), Vec2->getElementType())) return false; if (Vec1->getNumElements() != Vec2->getNumElements()) return false; if (Vec1->getVectorKind() != Vec2->getVectorKind()) return false; break; } case Type::FunctionProto: { const FunctionProtoType *Proto1 = cast
(T1); const FunctionProtoType *Proto2 = cast
(T2); if (Proto1->getNumParams() != Proto2->getNumParams()) return false; for (unsigned I = 0, N = Proto1->getNumParams(); I != N; ++I) { if (!IsStructurallyEquivalent(Context, Proto1->getParamType(I), Proto2->getParamType(I))) return false; } if (Proto1->isVariadic() != Proto2->isVariadic()) return false; if (Proto1->getExceptionSpecType() != Proto2->getExceptionSpecType()) return false; if (Proto1->getExceptionSpecType() == EST_Dynamic) { if (Proto1->getNumExceptions() != Proto2->getNumExceptions()) return false; for (unsigned I = 0, N = Proto1->getNumExceptions(); I != N; ++I) { if (!IsStructurallyEquivalent(Context, Proto1->getExceptionType(I), Proto2->getExceptionType(I))) return false; } } else if (Proto1->getExceptionSpecType() == EST_ComputedNoexcept) { if (!IsStructurallyEquivalent(Context, Proto1->getNoexceptExpr(), Proto2->getNoexceptExpr())) return false; } if (Proto1->getTypeQuals() != Proto2->getTypeQuals()) return false; // Fall through to check the bits common with FunctionNoProtoType. } case Type::FunctionNoProto: { const FunctionType *Function1 = cast
(T1); const FunctionType *Function2 = cast
(T2); if (!IsStructurallyEquivalent(Context, Function1->getReturnType(), Function2->getReturnType())) return false; if (Function1->getExtInfo() != Function2->getExtInfo()) return false; break; } case Type::UnresolvedUsing: if (!IsStructurallyEquivalent(Context, cast
(T1)->getDecl(), cast
(T2)->getDecl())) return false; break; case Type::Attributed: if (!IsStructurallyEquivalent(Context, cast
(T1)->getModifiedType(), cast
(T2)->getModifiedType())) return false; if (!IsStructurallyEquivalent(Context, cast
(T1)->getEquivalentType(), cast
(T2)->getEquivalentType())) return false; break; case Type::Paren: if (!IsStructurallyEquivalent(Context, cast
(T1)->getInnerType(), cast
(T2)->getInnerType())) return false; break; case Type::Typedef: if (!IsStructurallyEquivalent(Context, cast
(T1)->getDecl(), cast
(T2)->getDecl())) return false; break; case Type::TypeOfExpr: if (!IsStructurallyEquivalent(Context, cast
(T1)->getUnderlyingExpr(), cast
(T2)->getUnderlyingExpr())) return false; break; case Type::TypeOf: if (!IsStructurallyEquivalent(Context, cast
(T1)->getUnderlyingType(), cast
(T2)->getUnderlyingType())) return false; break; case Type::UnaryTransform: if (!IsStructurallyEquivalent(Context, cast
(T1)->getUnderlyingType(), cast
(T1)->getUnderlyingType())) return false; break; case Type::Decltype: if (!IsStructurallyEquivalent(Context, cast
(T1)->getUnderlyingExpr(), cast
(T2)->getUnderlyingExpr())) return false; break; case Type::Auto: if (!IsStructurallyEquivalent(Context, cast
(T1)->getDeducedType(), cast
(T2)->getDeducedType())) return false; break; case Type::Record: case Type::Enum: if (!IsStructurallyEquivalent(Context, cast
(T1)->getDecl(), cast
(T2)->getDecl())) return false; break; case Type::TemplateTypeParm: { const TemplateTypeParmType *Parm1 = cast
(T1); const TemplateTypeParmType *Parm2 = cast
(T2); if (Parm1->getDepth() != Parm2->getDepth()) return false; if (Parm1->getIndex() != Parm2->getIndex()) return false; if (Parm1->isParameterPack() != Parm2->isParameterPack()) return false; // Names of template type parameters are never significant. break; } case Type::SubstTemplateTypeParm: { const SubstTemplateTypeParmType *Subst1 = cast
(T1); const SubstTemplateTypeParmType *Subst2 = cast
(T2); if (!IsStructurallyEquivalent(Context, QualType(Subst1->getReplacedParameter(), 0), QualType(Subst2->getReplacedParameter(), 0))) return false; if (!IsStructurallyEquivalent(Context, Subst1->getReplacementType(), Subst2->getReplacementType())) return false; break; } case Type::SubstTemplateTypeParmPack: { const SubstTemplateTypeParmPackType *Subst1 = cast
(T1); const SubstTemplateTypeParmPackType *Subst2 = cast
(T2); if (!IsStructurallyEquivalent(Context, QualType(Subst1->getReplacedParameter(), 0), QualType(Subst2->getReplacedParameter(), 0))) return false; if (!IsStructurallyEquivalent(Context, Subst1->getArgumentPack(), Subst2->getArgumentPack())) return false; break; } case Type::TemplateSpecialization: { const TemplateSpecializationType *Spec1 = cast
(T1); const TemplateSpecializationType *Spec2 = cast
(T2); if (!IsStructurallyEquivalent(Context, Spec1->getTemplateName(), Spec2->getTemplateName())) return false; if (Spec1->getNumArgs() != Spec2->getNumArgs()) return false; for (unsigned I = 0, N = Spec1->getNumArgs(); I != N; ++I) { if (!IsStructurallyEquivalent(Context, Spec1->getArg(I), Spec2->getArg(I))) return false; } break; } case Type::Elaborated: { const ElaboratedType *Elab1 = cast
(T1); const ElaboratedType *Elab2 = cast
(T2); // CHECKME: what if a keyword is ETK_None or ETK_typename ? if (Elab1->getKeyword() != Elab2->getKeyword()) return false; if (!IsStructurallyEquivalent(Context, Elab1->getQualifier(), Elab2->getQualifier())) return false; if (!IsStructurallyEquivalent(Context, Elab1->getNamedType(), Elab2->getNamedType())) return false; break; } case Type::InjectedClassName: { const InjectedClassNameType *Inj1 = cast
(T1); const InjectedClassNameType *Inj2 = cast
(T2); if (!IsStructurallyEquivalent(Context, Inj1->getInjectedSpecializationType(), Inj2->getInjectedSpecializationType())) return false; break; } case Type::DependentName: { const DependentNameType *Typename1 = cast
(T1); const DependentNameType *Typename2 = cast
(T2); if (!IsStructurallyEquivalent(Context, Typename1->getQualifier(), Typename2->getQualifier())) return false; if (!IsStructurallyEquivalent(Typename1->getIdentifier(), Typename2->getIdentifier())) return false; break; } case Type::DependentTemplateSpecialization: { const DependentTemplateSpecializationType *Spec1 = cast
(T1); const DependentTemplateSpecializationType *Spec2 = cast
(T2); if (!IsStructurallyEquivalent(Context, Spec1->getQualifier(), Spec2->getQualifier())) return false; if (!IsStructurallyEquivalent(Spec1->getIdentifier(), Spec2->getIdentifier())) return false; if (Spec1->getNumArgs() != Spec2->getNumArgs()) return false; for (unsigned I = 0, N = Spec1->getNumArgs(); I != N; ++I) { if (!IsStructurallyEquivalent(Context, Spec1->getArg(I), Spec2->getArg(I))) return false; } break; } case Type::PackExpansion: if (!IsStructurallyEquivalent(Context, cast
(T1)->getPattern(), cast
(T2)->getPattern())) return false; break; case Type::ObjCInterface: { const ObjCInterfaceType *Iface1 = cast
(T1); const ObjCInterfaceType *Iface2 = cast
(T2); if (!IsStructurallyEquivalent(Context, Iface1->getDecl(), Iface2->getDecl())) return false; break; } case Type::ObjCObject: { const ObjCObjectType *Obj1 = cast
(T1); const ObjCObjectType *Obj2 = cast
(T2); if (!IsStructurallyEquivalent(Context, Obj1->getBaseType(), Obj2->getBaseType())) return false; if (Obj1->getNumProtocols() != Obj2->getNumProtocols()) return false; for (unsigned I = 0, N = Obj1->getNumProtocols(); I != N; ++I) { if (!IsStructurallyEquivalent(Context, Obj1->getProtocol(I), Obj2->getProtocol(I))) return false; } break; } case Type::ObjCObjectPointer: { const ObjCObjectPointerType *Ptr1 = cast
(T1); const ObjCObjectPointerType *Ptr2 = cast
(T2); if (!IsStructurallyEquivalent(Context, Ptr1->getPointeeType(), Ptr2->getPointeeType())) return false; break; } case Type::Atomic: { if (!IsStructurallyEquivalent(Context, cast
(T1)->getValueType(), cast
(T2)->getValueType())) return false; break; } case Type::Pipe: { if (!IsStructurallyEquivalent(Context, cast
(T1)->getElementType(), cast
(T2)->getElementType())) return false; break; } } // end switch return true; } /// \brief Determine structural equivalence of two fields. static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context, FieldDecl *Field1, FieldDecl *Field2) { RecordDecl *Owner2 = cast
(Field2->getDeclContext()); // For anonymous structs/unions, match up the anonymous struct/union type // declarations directly, so that we don't go off searching for anonymous // types if (Field1->isAnonymousStructOrUnion() && Field2->isAnonymousStructOrUnion()) { RecordDecl *D1 = Field1->getType()->castAs
()->getDecl(); RecordDecl *D2 = Field2->getType()->castAs
()->getDecl(); return IsStructurallyEquivalent(Context, D1, D2); } // Check for equivalent field names. IdentifierInfo *Name1 = Field1->getIdentifier(); IdentifierInfo *Name2 = Field2->getIdentifier(); if (!::IsStructurallyEquivalent(Name1, Name2)) return false; if (!IsStructurallyEquivalent(Context, Field1->getType(), Field2->getType())) { if (Context.Complain) { Context.Diag2(Owner2->getLocation(), diag::warn_odr_tag_type_inconsistent) << Context.C2.getTypeDeclType(Owner2); Context.Diag2(Field2->getLocation(), diag::note_odr_field) << Field2->getDeclName() << Field2->getType(); Context.Diag1(Field1->getLocation(), diag::note_odr_field) << Field1->getDeclName() << Field1->getType(); } return false; } if (Field1->isBitField() != Field2->isBitField()) { if (Context.Complain) { Context.Diag2(Owner2->getLocation(), diag::warn_odr_tag_type_inconsistent) << Context.C2.getTypeDeclType(Owner2); if (Field1->isBitField()) { Context.Diag1(Field1->getLocation(), diag::note_odr_bit_field) << Field1->getDeclName() << Field1->getType() << Field1->getBitWidthValue(Context.C1); Context.Diag2(Field2->getLocation(), diag::note_odr_not_bit_field) << Field2->getDeclName(); } else { Context.Diag2(Field2->getLocation(), diag::note_odr_bit_field) << Field2->getDeclName() << Field2->getType() << Field2->getBitWidthValue(Context.C2); Context.Diag1(Field1->getLocation(), diag::note_odr_not_bit_field) << Field1->getDeclName(); } } return false; } if (Field1->isBitField()) { // Make sure that the bit-fields are the same length. unsigned Bits1 = Field1->getBitWidthValue(Context.C1); unsigned Bits2 = Field2->getBitWidthValue(Context.C2); if (Bits1 != Bits2) { if (Context.Complain) { Context.Diag2(Owner2->getLocation(), diag::warn_odr_tag_type_inconsistent) << Context.C2.getTypeDeclType(Owner2); Context.Diag2(Field2->getLocation(), diag::note_odr_bit_field) << Field2->getDeclName() << Field2->getType() << Bits2; Context.Diag1(Field1->getLocation(), diag::note_odr_bit_field) << Field1->getDeclName() << Field1->getType() << Bits1; } return false; } } return true; } /// \brief Find the index of the given anonymous struct/union within its /// context. /// /// \returns Returns the index of this anonymous struct/union in its context, /// including the next assigned index (if none of them match). Returns an /// empty option if the context is not a record, i.e.. if the anonymous /// struct/union is at namespace or block scope. static Optional
findUntaggedStructOrUnionIndex(RecordDecl *Anon) { ASTContext &Context = Anon->getASTContext(); QualType AnonTy = Context.getRecordType(Anon); RecordDecl *Owner = dyn_cast
(Anon->getDeclContext()); if (!Owner) return None; unsigned Index = 0; for (const auto *D : Owner->noload_decls()) { const auto *F = dyn_cast
(D); if (!F) continue; if (F->isAnonymousStructOrUnion()) { if (Context.hasSameType(F->getType(), AnonTy)) break; ++Index; continue; } // If the field looks like this: // struct { ... } A; QualType FieldType = F->getType(); if (const auto *RecType = dyn_cast
(FieldType)) { const RecordDecl *RecDecl = RecType->getDecl(); if (RecDecl->getDeclContext() == Owner && !RecDecl->getIdentifier()) { if (Context.hasSameType(FieldType, AnonTy)) break; ++Index; continue; } } } return Index; } /// \brief Determine structural equivalence of two records. static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context, RecordDecl *D1, RecordDecl *D2) { if (D1->isUnion() != D2->isUnion()) { if (Context.Complain) { Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent) << Context.C2.getTypeDeclType(D2); Context.Diag1(D1->getLocation(), diag::note_odr_tag_kind_here) << D1->getDeclName() << (unsigned)D1->getTagKind(); } return false; } if (D1->isAnonymousStructOrUnion() && D2->isAnonymousStructOrUnion()) { // If both anonymous structs/unions are in a record context, make sure // they occur in the same location in the context records. if (Optional
Index1 = findUntaggedStructOrUnionIndex(D1)) { if (Optional
Index2 = findUntaggedStructOrUnionIndex(D2)) { if (*Index1 != *Index2) return false; } } } // If both declarations are class template specializations, we know // the ODR applies, so check the template and template arguments. ClassTemplateSpecializationDecl *Spec1 = dyn_cast
(D1); ClassTemplateSpecializationDecl *Spec2 = dyn_cast
(D2); if (Spec1 && Spec2) { // Check that the specialized templates are the same. if (!IsStructurallyEquivalent(Context, Spec1->getSpecializedTemplate(), Spec2->getSpecializedTemplate())) return false; // Check that the template arguments are the same. if (Spec1->getTemplateArgs().size() != Spec2->getTemplateArgs().size()) return false; for (unsigned I = 0, N = Spec1->getTemplateArgs().size(); I != N; ++I) if (!IsStructurallyEquivalent(Context, Spec1->getTemplateArgs().get(I), Spec2->getTemplateArgs().get(I))) return false; } // If one is a class template specialization and the other is not, these // structures are different. else if (Spec1 || Spec2) return false; // Compare the definitions of these two records. If either or both are // incomplete, we assume that they are equivalent. D1 = D1->getDefinition(); D2 = D2->getDefinition(); if (!D1 || !D2) return true; if (CXXRecordDecl *D1CXX = dyn_cast
(D1)) { if (CXXRecordDecl *D2CXX = dyn_cast
(D2)) { if (D1CXX->getNumBases() != D2CXX->getNumBases()) { if (Context.Complain) { Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent) << Context.C2.getTypeDeclType(D2); Context.Diag2(D2->getLocation(), diag::note_odr_number_of_bases) << D2CXX->getNumBases(); Context.Diag1(D1->getLocation(), diag::note_odr_number_of_bases) << D1CXX->getNumBases(); } return false; } // Check the base classes. for (CXXRecordDecl::base_class_iterator Base1 = D1CXX->bases_begin(), BaseEnd1 = D1CXX->bases_end(), Base2 = D2CXX->bases_begin(); Base1 != BaseEnd1; ++Base1, ++Base2) { if (!IsStructurallyEquivalent(Context, Base1->getType(), Base2->getType())) { if (Context.Complain) { Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent) << Context.C2.getTypeDeclType(D2); Context.Diag2(Base2->getLocStart(), diag::note_odr_base) << Base2->getType() << Base2->getSourceRange(); Context.Diag1(Base1->getLocStart(), diag::note_odr_base) << Base1->getType() << Base1->getSourceRange(); } return false; } // Check virtual vs. non-virtual inheritance mismatch. if (Base1->isVirtual() != Base2->isVirtual()) { if (Context.Complain) { Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent) << Context.C2.getTypeDeclType(D2); Context.Diag2(Base2->getLocStart(), diag::note_odr_virtual_base) << Base2->isVirtual() << Base2->getSourceRange(); Context.Diag1(Base1->getLocStart(), diag::note_odr_base) << Base1->isVirtual() << Base1->getSourceRange(); } return false; } } } else if (D1CXX->getNumBases() > 0) { if (Context.Complain) { Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent) << Context.C2.getTypeDeclType(D2); const CXXBaseSpecifier *Base1 = D1CXX->bases_begin(); Context.Diag1(Base1->getLocStart(), diag::note_odr_base) << Base1->getType() << Base1->getSourceRange(); Context.Diag2(D2->getLocation(), diag::note_odr_missing_base); } return false; } } // Check the fields for consistency. RecordDecl::field_iterator Field2 = D2->field_begin(), Field2End = D2->field_end(); for (RecordDecl::field_iterator Field1 = D1->field_begin(), Field1End = D1->field_end(); Field1 != Field1End; ++Field1, ++Field2) { if (Field2 == Field2End) { if (Context.Complain) { Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent) << Context.C2.getTypeDeclType(D2); Context.Diag1(Field1->getLocation(), diag::note_odr_field) << Field1->getDeclName() << Field1->getType(); Context.Diag2(D2->getLocation(), diag::note_odr_missing_field); } return false; } if (!IsStructurallyEquivalent(Context, *Field1, *Field2)) return false; } if (Field2 != Field2End) { if (Context.Complain) { Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent) << Context.C2.getTypeDeclType(D2); Context.Diag2(Field2->getLocation(), diag::note_odr_field) << Field2->getDeclName() << Field2->getType(); Context.Diag1(D1->getLocation(), diag::note_odr_missing_field); } return false; } return true; } /// \brief Determine structural equivalence of two enums. static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context, EnumDecl *D1, EnumDecl *D2) { EnumDecl::enumerator_iterator EC2 = D2->enumerator_begin(), EC2End = D2->enumerator_end(); for (EnumDecl::enumerator_iterator EC1 = D1->enumerator_begin(), EC1End = D1->enumerator_end(); EC1 != EC1End; ++EC1, ++EC2) { if (EC2 == EC2End) { if (Context.Complain) { Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent) << Context.C2.getTypeDeclType(D2); Context.Diag1(EC1->getLocation(), diag::note_odr_enumerator) << EC1->getDeclName() << EC1->getInitVal().toString(10); Context.Diag2(D2->getLocation(), diag::note_odr_missing_enumerator); } return false; } llvm::APSInt Val1 = EC1->getInitVal(); llvm::APSInt Val2 = EC2->getInitVal(); if (!llvm::APSInt::isSameValue(Val1, Val2) || !IsStructurallyEquivalent(EC1->getIdentifier(), EC2->getIdentifier())) { if (Context.Complain) { Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent) << Context.C2.getTypeDeclType(D2); Context.Diag2(EC2->getLocation(), diag::note_odr_enumerator) << EC2->getDeclName() << EC2->getInitVal().toString(10); Context.Diag1(EC1->getLocation(), diag::note_odr_enumerator) << EC1->getDeclName() << EC1->getInitVal().toString(10); } return false; } } if (EC2 != EC2End) { if (Context.Complain) { Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent) << Context.C2.getTypeDeclType(D2); Context.Diag2(EC2->getLocation(), diag::note_odr_enumerator) << EC2->getDeclName() << EC2->getInitVal().toString(10); Context.Diag1(D1->getLocation(), diag::note_odr_missing_enumerator); } return false; } return true; } static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context, TemplateParameterList *Params1, TemplateParameterList *Params2) { if (Params1->size() != Params2->size()) { if (Context.Complain) { Context.Diag2(Params2->getTemplateLoc(), diag::err_odr_different_num_template_parameters) << Params1->size() << Params2->size(); Context.Diag1(Params1->getTemplateLoc(), diag::note_odr_template_parameter_list); } return false; } for (unsigned I = 0, N = Params1->size(); I != N; ++I) { if (Params1->getParam(I)->getKind() != Params2->getParam(I)->getKind()) { if (Context.Complain) { Context.Diag2(Params2->getParam(I)->getLocation(), diag::err_odr_different_template_parameter_kind); Context.Diag1(Params1->getParam(I)->getLocation(), diag::note_odr_template_parameter_here); } return false; } if (!Context.IsStructurallyEquivalent(Params1->getParam(I), Params2->getParam(I))) { return false; } } return true; } static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context, TemplateTypeParmDecl *D1, TemplateTypeParmDecl *D2) { if (D1->isParameterPack() != D2->isParameterPack()) { if (Context.Complain) { Context.Diag2(D2->getLocation(), diag::err_odr_parameter_pack_non_pack) << D2->isParameterPack(); Context.Diag1(D1->getLocation(), diag::note_odr_parameter_pack_non_pack) << D1->isParameterPack(); } return false; } return true; } static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context, NonTypeTemplateParmDecl *D1, NonTypeTemplateParmDecl *D2) { if (D1->isParameterPack() != D2->isParameterPack()) { if (Context.Complain) { Context.Diag2(D2->getLocation(), diag::err_odr_parameter_pack_non_pack) << D2->isParameterPack(); Context.Diag1(D1->getLocation(), diag::note_odr_parameter_pack_non_pack) << D1->isParameterPack(); } return false; } // Check types. if (!Context.IsStructurallyEquivalent(D1->getType(), D2->getType())) { if (Context.Complain) { Context.Diag2(D2->getLocation(), diag::err_odr_non_type_parameter_type_inconsistent) << D2->getType() << D1->getType(); Context.Diag1(D1->getLocation(), diag::note_odr_value_here) << D1->getType(); } return false; } return true; } static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context, TemplateTemplateParmDecl *D1, TemplateTemplateParmDecl *D2) { if (D1->isParameterPack() != D2->isParameterPack()) { if (Context.Complain) { Context.Diag2(D2->getLocation(), diag::err_odr_parameter_pack_non_pack) << D2->isParameterPack(); Context.Diag1(D1->getLocation(), diag::note_odr_parameter_pack_non_pack) << D1->isParameterPack(); } return false; } // Check template parameter lists. return IsStructurallyEquivalent(Context, D1->getTemplateParameters(), D2->getTemplateParameters()); } static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context, ClassTemplateDecl *D1, ClassTemplateDecl *D2) { // Check template parameters. if (!IsStructurallyEquivalent(Context, D1->getTemplateParameters(), D2->getTemplateParameters())) return false; // Check the templated declaration. return Context.IsStructurallyEquivalent(D1->getTemplatedDecl(), D2->getTemplatedDecl()); } /// \brief Determine structural equivalence of two declarations. static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context, Decl *D1, Decl *D2) { // FIXME: Check for known structural equivalences via a callback of some sort. // Check whether we already know that these two declarations are not // structurally equivalent. if (Context.NonEquivalentDecls.count(std::make_pair(D1->getCanonicalDecl(), D2->getCanonicalDecl()))) return false; // Determine whether we've already produced a tentative equivalence for D1. Decl *&EquivToD1 = Context.TentativeEquivalences[D1->getCanonicalDecl()]; if (EquivToD1) return EquivToD1 == D2->getCanonicalDecl(); // Produce a tentative equivalence D1 <-> D2, which will be checked later. EquivToD1 = D2->getCanonicalDecl(); Context.DeclsToCheck.push_back(D1->getCanonicalDecl()); return true; } bool StructuralEquivalenceContext::IsStructurallyEquivalent(Decl *D1, Decl *D2) { if (!::IsStructurallyEquivalent(*this, D1, D2)) return false; return !Finish(); } bool StructuralEquivalenceContext::IsStructurallyEquivalent(QualType T1, QualType T2) { if (!::IsStructurallyEquivalent(*this, T1, T2)) return false; return !Finish(); } bool StructuralEquivalenceContext::Finish() { while (!DeclsToCheck.empty()) { // Check the next declaration. Decl *D1 = DeclsToCheck.front(); DeclsToCheck.pop_front(); Decl *D2 = TentativeEquivalences[D1]; assert(D2 && "Unrecorded tentative equivalence?"); bool Equivalent = true; // FIXME: Switch on all declaration kinds. For now, we're just going to // check the obvious ones. if (RecordDecl *Record1 = dyn_cast
(D1)) { if (RecordDecl *Record2 = dyn_cast
(D2)) { // Check for equivalent structure names. IdentifierInfo *Name1 = Record1->getIdentifier(); if (!Name1 && Record1->getTypedefNameForAnonDecl()) Name1 = Record1->getTypedefNameForAnonDecl()->getIdentifier(); IdentifierInfo *Name2 = Record2->getIdentifier(); if (!Name2 && Record2->getTypedefNameForAnonDecl()) Name2 = Record2->getTypedefNameForAnonDecl()->getIdentifier(); if (!::IsStructurallyEquivalent(Name1, Name2) || !::IsStructurallyEquivalent(*this, Record1, Record2)) Equivalent = false; } else { // Record/non-record mismatch. Equivalent = false; } } else if (EnumDecl *Enum1 = dyn_cast
(D1)) { if (EnumDecl *Enum2 = dyn_cast
(D2)) { // Check for equivalent enum names. IdentifierInfo *Name1 = Enum1->getIdentifier(); if (!Name1 && Enum1->getTypedefNameForAnonDecl()) Name1 = Enum1->getTypedefNameForAnonDecl()->getIdentifier(); IdentifierInfo *Name2 = Enum2->getIdentifier(); if (!Name2 && Enum2->getTypedefNameForAnonDecl()) Name2 = Enum2->getTypedefNameForAnonDecl()->getIdentifier(); if (!::IsStructurallyEquivalent(Name1, Name2) || !::IsStructurallyEquivalent(*this, Enum1, Enum2)) Equivalent = false; } else { // Enum/non-enum mismatch Equivalent = false; } } else if (TypedefNameDecl *Typedef1 = dyn_cast
(D1)) { if (TypedefNameDecl *Typedef2 = dyn_cast
(D2)) { if (!::IsStructurallyEquivalent(Typedef1->getIdentifier(), Typedef2->getIdentifier()) || !::IsStructurallyEquivalent(*this, Typedef1->getUnderlyingType(), Typedef2->getUnderlyingType())) Equivalent = false; } else { // Typedef/non-typedef mismatch. Equivalent = false; } } else if (ClassTemplateDecl *ClassTemplate1 = dyn_cast
(D1)) { if (ClassTemplateDecl *ClassTemplate2 = dyn_cast
(D2)) { if (!::IsStructurallyEquivalent(ClassTemplate1->getIdentifier(), ClassTemplate2->getIdentifier()) || !::IsStructurallyEquivalent(*this, ClassTemplate1, ClassTemplate2)) Equivalent = false; } else { // Class template/non-class-template mismatch. Equivalent = false; } } else if (TemplateTypeParmDecl *TTP1= dyn_cast
(D1)) { if (TemplateTypeParmDecl *TTP2 = dyn_cast
(D2)) { if (!::IsStructurallyEquivalent(*this, TTP1, TTP2)) Equivalent = false; } else { // Kind mismatch. Equivalent = false; } } else if (NonTypeTemplateParmDecl *NTTP1 = dyn_cast
(D1)) { if (NonTypeTemplateParmDecl *NTTP2 = dyn_cast
(D2)) { if (!::IsStructurallyEquivalent(*this, NTTP1, NTTP2)) Equivalent = false; } else { // Kind mismatch. Equivalent = false; } } else if (TemplateTemplateParmDecl *TTP1 = dyn_cast
(D1)) { if (TemplateTemplateParmDecl *TTP2 = dyn_cast
(D2)) { if (!::IsStructurallyEquivalent(*this, TTP1, TTP2)) Equivalent = false; } else { // Kind mismatch. Equivalent = false; } } if (!Equivalent) { // Note that these two declarations are not equivalent (and we already // know about it). NonEquivalentDecls.insert(std::make_pair(D1->getCanonicalDecl(), D2->getCanonicalDecl())); return true; } // FIXME: Check other declaration kinds! } return false; } //---------------------------------------------------------------------------- // Import Types //---------------------------------------------------------------------------- QualType ASTNodeImporter::VisitType(const Type *T) { Importer.FromDiag(SourceLocation(), diag::err_unsupported_ast_node) << T->getTypeClassName(); return QualType(); } QualType ASTNodeImporter::VisitBuiltinType(const BuiltinType *T) { switch (T->getKind()) { #define IMAGE_TYPE(ImgType, Id, SingletonId, Access, Suffix) \ case BuiltinType::Id: \ return Importer.getToContext().SingletonId; #include "clang/Basic/OpenCLImageTypes.def" #define SHARED_SINGLETON_TYPE(Expansion) #define BUILTIN_TYPE(Id, SingletonId) \ case BuiltinType::Id: return Importer.getToContext().SingletonId; #include "clang/AST/BuiltinTypes.def" // FIXME: for Char16, Char32, and NullPtr, make sure that the "to" // context supports C++. // FIXME: for ObjCId, ObjCClass, and ObjCSel, make sure that the "to" // context supports ObjC. case BuiltinType::Char_U: // The context we're importing from has an unsigned 'char'. If we're // importing into a context with a signed 'char', translate to // 'unsigned char' instead. if (Importer.getToContext().getLangOpts().CharIsSigned) return Importer.getToContext().UnsignedCharTy; return Importer.getToContext().CharTy; case BuiltinType::Char_S: // The context we're importing from has an unsigned 'char'. If we're // importing into a context with a signed 'char', translate to // 'unsigned char' instead. if (!Importer.getToContext().getLangOpts().CharIsSigned) return Importer.getToContext().SignedCharTy; return Importer.getToContext().CharTy; case BuiltinType::WChar_S: case BuiltinType::WChar_U: // FIXME: If not in C++, shall we translate to the C equivalent of // wchar_t? return Importer.getToContext().WCharTy; } llvm_unreachable("Invalid BuiltinType Kind!"); } QualType ASTNodeImporter::VisitComplexType(const ComplexType *T) { QualType ToElementType = Importer.Import(T->getElementType()); if (ToElementType.isNull()) return QualType(); return Importer.getToContext().getComplexType(ToElementType); } QualType ASTNodeImporter::VisitPointerType(const PointerType *T) { QualType ToPointeeType = Importer.Import(T->getPointeeType()); if (ToPointeeType.isNull()) return QualType(); return Importer.getToContext().getPointerType(ToPointeeType); } QualType ASTNodeImporter::VisitBlockPointerType(const BlockPointerType *T) { // FIXME: Check for blocks support in "to" context. QualType ToPointeeType = Importer.Import(T->getPointeeType()); if (ToPointeeType.isNull()) return QualType(); return Importer.getToContext().getBlockPointerType(ToPointeeType); } QualType ASTNodeImporter::VisitLValueReferenceType(const LValueReferenceType *T) { // FIXME: Check for C++ support in "to" context. QualType ToPointeeType = Importer.Import(T->getPointeeTypeAsWritten()); if (ToPointeeType.isNull()) return QualType(); return Importer.getToContext().getLValueReferenceType(ToPointeeType); } QualType ASTNodeImporter::VisitRValueReferenceType(const RValueReferenceType *T) { // FIXME: Check for C++0x support in "to" context. QualType ToPointeeType = Importer.Import(T->getPointeeTypeAsWritten()); if (ToPointeeType.isNull()) return QualType(); return Importer.getToContext().getRValueReferenceType(ToPointeeType); } QualType ASTNodeImporter::VisitMemberPointerType(const MemberPointerType *T) { // FIXME: Check for C++ support in "to" context. QualType ToPointeeType = Importer.Import(T->getPointeeType()); if (ToPointeeType.isNull()) return QualType(); QualType ClassType = Importer.Import(QualType(T->getClass(), 0)); return Importer.getToContext().getMemberPointerType(ToPointeeType, ClassType.getTypePtr()); } QualType ASTNodeImporter::VisitConstantArrayType(const ConstantArrayType *T) { QualType ToElementType = Importer.Import(T->getElementType()); if (ToElementType.isNull()) return QualType(); return Importer.getToContext().getConstantArrayType(ToElementType, T->getSize(), T->getSizeModifier(), T->getIndexTypeCVRQualifiers()); } QualType ASTNodeImporter::VisitIncompleteArrayType(const IncompleteArrayType *T) { QualType ToElementType = Importer.Import(T->getElementType()); if (ToElementType.isNull()) return QualType(); return Importer.getToContext().getIncompleteArrayType(ToElementType, T->getSizeModifier(), T->getIndexTypeCVRQualifiers()); } QualType ASTNodeImporter::VisitVariableArrayType(const VariableArrayType *T) { QualType ToElementType = Importer.Import(T->getElementType()); if (ToElementType.isNull()) return QualType(); Expr *Size = Importer.Import(T->getSizeExpr()); if (!Size) return QualType(); SourceRange Brackets = Importer.Import(T->getBracketsRange()); return Importer.getToContext().getVariableArrayType(ToElementType, Size, T->getSizeModifier(), T->getIndexTypeCVRQualifiers(), Brackets); } QualType ASTNodeImporter::VisitVectorType(const VectorType *T) { QualType ToElementType = Importer.Import(T->getElementType()); if (ToElementType.isNull()) return QualType(); return Importer.getToContext().getVectorType(ToElementType, T->getNumElements(), T->getVectorKind()); } QualType ASTNodeImporter::VisitExtVectorType(const ExtVectorType *T) { QualType ToElementType = Importer.Import(T->getElementType()); if (ToElementType.isNull()) return QualType(); return Importer.getToContext().getExtVectorType(ToElementType, T->getNumElements()); } QualType ASTNodeImporter::VisitFunctionNoProtoType(const FunctionNoProtoType *T) { // FIXME: What happens if we're importing a function without a prototype // into C++? Should we make it variadic? QualType ToResultType = Importer.Import(T->getReturnType()); if (ToResultType.isNull()) return QualType(); return Importer.getToContext().getFunctionNoProtoType(ToResultType, T->getExtInfo()); } QualType ASTNodeImporter::VisitFunctionProtoType(const FunctionProtoType *T) { QualType ToResultType = Importer.Import(T->getReturnType()); if (ToResultType.isNull()) return QualType(); // Import argument types SmallVector
ArgTypes; for (const auto &A : T->param_types()) { QualType ArgType = Importer.Import(A); if (ArgType.isNull()) return QualType(); ArgTypes.push_back(ArgType); } // Import exception types SmallVector
ExceptionTypes; for (const auto &E : T->exceptions()) { QualType ExceptionType = Importer.Import(E); if (ExceptionType.isNull()) return QualType(); ExceptionTypes.push_back(ExceptionType); } FunctionProtoType::ExtProtoInfo FromEPI = T->getExtProtoInfo(); FunctionProtoType::ExtProtoInfo ToEPI; ToEPI.ExtInfo = FromEPI.ExtInfo; ToEPI.Variadic = FromEPI.Variadic; ToEPI.HasTrailingReturn = FromEPI.HasTrailingReturn; ToEPI.TypeQuals = FromEPI.TypeQuals; ToEPI.RefQualifier = FromEPI.RefQualifier; ToEPI.ExceptionSpec.Type = FromEPI.ExceptionSpec.Type; ToEPI.ExceptionSpec.Exceptions = ExceptionTypes; ToEPI.ExceptionSpec.NoexceptExpr = Importer.Import(FromEPI.ExceptionSpec.NoexceptExpr); ToEPI.ExceptionSpec.SourceDecl = cast_or_null
( Importer.Import(FromEPI.ExceptionSpec.SourceDecl)); ToEPI.ExceptionSpec.SourceTemplate = cast_or_null
( Importer.Import(FromEPI.ExceptionSpec.SourceTemplate)); return Importer.getToContext().getFunctionType(ToResultType, ArgTypes, ToEPI); } QualType ASTNodeImporter::VisitParenType(const ParenType *T) { QualType ToInnerType = Importer.Import(T->getInnerType()); if (ToInnerType.isNull()) return QualType(); return Importer.getToContext().getParenType(ToInnerType); } QualType ASTNodeImporter::VisitTypedefType(const TypedefType *T) { TypedefNameDecl *ToDecl = dyn_cast_or_null
(Importer.Import(T->getDecl())); if (!ToDecl) return QualType(); return Importer.getToContext().getTypeDeclType(ToDecl); } QualType ASTNodeImporter::VisitTypeOfExprType(const TypeOfExprType *T) { Expr *ToExpr = Importer.Import(T->getUnderlyingExpr()); if (!ToExpr) return QualType(); return Importer.getToContext().getTypeOfExprType(ToExpr); } QualType ASTNodeImporter::VisitTypeOfType(const TypeOfType *T) { QualType ToUnderlyingType = Importer.Import(T->getUnderlyingType()); if (ToUnderlyingType.isNull()) return QualType(); return Importer.getToContext().getTypeOfType(ToUnderlyingType); } QualType ASTNodeImporter::VisitDecltypeType(const DecltypeType *T) { // FIXME: Make sure that the "to" context supports C++0x! Expr *ToExpr = Importer.Import(T->getUnderlyingExpr()); if (!ToExpr) return QualType(); QualType UnderlyingType = Importer.Import(T->getUnderlyingType()); if (UnderlyingType.isNull()) return QualType(); return Importer.getToContext().getDecltypeType(ToExpr, UnderlyingType); } QualType ASTNodeImporter::VisitUnaryTransformType(const UnaryTransformType *T) { QualType ToBaseType = Importer.Import(T->getBaseType()); QualType ToUnderlyingType = Importer.Import(T->getUnderlyingType()); if (ToBaseType.isNull() || ToUnderlyingType.isNull()) return QualType(); return Importer.getToContext().getUnaryTransformType(ToBaseType, ToUnderlyingType, T->getUTTKind()); } QualType ASTNodeImporter::VisitAutoType(const AutoType *T) { // FIXME: Make sure that the "to" context supports C++11! QualType FromDeduced = T->getDeducedType(); QualType ToDeduced; if (!FromDeduced.isNull()) { ToDeduced = Importer.Import(FromDeduced); if (ToDeduced.isNull()) return QualType(); } return Importer.getToContext().getAutoType(ToDeduced, T->getKeyword(), /*IsDependent*/false); } QualType ASTNodeImporter::VisitInjectedClassNameType( const InjectedClassNameType *T) { CXXRecordDecl *D = cast_or_null
(Importer.Import(T->getDecl())); if (!D) return QualType(); QualType InjType = Importer.Import(T->getInjectedSpecializationType()); if (InjType.isNull()) return QualType(); // FIXME: ASTContext::getInjectedClassNameType is not suitable for AST reading // See comments in InjectedClassNameType definition for details // return Importer.getToContext().getInjectedClassNameType(D, InjType); enum { TypeAlignmentInBits = 4, TypeAlignment = 1 << TypeAlignmentInBits }; return QualType(new (Importer.getToContext(), TypeAlignment) InjectedClassNameType(D, InjType), 0); } QualType ASTNodeImporter::VisitRecordType(const RecordType *T) { RecordDecl *ToDecl = dyn_cast_or_null
(Importer.Import(T->getDecl())); if (!ToDecl) return QualType(); return Importer.getToContext().getTagDeclType(ToDecl); } QualType ASTNodeImporter::VisitEnumType(const EnumType *T) { EnumDecl *ToDecl = dyn_cast_or_null
(Importer.Import(T->getDecl())); if (!ToDecl) return QualType(); return Importer.getToContext().getTagDeclType(ToDecl); } QualType ASTNodeImporter::VisitAttributedType(const AttributedType *T) { QualType FromModifiedType = T->getModifiedType(); QualType FromEquivalentType = T->getEquivalentType(); QualType ToModifiedType; QualType ToEquivalentType; if (!FromModifiedType.isNull()) { ToModifiedType = Importer.Import(FromModifiedType); if (ToModifiedType.isNull()) return QualType(); } if (!FromEquivalentType.isNull()) { ToEquivalentType = Importer.Import(FromEquivalentType); if (ToEquivalentType.isNull()) return QualType(); } return Importer.getToContext().getAttributedType(T->getAttrKind(), ToModifiedType, ToEquivalentType); } QualType ASTNodeImporter::VisitTemplateTypeParmType( const TemplateTypeParmType *T) { TemplateTypeParmDecl *ParmDecl = cast_or_null
(Importer.Import(T->getDecl())); if (!ParmDecl && T->getDecl()) return QualType(); return Importer.getToContext().getTemplateTypeParmType( T->getDepth(), T->getIndex(), T->isParameterPack(), ParmDecl); } QualType ASTNodeImporter::VisitTemplateSpecializationType( const TemplateSpecializationType *T) { TemplateName ToTemplate = Importer.Import(T->getTemplateName()); if (ToTemplate.isNull()) return QualType(); SmallVector
ToTemplateArgs; if (ImportTemplateArguments(T->getArgs(), T->getNumArgs(), ToTemplateArgs)) return QualType(); QualType ToCanonType; if (!QualType(T, 0).isCanonical()) { QualType FromCanonType = Importer.getFromContext().getCanonicalType(QualType(T, 0)); ToCanonType =Importer.Import(FromCanonType); if (ToCanonType.isNull()) return QualType(); } return Importer.getToContext().getTemplateSpecializationType(ToTemplate, ToTemplateArgs, ToCanonType); } QualType ASTNodeImporter::VisitElaboratedType(const ElaboratedType *T) { NestedNameSpecifier *ToQualifier = nullptr; // Note: the qualifier in an ElaboratedType is optional. if (T->getQualifier()) { ToQualifier = Importer.Import(T->getQualifier()); if (!ToQualifier) return QualType(); } QualType ToNamedType = Importer.Import(T->getNamedType()); if (ToNamedType.isNull()) return QualType(); return Importer.getToContext().getElaboratedType(T->getKeyword(), ToQualifier, ToNamedType); } QualType ASTNodeImporter::VisitObjCInterfaceType(const ObjCInterfaceType *T) { ObjCInterfaceDecl *Class = dyn_cast_or_null
(Importer.Import(T->getDecl())); if (!Class) return QualType(); return Importer.getToContext().getObjCInterfaceType(Class); } QualType ASTNodeImporter::VisitObjCObjectType(const ObjCObjectType *T) { QualType ToBaseType = Importer.Import(T->getBaseType()); if (ToBaseType.isNull()) return QualType(); SmallVector
TypeArgs; for (auto TypeArg : T->getTypeArgsAsWritten()) { QualType ImportedTypeArg = Importer.Import(TypeArg); if (ImportedTypeArg.isNull()) return QualType(); TypeArgs.push_back(ImportedTypeArg); } SmallVector
Protocols; for (auto *P : T->quals()) { ObjCProtocolDecl *Protocol = dyn_cast_or_null
(Importer.Import(P)); if (!Protocol) return QualType(); Protocols.push_back(Protocol); } return Importer.getToContext().getObjCObjectType(ToBaseType, TypeArgs, Protocols, T->isKindOfTypeAsWritten()); } QualType ASTNodeImporter::VisitObjCObjectPointerType(const ObjCObjectPointerType *T) { QualType ToPointeeType = Importer.Import(T->getPointeeType()); if (ToPointeeType.isNull()) return QualType(); return Importer.getToContext().getObjCObjectPointerType(ToPointeeType); } //---------------------------------------------------------------------------- // Import Declarations //---------------------------------------------------------------------------- bool ASTNodeImporter::ImportDeclParts(NamedDecl *D, DeclContext *&DC, DeclContext *&LexicalDC, DeclarationName &Name, NamedDecl *&ToD, SourceLocation &Loc) { // Import the context of this declaration. DC = Importer.ImportContext(D->getDeclContext()); if (!DC) return true; LexicalDC = DC; if (D->getDeclContext() != D->getLexicalDeclContext()) { LexicalDC = Importer.ImportContext(D->getLexicalDeclContext()); if (!LexicalDC) return true; } // Import the name of this declaration. Name = Importer.Import(D->getDeclName()); if (D->getDeclName() && !Name) return true; // Import the location of this declaration. Loc = Importer.Import(D->getLocation()); ToD = cast_or_null
(Importer.GetAlreadyImportedOrNull(D)); return false; } void ASTNodeImporter::ImportDefinitionIfNeeded(Decl *FromD, Decl *ToD) { if (!FromD) return; if (!ToD) { ToD = Importer.Import(FromD); if (!ToD) return; } if (RecordDecl *FromRecord = dyn_cast
(FromD)) { if (RecordDecl *ToRecord = cast_or_null
(ToD)) { if (FromRecord->getDefinition() && FromRecord->isCompleteDefinition() && !ToRecord->getDefinition()) { ImportDefinition(FromRecord, ToRecord); } } return; } if (EnumDecl *FromEnum = dyn_cast
(FromD)) { if (EnumDecl *ToEnum = cast_or_null
(ToD)) { if (FromEnum->getDefinition() && !ToEnum->getDefinition()) { ImportDefinition(FromEnum, ToEnum); } } return; } } void ASTNodeImporter::ImportDeclarationNameLoc(const DeclarationNameInfo &From, DeclarationNameInfo& To) { // NOTE: To.Name and To.Loc are already imported. // We only have to import To.LocInfo. switch (To.getName().getNameKind()) { case DeclarationName::Identifier: case DeclarationName::ObjCZeroArgSelector: case DeclarationName::ObjCOneArgSelector: case DeclarationName::ObjCMultiArgSelector: case DeclarationName::CXXUsingDirective: return; case DeclarationName::CXXOperatorName: { SourceRange Range = From.getCXXOperatorNameRange(); To.setCXXOperatorNameRange(Importer.Import(Range)); return; } case DeclarationName::CXXLiteralOperatorName: { SourceLocation Loc = From.getCXXLiteralOperatorNameLoc(); To.setCXXLiteralOperatorNameLoc(Importer.Import(Loc)); return; } case DeclarationName::CXXConstructorName: case DeclarationName::CXXDestructorName: case DeclarationName::CXXConversionFunctionName: { TypeSourceInfo *FromTInfo = From.getNamedTypeInfo(); To.setNamedTypeInfo(Importer.Import(FromTInfo)); return; } } llvm_unreachable("Unknown name kind."); } void ASTNodeImporter::ImportDeclContext(DeclContext *FromDC, bool ForceImport) { if (Importer.isMinimalImport() && !ForceImport) { Importer.ImportContext(FromDC); return; } for (auto *From : FromDC->decls()) Importer.Import(From); } bool ASTNodeImporter::ImportDefinition(RecordDecl *From, RecordDecl *To, ImportDefinitionKind Kind) { if (To->getDefinition() || To->isBeingDefined()) { if (Kind == IDK_Everything) ImportDeclContext(From, /*ForceImport=*/true); return false; } To->startDefinition(); // Add base classes. if (CXXRecordDecl *ToCXX = dyn_cast
(To)) { CXXRecordDecl *FromCXX = cast
(From); struct CXXRecordDecl::DefinitionData &ToData = ToCXX->data(); struct CXXRecordDecl::DefinitionData &FromData = FromCXX->data(); ToData.UserDeclaredConstructor = FromData.UserDeclaredConstructor; ToData.UserDeclaredSpecialMembers = FromData.UserDeclaredSpecialMembers; ToData.Aggregate = FromData.Aggregate; ToData.PlainOldData = FromData.PlainOldData; ToData.Empty = FromData.Empty; ToData.Polymorphic = FromData.Polymorphic; ToData.Abstract = FromData.Abstract; ToData.IsStandardLayout = FromData.IsStandardLayout; ToData.HasNoNonEmptyBases = FromData.HasNoNonEmptyBases; ToData.HasPrivateFields = FromData.HasPrivateFields; ToData.HasProtectedFields = FromData.HasProtectedFields; ToData.HasPublicFields = FromData.HasPublicFields; ToData.HasMutableFields = FromData.HasMutableFields; ToData.HasVariantMembers = FromData.HasVariantMembers; ToData.HasOnlyCMembers = FromData.HasOnlyCMembers; ToData.HasInClassInitializer = FromData.HasInClassInitializer; ToData.HasUninitializedReferenceMember = FromData.HasUninitializedReferenceMember; ToData.HasUninitializedFields = FromData.HasUninitializedFields; ToData.HasInheritedConstructor = FromData.HasInheritedConstructor; ToData.HasInheritedAssignment = FromData.HasInheritedAssignment; ToData.NeedOverloadResolutionForMoveConstructor = FromData.NeedOverloadResolutionForMoveConstructor; ToData.NeedOverloadResolutionForMoveAssignment = FromData.NeedOverloadResolutionForMoveAssignment; ToData.NeedOverloadResolutionForDestructor = FromData.NeedOverloadResolutionForDestructor; ToData.DefaultedMoveConstructorIsDeleted = FromData.DefaultedMoveConstructorIsDeleted; ToData.DefaultedMoveAssignmentIsDeleted = FromData.DefaultedMoveAssignmentIsDeleted; ToData.DefaultedDestructorIsDeleted = FromData.DefaultedDestructorIsDeleted; ToData.HasTrivialSpecialMembers = FromData.HasTrivialSpecialMembers; ToData.HasIrrelevantDestructor = FromData.HasIrrelevantDestructor; ToData.HasConstexprNonCopyMoveConstructor = FromData.HasConstexprNonCopyMoveConstructor; ToData.HasDefaultedDefaultConstructor = FromData.HasDefaultedDefaultConstructor; ToData.DefaultedDefaultConstructorIsConstexpr = FromData.DefaultedDefaultConstructorIsConstexpr; ToData.HasConstexprDefaultConstructor = FromData.HasConstexprDefaultConstructor; ToData.HasNonLiteralTypeFieldsOrBases = FromData.HasNonLiteralTypeFieldsOrBases; // ComputedVisibleConversions not imported. ToData.UserProvidedDefaultConstructor = FromData.UserProvidedDefaultConstructor; ToData.DeclaredSpecialMembers = FromData.DeclaredSpecialMembers; ToData.ImplicitCopyConstructorHasConstParam = FromData.ImplicitCopyConstructorHasConstParam; ToData.ImplicitCopyAssignmentHasConstParam = FromData.ImplicitCopyAssignmentHasConstParam; ToData.HasDeclaredCopyConstructorWithConstParam = FromData.HasDeclaredCopyConstructorWithConstParam; ToData.HasDeclaredCopyAssignmentWithConstParam = FromData.HasDeclaredCopyAssignmentWithConstParam; ToData.IsLambda = FromData.IsLambda; SmallVector
Bases; for (const auto &Base1 : FromCXX->bases()) { QualType T = Importer.Import(Base1.getType()); if (T.isNull()) return true; SourceLocation EllipsisLoc; if (Base1.isPackExpansion()) EllipsisLoc = Importer.Import(Base1.getEllipsisLoc()); // Ensure that we have a definition for the base. ImportDefinitionIfNeeded(Base1.getType()->getAsCXXRecordDecl()); Bases.push_back( new (Importer.getToContext()) CXXBaseSpecifier(Importer.Import(Base1.getSourceRange()), Base1.isVirtual(), Base1.isBaseOfClass(), Base1.getAccessSpecifierAsWritten(), Importer.Import(Base1.getTypeSourceInfo()), EllipsisLoc)); } if (!Bases.empty()) ToCXX->setBases(Bases.data(), Bases.size()); } if (shouldForceImportDeclContext(Kind)) ImportDeclContext(From, /*ForceImport=*/true); To->completeDefinition(); return false; } bool ASTNodeImporter::ImportDefinition(VarDecl *From, VarDecl *To, ImportDefinitionKind Kind) { if (To->getAnyInitializer()) return false; // FIXME: Can we really import any initializer? Alternatively, we could force // ourselves to import every declaration of a variable and then only use // getInit() here. To->setInit(Importer.Import(const_cast
(From->getAnyInitializer()))); // FIXME: Other bits to merge? return false; } bool ASTNodeImporter::ImportDefinition(EnumDecl *From, EnumDecl *To, ImportDefinitionKind Kind) { if (To->getDefinition() || To->isBeingDefined()) { if (Kind == IDK_Everything) ImportDeclContext(From, /*ForceImport=*/true); return false; } To->startDefinition(); QualType T = Importer.Import(Importer.getFromContext().getTypeDeclType(From)); if (T.isNull()) return true; QualType ToPromotionType = Importer.Import(From->getPromotionType()); if (ToPromotionType.isNull()) return true; if (shouldForceImportDeclContext(Kind)) ImportDeclContext(From, /*ForceImport=*/true); // FIXME: we might need to merge the number of positive or negative bits // if the enumerator lists don't match. To->completeDefinition(T, ToPromotionType, From->getNumPositiveBits(), From->getNumNegativeBits()); return false; } TemplateParameterList *ASTNodeImporter::ImportTemplateParameterList( TemplateParameterList *Params) { SmallVector
ToParams; ToParams.reserve(Params->size()); for (TemplateParameterList::iterator P = Params->begin(), PEnd = Params->end(); P != PEnd; ++P) { Decl *To = Importer.Import(*P); if (!To) return nullptr; ToParams.push_back(cast
(To)); } return TemplateParameterList::Create(Importer.getToContext(), Importer.Import(Params->getTemplateLoc()), Importer.Import(Params->getLAngleLoc()), ToParams, Importer.Import(Params->getRAngleLoc())); } TemplateArgument ASTNodeImporter::ImportTemplateArgument(const TemplateArgument &From) { switch (From.getKind()) { case TemplateArgument::Null: return TemplateArgument(); case TemplateArgument::Type: { QualType ToType = Importer.Import(From.getAsType()); if (ToType.isNull()) return TemplateArgument(); return TemplateArgument(ToType); } case TemplateArgument::Integral: { QualType ToType = Importer.Import(From.getIntegralType()); if (ToType.isNull()) return TemplateArgument(); return TemplateArgument(From, ToType); } case TemplateArgument::Declaration: { ValueDecl *To = cast_or_null
(Importer.Import(From.getAsDecl())); QualType ToType = Importer.Import(From.getParamTypeForDecl()); if (!To || ToType.isNull()) return TemplateArgument(); return TemplateArgument(To, ToType); } case TemplateArgument::NullPtr: { QualType ToType = Importer.Import(From.getNullPtrType()); if (ToType.isNull()) return TemplateArgument(); return TemplateArgument(ToType, /*isNullPtr*/true); } case TemplateArgument::Template: { TemplateName ToTemplate = Importer.Import(From.getAsTemplate()); if (ToTemplate.isNull()) return TemplateArgument(); return TemplateArgument(ToTemplate); } case TemplateArgument::TemplateExpansion: { TemplateName ToTemplate = Importer.Import(From.getAsTemplateOrTemplatePattern()); if (ToTemplate.isNull()) return TemplateArgument(); return TemplateArgument(ToTemplate, From.getNumTemplateExpansions()); } case TemplateArgument::Expression: if (Expr *ToExpr = Importer.Import(From.getAsExpr())) return TemplateArgument(ToExpr); return TemplateArgument(); case TemplateArgument::Pack: { SmallVector
ToPack; ToPack.reserve(From.pack_size()); if (ImportTemplateArguments(From.pack_begin(), From.pack_size(), ToPack)) return TemplateArgument(); return TemplateArgument( llvm::makeArrayRef(ToPack).copy(Importer.getToContext())); } } llvm_unreachable("Invalid template argument kind"); } bool ASTNodeImporter::ImportTemplateArguments(const TemplateArgument *FromArgs, unsigned NumFromArgs, SmallVectorImpl
&ToArgs) { for (unsigned I = 0; I != NumFromArgs; ++I) { TemplateArgument To = ImportTemplateArgument(FromArgs[I]); if (To.isNull() && !FromArgs[I].isNull()) return true; ToArgs.push_back(To); } return false; } bool ASTNodeImporter::IsStructuralMatch(RecordDecl *FromRecord, RecordDecl *ToRecord, bool Complain) { // Eliminate a potential failure point where we attempt to re-import // something we're trying to import while completing ToRecord. Decl *ToOrigin = Importer.GetOriginalDecl(ToRecord); if (ToOrigin) { RecordDecl *ToOriginRecord = dyn_cast
(ToOrigin); if (ToOriginRecord) ToRecord = ToOriginRecord; } StructuralEquivalenceContext Ctx(Importer.getFromContext(), ToRecord->getASTContext(), Importer.getNonEquivalentDecls(), false, Complain); return Ctx.IsStructurallyEquivalent(FromRecord, ToRecord); } bool ASTNodeImporter::IsStructuralMatch(VarDecl *FromVar, VarDecl *ToVar, bool Complain) { StructuralEquivalenceContext Ctx( Importer.getFromContext(), Importer.getToContext(), Importer.getNonEquivalentDecls(), false, Complain); return Ctx.IsStructurallyEquivalent(FromVar, ToVar); } bool ASTNodeImporter::IsStructuralMatch(EnumDecl *FromEnum, EnumDecl *ToEnum) { StructuralEquivalenceContext Ctx(Importer.getFromContext(), Importer.getToContext(), Importer.getNonEquivalentDecls()); return Ctx.IsStructurallyEquivalent(FromEnum, ToEnum); } bool ASTNodeImporter::IsStructuralMatch(EnumConstantDecl *FromEC, EnumConstantDecl *ToEC) { const llvm::APSInt &FromVal = FromEC->getInitVal(); const llvm::APSInt &ToVal = ToEC->getInitVal(); return FromVal.isSigned() == ToVal.isSigned() && FromVal.getBitWidth() == ToVal.getBitWidth() && FromVal == ToVal; } bool ASTNodeImporter::IsStructuralMatch(ClassTemplateDecl *From, ClassTemplateDecl *To) { StructuralEquivalenceContext Ctx(Importer.getFromContext(), Importer.getToContext(), Importer.getNonEquivalentDecls()); return Ctx.IsStructurallyEquivalent(From, To); } bool ASTNodeImporter::IsStructuralMatch(VarTemplateDecl *From, VarTemplateDecl *To) { StructuralEquivalenceContext Ctx(Importer.getFromContext(), Importer.getToContext(), Importer.getNonEquivalentDecls()); return Ctx.IsStructurallyEquivalent(From, To); } Decl *ASTNodeImporter::VisitDecl(Decl *D) { Importer.FromDiag(D->getLocation(), diag::err_unsupported_ast_node) << D->getDeclKindName(); return nullptr; } Decl *ASTNodeImporter::VisitTranslationUnitDecl(TranslationUnitDecl *D) { TranslationUnitDecl *ToD = Importer.getToContext().getTranslationUnitDecl(); Importer.Imported(D, ToD); return ToD; } Decl *ASTNodeImporter::VisitAccessSpecDecl(AccessSpecDecl *D) { SourceLocation Loc = Importer.Import(D->getLocation()); SourceLocation ColonLoc = Importer.Import(D->getColonLoc()); // Import the context of this declaration. DeclContext *DC = Importer.ImportContext(D->getDeclContext()); if (!DC) return nullptr; AccessSpecDecl *accessSpecDecl = AccessSpecDecl::Create(Importer.getToContext(), D->getAccess(), DC, Loc, ColonLoc); if (!accessSpecDecl) return nullptr; // Lexical DeclContext and Semantic DeclContext // is always the same for the accessSpec. accessSpecDecl->setLexicalDeclContext(DC); DC->addDeclInternal(accessSpecDecl); return accessSpecDecl; } Decl *ASTNodeImporter::VisitNamespaceDecl(NamespaceDecl *D) { // Import the major distinguishing characteristics of this namespace. DeclContext *DC, *LexicalDC; DeclarationName Name; SourceLocation Loc; NamedDecl *ToD; if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc)) return nullptr; if (ToD) return ToD; NamespaceDecl *MergeWithNamespace = nullptr; if (!Name) { // This is an anonymous namespace. Adopt an existing anonymous // namespace if we can. // FIXME: Not testable. if (TranslationUnitDecl *TU = dyn_cast
(DC)) MergeWithNamespace = TU->getAnonymousNamespace(); else MergeWithNamespace = cast
(DC)->getAnonymousNamespace(); } else { SmallVector
ConflictingDecls; SmallVector
FoundDecls; DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls); for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) { if (!FoundDecls[I]->isInIdentifierNamespace(Decl::IDNS_Namespace)) continue; if (NamespaceDecl *FoundNS = dyn_cast
(FoundDecls[I])) { MergeWithNamespace = FoundNS; ConflictingDecls.clear(); break; } ConflictingDecls.push_back(FoundDecls[I]); } if (!ConflictingDecls.empty()) { Name = Importer.HandleNameConflict(Name, DC, Decl::IDNS_Namespace, ConflictingDecls.data(), ConflictingDecls.size()); } } // Create the "to" namespace, if needed. NamespaceDecl *ToNamespace = MergeWithNamespace; if (!ToNamespace) { ToNamespace = NamespaceDecl::Create(Importer.getToContext(), DC, D->isInline(), Importer.Import(D->getLocStart()), Loc, Name.getAsIdentifierInfo(), /*PrevDecl=*/nullptr); ToNamespace->setLexicalDeclContext(LexicalDC); LexicalDC->addDeclInternal(ToNamespace); // If this is an anonymous namespace, register it as the anonymous // namespace within its context. if (!Name) { if (TranslationUnitDecl *TU = dyn_cast
(DC)) TU->setAnonymousNamespace(ToNamespace); else cast
(DC)->setAnonymousNamespace(ToNamespace); } } Importer.Imported(D, ToNamespace); ImportDeclContext(D); return ToNamespace; } Decl *ASTNodeImporter::VisitTypedefNameDecl(TypedefNameDecl *D, bool IsAlias) { // Import the major distinguishing characteristics of this typedef. DeclContext *DC, *LexicalDC; DeclarationName Name; SourceLocation Loc; NamedDecl *ToD; if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc)) return nullptr; if (ToD) return ToD; // If this typedef is not in block scope, determine whether we've // seen a typedef with the same name (that we can merge with) or any // other entity by that name (which name lookup could conflict with). if (!DC->isFunctionOrMethod()) { SmallVector
ConflictingDecls; unsigned IDNS = Decl::IDNS_Ordinary; SmallVector
FoundDecls; DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls); for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) { if (!FoundDecls[I]->isInIdentifierNamespace(IDNS)) continue; if (TypedefNameDecl *FoundTypedef = dyn_cast
(FoundDecls[I])) { if (Importer.IsStructurallyEquivalent(D->getUnderlyingType(), FoundTypedef->getUnderlyingType())) return Importer.Imported(D, FoundTypedef); } ConflictingDecls.push_back(FoundDecls[I]); } if (!ConflictingDecls.empty()) { Name = Importer.HandleNameConflict(Name, DC, IDNS, ConflictingDecls.data(), ConflictingDecls.size()); if (!Name) return nullptr; } } // Import the underlying type of this typedef; QualType T = Importer.Import(D->getUnderlyingType()); if (T.isNull()) return nullptr; // Create the new typedef node. TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo()); SourceLocation StartL = Importer.Import(D->getLocStart()); TypedefNameDecl *ToTypedef; if (IsAlias) ToTypedef = TypeAliasDecl::Create(Importer.getToContext(), DC, StartL, Loc, Name.getAsIdentifierInfo(), TInfo); else ToTypedef = TypedefDecl::Create(Importer.getToContext(), DC, StartL, Loc, Name.getAsIdentifierInfo(), TInfo); ToTypedef->setAccess(D->getAccess()); ToTypedef->setLexicalDeclContext(LexicalDC); Importer.Imported(D, ToTypedef); LexicalDC->addDeclInternal(ToTypedef); return ToTypedef; } Decl *ASTNodeImporter::VisitTypedefDecl(TypedefDecl *D) { return VisitTypedefNameDecl(D, /*IsAlias=*/false); } Decl *ASTNodeImporter::VisitTypeAliasDecl(TypeAliasDecl *D) { return VisitTypedefNameDecl(D, /*IsAlias=*/true); } Decl *ASTNodeImporter::VisitLabelDecl(LabelDecl *D) { // Import the major distinguishing characteristics of this label. DeclContext *DC, *LexicalDC; DeclarationName Name; SourceLocation Loc; NamedDecl *ToD; if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc)) return nullptr; if (ToD) return ToD; assert(LexicalDC->isFunctionOrMethod()); LabelDecl *ToLabel = D->isGnuLocal() ? LabelDecl::Create(Importer.getToContext(), DC, Importer.Import(D->getLocation()), Name.getAsIdentifierInfo(), Importer.Import(D->getLocStart())) : LabelDecl::Create(Importer.getToContext(), DC, Importer.Import(D->getLocation()), Name.getAsIdentifierInfo()); Importer.Imported(D, ToLabel); LabelStmt *Label = cast_or_null
(Importer.Import(D->getStmt())); if (!Label) return nullptr; ToLabel->setStmt(Label); ToLabel->setLexicalDeclContext(LexicalDC); LexicalDC->addDeclInternal(ToLabel); return ToLabel; } Decl *ASTNodeImporter::VisitEnumDecl(EnumDecl *D) { // Import the major distinguishing characteristics of this enum. DeclContext *DC, *LexicalDC; DeclarationName Name; SourceLocation Loc; NamedDecl *ToD; if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc)) return nullptr; if (ToD) return ToD; // Figure out what enum name we're looking for. unsigned IDNS = Decl::IDNS_Tag; DeclarationName SearchName = Name; if (!SearchName && D->getTypedefNameForAnonDecl()) { SearchName = Importer.Import(D->getTypedefNameForAnonDecl()->getDeclName()); IDNS = Decl::IDNS_Ordinary; } else if (Importer.getToContext().getLangOpts().CPlusPlus) IDNS |= Decl::IDNS_Ordinary; // We may already have an enum of the same name; try to find and match it. if (!DC->isFunctionOrMethod() && SearchName) { SmallVector
ConflictingDecls; SmallVector
FoundDecls; DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls); for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) { if (!FoundDecls[I]->isInIdentifierNamespace(IDNS)) continue; Decl *Found = FoundDecls[I]; if (TypedefNameDecl *Typedef = dyn_cast
(Found)) { if (const TagType *Tag = Typedef->getUnderlyingType()->getAs
()) Found = Tag->getDecl(); } if (EnumDecl *FoundEnum = dyn_cast
(Found)) { if (IsStructuralMatch(D, FoundEnum)) return Importer.Imported(D, FoundEnum); } ConflictingDecls.push_back(FoundDecls[I]); } if (!ConflictingDecls.empty()) { Name = Importer.HandleNameConflict(Name, DC, IDNS, ConflictingDecls.data(), ConflictingDecls.size()); } } // Create the enum declaration. EnumDecl *D2 = EnumDecl::Create(Importer.getToContext(), DC, Importer.Import(D->getLocStart()), Loc, Name.getAsIdentifierInfo(), nullptr, D->isScoped(), D->isScopedUsingClassTag(), D->isFixed()); // Import the qualifier, if any. D2->setQualifierInfo(Importer.Import(D->getQualifierLoc())); D2->setAccess(D->getAccess()); D2->setLexicalDeclContext(LexicalDC); Importer.Imported(D, D2); LexicalDC->addDeclInternal(D2); // Import the integer type. QualType ToIntegerType = Importer.Import(D->getIntegerType()); if (ToIntegerType.isNull()) return nullptr; D2->setIntegerType(ToIntegerType); // Import the definition if (D->isCompleteDefinition() && ImportDefinition(D, D2)) return nullptr; return D2; } Decl *ASTNodeImporter::VisitRecordDecl(RecordDecl *D) { // If this record has a definition in the translation unit we're coming from, // but this particular declaration is not that definition, import the // definition and map to that. TagDecl *Definition = D->getDefinition(); if (Definition && Definition != D) { Decl *ImportedDef = Importer.Import(Definition); if (!ImportedDef) return nullptr; return Importer.Imported(D, ImportedDef); } // Import the major distinguishing characteristics of this record. DeclContext *DC, *LexicalDC; DeclarationName Name; SourceLocation Loc; NamedDecl *ToD; if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc)) return nullptr; if (ToD) return ToD; // Figure out what structure name we're looking for. unsigned IDNS = Decl::IDNS_Tag; DeclarationName SearchName = Name; if (!SearchName && D->getTypedefNameForAnonDecl()) { SearchName = Importer.Import(D->getTypedefNameForAnonDecl()->getDeclName()); IDNS = Decl::IDNS_Ordinary; } else if (Importer.getToContext().getLangOpts().CPlusPlus) IDNS |= Decl::IDNS_Ordinary; // We may already have a record of the same name; try to find and match it. RecordDecl *AdoptDecl = nullptr; if (!DC->isFunctionOrMethod()) { SmallVector
ConflictingDecls; SmallVector
FoundDecls; DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls); for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) { if (!FoundDecls[I]->isInIdentifierNamespace(IDNS)) continue; Decl *Found = FoundDecls[I]; if (TypedefNameDecl *Typedef = dyn_cast
(Found)) { if (const TagType *Tag = Typedef->getUnderlyingType()->getAs
()) Found = Tag->getDecl(); } if (RecordDecl *FoundRecord = dyn_cast
(Found)) { if (D->isAnonymousStructOrUnion() && FoundRecord->isAnonymousStructOrUnion()) { // If both anonymous structs/unions are in a record context, make sure // they occur in the same location in the context records. if (Optional
Index1 = findUntaggedStructOrUnionIndex(D)) { if (Optional
Index2 = findUntaggedStructOrUnionIndex(FoundRecord)) { if (*Index1 != *Index2) continue; } } } if (RecordDecl *FoundDef = FoundRecord->getDefinition()) { if ((SearchName && !D->isCompleteDefinition()) || (D->isCompleteDefinition() && D->isAnonymousStructOrUnion() == FoundDef->isAnonymousStructOrUnion() && IsStructuralMatch(D, FoundDef))) { // The record types structurally match, or the "from" translation // unit only had a forward declaration anyway; call it the same // function. // FIXME: For C++, we should also merge methods here. return Importer.Imported(D, FoundDef); } } else if (!D->isCompleteDefinition()) { // We have a forward declaration of this type, so adopt that forward // declaration rather than building a new one. // If one or both can be completed from external storage then try one // last time to complete and compare them before doing this. if (FoundRecord->hasExternalLexicalStorage() && !FoundRecord->isCompleteDefinition()) FoundRecord->getASTContext().getExternalSource()->CompleteType(FoundRecord); if (D->hasExternalLexicalStorage()) D->getASTContext().getExternalSource()->CompleteType(D); if (FoundRecord->isCompleteDefinition() && D->isCompleteDefinition() && !IsStructuralMatch(D, FoundRecord)) continue; AdoptDecl = FoundRecord; continue; } else if (!SearchName) { continue; } } ConflictingDecls.push_back(FoundDecls[I]); } if (!ConflictingDecls.empty() && SearchName) { Name = Importer.HandleNameConflict(Name, DC, IDNS, ConflictingDecls.data(), ConflictingDecls.size()); } } // Create the record declaration. RecordDecl *D2 = AdoptDecl; SourceLocation StartLoc = Importer.Import(D->getLocStart()); if (!D2) { CXXRecordDecl *D2CXX = nullptr; if (CXXRecordDecl *DCXX = llvm::dyn_cast
(D)) { if (DCXX->isLambda()) { TypeSourceInfo *TInfo = Importer.Import(DCXX->getLambdaTypeInfo()); D2CXX = CXXRecordDecl::CreateLambda(Importer.getToContext(), DC, TInfo, Loc, DCXX->isDependentLambda(), DCXX->isGenericLambda(), DCXX->getLambdaCaptureDefault()); Decl *CDecl = Importer.Import(DCXX->getLambdaContextDecl()); if (DCXX->getLambdaContextDecl() && !CDecl) return nullptr; D2CXX->setLambdaMangling(DCXX->getLambdaManglingNumber(), CDecl); } else if (DCXX->isInjectedClassName()) { // We have to be careful to do a similar dance to the one in // Sema::ActOnStartCXXMemberDeclarations CXXRecordDecl *const PrevDecl = nullptr; const bool DelayTypeCreation = true; D2CXX = CXXRecordDecl::Create( Importer.getToContext(), D->getTagKind(), DC, StartLoc, Loc, Name.getAsIdentifierInfo(), PrevDecl, DelayTypeCreation); Importer.getToContext().getTypeDeclType( D2CXX, llvm::dyn_cast
(DC)); } else { D2CXX = CXXRecordDecl::Create(Importer.getToContext(), D->getTagKind(), DC, StartLoc, Loc, Name.getAsIdentifierInfo()); } D2 = D2CXX; D2->setAccess(D->getAccess()); } else { D2 = RecordDecl::Create(Importer.getToContext(), D->getTagKind(), DC, StartLoc, Loc, Name.getAsIdentifierInfo()); } D2->setQualifierInfo(Importer.Import(D->getQualifierLoc())); D2->setLexicalDeclContext(LexicalDC); LexicalDC->addDeclInternal(D2); if (D->isAnonymousStructOrUnion()) D2->setAnonymousStructOrUnion(true); } Importer.Imported(D, D2); if (D->isCompleteDefinition() && ImportDefinition(D, D2, IDK_Default)) return nullptr; return D2; } Decl *ASTNodeImporter::VisitEnumConstantDecl(EnumConstantDecl *D) { // Import the major distinguishing characteristics of this enumerator. DeclContext *DC, *LexicalDC; DeclarationName Name; SourceLocation Loc; NamedDecl *ToD; if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc)) return nullptr; if (ToD) return ToD; QualType T = Importer.Import(D->getType()); if (T.isNull()) return nullptr; // Determine whether there are any other declarations with the same name and // in the same context. if (!LexicalDC->isFunctionOrMethod()) { SmallVector
ConflictingDecls; unsigned IDNS = Decl::IDNS_Ordinary; SmallVector
FoundDecls; DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls); for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) { if (!FoundDecls[I]->isInIdentifierNamespace(IDNS)) continue; if (EnumConstantDecl *FoundEnumConstant = dyn_cast
(FoundDecls[I])) { if (IsStructuralMatch(D, FoundEnumConstant)) return Importer.Imported(D, FoundEnumConstant); } ConflictingDecls.push_back(FoundDecls[I]); } if (!ConflictingDecls.empty()) { Name = Importer.HandleNameConflict(Name, DC, IDNS, ConflictingDecls.data(), ConflictingDecls.size()); if (!Name) return nullptr; } } Expr *Init = Importer.Import(D->getInitExpr()); if (D->getInitExpr() && !Init) return nullptr; EnumConstantDecl *ToEnumerator = EnumConstantDecl::Create(Importer.getToContext(), cast
(DC), Loc, Name.getAsIdentifierInfo(), T, Init, D->getInitVal()); ToEnumerator->setAccess(D->getAccess()); ToEnumerator->setLexicalDeclContext(LexicalDC); Importer.Imported(D, ToEnumerator); LexicalDC->addDeclInternal(ToEnumerator); return ToEnumerator; } Decl *ASTNodeImporter::VisitFunctionDecl(FunctionDecl *D) { // Import the major distinguishing characteristics of this function. DeclContext *DC, *LexicalDC; DeclarationName Name; SourceLocation Loc; NamedDecl *ToD; if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc)) return nullptr; if (ToD) return ToD; // Try to find a function in our own ("to") context with the same name, same // type, and in the same context as the function we're importing. if (!LexicalDC->isFunctionOrMethod()) { SmallVector
ConflictingDecls; unsigned IDNS = Decl::IDNS_Ordinary; SmallVector
FoundDecls; DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls); for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) { if (!FoundDecls[I]->isInIdentifierNamespace(IDNS)) continue; if (FunctionDecl *FoundFunction = dyn_cast
(FoundDecls[I])) { if (FoundFunction->hasExternalFormalLinkage() && D->hasExternalFormalLinkage()) { if (Importer.IsStructurallyEquivalent(D->getType(), FoundFunction->getType())) { // FIXME: Actually try to merge the body and other attributes. return Importer.Imported(D, FoundFunction); } // FIXME: Check for overloading more carefully, e.g., by boosting // Sema::IsOverload out to the AST library. // Function overloading is okay in C++. if (Importer.getToContext().getLangOpts().CPlusPlus) continue; // Complain about inconsistent function types. Importer.ToDiag(Loc, diag::err_odr_function_type_inconsistent) << Name << D->getType() << FoundFunction->getType(); Importer.ToDiag(FoundFunction->getLocation(), diag::note_odr_value_here) << FoundFunction->getType(); } } ConflictingDecls.push_back(FoundDecls[I]); } if (!ConflictingDecls.empty()) { Name = Importer.HandleNameConflict(Name, DC, IDNS, ConflictingDecls.data(), ConflictingDecls.size()); if (!Name) return nullptr; } } DeclarationNameInfo NameInfo(Name, Loc); // Import additional name location/type info. ImportDeclarationNameLoc(D->getNameInfo(), NameInfo); QualType FromTy = D->getType(); bool usedDifferentExceptionSpec = false; if (const FunctionProtoType * FromFPT = D->getType()->getAs
()) { FunctionProtoType::ExtProtoInfo FromEPI = FromFPT->getExtProtoInfo(); // FunctionProtoType::ExtProtoInfo's ExceptionSpecDecl can point to the // FunctionDecl that we are importing the FunctionProtoType for. // To avoid an infinite recursion when importing, create the FunctionDecl // with a simplified function type and update it afterwards. if (FromEPI.ExceptionSpec.SourceDecl || FromEPI.ExceptionSpec.SourceTemplate || FromEPI.ExceptionSpec.NoexceptExpr) { FunctionProtoType::ExtProtoInfo DefaultEPI; FromTy = Importer.getFromContext().getFunctionType( FromFPT->getReturnType(), FromFPT->getParamTypes(), DefaultEPI); usedDifferentExceptionSpec = true; } } // Import the type. QualType T = Importer.Import(FromTy); if (T.isNull()) return nullptr; // Import the function parameters. SmallVector
Parameters; for (auto P : D->parameters()) { ParmVarDecl *ToP = cast_or_null
(Importer.Import(P)); if (!ToP) return nullptr; Parameters.push_back(ToP); } // Create the imported function. TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo()); FunctionDecl *ToFunction = nullptr; SourceLocation InnerLocStart = Importer.Import(D->getInnerLocStart()); if (CXXConstructorDecl *FromConstructor = dyn_cast
(D)) { ToFunction = CXXConstructorDecl::Create(Importer.getToContext(), cast
(DC), InnerLocStart, NameInfo, T, TInfo, FromConstructor->isExplicit(), D->isInlineSpecified(), D->isImplicit(), D->isConstexpr()); if (unsigned NumInitializers = FromConstructor->getNumCtorInitializers()) { SmallVector
CtorInitializers; for (CXXCtorInitializer *I : FromConstructor->inits()) { CXXCtorInitializer *ToI = cast_or_null
(Importer.Import(I)); if (!ToI && I) return nullptr; CtorInitializers.push_back(ToI); } CXXCtorInitializer **Memory = new (Importer.getToContext()) CXXCtorInitializer *[NumInitializers]; std::copy(CtorInitializers.begin(), CtorInitializers.end(), Memory); CXXConstructorDecl *ToCtor = llvm::cast
(ToFunction); ToCtor->setCtorInitializers(Memory); ToCtor->setNumCtorInitializers(NumInitializers); } } else if (isa
(D)) { ToFunction = CXXDestructorDecl::Create(Importer.getToContext(), cast
(DC), InnerLocStart, NameInfo, T, TInfo, D->isInlineSpecified(), D->isImplicit()); } else if (CXXConversionDecl *FromConversion = dyn_cast
(D)) { ToFunction = CXXConversionDecl::Create(Importer.getToContext(), cast
(DC), InnerLocStart, NameInfo, T, TInfo, D->isInlineSpecified(), FromConversion->isExplicit(), D->isConstexpr(), Importer.Import(D->getLocEnd())); } else if (CXXMethodDecl *Method = dyn_cast
(D)) { ToFunction = CXXMethodDecl::Create(Importer.getToContext(), cast
(DC), InnerLocStart, NameInfo, T, TInfo, Method->getStorageClass(), Method->isInlineSpecified(), D->isConstexpr(), Importer.Import(D->getLocEnd())); } else { ToFunction = FunctionDecl::Create(Importer.getToContext(), DC, InnerLocStart, NameInfo, T, TInfo, D->getStorageClass(), D->isInlineSpecified(), D->hasWrittenPrototype(), D->isConstexpr()); } // Import the qualifier, if any. ToFunction->setQualifierInfo(Importer.Import(D->getQualifierLoc())); ToFunction->setAccess(D->getAccess()); ToFunction->setLexicalDeclContext(LexicalDC); ToFunction->setVirtualAsWritten(D->isVirtualAsWritten()); ToFunction->setTrivial(D->isTrivial()); ToFunction->setPure(D->isPure()); Importer.Imported(D, ToFunction); // Set the parameters. for (unsigned I = 0, N = Parameters.size(); I != N; ++I) { Parameters[I]->setOwningFunction(ToFunction); ToFunction->addDeclInternal(Parameters[I]); } ToFunction->setParams(Parameters); if (usedDifferentExceptionSpec) { // Update FunctionProtoType::ExtProtoInfo. QualType T = Importer.Import(D->getType()); if (T.isNull()) return nullptr; ToFunction->setType(T); } // Import the body, if any. if (Stmt *FromBody = D->getBody()) { if (Stmt *ToBody = Importer.Import(FromBody)) { ToFunction->setBody(ToBody); } } // FIXME: Other bits to merge? // Add this function to the lexical context. LexicalDC->addDeclInternal(ToFunction); return ToFunction; } Decl *ASTNodeImporter::VisitCXXMethodDecl(CXXMethodDecl *D) { return VisitFunctionDecl(D); } Decl *ASTNodeImporter::VisitCXXConstructorDecl(CXXConstructorDecl *D) { return VisitCXXMethodDecl(D); } Decl *ASTNodeImporter::VisitCXXDestructorDecl(CXXDestructorDecl *D) { return VisitCXXMethodDecl(D); } Decl *ASTNodeImporter::VisitCXXConversionDecl(CXXConversionDecl *D) { return VisitCXXMethodDecl(D); } static unsigned getFieldIndex(Decl *F) { RecordDecl *Owner = dyn_cast
(F->getDeclContext()); if (!Owner) return 0; unsigned Index = 1; for (const auto *D : Owner->noload_decls()) { if (D == F) return Index; if (isa
(*D) || isa
(*D)) ++Index; } return Index; } Decl *ASTNodeImporter::VisitFieldDecl(FieldDecl *D) { // Import the major distinguishing characteristics of a variable. DeclContext *DC, *LexicalDC; DeclarationName Name; SourceLocation Loc; NamedDecl *ToD; if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc)) return nullptr; if (ToD) return ToD; // Determine whether we've already imported this field. SmallVector
FoundDecls; DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls); for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) { if (FieldDecl *FoundField = dyn_cast
(FoundDecls[I])) { // For anonymous fields, match up by index. if (!Name && getFieldIndex(D) != getFieldIndex(FoundField)) continue; if (Importer.IsStructurallyEquivalent(D->getType(), FoundField->getType())) { Importer.Imported(D, FoundField); return FoundField; } Importer.ToDiag(Loc, diag::err_odr_field_type_inconsistent) << Name << D->getType() << FoundField->getType(); Importer.ToDiag(FoundField->getLocation(), diag::note_odr_value_here) << FoundField->getType(); return nullptr; } } // Import the type. QualType T = Importer.Import(D->getType()); if (T.isNull()) return nullptr; TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo()); Expr *BitWidth = Importer.Import(D->getBitWidth()); if (!BitWidth && D->getBitWidth()) return nullptr; FieldDecl *ToField = FieldDecl::Create(Importer.getToContext(), DC, Importer.Import(D->getInnerLocStart()), Loc, Name.getAsIdentifierInfo(), T, TInfo, BitWidth, D->isMutable(), D->getInClassInitStyle()); ToField->setAccess(D->getAccess()); ToField->setLexicalDeclContext(LexicalDC); if (Expr *FromInitializer = D->getInClassInitializer()) { Expr *ToInitializer = Importer.Import(FromInitializer); if (ToInitializer) ToField->setInClassInitializer(ToInitializer); else return nullptr; } ToField->setImplicit(D->isImplicit()); Importer.Imported(D, ToField); LexicalDC->addDeclInternal(ToField); return ToField; } Decl *ASTNodeImporter::VisitIndirectFieldDecl(IndirectFieldDecl *D) { // Import the major distinguishing characteristics of a variable. DeclContext *DC, *LexicalDC; DeclarationName Name; SourceLocation Loc; NamedDecl *ToD; if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc)) return nullptr; if (ToD) return ToD; // Determine whether we've already imported this field. SmallVector
FoundDecls; DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls); for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) { if (IndirectFieldDecl *FoundField = dyn_cast
(FoundDecls[I])) { // For anonymous indirect fields, match up by index. if (!Name && getFieldIndex(D) != getFieldIndex(FoundField)) continue; if (Importer.IsStructurallyEquivalent(D->getType(), FoundField->getType(), !Name.isEmpty())) { Importer.Imported(D, FoundField); return FoundField; } // If there are more anonymous fields to check, continue. if (!Name && I < N-1) continue; Importer.ToDiag(Loc, diag::err_odr_field_type_inconsistent) << Name << D->getType() << FoundField->getType(); Importer.ToDiag(FoundField->getLocation(), diag::note_odr_value_here) << FoundField->getType(); return nullptr; } } // Import the type. QualType T = Importer.Import(D->getType()); if (T.isNull()) return nullptr; NamedDecl **NamedChain = new (Importer.getToContext())NamedDecl*[D->getChainingSize()]; unsigned i = 0; for (auto *PI : D->chain()) { Decl *D = Importer.Import(PI); if (!D) return nullptr; NamedChain[i++] = cast
(D); } IndirectFieldDecl *ToIndirectField = IndirectFieldDecl::Create( Importer.getToContext(), DC, Loc, Name.getAsIdentifierInfo(), T, {NamedChain, D->getChainingSize()}); for (const auto *Attr : D->attrs()) ToIndirectField->addAttr(Attr->clone(Importer.getToContext())); ToIndirectField->setAccess(D->getAccess()); ToIndirectField->setLexicalDeclContext(LexicalDC); Importer.Imported(D, ToIndirectField); LexicalDC->addDeclInternal(ToIndirectField); return ToIndirectField; } Decl *ASTNodeImporter::VisitObjCIvarDecl(ObjCIvarDecl *D) { // Import the major distinguishing characteristics of an ivar. DeclContext *DC, *LexicalDC; DeclarationName Name; SourceLocation Loc; NamedDecl *ToD; if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc)) return nullptr; if (ToD) return ToD; // Determine whether we've already imported this ivar SmallVector
FoundDecls; DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls); for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) { if (ObjCIvarDecl *FoundIvar = dyn_cast
(FoundDecls[I])) { if (Importer.IsStructurallyEquivalent(D->getType(), FoundIvar->getType())) { Importer.Imported(D, FoundIvar); return FoundIvar; } Importer.ToDiag(Loc, diag::err_odr_ivar_type_inconsistent) << Name << D->getType() << FoundIvar->getType(); Importer.ToDiag(FoundIvar->getLocation(), diag::note_odr_value_here) << FoundIvar->getType(); return nullptr; } } // Import the type. QualType T = Importer.Import(D->getType()); if (T.isNull()) return nullptr; TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo()); Expr *BitWidth = Importer.Import(D->getBitWidth()); if (!BitWidth && D->getBitWidth()) return nullptr; ObjCIvarDecl *ToIvar = ObjCIvarDecl::Create(Importer.getToContext(), cast
(DC), Importer.Import(D->getInnerLocStart()), Loc, Name.getAsIdentifierInfo(), T, TInfo, D->getAccessControl(), BitWidth, D->getSynthesize()); ToIvar->setLexicalDeclContext(LexicalDC); Importer.Imported(D, ToIvar); LexicalDC->addDeclInternal(ToIvar); return ToIvar; } Decl *ASTNodeImporter::VisitVarDecl(VarDecl *D) { // Import the major distinguishing characteristics of a variable. DeclContext *DC, *LexicalDC; DeclarationName Name; SourceLocation Loc; NamedDecl *ToD; if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc)) return nullptr; if (ToD) return ToD; // Try to find a variable in our own ("to") context with the same name and // in the same context as the variable we're importing. if (D->isFileVarDecl()) { VarDecl *MergeWithVar = nullptr; SmallVector
ConflictingDecls; unsigned IDNS = Decl::IDNS_Ordinary; SmallVector
FoundDecls; DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls); for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) { if (!FoundDecls[I]->isInIdentifierNamespace(IDNS)) continue; if (VarDecl *FoundVar = dyn_cast
(FoundDecls[I])) { // We have found a variable that we may need to merge with. Check it. if (FoundVar->hasExternalFormalLinkage() && D->hasExternalFormalLinkage()) { if (Importer.IsStructurallyEquivalent(D->getType(), FoundVar->getType())) { MergeWithVar = FoundVar; break; } const ArrayType *FoundArray = Importer.getToContext().getAsArrayType(FoundVar->getType()); const ArrayType *TArray = Importer.getToContext().getAsArrayType(D->getType()); if (FoundArray && TArray) { if (isa
(FoundArray) && isa
(TArray)) { // Import the type. QualType T = Importer.Import(D->getType()); if (T.isNull()) return nullptr; FoundVar->setType(T); MergeWithVar = FoundVar; break; } else if (isa
(TArray) && isa
(FoundArray)) { MergeWithVar = FoundVar; break; } } Importer.ToDiag(Loc, diag::err_odr_variable_type_inconsistent) << Name << D->getType() << FoundVar->getType(); Importer.ToDiag(FoundVar->getLocation(), diag::note_odr_value_here) << FoundVar->getType(); } } ConflictingDecls.push_back(FoundDecls[I]); } if (MergeWithVar) { // An equivalent variable with external linkage has been found. Link // the two declarations, then merge them. Importer.Imported(D, MergeWithVar); if (VarDecl *DDef = D->getDefinition()) { if (VarDecl *ExistingDef = MergeWithVar->getDefinition()) { Importer.ToDiag(ExistingDef->getLocation(), diag::err_odr_variable_multiple_def) << Name; Importer.FromDiag(DDef->getLocation(), diag::note_odr_defined_here); } else { Expr *Init = Importer.Import(DDef->getInit()); MergeWithVar->setInit(Init); if (DDef->isInitKnownICE()) { EvaluatedStmt *Eval = MergeWithVar->ensureEvaluatedStmt(); Eval->CheckedICE = true; Eval->IsICE = DDef->isInitICE(); } } } return MergeWithVar; } if (!ConflictingDecls.empty()) { Name = Importer.HandleNameConflict(Name, DC, IDNS, ConflictingDecls.data(), ConflictingDecls.size()); if (!Name) return nullptr; } } // Import the type. QualType T = Importer.Import(D->getType()); if (T.isNull()) return nullptr; // Create the imported variable. TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo()); VarDecl *ToVar = VarDecl::Create(Importer.getToContext(), DC, Importer.Import(D->getInnerLocStart()), Loc, Name.getAsIdentifierInfo(), T, TInfo, D->getStorageClass()); ToVar->setQualifierInfo(Importer.Import(D->getQualifierLoc())); ToVar->setAccess(D->getAccess()); ToVar->setLexicalDeclContext(LexicalDC); Importer.Imported(D, ToVar); LexicalDC->addDeclInternal(ToVar); if (!D->isFileVarDecl() && D->isUsed()) ToVar->setIsUsed(); // Merge the initializer. if (ImportDefinition(D, ToVar)) return nullptr; return ToVar; } Decl *ASTNodeImporter::VisitImplicitParamDecl(ImplicitParamDecl *D) { // Parameters are created in the translation unit's context, then moved // into the function declaration's context afterward. DeclContext *DC = Importer.getToContext().getTranslationUnitDecl(); // Import the name of this declaration. DeclarationName Name = Importer.Import(D->getDeclName()); if (D->getDeclName() && !Name) return nullptr; // Import the location of this declaration. SourceLocation Loc = Importer.Import(D->getLocation()); // Import the parameter's type. QualType T = Importer.Import(D->getType()); if (T.isNull()) return nullptr; // Create the imported parameter. ImplicitParamDecl *ToParm = ImplicitParamDecl::Create(Importer.getToContext(), DC, Loc, Name.getAsIdentifierInfo(), T); return Importer.Imported(D, ToParm); } Decl *ASTNodeImporter::VisitParmVarDecl(ParmVarDecl *D) { // Parameters are created in the translation unit's context, then moved // into the function declaration's context afterward. DeclContext *DC = Importer.getToContext().getTranslationUnitDecl(); // Import the name of this declaration. DeclarationName Name = Importer.Import(D->getDeclName()); if (D->getDeclName() && !Name) return nullptr; // Import the location of this declaration. SourceLocation Loc = Importer.Import(D->getLocation()); // Import the parameter's type. QualType T = Importer.Import(D->getType()); if (T.isNull()) return nullptr; // Create the imported parameter. TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo()); ParmVarDecl *ToParm = ParmVarDecl::Create(Importer.getToContext(), DC, Importer.Import(D->getInnerLocStart()), Loc, Name.getAsIdentifierInfo(), T, TInfo, D->getStorageClass(), /*FIXME: Default argument*/nullptr); ToParm->setHasInheritedDefaultArg(D->hasInheritedDefaultArg()); if (D->isUsed()) ToParm->setIsUsed(); return Importer.Imported(D, ToParm); } Decl *ASTNodeImporter::VisitObjCMethodDecl(ObjCMethodDecl *D) { // Import the major distinguishing characteristics of a method. DeclContext *DC, *LexicalDC; DeclarationName Name; SourceLocation Loc; NamedDecl *ToD; if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc)) return nullptr; if (ToD) return ToD; SmallVector
FoundDecls; DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls); for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) { if (ObjCMethodDecl *FoundMethod = dyn_cast
(FoundDecls[I])) { if (FoundMethod->isInstanceMethod() != D->isInstanceMethod()) continue; // Check return types. if (!Importer.IsStructurallyEquivalent(D->getReturnType(), FoundMethod->getReturnType())) { Importer.ToDiag(Loc, diag::err_odr_objc_method_result_type_inconsistent) << D->isInstanceMethod() << Name << D->getReturnType() << FoundMethod->getReturnType(); Importer.ToDiag(FoundMethod->getLocation(), diag::note_odr_objc_method_here) << D->isInstanceMethod() << Name; return nullptr; } // Check the number of parameters. if (D->param_size() != FoundMethod->param_size()) { Importer.ToDiag(Loc, diag::err_odr_objc_method_num_params_inconsistent) << D->isInstanceMethod() << Name << D->param_size() << FoundMethod->param_size(); Importer.ToDiag(FoundMethod->getLocation(), diag::note_odr_objc_method_here) << D->isInstanceMethod() << Name; return nullptr; } // Check parameter types. for (ObjCMethodDecl::param_iterator P = D->param_begin(), PEnd = D->param_end(), FoundP = FoundMethod->param_begin(); P != PEnd; ++P, ++FoundP) { if (!Importer.IsStructurallyEquivalent((*P)->getType(), (*FoundP)->getType())) { Importer.FromDiag((*P)->getLocation(), diag::err_odr_objc_method_param_type_inconsistent) << D->isInstanceMethod() << Name << (*P)->getType() << (*FoundP)->getType(); Importer.ToDiag((*FoundP)->getLocation(), diag::note_odr_value_here) << (*FoundP)->getType(); return nullptr; } } // Check variadic/non-variadic. // Check the number of parameters. if (D->isVariadic() != FoundMethod->isVariadic()) { Importer.ToDiag(Loc, diag::err_odr_objc_method_variadic_inconsistent) << D->isInstanceMethod() << Name; Importer.ToDiag(FoundMethod->getLocation(), diag::note_odr_objc_method_here) << D->isInstanceMethod() << Name; return nullptr; } // FIXME: Any other bits we need to merge? return Importer.Imported(D, FoundMethod); } } // Import the result type. QualType ResultTy = Importer.Import(D->getReturnType()); if (ResultTy.isNull()) return nullptr; TypeSourceInfo *ReturnTInfo = Importer.Import(D->getReturnTypeSourceInfo()); ObjCMethodDecl *ToMethod = ObjCMethodDecl::Create( Importer.getToContext(), Loc, Importer.Import(D->getLocEnd()), Name.getObjCSelector(), ResultTy, ReturnTInfo, DC, D->isInstanceMethod(), D->isVariadic(), D->isPropertyAccessor(), D->isImplicit(), D->isDefined(), D->getImplementationControl(), D->hasRelatedResultType()); // FIXME: When we decide to merge method definitions, we'll need to // deal with implicit parameters. // Import the parameters SmallVector
ToParams; for (auto *FromP : D->parameters()) { ParmVarDecl *ToP = cast_or_null
(Importer.Import(FromP)); if (!ToP) return nullptr; ToParams.push_back(ToP); } // Set the parameters. for (unsigned I = 0, N = ToParams.size(); I != N; ++I) { ToParams[I]->setOwningFunction(ToMethod); ToMethod->addDeclInternal(ToParams[I]); } SmallVector
SelLocs; D->getSelectorLocs(SelLocs); ToMethod->setMethodParams(Importer.getToContext(), ToParams, SelLocs); ToMethod->setLexicalDeclContext(LexicalDC); Importer.Imported(D, ToMethod); LexicalDC->addDeclInternal(ToMethod); return ToMethod; } Decl *ASTNodeImporter::VisitObjCTypeParamDecl(ObjCTypeParamDecl *D) { // Import the major distinguishing characteristics of a category. DeclContext *DC, *LexicalDC; DeclarationName Name; SourceLocation Loc; NamedDecl *ToD; if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc)) return nullptr; if (ToD) return ToD; TypeSourceInfo *BoundInfo = Importer.Import(D->getTypeSourceInfo()); if (!BoundInfo) return nullptr; ObjCTypeParamDecl *Result = ObjCTypeParamDecl::Create( Importer.getToContext(), DC, D->getVariance(), Importer.Import(D->getVarianceLoc()), D->getIndex(), Importer.Import(D->getLocation()), Name.getAsIdentifierInfo(), Importer.Import(D->getColonLoc()), BoundInfo); Importer.Imported(D, Result); Result->setLexicalDeclContext(LexicalDC); return Result; } Decl *ASTNodeImporter::VisitObjCCategoryDecl(ObjCCategoryDecl *D) { // Import the major distinguishing characteristics of a category. DeclContext *DC, *LexicalDC; DeclarationName Name; SourceLocation Loc; NamedDecl *ToD; if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc)) return nullptr; if (ToD) return ToD; ObjCInterfaceDecl *ToInterface = cast_or_null
(Importer.Import(D->getClassInterface())); if (!ToInterface) return nullptr; // Determine if we've already encountered this category. ObjCCategoryDecl *MergeWithCategory = ToInterface->FindCategoryDeclaration(Name.getAsIdentifierInfo()); ObjCCategoryDecl *ToCategory = MergeWithCategory; if (!ToCategory) { ToCategory = ObjCCategoryDecl::Create(Importer.getToContext(), DC, Importer.Import(D->getAtStartLoc()), Loc, Importer.Import(D->getCategoryNameLoc()), Name.getAsIdentifierInfo(), ToInterface, /*TypeParamList=*/nullptr, Importer.Import(D->getIvarLBraceLoc()), Importer.Import(D->getIvarRBraceLoc())); ToCategory->setLexicalDeclContext(LexicalDC); LexicalDC->addDeclInternal(ToCategory); Importer.Imported(D, ToCategory); // Import the type parameter list after calling Imported, to avoid // loops when bringing in their DeclContext. ToCategory->setTypeParamList(ImportObjCTypeParamList( D->getTypeParamList())); // Import protocols SmallVector
Protocols; SmallVector
ProtocolLocs; ObjCCategoryDecl::protocol_loc_iterator FromProtoLoc = D->protocol_loc_begin(); for (ObjCCategoryDecl::protocol_iterator FromProto = D->protocol_begin(), FromProtoEnd = D->protocol_end(); FromProto != FromProtoEnd; ++FromProto, ++FromProtoLoc) { ObjCProtocolDecl *ToProto = cast_or_null
(Importer.Import(*FromProto)); if (!ToProto) return nullptr; Protocols.push_back(ToProto); ProtocolLocs.push_back(Importer.Import(*FromProtoLoc)); } // FIXME: If we're merging, make sure that the protocol list is the same. ToCategory->setProtocolList(Protocols.data(), Protocols.size(), ProtocolLocs.data(), Importer.getToContext()); } else { Importer.Imported(D, ToCategory); } // Import all of the members of this category. ImportDeclContext(D); // If we have an implementation, import it as well. if (D->getImplementation()) { ObjCCategoryImplDecl *Impl = cast_or_null
( Importer.Import(D->getImplementation())); if (!Impl) return nullptr; ToCategory->setImplementation(Impl); } return ToCategory; } bool ASTNodeImporter::ImportDefinition(ObjCProtocolDecl *From, ObjCProtocolDecl *To, ImportDefinitionKind Kind) { if (To->getDefinition()) { if (shouldForceImportDeclContext(Kind)) ImportDeclContext(From); return false; } // Start the protocol definition To->startDefinition(); // Import protocols SmallVector
Protocols; SmallVector
ProtocolLocs; ObjCProtocolDecl::protocol_loc_iterator FromProtoLoc = From->protocol_loc_begin(); for (ObjCProtocolDecl::protocol_iterator FromProto = From->protocol_begin(), FromProtoEnd = From->protocol_end(); FromProto != FromProtoEnd; ++FromProto, ++FromProtoLoc) { ObjCProtocolDecl *ToProto = cast_or_null
(Importer.Import(*FromProto)); if (!ToProto) return true; Protocols.push_back(ToProto); ProtocolLocs.push_back(Importer.Import(*FromProtoLoc)); } // FIXME: If we're merging, make sure that the protocol list is the same. To->setProtocolList(Protocols.data(), Protocols.size(), ProtocolLocs.data(), Importer.getToContext()); if (shouldForceImportDeclContext(Kind)) { // Import all of the members of this protocol. ImportDeclContext(From, /*ForceImport=*/true); } return false; } Decl *ASTNodeImporter::VisitObjCProtocolDecl(ObjCProtocolDecl *D) { // If this protocol has a definition in the translation unit we're coming // from, but this particular declaration is not that definition, import the // definition and map to that. ObjCProtocolDecl *Definition = D->getDefinition(); if (Definition && Definition != D) { Decl *ImportedDef = Importer.Import(Definition); if (!ImportedDef) return nullptr; return Importer.Imported(D, ImportedDef); } // Import the major distinguishing characteristics of a protocol. DeclContext *DC, *LexicalDC; DeclarationName Name; SourceLocation Loc; NamedDecl *ToD; if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc)) return nullptr; if (ToD) return ToD; ObjCProtocolDecl *MergeWithProtocol = nullptr; SmallVector
FoundDecls; DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls); for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) { if (!FoundDecls[I]->isInIdentifierNamespace(Decl::IDNS_ObjCProtocol)) continue; if ((MergeWithProtocol = dyn_cast
(FoundDecls[I]))) break; } ObjCProtocolDecl *ToProto = MergeWithProtocol; if (!ToProto) { ToProto = ObjCProtocolDecl::Create(Importer.getToContext(), DC, Name.getAsIdentifierInfo(), Loc, Importer.Import(D->getAtStartLoc()), /*PrevDecl=*/nullptr); ToProto->setLexicalDeclContext(LexicalDC); LexicalDC->addDeclInternal(ToProto); } Importer.Imported(D, ToProto); if (D->isThisDeclarationADefinition() && ImportDefinition(D, ToProto)) return nullptr; return ToProto; } Decl *ASTNodeImporter::VisitLinkageSpecDecl(LinkageSpecDecl *D) { DeclContext *DC = Importer.ImportContext(D->getDeclContext()); DeclContext *LexicalDC = Importer.ImportContext(D->getLexicalDeclContext()); SourceLocation ExternLoc = Importer.Import(D->getExternLoc()); SourceLocation LangLoc = Importer.Import(D->getLocation()); bool HasBraces = D->hasBraces(); LinkageSpecDecl *ToLinkageSpec = LinkageSpecDecl::Create(Importer.getToContext(), DC, ExternLoc, LangLoc, D->getLanguage(), HasBraces); if (HasBraces) { SourceLocation RBraceLoc = Importer.Import(D->getRBraceLoc()); ToLinkageSpec->setRBraceLoc(RBraceLoc); } ToLinkageSpec->setLexicalDeclContext(LexicalDC); LexicalDC->addDeclInternal(ToLinkageSpec); Importer.Imported(D, ToLinkageSpec); return ToLinkageSpec; } bool ASTNodeImporter::ImportDefinition(ObjCInterfaceDecl *From, ObjCInterfaceDecl *To, ImportDefinitionKind Kind) { if (To->getDefinition()) { // Check consistency of superclass. ObjCInterfaceDecl *FromSuper = From->getSuperClass(); if (FromSuper) { FromSuper = cast_or_null
(Importer.Import(FromSuper)); if (!FromSuper) return true; } ObjCInterfaceDecl *ToSuper = To->getSuperClass(); if ((bool)FromSuper != (bool)ToSuper || (FromSuper && !declaresSameEntity(FromSuper, ToSuper))) { Importer.ToDiag(To->getLocation(), diag::err_odr_objc_superclass_inconsistent) << To->getDeclName(); if (ToSuper) Importer.ToDiag(To->getSuperClassLoc(), diag::note_odr_objc_superclass) << To->getSuperClass()->getDeclName(); else Importer.ToDiag(To->getLocation(), diag::note_odr_objc_missing_superclass); if (From->getSuperClass()) Importer.FromDiag(From->getSuperClassLoc(), diag::note_odr_objc_superclass) << From->getSuperClass()->getDeclName(); else Importer.FromDiag(From->getLocation(), diag::note_odr_objc_missing_superclass); } if (shouldForceImportDeclContext(Kind)) ImportDeclContext(From); return false; } // Start the definition. To->startDefinition(); // If this class has a superclass, import it. if (From->getSuperClass()) { TypeSourceInfo *SuperTInfo = Importer.Import(From->getSuperClassTInfo()); if (!SuperTInfo) return true; To->setSuperClass(SuperTInfo); } // Import protocols SmallVector
Protocols; SmallVector
ProtocolLocs; ObjCInterfaceDecl::protocol_loc_iterator FromProtoLoc = From->protocol_loc_begin(); for (ObjCInterfaceDecl::protocol_iterator FromProto = From->protocol_begin(), FromProtoEnd = From->protocol_end(); FromProto != FromProtoEnd; ++FromProto, ++FromProtoLoc) { ObjCProtocolDecl *ToProto = cast_or_null
(Importer.Import(*FromProto)); if (!ToProto) return true; Protocols.push_back(ToProto); ProtocolLocs.push_back(Importer.Import(*FromProtoLoc)); } // FIXME: If we're merging, make sure that the protocol list is the same. To->setProtocolList(Protocols.data(), Protocols.size(), ProtocolLocs.data(), Importer.getToContext()); // Import categories. When the categories themselves are imported, they'll // hook themselves into this interface. for (auto *Cat : From->known_categories()) Importer.Import(Cat); // If we have an @implementation, import it as well. if (From->getImplementation()) { ObjCImplementationDecl *Impl = cast_or_null
( Importer.Import(From->getImplementation())); if (!Impl) return true; To->setImplementation(Impl); } if (shouldForceImportDeclContext(Kind)) { // Import all of the members of this class. ImportDeclContext(From, /*ForceImport=*/true); } return false; } ObjCTypeParamList * ASTNodeImporter::ImportObjCTypeParamList(ObjCTypeParamList *list) { if (!list) return nullptr; SmallVector
toTypeParams; for (auto fromTypeParam : *list) { auto toTypeParam = cast_or_null
( Importer.Import(fromTypeParam)); if (!toTypeParam) return nullptr; toTypeParams.push_back(toTypeParam); } return ObjCTypeParamList::create(Importer.getToContext(), Importer.Import(list->getLAngleLoc()), toTypeParams, Importer.Import(list->getRAngleLoc())); } Decl *ASTNodeImporter::VisitObjCInterfaceDecl(ObjCInterfaceDecl *D) { // If this class has a definition in the translation unit we're coming from, // but this particular declaration is not that definition, import the // definition and map to that. ObjCInterfaceDecl *Definition = D->getDefinition(); if (Definition && Definition != D) { Decl *ImportedDef = Importer.Import(Definition); if (!ImportedDef) return nullptr; return Importer.Imported(D, ImportedDef); } // Import the major distinguishing characteristics of an @interface. DeclContext *DC, *LexicalDC; DeclarationName Name; SourceLocation Loc; NamedDecl *ToD; if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc)) return nullptr; if (ToD) return ToD; // Look for an existing interface with the same name. ObjCInterfaceDecl *MergeWithIface = nullptr; SmallVector
FoundDecls; DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls); for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) { if (!FoundDecls[I]->isInIdentifierNamespace(Decl::IDNS_Ordinary)) continue; if ((MergeWithIface = dyn_cast
(FoundDecls[I]))) break; } // Create an interface declaration, if one does not already exist. ObjCInterfaceDecl *ToIface = MergeWithIface; if (!ToIface) { ToIface = ObjCInterfaceDecl::Create(Importer.getToContext(), DC, Importer.Import(D->getAtStartLoc()), Name.getAsIdentifierInfo(), /*TypeParamList=*/nullptr, /*PrevDecl=*/nullptr, Loc, D->isImplicitInterfaceDecl()); ToIface->setLexicalDeclContext(LexicalDC); LexicalDC->addDeclInternal(ToIface); } Importer.Imported(D, ToIface); // Import the type parameter list after calling Imported, to avoid // loops when bringing in their DeclContext. ToIface->setTypeParamList(ImportObjCTypeParamList( D->getTypeParamListAsWritten())); if (D->isThisDeclarationADefinition() && ImportDefinition(D, ToIface)) return nullptr; return ToIface; } Decl *ASTNodeImporter::VisitObjCCategoryImplDecl(ObjCCategoryImplDecl *D) { ObjCCategoryDecl *Category = cast_or_null
( Importer.Import(D->getCategoryDecl())); if (!Category) return nullptr; ObjCCategoryImplDecl *ToImpl = Category->getImplementation(); if (!ToImpl) { DeclContext *DC = Importer.ImportContext(D->getDeclContext()); if (!DC) return nullptr; SourceLocation CategoryNameLoc = Importer.Import(D->getCategoryNameLoc()); ToImpl = ObjCCategoryImplDecl::Create(Importer.getToContext(), DC, Importer.Import(D->getIdentifier()), Category->getClassInterface(), Importer.Import(D->getLocation()), Importer.Import(D->getAtStartLoc()), CategoryNameLoc); DeclContext *LexicalDC = DC; if (D->getDeclContext() != D->getLexicalDeclContext()) { LexicalDC = Importer.ImportContext(D->getLexicalDeclContext()); if (!LexicalDC) return nullptr; ToImpl->setLexicalDeclContext(LexicalDC); } LexicalDC->addDeclInternal(ToImpl); Category->setImplementation(ToImpl); } Importer.Imported(D, ToImpl); ImportDeclContext(D); return ToImpl; } Decl *ASTNodeImporter::VisitObjCImplementationDecl(ObjCImplementationDecl *D) { // Find the corresponding interface. ObjCInterfaceDecl *Iface = cast_or_null
( Importer.Import(D->getClassInterface())); if (!Iface) return nullptr; // Import the superclass, if any. ObjCInterfaceDecl *Super = nullptr; if (D->getSuperClass()) { Super = cast_or_null
( Importer.Import(D->getSuperClass())); if (!Super) return nullptr; } ObjCImplementationDecl *Impl = Iface->getImplementation(); if (!Impl) { // We haven't imported an implementation yet. Create a new @implementation // now. Impl = ObjCImplementationDecl::Create(Importer.getToContext(), Importer.ImportContext(D->getDeclContext()), Iface, Super, Importer.Import(D->getLocation()), Importer.Import(D->getAtStartLoc()), Importer.Import(D->getSuperClassLoc()), Importer.Import(D->getIvarLBraceLoc()), Importer.Import(D->getIvarRBraceLoc())); if (D->getDeclContext() != D->getLexicalDeclContext()) { DeclContext *LexicalDC = Importer.ImportContext(D->getLexicalDeclContext()); if (!LexicalDC) return nullptr; Impl->setLexicalDeclContext(LexicalDC); } // Associate the implementation with the class it implements. Iface->setImplementation(Impl); Importer.Imported(D, Iface->getImplementation()); } else { Importer.Imported(D, Iface->getImplementation()); // Verify that the existing @implementation has the same superclass. if ((Super && !Impl->getSuperClass()) || (!Super && Impl->getSuperClass()) || (Super && Impl->getSuperClass() && !declaresSameEntity(Super->getCanonicalDecl(), Impl->getSuperClass()))) { Importer.ToDiag(Impl->getLocation(), diag::err_odr_objc_superclass_inconsistent) << Iface->getDeclName(); // FIXME: It would be nice to have the location of the superclass // below. if (Impl->getSuperClass()) Importer.ToDiag(Impl->getLocation(), diag::note_odr_objc_superclass) << Impl->getSuperClass()->getDeclName(); else Importer.ToDiag(Impl->getLocation(), diag::note_odr_objc_missing_superclass); if (D->getSuperClass()) Importer.FromDiag(D->getLocation(), diag::note_odr_objc_superclass) << D->getSuperClass()->getDeclName(); else Importer.FromDiag(D->getLocation(), diag::note_odr_objc_missing_superclass); return nullptr; } } // Import all of the members of this @implementation. ImportDeclContext(D); return Impl; } Decl *ASTNodeImporter::VisitObjCPropertyDecl(ObjCPropertyDecl *D) { // Import the major distinguishing characteristics of an @property. DeclContext *DC, *LexicalDC; DeclarationName Name; SourceLocation Loc; NamedDecl *ToD; if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc)) return nullptr; if (ToD) return ToD; // Check whether we have already imported this property. SmallVector
FoundDecls; DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls); for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) { if (ObjCPropertyDecl *FoundProp = dyn_cast
(FoundDecls[I])) { // Check property types. if (!Importer.IsStructurallyEquivalent(D->getType(), FoundProp->getType())) { Importer.ToDiag(Loc, diag::err_odr_objc_property_type_inconsistent) << Name << D->getType() << FoundProp->getType(); Importer.ToDiag(FoundProp->getLocation(), diag::note_odr_value_here) << FoundProp->getType(); return nullptr; } // FIXME: Check property attributes, getters, setters, etc.? // Consider these properties to be equivalent. Importer.Imported(D, FoundProp); return FoundProp; } } // Import the type. TypeSourceInfo *TSI = Importer.Import(D->getTypeSourceInfo()); if (!TSI) return nullptr; // Create the new property. ObjCPropertyDecl *ToProperty = ObjCPropertyDecl::Create(Importer.getToContext(), DC, Loc, Name.getAsIdentifierInfo(), Importer.Import(D->getAtLoc()), Importer.Import(D->getLParenLoc()), Importer.Import(D->getType()), TSI, D->getPropertyImplementation()); Importer.Imported(D, ToProperty); ToProperty->setLexicalDeclContext(LexicalDC); LexicalDC->addDeclInternal(ToProperty); ToProperty->setPropertyAttributes(D->getPropertyAttributes()); ToProperty->setPropertyAttributesAsWritten( D->getPropertyAttributesAsWritten()); ToProperty->setGetterName(Importer.Import(D->getGetterName())); ToProperty->setSetterName(Importer.Import(D->getSetterName())); ToProperty->setGetterMethodDecl( cast_or_null
(Importer.Import(D->getGetterMethodDecl()))); ToProperty->setSetterMethodDecl( cast_or_null
(Importer.Import(D->getSetterMethodDecl()))); ToProperty->setPropertyIvarDecl( cast_or_null
(Importer.Import(D->getPropertyIvarDecl()))); return ToProperty; } Decl *ASTNodeImporter::VisitObjCPropertyImplDecl(ObjCPropertyImplDecl *D) { ObjCPropertyDecl *Property = cast_or_null
( Importer.Import(D->getPropertyDecl())); if (!Property) return nullptr; DeclContext *DC = Importer.ImportContext(D->getDeclContext()); if (!DC) return nullptr; // Import the lexical declaration context. DeclContext *LexicalDC = DC; if (D->getDeclContext() != D->getLexicalDeclContext()) { LexicalDC = Importer.ImportContext(D->getLexicalDeclContext()); if (!LexicalDC) return nullptr; } ObjCImplDecl *InImpl = dyn_cast
(LexicalDC); if (!InImpl) return nullptr; // Import the ivar (for an @synthesize). ObjCIvarDecl *Ivar = nullptr; if (D->getPropertyIvarDecl()) { Ivar = cast_or_null
( Importer.Import(D->getPropertyIvarDecl())); if (!Ivar) return nullptr; } ObjCPropertyImplDecl *ToImpl = InImpl->FindPropertyImplDecl(Property->getIdentifier(), Property->getQueryKind()); if (!ToImpl) { ToImpl = ObjCPropertyImplDecl::Create(Importer.getToContext(), DC, Importer.Import(D->getLocStart()), Importer.Import(D->getLocation()), Property, D->getPropertyImplementation(), Ivar, Importer.Import(D->getPropertyIvarDeclLoc())); ToImpl->setLexicalDeclContext(LexicalDC); Importer.Imported(D, ToImpl); LexicalDC->addDeclInternal(ToImpl); } else { // Check that we have the same kind of property implementation (@synthesize // vs. @dynamic). if (D->getPropertyImplementation() != ToImpl->getPropertyImplementation()) { Importer.ToDiag(ToImpl->getLocation(), diag::err_odr_objc_property_impl_kind_inconsistent) << Property->getDeclName() << (ToImpl->getPropertyImplementation() == ObjCPropertyImplDecl::Dynamic); Importer.FromDiag(D->getLocation(), diag::note_odr_objc_property_impl_kind) << D->getPropertyDecl()->getDeclName() << (D->getPropertyImplementation() == ObjCPropertyImplDecl::Dynamic); return nullptr; } // For @synthesize, check that we have the same if (D->getPropertyImplementation() == ObjCPropertyImplDecl::Synthesize && Ivar != ToImpl->getPropertyIvarDecl()) { Importer.ToDiag(ToImpl->getPropertyIvarDeclLoc(), diag::err_odr_objc_synthesize_ivar_inconsistent) << Property->getDeclName() << ToImpl->getPropertyIvarDecl()->getDeclName() << Ivar->getDeclName(); Importer.FromDiag(D->getPropertyIvarDeclLoc(), diag::note_odr_objc_synthesize_ivar_here) << D->getPropertyIvarDecl()->getDeclName(); return nullptr; } // Merge the existing implementation with the new implementation. Importer.Imported(D, ToImpl); } return ToImpl; } Decl *ASTNodeImporter::VisitTemplateTypeParmDecl(TemplateTypeParmDecl *D) { // For template arguments, we adopt the translation unit as our declaration // context. This context will be fixed when the actual template declaration // is created. // FIXME: Import default argument. return TemplateTypeParmDecl::Create(Importer.getToContext(), Importer.getToContext().getTranslationUnitDecl(), Importer.Import(D->getLocStart()), Importer.Import(D->getLocation()), D->getDepth(), D->getIndex(), Importer.Import(D->getIdentifier()), D->wasDeclaredWithTypename(), D->isParameterPack()); } Decl * ASTNodeImporter::VisitNonTypeTemplateParmDecl(NonTypeTemplateParmDecl *D) { // Import the name of this declaration. DeclarationName Name = Importer.Import(D->getDeclName()); if (D->getDeclName() && !Name) return nullptr; // Import the location of this declaration. SourceLocation Loc = Importer.Import(D->getLocation()); // Import the type of this declaration. QualType T = Importer.Import(D->getType()); if (T.isNull()) return nullptr; // Import type-source information. TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo()); if (D->getTypeSourceInfo() && !TInfo) return nullptr; // FIXME: Import default argument. return NonTypeTemplateParmDecl::Create(Importer.getToContext(), Importer.getToContext().getTranslationUnitDecl(), Importer.Import(D->getInnerLocStart()), Loc, D->getDepth(), D->getPosition(), Name.getAsIdentifierInfo(), T, D->isParameterPack(), TInfo); } Decl * ASTNodeImporter::VisitTemplateTemplateParmDecl(TemplateTemplateParmDecl *D) { // Import the name of this declaration. DeclarationName Name = Importer.Import(D->getDeclName()); if (D->getDeclName() && !Name) return nullptr; // Import the location of this declaration. SourceLocation Loc = Importer.Import(D->getLocation()); // Import template parameters. TemplateParameterList *TemplateParams = ImportTemplateParameterList(D->getTemplateParameters()); if (!TemplateParams) return nullptr; // FIXME: Import default argument. return TemplateTemplateParmDecl::Create(Importer.getToContext(), Importer.getToContext().getTranslationUnitDecl(), Loc, D->getDepth(), D->getPosition(), D->isParameterPack(), Name.getAsIdentifierInfo(), TemplateParams); } Decl *ASTNodeImporter::VisitClassTemplateDecl(ClassTemplateDecl *D) { // If this record has a definition in the translation unit we're coming from, // but this particular declaration is not that definition, import the // definition and map to that. CXXRecordDecl *Definition = cast_or_null
(D->getTemplatedDecl()->getDefinition()); if (Definition && Definition != D->getTemplatedDecl()) { Decl *ImportedDef = Importer.Import(Definition->getDescribedClassTemplate()); if (!ImportedDef) return nullptr; return Importer.Imported(D, ImportedDef); } // Import the major distinguishing characteristics of this class template. DeclContext *DC, *LexicalDC; DeclarationName Name; SourceLocation Loc; NamedDecl *ToD; if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc)) return nullptr; if (ToD) return ToD; // We may already have a template of the same name; try to find and match it. if (!DC->isFunctionOrMethod()) { SmallVector
ConflictingDecls; SmallVector