Douglas Gregor | 96e578d | 2010-02-05 17:54:41 +0000 | [diff] [blame] | 1 | //===--- ASTImporter.cpp - Importing ASTs from other Contexts ---*- C++ -*-===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This file defines the ASTImporter class which imports AST nodes from one |
| 11 | // context into another context. |
| 12 | // |
| 13 | //===----------------------------------------------------------------------===// |
| 14 | #include "clang/AST/ASTImporter.h" |
Douglas Gregor | 96e578d | 2010-02-05 17:54:41 +0000 | [diff] [blame] | 15 | #include "clang/AST/ASTContext.h" |
Douglas Gregor | 811663e | 2010-02-10 00:15:17 +0000 | [diff] [blame] | 16 | #include "clang/AST/ASTDiagnostic.h" |
Douglas Gregor | 5c73e91 | 2010-02-11 00:48:18 +0000 | [diff] [blame] | 17 | #include "clang/AST/DeclCXX.h" |
Douglas Gregor | 96e578d | 2010-02-05 17:54:41 +0000 | [diff] [blame] | 18 | #include "clang/AST/DeclObjC.h" |
Douglas Gregor | 3aed6cd | 2010-02-08 21:09:39 +0000 | [diff] [blame] | 19 | #include "clang/AST/DeclVisitor.h" |
Douglas Gregor | 7eeb597 | 2010-02-11 19:21:55 +0000 | [diff] [blame] | 20 | #include "clang/AST/StmtVisitor.h" |
Douglas Gregor | 96e578d | 2010-02-05 17:54:41 +0000 | [diff] [blame] | 21 | #include "clang/AST/TypeVisitor.h" |
Douglas Gregor | 811663e | 2010-02-10 00:15:17 +0000 | [diff] [blame] | 22 | #include "clang/Basic/FileManager.h" |
| 23 | #include "clang/Basic/SourceManager.h" |
| 24 | #include "llvm/Support/MemoryBuffer.h" |
Douglas Gregor | 3996e24 | 2010-02-15 22:01:00 +0000 | [diff] [blame] | 25 | #include <deque> |
Douglas Gregor | 96e578d | 2010-02-05 17:54:41 +0000 | [diff] [blame] | 26 | |
Douglas Gregor | 3c2404b | 2011-11-03 18:07:07 +0000 | [diff] [blame] | 27 | namespace clang { |
Douglas Gregor | 3aed6cd | 2010-02-08 21:09:39 +0000 | [diff] [blame] | 28 | class ASTNodeImporter : public TypeVisitor<ASTNodeImporter, QualType>, |
Douglas Gregor | 7eeb597 | 2010-02-11 19:21:55 +0000 | [diff] [blame] | 29 | public DeclVisitor<ASTNodeImporter, Decl *>, |
| 30 | public StmtVisitor<ASTNodeImporter, Stmt *> { |
Douglas Gregor | 96e578d | 2010-02-05 17:54:41 +0000 | [diff] [blame] | 31 | ASTImporter &Importer; |
| 32 | |
| 33 | public: |
| 34 | explicit ASTNodeImporter(ASTImporter &Importer) : Importer(Importer) { } |
| 35 | |
| 36 | using TypeVisitor<ASTNodeImporter, QualType>::Visit; |
Douglas Gregor | 62d311f | 2010-02-09 19:21:46 +0000 | [diff] [blame] | 37 | using DeclVisitor<ASTNodeImporter, Decl *>::Visit; |
Douglas Gregor | 7eeb597 | 2010-02-11 19:21:55 +0000 | [diff] [blame] | 38 | using StmtVisitor<ASTNodeImporter, Stmt *>::Visit; |
Douglas Gregor | 96e578d | 2010-02-05 17:54:41 +0000 | [diff] [blame] | 39 | |
| 40 | // Importing types |
John McCall | 424cec9 | 2011-01-19 06:33:43 +0000 | [diff] [blame] | 41 | QualType VisitType(const Type *T); |
| 42 | QualType VisitBuiltinType(const BuiltinType *T); |
| 43 | QualType VisitComplexType(const ComplexType *T); |
| 44 | QualType VisitPointerType(const PointerType *T); |
| 45 | QualType VisitBlockPointerType(const BlockPointerType *T); |
| 46 | QualType VisitLValueReferenceType(const LValueReferenceType *T); |
| 47 | QualType VisitRValueReferenceType(const RValueReferenceType *T); |
| 48 | QualType VisitMemberPointerType(const MemberPointerType *T); |
| 49 | QualType VisitConstantArrayType(const ConstantArrayType *T); |
| 50 | QualType VisitIncompleteArrayType(const IncompleteArrayType *T); |
| 51 | QualType VisitVariableArrayType(const VariableArrayType *T); |
Douglas Gregor | 96e578d | 2010-02-05 17:54:41 +0000 | [diff] [blame] | 52 | // FIXME: DependentSizedArrayType |
| 53 | // FIXME: DependentSizedExtVectorType |
John McCall | 424cec9 | 2011-01-19 06:33:43 +0000 | [diff] [blame] | 54 | QualType VisitVectorType(const VectorType *T); |
| 55 | QualType VisitExtVectorType(const ExtVectorType *T); |
| 56 | QualType VisitFunctionNoProtoType(const FunctionNoProtoType *T); |
| 57 | QualType VisitFunctionProtoType(const FunctionProtoType *T); |
Douglas Gregor | 96e578d | 2010-02-05 17:54:41 +0000 | [diff] [blame] | 58 | // FIXME: UnresolvedUsingType |
Sean Callanan | da6df8a | 2011-08-11 16:56:07 +0000 | [diff] [blame] | 59 | QualType VisitParenType(const ParenType *T); |
John McCall | 424cec9 | 2011-01-19 06:33:43 +0000 | [diff] [blame] | 60 | QualType VisitTypedefType(const TypedefType *T); |
| 61 | QualType VisitTypeOfExprType(const TypeOfExprType *T); |
Douglas Gregor | 96e578d | 2010-02-05 17:54:41 +0000 | [diff] [blame] | 62 | // FIXME: DependentTypeOfExprType |
John McCall | 424cec9 | 2011-01-19 06:33:43 +0000 | [diff] [blame] | 63 | QualType VisitTypeOfType(const TypeOfType *T); |
| 64 | QualType VisitDecltypeType(const DecltypeType *T); |
Alexis Hunt | e852b10 | 2011-05-24 22:41:36 +0000 | [diff] [blame] | 65 | QualType VisitUnaryTransformType(const UnaryTransformType *T); |
Richard Smith | 30482bc | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 66 | QualType VisitAutoType(const AutoType *T); |
Douglas Gregor | 96e578d | 2010-02-05 17:54:41 +0000 | [diff] [blame] | 67 | // FIXME: DependentDecltypeType |
John McCall | 424cec9 | 2011-01-19 06:33:43 +0000 | [diff] [blame] | 68 | QualType VisitRecordType(const RecordType *T); |
| 69 | QualType VisitEnumType(const EnumType *T); |
Douglas Gregor | 96e578d | 2010-02-05 17:54:41 +0000 | [diff] [blame] | 70 | // FIXME: TemplateTypeParmType |
| 71 | // FIXME: SubstTemplateTypeParmType |
John McCall | 424cec9 | 2011-01-19 06:33:43 +0000 | [diff] [blame] | 72 | QualType VisitTemplateSpecializationType(const TemplateSpecializationType *T); |
| 73 | QualType VisitElaboratedType(const ElaboratedType *T); |
Douglas Gregor | c1d2d8a | 2010-03-31 17:34:00 +0000 | [diff] [blame] | 74 | // FIXME: DependentNameType |
John McCall | c392f37 | 2010-06-11 00:33:02 +0000 | [diff] [blame] | 75 | // FIXME: DependentTemplateSpecializationType |
John McCall | 424cec9 | 2011-01-19 06:33:43 +0000 | [diff] [blame] | 76 | QualType VisitObjCInterfaceType(const ObjCInterfaceType *T); |
| 77 | QualType VisitObjCObjectType(const ObjCObjectType *T); |
| 78 | QualType VisitObjCObjectPointerType(const ObjCObjectPointerType *T); |
Douglas Gregor | 3aed6cd | 2010-02-08 21:09:39 +0000 | [diff] [blame] | 79 | |
Douglas Gregor | 95d8283 | 2012-01-24 18:36:04 +0000 | [diff] [blame] | 80 | // Importing declarations |
Douglas Gregor | bb7930c | 2010-02-10 19:54:31 +0000 | [diff] [blame] | 81 | bool ImportDeclParts(NamedDecl *D, DeclContext *&DC, |
| 82 | DeclContext *&LexicalDC, DeclarationName &Name, |
Douglas Gregor | f18a2c7 | 2010-02-21 18:26:36 +0000 | [diff] [blame] | 83 | SourceLocation &Loc); |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 84 | void ImportDefinitionIfNeeded(Decl *FromD, Decl *ToD = nullptr); |
Abramo Bagnara | d6d2f18 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 85 | void ImportDeclarationNameLoc(const DeclarationNameInfo &From, |
| 86 | DeclarationNameInfo& To); |
Douglas Gregor | 0a79167 | 2011-01-18 03:11:38 +0000 | [diff] [blame] | 87 | void ImportDeclContext(DeclContext *FromDC, bool ForceImport = false); |
Douglas Gregor | 2e15c84 | 2012-02-01 21:00:38 +0000 | [diff] [blame] | 88 | |
Douglas Gregor | 95d8283 | 2012-01-24 18:36:04 +0000 | [diff] [blame] | 89 | /// \brief What we should import from the definition. |
| 90 | enum ImportDefinitionKind { |
| 91 | /// \brief Import the default subset of the definition, which might be |
| 92 | /// nothing (if minimal import is set) or might be everything (if minimal |
| 93 | /// import is not set). |
| 94 | IDK_Default, |
| 95 | /// \brief Import everything. |
| 96 | IDK_Everything, |
| 97 | /// \brief Import only the bare bones needed to establish a valid |
| 98 | /// DeclContext. |
| 99 | IDK_Basic |
| 100 | }; |
| 101 | |
Douglas Gregor | 2e15c84 | 2012-02-01 21:00:38 +0000 | [diff] [blame] | 102 | bool shouldForceImportDeclContext(ImportDefinitionKind IDK) { |
| 103 | return IDK == IDK_Everything || |
| 104 | (IDK == IDK_Default && !Importer.isMinimalImport()); |
| 105 | } |
| 106 | |
Douglas Gregor | d451ea9 | 2011-07-29 23:31:30 +0000 | [diff] [blame] | 107 | bool ImportDefinition(RecordDecl *From, RecordDecl *To, |
Douglas Gregor | 95d8283 | 2012-01-24 18:36:04 +0000 | [diff] [blame] | 108 | ImportDefinitionKind Kind = IDK_Default); |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 109 | bool ImportDefinition(VarDecl *From, VarDecl *To, |
| 110 | ImportDefinitionKind Kind = IDK_Default); |
Douglas Gregor | d451ea9 | 2011-07-29 23:31:30 +0000 | [diff] [blame] | 111 | bool ImportDefinition(EnumDecl *From, EnumDecl *To, |
Douglas Gregor | 2e15c84 | 2012-02-01 21:00:38 +0000 | [diff] [blame] | 112 | ImportDefinitionKind Kind = IDK_Default); |
Douglas Gregor | 2aa5377 | 2012-01-24 17:42:07 +0000 | [diff] [blame] | 113 | bool ImportDefinition(ObjCInterfaceDecl *From, ObjCInterfaceDecl *To, |
Douglas Gregor | 2e15c84 | 2012-02-01 21:00:38 +0000 | [diff] [blame] | 114 | ImportDefinitionKind Kind = IDK_Default); |
Douglas Gregor | 2aa5377 | 2012-01-24 17:42:07 +0000 | [diff] [blame] | 115 | bool ImportDefinition(ObjCProtocolDecl *From, ObjCProtocolDecl *To, |
Douglas Gregor | 2e15c84 | 2012-02-01 21:00:38 +0000 | [diff] [blame] | 116 | ImportDefinitionKind Kind = IDK_Default); |
Douglas Gregor | a082a49 | 2010-11-30 19:14:50 +0000 | [diff] [blame] | 117 | TemplateParameterList *ImportTemplateParameterList( |
| 118 | TemplateParameterList *Params); |
Douglas Gregor | e2e50d33 | 2010-12-01 01:36:18 +0000 | [diff] [blame] | 119 | TemplateArgument ImportTemplateArgument(const TemplateArgument &From); |
| 120 | bool ImportTemplateArguments(const TemplateArgument *FromArgs, |
| 121 | unsigned NumFromArgs, |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 122 | SmallVectorImpl<TemplateArgument> &ToArgs); |
Douglas Gregor | dd6006f | 2012-07-17 21:16:27 +0000 | [diff] [blame] | 123 | bool IsStructuralMatch(RecordDecl *FromRecord, RecordDecl *ToRecord, |
| 124 | bool Complain = true); |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 125 | bool IsStructuralMatch(VarDecl *FromVar, VarDecl *ToVar, |
| 126 | bool Complain = true); |
Douglas Gregor | 3996e24 | 2010-02-15 22:01:00 +0000 | [diff] [blame] | 127 | bool IsStructuralMatch(EnumDecl *FromEnum, EnumDecl *ToRecord); |
Douglas Gregor | 9115508 | 2012-11-14 22:29:20 +0000 | [diff] [blame] | 128 | bool IsStructuralMatch(EnumConstantDecl *FromEC, EnumConstantDecl *ToEC); |
Douglas Gregor | a082a49 | 2010-11-30 19:14:50 +0000 | [diff] [blame] | 129 | bool IsStructuralMatch(ClassTemplateDecl *From, ClassTemplateDecl *To); |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 130 | bool IsStructuralMatch(VarTemplateDecl *From, VarTemplateDecl *To); |
Douglas Gregor | e4c83e4 | 2010-02-09 22:48:33 +0000 | [diff] [blame] | 131 | Decl *VisitDecl(Decl *D); |
Sean Callanan | 6519827 | 2011-11-17 23:20:56 +0000 | [diff] [blame] | 132 | Decl *VisitTranslationUnitDecl(TranslationUnitDecl *D); |
Douglas Gregor | f18a2c7 | 2010-02-21 18:26:36 +0000 | [diff] [blame] | 133 | Decl *VisitNamespaceDecl(NamespaceDecl *D); |
Richard Smith | dda56e4 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 134 | Decl *VisitTypedefNameDecl(TypedefNameDecl *D, bool IsAlias); |
Douglas Gregor | 5fa74c3 | 2010-02-10 21:10:29 +0000 | [diff] [blame] | 135 | Decl *VisitTypedefDecl(TypedefDecl *D); |
Richard Smith | dda56e4 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 136 | Decl *VisitTypeAliasDecl(TypeAliasDecl *D); |
Douglas Gregor | 98c1018 | 2010-02-12 22:17:39 +0000 | [diff] [blame] | 137 | Decl *VisitEnumDecl(EnumDecl *D); |
Douglas Gregor | 5c73e91 | 2010-02-11 00:48:18 +0000 | [diff] [blame] | 138 | Decl *VisitRecordDecl(RecordDecl *D); |
Douglas Gregor | 98c1018 | 2010-02-12 22:17:39 +0000 | [diff] [blame] | 139 | Decl *VisitEnumConstantDecl(EnumConstantDecl *D); |
Douglas Gregor | bb7930c | 2010-02-10 19:54:31 +0000 | [diff] [blame] | 140 | Decl *VisitFunctionDecl(FunctionDecl *D); |
Douglas Gregor | 00eace1 | 2010-02-21 18:29:16 +0000 | [diff] [blame] | 141 | Decl *VisitCXXMethodDecl(CXXMethodDecl *D); |
| 142 | Decl *VisitCXXConstructorDecl(CXXConstructorDecl *D); |
| 143 | Decl *VisitCXXDestructorDecl(CXXDestructorDecl *D); |
| 144 | Decl *VisitCXXConversionDecl(CXXConversionDecl *D); |
Douglas Gregor | 5c73e91 | 2010-02-11 00:48:18 +0000 | [diff] [blame] | 145 | Decl *VisitFieldDecl(FieldDecl *D); |
Francois Pichet | 783dd6e | 2010-11-21 06:08:52 +0000 | [diff] [blame] | 146 | Decl *VisitIndirectFieldDecl(IndirectFieldDecl *D); |
Douglas Gregor | 7244b0b | 2010-02-17 00:34:30 +0000 | [diff] [blame] | 147 | Decl *VisitObjCIvarDecl(ObjCIvarDecl *D); |
Douglas Gregor | 3aed6cd | 2010-02-08 21:09:39 +0000 | [diff] [blame] | 148 | Decl *VisitVarDecl(VarDecl *D); |
Douglas Gregor | 8b228d7 | 2010-02-17 21:22:52 +0000 | [diff] [blame] | 149 | Decl *VisitImplicitParamDecl(ImplicitParamDecl *D); |
Douglas Gregor | bb7930c | 2010-02-10 19:54:31 +0000 | [diff] [blame] | 150 | Decl *VisitParmVarDecl(ParmVarDecl *D); |
Douglas Gregor | 43f5479 | 2010-02-17 02:12:47 +0000 | [diff] [blame] | 151 | Decl *VisitObjCMethodDecl(ObjCMethodDecl *D); |
Douglas Gregor | 84c51c3 | 2010-02-18 01:47:50 +0000 | [diff] [blame] | 152 | Decl *VisitObjCCategoryDecl(ObjCCategoryDecl *D); |
Douglas Gregor | 98d156a | 2010-02-17 16:12:00 +0000 | [diff] [blame] | 153 | Decl *VisitObjCProtocolDecl(ObjCProtocolDecl *D); |
Douglas Gregor | 4563532 | 2010-02-16 01:20:57 +0000 | [diff] [blame] | 154 | Decl *VisitObjCInterfaceDecl(ObjCInterfaceDecl *D); |
Douglas Gregor | 4da9d68 | 2010-12-07 15:32:12 +0000 | [diff] [blame] | 155 | Decl *VisitObjCCategoryImplDecl(ObjCCategoryImplDecl *D); |
Douglas Gregor | da8025c | 2010-12-07 01:26:03 +0000 | [diff] [blame] | 156 | Decl *VisitObjCImplementationDecl(ObjCImplementationDecl *D); |
Douglas Gregor | a11c458 | 2010-02-17 18:02:10 +0000 | [diff] [blame] | 157 | Decl *VisitObjCPropertyDecl(ObjCPropertyDecl *D); |
Douglas Gregor | 14a49e2 | 2010-12-07 18:32:03 +0000 | [diff] [blame] | 158 | Decl *VisitObjCPropertyImplDecl(ObjCPropertyImplDecl *D); |
Douglas Gregor | a082a49 | 2010-11-30 19:14:50 +0000 | [diff] [blame] | 159 | Decl *VisitTemplateTypeParmDecl(TemplateTypeParmDecl *D); |
| 160 | Decl *VisitNonTypeTemplateParmDecl(NonTypeTemplateParmDecl *D); |
| 161 | Decl *VisitTemplateTemplateParmDecl(TemplateTemplateParmDecl *D); |
| 162 | Decl *VisitClassTemplateDecl(ClassTemplateDecl *D); |
Douglas Gregor | e2e50d33 | 2010-12-01 01:36:18 +0000 | [diff] [blame] | 163 | Decl *VisitClassTemplateSpecializationDecl( |
| 164 | ClassTemplateSpecializationDecl *D); |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 165 | Decl *VisitVarTemplateDecl(VarTemplateDecl *D); |
| 166 | Decl *VisitVarTemplateSpecializationDecl(VarTemplateSpecializationDecl *D); |
| 167 | |
Douglas Gregor | 7eeb597 | 2010-02-11 19:21:55 +0000 | [diff] [blame] | 168 | // Importing statements |
| 169 | Stmt *VisitStmt(Stmt *S); |
| 170 | |
| 171 | // Importing expressions |
| 172 | Expr *VisitExpr(Expr *E); |
Douglas Gregor | 52f820e | 2010-02-19 01:17:02 +0000 | [diff] [blame] | 173 | Expr *VisitDeclRefExpr(DeclRefExpr *E); |
Douglas Gregor | 7eeb597 | 2010-02-11 19:21:55 +0000 | [diff] [blame] | 174 | Expr *VisitIntegerLiteral(IntegerLiteral *E); |
Douglas Gregor | 623421d | 2010-02-18 02:21:22 +0000 | [diff] [blame] | 175 | Expr *VisitCharacterLiteral(CharacterLiteral *E); |
Douglas Gregor | c74247e | 2010-02-19 01:07:06 +0000 | [diff] [blame] | 176 | Expr *VisitParenExpr(ParenExpr *E); |
| 177 | Expr *VisitUnaryOperator(UnaryOperator *E); |
Peter Collingbourne | e190dee | 2011-03-11 19:24:49 +0000 | [diff] [blame] | 178 | Expr *VisitUnaryExprOrTypeTraitExpr(UnaryExprOrTypeTraitExpr *E); |
Douglas Gregor | c74247e | 2010-02-19 01:07:06 +0000 | [diff] [blame] | 179 | Expr *VisitBinaryOperator(BinaryOperator *E); |
| 180 | Expr *VisitCompoundAssignOperator(CompoundAssignOperator *E); |
Douglas Gregor | 98c1018 | 2010-02-12 22:17:39 +0000 | [diff] [blame] | 181 | Expr *VisitImplicitCastExpr(ImplicitCastExpr *E); |
Douglas Gregor | 5481d32 | 2010-02-19 01:32:14 +0000 | [diff] [blame] | 182 | Expr *VisitCStyleCastExpr(CStyleCastExpr *E); |
Douglas Gregor | 96e578d | 2010-02-05 17:54:41 +0000 | [diff] [blame] | 183 | }; |
| 184 | } |
Douglas Gregor | 3c2404b | 2011-11-03 18:07:07 +0000 | [diff] [blame] | 185 | using namespace clang; |
Douglas Gregor | 96e578d | 2010-02-05 17:54:41 +0000 | [diff] [blame] | 186 | |
| 187 | //---------------------------------------------------------------------------- |
Douglas Gregor | 3996e24 | 2010-02-15 22:01:00 +0000 | [diff] [blame] | 188 | // Structural Equivalence |
| 189 | //---------------------------------------------------------------------------- |
| 190 | |
| 191 | namespace { |
| 192 | struct StructuralEquivalenceContext { |
| 193 | /// \brief AST contexts for which we are checking structural equivalence. |
| 194 | ASTContext &C1, &C2; |
| 195 | |
Douglas Gregor | 3996e24 | 2010-02-15 22:01:00 +0000 | [diff] [blame] | 196 | /// \brief The set of "tentative" equivalences between two canonical |
| 197 | /// declarations, mapping from a declaration in the first context to the |
| 198 | /// declaration in the second context that we believe to be equivalent. |
| 199 | llvm::DenseMap<Decl *, Decl *> TentativeEquivalences; |
| 200 | |
| 201 | /// \brief Queue of declarations in the first context whose equivalence |
| 202 | /// with a declaration in the second context still needs to be verified. |
| 203 | std::deque<Decl *> DeclsToCheck; |
| 204 | |
Douglas Gregor | b4964f7 | 2010-02-15 23:54:17 +0000 | [diff] [blame] | 205 | /// \brief Declaration (from, to) pairs that are known not to be equivalent |
| 206 | /// (which we have already complained about). |
| 207 | llvm::DenseSet<std::pair<Decl *, Decl *> > &NonEquivalentDecls; |
| 208 | |
Douglas Gregor | 3996e24 | 2010-02-15 22:01:00 +0000 | [diff] [blame] | 209 | /// \brief Whether we're being strict about the spelling of types when |
| 210 | /// unifying two types. |
| 211 | bool StrictTypeSpelling; |
Douglas Gregor | dd6006f | 2012-07-17 21:16:27 +0000 | [diff] [blame] | 212 | |
| 213 | /// \brief Whether to complain about failures. |
| 214 | bool Complain; |
| 215 | |
Richard Smith | 5bb4cdf | 2012-12-20 02:22:15 +0000 | [diff] [blame] | 216 | /// \brief \c true if the last diagnostic came from C2. |
| 217 | bool LastDiagFromC2; |
| 218 | |
Douglas Gregor | 3996e24 | 2010-02-15 22:01:00 +0000 | [diff] [blame] | 219 | StructuralEquivalenceContext(ASTContext &C1, ASTContext &C2, |
Douglas Gregor | b4964f7 | 2010-02-15 23:54:17 +0000 | [diff] [blame] | 220 | llvm::DenseSet<std::pair<Decl *, Decl *> > &NonEquivalentDecls, |
Douglas Gregor | dd6006f | 2012-07-17 21:16:27 +0000 | [diff] [blame] | 221 | bool StrictTypeSpelling = false, |
| 222 | bool Complain = true) |
Argyrios Kyrtzidis | d004064 | 2010-11-18 20:06:41 +0000 | [diff] [blame] | 223 | : C1(C1), C2(C2), NonEquivalentDecls(NonEquivalentDecls), |
Richard Smith | 5bb4cdf | 2012-12-20 02:22:15 +0000 | [diff] [blame] | 224 | StrictTypeSpelling(StrictTypeSpelling), Complain(Complain), |
| 225 | LastDiagFromC2(false) {} |
Douglas Gregor | 3996e24 | 2010-02-15 22:01:00 +0000 | [diff] [blame] | 226 | |
| 227 | /// \brief Determine whether the two declarations are structurally |
| 228 | /// equivalent. |
| 229 | bool IsStructurallyEquivalent(Decl *D1, Decl *D2); |
| 230 | |
| 231 | /// \brief Determine whether the two types are structurally equivalent. |
| 232 | bool IsStructurallyEquivalent(QualType T1, QualType T2); |
| 233 | |
| 234 | private: |
| 235 | /// \brief Finish checking all of the structural equivalences. |
| 236 | /// |
| 237 | /// \returns true if an error occurred, false otherwise. |
| 238 | bool Finish(); |
| 239 | |
| 240 | public: |
| 241 | DiagnosticBuilder Diag1(SourceLocation Loc, unsigned DiagID) { |
Douglas Gregor | 069bbaf | 2012-10-26 15:34:11 +0000 | [diff] [blame] | 242 | assert(Complain && "Not allowed to complain"); |
Richard Smith | 5bb4cdf | 2012-12-20 02:22:15 +0000 | [diff] [blame] | 243 | if (LastDiagFromC2) |
| 244 | C1.getDiagnostics().notePriorDiagnosticFrom(C2.getDiagnostics()); |
| 245 | LastDiagFromC2 = false; |
Argyrios Kyrtzidis | d004064 | 2010-11-18 20:06:41 +0000 | [diff] [blame] | 246 | return C1.getDiagnostics().Report(Loc, DiagID); |
Douglas Gregor | 3996e24 | 2010-02-15 22:01:00 +0000 | [diff] [blame] | 247 | } |
| 248 | |
| 249 | DiagnosticBuilder Diag2(SourceLocation Loc, unsigned DiagID) { |
Douglas Gregor | 069bbaf | 2012-10-26 15:34:11 +0000 | [diff] [blame] | 250 | assert(Complain && "Not allowed to complain"); |
Richard Smith | 5bb4cdf | 2012-12-20 02:22:15 +0000 | [diff] [blame] | 251 | if (!LastDiagFromC2) |
| 252 | C2.getDiagnostics().notePriorDiagnosticFrom(C1.getDiagnostics()); |
| 253 | LastDiagFromC2 = true; |
Argyrios Kyrtzidis | d004064 | 2010-11-18 20:06:41 +0000 | [diff] [blame] | 254 | return C2.getDiagnostics().Report(Loc, DiagID); |
Douglas Gregor | 3996e24 | 2010-02-15 22:01:00 +0000 | [diff] [blame] | 255 | } |
| 256 | }; |
| 257 | } |
| 258 | |
| 259 | static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context, |
| 260 | QualType T1, QualType T2); |
| 261 | static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context, |
| 262 | Decl *D1, Decl *D2); |
| 263 | |
Douglas Gregor | 3996e24 | 2010-02-15 22:01:00 +0000 | [diff] [blame] | 264 | /// \brief Determine structural equivalence of two expressions. |
| 265 | static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context, |
| 266 | Expr *E1, Expr *E2) { |
| 267 | if (!E1 || !E2) |
| 268 | return E1 == E2; |
| 269 | |
| 270 | // FIXME: Actually perform a structural comparison! |
| 271 | return true; |
| 272 | } |
| 273 | |
| 274 | /// \brief Determine whether two identifiers are equivalent. |
| 275 | static bool IsStructurallyEquivalent(const IdentifierInfo *Name1, |
| 276 | const IdentifierInfo *Name2) { |
| 277 | if (!Name1 || !Name2) |
| 278 | return Name1 == Name2; |
| 279 | |
| 280 | return Name1->getName() == Name2->getName(); |
| 281 | } |
| 282 | |
| 283 | /// \brief Determine whether two nested-name-specifiers are equivalent. |
| 284 | static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context, |
| 285 | NestedNameSpecifier *NNS1, |
| 286 | NestedNameSpecifier *NNS2) { |
| 287 | // FIXME: Implement! |
| 288 | return true; |
| 289 | } |
| 290 | |
| 291 | /// \brief Determine whether two template arguments are equivalent. |
| 292 | static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context, |
| 293 | const TemplateArgument &Arg1, |
| 294 | const TemplateArgument &Arg2) { |
Douglas Gregor | e2e50d33 | 2010-12-01 01:36:18 +0000 | [diff] [blame] | 295 | if (Arg1.getKind() != Arg2.getKind()) |
| 296 | return false; |
| 297 | |
| 298 | switch (Arg1.getKind()) { |
| 299 | case TemplateArgument::Null: |
| 300 | return true; |
| 301 | |
| 302 | case TemplateArgument::Type: |
| 303 | return Context.IsStructurallyEquivalent(Arg1.getAsType(), Arg2.getAsType()); |
Eli Friedman | b826a00 | 2012-09-26 02:36:12 +0000 | [diff] [blame] | 304 | |
Douglas Gregor | e2e50d33 | 2010-12-01 01:36:18 +0000 | [diff] [blame] | 305 | case TemplateArgument::Integral: |
| 306 | if (!Context.IsStructurallyEquivalent(Arg1.getIntegralType(), |
| 307 | Arg2.getIntegralType())) |
| 308 | return false; |
| 309 | |
Eric Christopher | 6dcc376 | 2012-07-15 00:23:57 +0000 | [diff] [blame] | 310 | return llvm::APSInt::isSameValue(Arg1.getAsIntegral(), Arg2.getAsIntegral()); |
Douglas Gregor | e2e50d33 | 2010-12-01 01:36:18 +0000 | [diff] [blame] | 311 | |
| 312 | case TemplateArgument::Declaration: |
| 313 | return Context.IsStructurallyEquivalent(Arg1.getAsDecl(), Arg2.getAsDecl()); |
Eli Friedman | b826a00 | 2012-09-26 02:36:12 +0000 | [diff] [blame] | 314 | |
| 315 | case TemplateArgument::NullPtr: |
| 316 | return true; // FIXME: Is this correct? |
| 317 | |
Douglas Gregor | e2e50d33 | 2010-12-01 01:36:18 +0000 | [diff] [blame] | 318 | case TemplateArgument::Template: |
| 319 | return IsStructurallyEquivalent(Context, |
| 320 | Arg1.getAsTemplate(), |
| 321 | Arg2.getAsTemplate()); |
Douglas Gregor | e4ff4b5 | 2011-01-05 18:58:31 +0000 | [diff] [blame] | 322 | |
| 323 | case TemplateArgument::TemplateExpansion: |
| 324 | return IsStructurallyEquivalent(Context, |
| 325 | Arg1.getAsTemplateOrTemplatePattern(), |
| 326 | Arg2.getAsTemplateOrTemplatePattern()); |
| 327 | |
Douglas Gregor | e2e50d33 | 2010-12-01 01:36:18 +0000 | [diff] [blame] | 328 | case TemplateArgument::Expression: |
| 329 | return IsStructurallyEquivalent(Context, |
| 330 | Arg1.getAsExpr(), Arg2.getAsExpr()); |
| 331 | |
| 332 | case TemplateArgument::Pack: |
| 333 | if (Arg1.pack_size() != Arg2.pack_size()) |
| 334 | return false; |
| 335 | |
| 336 | for (unsigned I = 0, N = Arg1.pack_size(); I != N; ++I) |
| 337 | if (!IsStructurallyEquivalent(Context, |
| 338 | Arg1.pack_begin()[I], |
| 339 | Arg2.pack_begin()[I])) |
| 340 | return false; |
| 341 | |
| 342 | return true; |
| 343 | } |
| 344 | |
| 345 | llvm_unreachable("Invalid template argument kind"); |
Douglas Gregor | 3996e24 | 2010-02-15 22:01:00 +0000 | [diff] [blame] | 346 | } |
| 347 | |
| 348 | /// \brief Determine structural equivalence for the common part of array |
| 349 | /// types. |
| 350 | static bool IsArrayStructurallyEquivalent(StructuralEquivalenceContext &Context, |
| 351 | const ArrayType *Array1, |
| 352 | const ArrayType *Array2) { |
| 353 | if (!IsStructurallyEquivalent(Context, |
| 354 | Array1->getElementType(), |
| 355 | Array2->getElementType())) |
| 356 | return false; |
| 357 | if (Array1->getSizeModifier() != Array2->getSizeModifier()) |
| 358 | return false; |
| 359 | if (Array1->getIndexTypeQualifiers() != Array2->getIndexTypeQualifiers()) |
| 360 | return false; |
| 361 | |
| 362 | return true; |
| 363 | } |
| 364 | |
| 365 | /// \brief Determine structural equivalence of two types. |
| 366 | static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context, |
| 367 | QualType T1, QualType T2) { |
| 368 | if (T1.isNull() || T2.isNull()) |
| 369 | return T1.isNull() && T2.isNull(); |
| 370 | |
| 371 | if (!Context.StrictTypeSpelling) { |
| 372 | // We aren't being strict about token-to-token equivalence of types, |
| 373 | // so map down to the canonical type. |
| 374 | T1 = Context.C1.getCanonicalType(T1); |
| 375 | T2 = Context.C2.getCanonicalType(T2); |
| 376 | } |
| 377 | |
| 378 | if (T1.getQualifiers() != T2.getQualifiers()) |
| 379 | return false; |
| 380 | |
Douglas Gregor | b4964f7 | 2010-02-15 23:54:17 +0000 | [diff] [blame] | 381 | Type::TypeClass TC = T1->getTypeClass(); |
Douglas Gregor | 3996e24 | 2010-02-15 22:01:00 +0000 | [diff] [blame] | 382 | |
Douglas Gregor | b4964f7 | 2010-02-15 23:54:17 +0000 | [diff] [blame] | 383 | if (T1->getTypeClass() != T2->getTypeClass()) { |
| 384 | // Compare function types with prototypes vs. without prototypes as if |
| 385 | // both did not have prototypes. |
| 386 | if (T1->getTypeClass() == Type::FunctionProto && |
| 387 | T2->getTypeClass() == Type::FunctionNoProto) |
| 388 | TC = Type::FunctionNoProto; |
| 389 | else if (T1->getTypeClass() == Type::FunctionNoProto && |
| 390 | T2->getTypeClass() == Type::FunctionProto) |
| 391 | TC = Type::FunctionNoProto; |
| 392 | else |
| 393 | return false; |
| 394 | } |
| 395 | |
| 396 | switch (TC) { |
| 397 | case Type::Builtin: |
Douglas Gregor | 3996e24 | 2010-02-15 22:01:00 +0000 | [diff] [blame] | 398 | // FIXME: Deal with Char_S/Char_U. |
| 399 | if (cast<BuiltinType>(T1)->getKind() != cast<BuiltinType>(T2)->getKind()) |
| 400 | return false; |
| 401 | break; |
| 402 | |
| 403 | case Type::Complex: |
| 404 | if (!IsStructurallyEquivalent(Context, |
| 405 | cast<ComplexType>(T1)->getElementType(), |
| 406 | cast<ComplexType>(T2)->getElementType())) |
| 407 | return false; |
| 408 | break; |
| 409 | |
Reid Kleckner | 0503a87 | 2013-12-05 01:23:43 +0000 | [diff] [blame] | 410 | case Type::Adjusted: |
Reid Kleckner | 8a36502 | 2013-06-24 17:51:48 +0000 | [diff] [blame] | 411 | case Type::Decayed: |
| 412 | if (!IsStructurallyEquivalent(Context, |
Reid Kleckner | 0503a87 | 2013-12-05 01:23:43 +0000 | [diff] [blame] | 413 | cast<AdjustedType>(T1)->getOriginalType(), |
| 414 | cast<AdjustedType>(T2)->getOriginalType())) |
Reid Kleckner | 8a36502 | 2013-06-24 17:51:48 +0000 | [diff] [blame] | 415 | return false; |
| 416 | break; |
| 417 | |
Douglas Gregor | 3996e24 | 2010-02-15 22:01:00 +0000 | [diff] [blame] | 418 | case Type::Pointer: |
| 419 | if (!IsStructurallyEquivalent(Context, |
| 420 | cast<PointerType>(T1)->getPointeeType(), |
| 421 | cast<PointerType>(T2)->getPointeeType())) |
| 422 | return false; |
| 423 | break; |
| 424 | |
| 425 | case Type::BlockPointer: |
| 426 | if (!IsStructurallyEquivalent(Context, |
| 427 | cast<BlockPointerType>(T1)->getPointeeType(), |
| 428 | cast<BlockPointerType>(T2)->getPointeeType())) |
| 429 | return false; |
| 430 | break; |
| 431 | |
| 432 | case Type::LValueReference: |
| 433 | case Type::RValueReference: { |
| 434 | const ReferenceType *Ref1 = cast<ReferenceType>(T1); |
| 435 | const ReferenceType *Ref2 = cast<ReferenceType>(T2); |
| 436 | if (Ref1->isSpelledAsLValue() != Ref2->isSpelledAsLValue()) |
| 437 | return false; |
| 438 | if (Ref1->isInnerRef() != Ref2->isInnerRef()) |
| 439 | return false; |
| 440 | if (!IsStructurallyEquivalent(Context, |
| 441 | Ref1->getPointeeTypeAsWritten(), |
| 442 | Ref2->getPointeeTypeAsWritten())) |
| 443 | return false; |
| 444 | break; |
| 445 | } |
| 446 | |
| 447 | case Type::MemberPointer: { |
| 448 | const MemberPointerType *MemPtr1 = cast<MemberPointerType>(T1); |
| 449 | const MemberPointerType *MemPtr2 = cast<MemberPointerType>(T2); |
| 450 | if (!IsStructurallyEquivalent(Context, |
| 451 | MemPtr1->getPointeeType(), |
| 452 | MemPtr2->getPointeeType())) |
| 453 | return false; |
| 454 | if (!IsStructurallyEquivalent(Context, |
| 455 | QualType(MemPtr1->getClass(), 0), |
| 456 | QualType(MemPtr2->getClass(), 0))) |
| 457 | return false; |
| 458 | break; |
| 459 | } |
| 460 | |
| 461 | case Type::ConstantArray: { |
| 462 | const ConstantArrayType *Array1 = cast<ConstantArrayType>(T1); |
| 463 | const ConstantArrayType *Array2 = cast<ConstantArrayType>(T2); |
Eric Christopher | 6dcc376 | 2012-07-15 00:23:57 +0000 | [diff] [blame] | 464 | if (!llvm::APInt::isSameValue(Array1->getSize(), Array2->getSize())) |
Douglas Gregor | 3996e24 | 2010-02-15 22:01:00 +0000 | [diff] [blame] | 465 | return false; |
| 466 | |
| 467 | if (!IsArrayStructurallyEquivalent(Context, Array1, Array2)) |
| 468 | return false; |
| 469 | break; |
| 470 | } |
| 471 | |
| 472 | case Type::IncompleteArray: |
| 473 | if (!IsArrayStructurallyEquivalent(Context, |
| 474 | cast<ArrayType>(T1), |
| 475 | cast<ArrayType>(T2))) |
| 476 | return false; |
| 477 | break; |
| 478 | |
| 479 | case Type::VariableArray: { |
| 480 | const VariableArrayType *Array1 = cast<VariableArrayType>(T1); |
| 481 | const VariableArrayType *Array2 = cast<VariableArrayType>(T2); |
| 482 | if (!IsStructurallyEquivalent(Context, |
| 483 | Array1->getSizeExpr(), Array2->getSizeExpr())) |
| 484 | return false; |
| 485 | |
| 486 | if (!IsArrayStructurallyEquivalent(Context, Array1, Array2)) |
| 487 | return false; |
| 488 | |
| 489 | break; |
| 490 | } |
| 491 | |
| 492 | case Type::DependentSizedArray: { |
| 493 | const DependentSizedArrayType *Array1 = cast<DependentSizedArrayType>(T1); |
| 494 | const DependentSizedArrayType *Array2 = cast<DependentSizedArrayType>(T2); |
| 495 | if (!IsStructurallyEquivalent(Context, |
| 496 | Array1->getSizeExpr(), Array2->getSizeExpr())) |
| 497 | return false; |
| 498 | |
| 499 | if (!IsArrayStructurallyEquivalent(Context, Array1, Array2)) |
| 500 | return false; |
| 501 | |
| 502 | break; |
| 503 | } |
| 504 | |
| 505 | case Type::DependentSizedExtVector: { |
| 506 | const DependentSizedExtVectorType *Vec1 |
| 507 | = cast<DependentSizedExtVectorType>(T1); |
| 508 | const DependentSizedExtVectorType *Vec2 |
| 509 | = cast<DependentSizedExtVectorType>(T2); |
| 510 | if (!IsStructurallyEquivalent(Context, |
| 511 | Vec1->getSizeExpr(), Vec2->getSizeExpr())) |
| 512 | return false; |
| 513 | if (!IsStructurallyEquivalent(Context, |
| 514 | Vec1->getElementType(), |
| 515 | Vec2->getElementType())) |
| 516 | return false; |
| 517 | break; |
| 518 | } |
| 519 | |
| 520 | case Type::Vector: |
| 521 | case Type::ExtVector: { |
| 522 | const VectorType *Vec1 = cast<VectorType>(T1); |
| 523 | const VectorType *Vec2 = cast<VectorType>(T2); |
| 524 | if (!IsStructurallyEquivalent(Context, |
| 525 | Vec1->getElementType(), |
| 526 | Vec2->getElementType())) |
| 527 | return false; |
| 528 | if (Vec1->getNumElements() != Vec2->getNumElements()) |
| 529 | return false; |
Bob Wilson | aeb5644 | 2010-11-10 21:56:12 +0000 | [diff] [blame] | 530 | if (Vec1->getVectorKind() != Vec2->getVectorKind()) |
Douglas Gregor | 3996e24 | 2010-02-15 22:01:00 +0000 | [diff] [blame] | 531 | return false; |
Douglas Gregor | 01cc437 | 2010-02-19 01:36:36 +0000 | [diff] [blame] | 532 | break; |
Douglas Gregor | 3996e24 | 2010-02-15 22:01:00 +0000 | [diff] [blame] | 533 | } |
| 534 | |
| 535 | case Type::FunctionProto: { |
| 536 | const FunctionProtoType *Proto1 = cast<FunctionProtoType>(T1); |
| 537 | const FunctionProtoType *Proto2 = cast<FunctionProtoType>(T2); |
Alp Toker | 9cacbab | 2014-01-20 20:26:09 +0000 | [diff] [blame] | 538 | if (Proto1->getNumParams() != Proto2->getNumParams()) |
Douglas Gregor | 3996e24 | 2010-02-15 22:01:00 +0000 | [diff] [blame] | 539 | return false; |
Alp Toker | 9cacbab | 2014-01-20 20:26:09 +0000 | [diff] [blame] | 540 | for (unsigned I = 0, N = Proto1->getNumParams(); I != N; ++I) { |
| 541 | if (!IsStructurallyEquivalent(Context, Proto1->getParamType(I), |
| 542 | Proto2->getParamType(I))) |
Douglas Gregor | 3996e24 | 2010-02-15 22:01:00 +0000 | [diff] [blame] | 543 | return false; |
| 544 | } |
| 545 | if (Proto1->isVariadic() != Proto2->isVariadic()) |
| 546 | return false; |
Sebastian Redl | fa453cf | 2011-03-12 11:50:43 +0000 | [diff] [blame] | 547 | if (Proto1->getExceptionSpecType() != Proto2->getExceptionSpecType()) |
Douglas Gregor | 3996e24 | 2010-02-15 22:01:00 +0000 | [diff] [blame] | 548 | return false; |
Sebastian Redl | fa453cf | 2011-03-12 11:50:43 +0000 | [diff] [blame] | 549 | if (Proto1->getExceptionSpecType() == EST_Dynamic) { |
| 550 | if (Proto1->getNumExceptions() != Proto2->getNumExceptions()) |
| 551 | return false; |
| 552 | for (unsigned I = 0, N = Proto1->getNumExceptions(); I != N; ++I) { |
| 553 | if (!IsStructurallyEquivalent(Context, |
| 554 | Proto1->getExceptionType(I), |
| 555 | Proto2->getExceptionType(I))) |
| 556 | return false; |
| 557 | } |
| 558 | } else if (Proto1->getExceptionSpecType() == EST_ComputedNoexcept) { |
Douglas Gregor | 3996e24 | 2010-02-15 22:01:00 +0000 | [diff] [blame] | 559 | if (!IsStructurallyEquivalent(Context, |
Sebastian Redl | fa453cf | 2011-03-12 11:50:43 +0000 | [diff] [blame] | 560 | Proto1->getNoexceptExpr(), |
| 561 | Proto2->getNoexceptExpr())) |
Douglas Gregor | 3996e24 | 2010-02-15 22:01:00 +0000 | [diff] [blame] | 562 | return false; |
| 563 | } |
| 564 | if (Proto1->getTypeQuals() != Proto2->getTypeQuals()) |
| 565 | return false; |
| 566 | |
| 567 | // Fall through to check the bits common with FunctionNoProtoType. |
| 568 | } |
| 569 | |
| 570 | case Type::FunctionNoProto: { |
| 571 | const FunctionType *Function1 = cast<FunctionType>(T1); |
| 572 | const FunctionType *Function2 = cast<FunctionType>(T2); |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 573 | if (!IsStructurallyEquivalent(Context, Function1->getReturnType(), |
| 574 | Function2->getReturnType())) |
Douglas Gregor | 3996e24 | 2010-02-15 22:01:00 +0000 | [diff] [blame] | 575 | return false; |
Rafael Espindola | c50c27c | 2010-03-30 20:24:48 +0000 | [diff] [blame] | 576 | if (Function1->getExtInfo() != Function2->getExtInfo()) |
| 577 | return false; |
Douglas Gregor | 3996e24 | 2010-02-15 22:01:00 +0000 | [diff] [blame] | 578 | break; |
| 579 | } |
| 580 | |
| 581 | case Type::UnresolvedUsing: |
| 582 | if (!IsStructurallyEquivalent(Context, |
| 583 | cast<UnresolvedUsingType>(T1)->getDecl(), |
| 584 | cast<UnresolvedUsingType>(T2)->getDecl())) |
| 585 | return false; |
| 586 | |
| 587 | break; |
John McCall | 8190451 | 2011-01-06 01:58:22 +0000 | [diff] [blame] | 588 | |
| 589 | case Type::Attributed: |
| 590 | if (!IsStructurallyEquivalent(Context, |
| 591 | cast<AttributedType>(T1)->getModifiedType(), |
| 592 | cast<AttributedType>(T2)->getModifiedType())) |
| 593 | return false; |
| 594 | if (!IsStructurallyEquivalent(Context, |
| 595 | cast<AttributedType>(T1)->getEquivalentType(), |
| 596 | cast<AttributedType>(T2)->getEquivalentType())) |
| 597 | return false; |
| 598 | break; |
Douglas Gregor | 3996e24 | 2010-02-15 22:01:00 +0000 | [diff] [blame] | 599 | |
Abramo Bagnara | 924a8f3 | 2010-12-10 16:29:40 +0000 | [diff] [blame] | 600 | case Type::Paren: |
| 601 | if (!IsStructurallyEquivalent(Context, |
| 602 | cast<ParenType>(T1)->getInnerType(), |
| 603 | cast<ParenType>(T2)->getInnerType())) |
| 604 | return false; |
| 605 | break; |
| 606 | |
Douglas Gregor | 3996e24 | 2010-02-15 22:01:00 +0000 | [diff] [blame] | 607 | case Type::Typedef: |
| 608 | if (!IsStructurallyEquivalent(Context, |
| 609 | cast<TypedefType>(T1)->getDecl(), |
| 610 | cast<TypedefType>(T2)->getDecl())) |
| 611 | return false; |
| 612 | break; |
| 613 | |
| 614 | case Type::TypeOfExpr: |
| 615 | if (!IsStructurallyEquivalent(Context, |
| 616 | cast<TypeOfExprType>(T1)->getUnderlyingExpr(), |
| 617 | cast<TypeOfExprType>(T2)->getUnderlyingExpr())) |
| 618 | return false; |
| 619 | break; |
| 620 | |
| 621 | case Type::TypeOf: |
| 622 | if (!IsStructurallyEquivalent(Context, |
| 623 | cast<TypeOfType>(T1)->getUnderlyingType(), |
| 624 | cast<TypeOfType>(T2)->getUnderlyingType())) |
| 625 | return false; |
| 626 | break; |
Alexis Hunt | e852b10 | 2011-05-24 22:41:36 +0000 | [diff] [blame] | 627 | |
| 628 | case Type::UnaryTransform: |
| 629 | if (!IsStructurallyEquivalent(Context, |
| 630 | cast<UnaryTransformType>(T1)->getUnderlyingType(), |
| 631 | cast<UnaryTransformType>(T1)->getUnderlyingType())) |
| 632 | return false; |
| 633 | break; |
| 634 | |
Douglas Gregor | 3996e24 | 2010-02-15 22:01:00 +0000 | [diff] [blame] | 635 | case Type::Decltype: |
| 636 | if (!IsStructurallyEquivalent(Context, |
| 637 | cast<DecltypeType>(T1)->getUnderlyingExpr(), |
| 638 | cast<DecltypeType>(T2)->getUnderlyingExpr())) |
| 639 | return false; |
| 640 | break; |
| 641 | |
Richard Smith | 30482bc | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 642 | case Type::Auto: |
| 643 | if (!IsStructurallyEquivalent(Context, |
| 644 | cast<AutoType>(T1)->getDeducedType(), |
| 645 | cast<AutoType>(T2)->getDeducedType())) |
| 646 | return false; |
| 647 | break; |
| 648 | |
Douglas Gregor | 3996e24 | 2010-02-15 22:01:00 +0000 | [diff] [blame] | 649 | case Type::Record: |
| 650 | case Type::Enum: |
| 651 | if (!IsStructurallyEquivalent(Context, |
| 652 | cast<TagType>(T1)->getDecl(), |
| 653 | cast<TagType>(T2)->getDecl())) |
| 654 | return false; |
| 655 | break; |
Abramo Bagnara | 6150c88 | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 656 | |
Douglas Gregor | 3996e24 | 2010-02-15 22:01:00 +0000 | [diff] [blame] | 657 | case Type::TemplateTypeParm: { |
| 658 | const TemplateTypeParmType *Parm1 = cast<TemplateTypeParmType>(T1); |
| 659 | const TemplateTypeParmType *Parm2 = cast<TemplateTypeParmType>(T2); |
| 660 | if (Parm1->getDepth() != Parm2->getDepth()) |
| 661 | return false; |
| 662 | if (Parm1->getIndex() != Parm2->getIndex()) |
| 663 | return false; |
| 664 | if (Parm1->isParameterPack() != Parm2->isParameterPack()) |
| 665 | return false; |
| 666 | |
| 667 | // Names of template type parameters are never significant. |
| 668 | break; |
| 669 | } |
| 670 | |
| 671 | case Type::SubstTemplateTypeParm: { |
| 672 | const SubstTemplateTypeParmType *Subst1 |
| 673 | = cast<SubstTemplateTypeParmType>(T1); |
| 674 | const SubstTemplateTypeParmType *Subst2 |
| 675 | = cast<SubstTemplateTypeParmType>(T2); |
| 676 | if (!IsStructurallyEquivalent(Context, |
| 677 | QualType(Subst1->getReplacedParameter(), 0), |
| 678 | QualType(Subst2->getReplacedParameter(), 0))) |
| 679 | return false; |
| 680 | if (!IsStructurallyEquivalent(Context, |
| 681 | Subst1->getReplacementType(), |
| 682 | Subst2->getReplacementType())) |
| 683 | return false; |
| 684 | break; |
| 685 | } |
| 686 | |
Douglas Gregor | fb322d8 | 2011-01-14 05:11:40 +0000 | [diff] [blame] | 687 | case Type::SubstTemplateTypeParmPack: { |
| 688 | const SubstTemplateTypeParmPackType *Subst1 |
| 689 | = cast<SubstTemplateTypeParmPackType>(T1); |
| 690 | const SubstTemplateTypeParmPackType *Subst2 |
| 691 | = cast<SubstTemplateTypeParmPackType>(T2); |
| 692 | if (!IsStructurallyEquivalent(Context, |
| 693 | QualType(Subst1->getReplacedParameter(), 0), |
| 694 | QualType(Subst2->getReplacedParameter(), 0))) |
| 695 | return false; |
| 696 | if (!IsStructurallyEquivalent(Context, |
| 697 | Subst1->getArgumentPack(), |
| 698 | Subst2->getArgumentPack())) |
| 699 | return false; |
| 700 | break; |
| 701 | } |
Douglas Gregor | 3996e24 | 2010-02-15 22:01:00 +0000 | [diff] [blame] | 702 | case Type::TemplateSpecialization: { |
| 703 | const TemplateSpecializationType *Spec1 |
| 704 | = cast<TemplateSpecializationType>(T1); |
| 705 | const TemplateSpecializationType *Spec2 |
| 706 | = cast<TemplateSpecializationType>(T2); |
| 707 | if (!IsStructurallyEquivalent(Context, |
| 708 | Spec1->getTemplateName(), |
| 709 | Spec2->getTemplateName())) |
| 710 | return false; |
| 711 | if (Spec1->getNumArgs() != Spec2->getNumArgs()) |
| 712 | return false; |
| 713 | for (unsigned I = 0, N = Spec1->getNumArgs(); I != N; ++I) { |
| 714 | if (!IsStructurallyEquivalent(Context, |
| 715 | Spec1->getArg(I), Spec2->getArg(I))) |
| 716 | return false; |
| 717 | } |
| 718 | break; |
| 719 | } |
| 720 | |
Abramo Bagnara | 6150c88 | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 721 | case Type::Elaborated: { |
| 722 | const ElaboratedType *Elab1 = cast<ElaboratedType>(T1); |
| 723 | const ElaboratedType *Elab2 = cast<ElaboratedType>(T2); |
| 724 | // CHECKME: what if a keyword is ETK_None or ETK_typename ? |
| 725 | if (Elab1->getKeyword() != Elab2->getKeyword()) |
| 726 | return false; |
Douglas Gregor | 3996e24 | 2010-02-15 22:01:00 +0000 | [diff] [blame] | 727 | if (!IsStructurallyEquivalent(Context, |
Abramo Bagnara | 6150c88 | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 728 | Elab1->getQualifier(), |
| 729 | Elab2->getQualifier())) |
Douglas Gregor | 3996e24 | 2010-02-15 22:01:00 +0000 | [diff] [blame] | 730 | return false; |
| 731 | if (!IsStructurallyEquivalent(Context, |
Abramo Bagnara | 6150c88 | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 732 | Elab1->getNamedType(), |
| 733 | Elab2->getNamedType())) |
Douglas Gregor | 3996e24 | 2010-02-15 22:01:00 +0000 | [diff] [blame] | 734 | return false; |
| 735 | break; |
| 736 | } |
| 737 | |
John McCall | e78aac4 | 2010-03-10 03:28:59 +0000 | [diff] [blame] | 738 | case Type::InjectedClassName: { |
| 739 | const InjectedClassNameType *Inj1 = cast<InjectedClassNameType>(T1); |
| 740 | const InjectedClassNameType *Inj2 = cast<InjectedClassNameType>(T2); |
| 741 | if (!IsStructurallyEquivalent(Context, |
John McCall | 2408e32 | 2010-04-27 00:57:59 +0000 | [diff] [blame] | 742 | Inj1->getInjectedSpecializationType(), |
| 743 | Inj2->getInjectedSpecializationType())) |
John McCall | e78aac4 | 2010-03-10 03:28:59 +0000 | [diff] [blame] | 744 | return false; |
| 745 | break; |
| 746 | } |
| 747 | |
Douglas Gregor | c1d2d8a | 2010-03-31 17:34:00 +0000 | [diff] [blame] | 748 | case Type::DependentName: { |
| 749 | const DependentNameType *Typename1 = cast<DependentNameType>(T1); |
| 750 | const DependentNameType *Typename2 = cast<DependentNameType>(T2); |
Douglas Gregor | 3996e24 | 2010-02-15 22:01:00 +0000 | [diff] [blame] | 751 | if (!IsStructurallyEquivalent(Context, |
| 752 | Typename1->getQualifier(), |
| 753 | Typename2->getQualifier())) |
| 754 | return false; |
| 755 | if (!IsStructurallyEquivalent(Typename1->getIdentifier(), |
| 756 | Typename2->getIdentifier())) |
| 757 | return false; |
Douglas Gregor | 3996e24 | 2010-02-15 22:01:00 +0000 | [diff] [blame] | 758 | |
| 759 | break; |
| 760 | } |
| 761 | |
John McCall | c392f37 | 2010-06-11 00:33:02 +0000 | [diff] [blame] | 762 | case Type::DependentTemplateSpecialization: { |
| 763 | const DependentTemplateSpecializationType *Spec1 = |
| 764 | cast<DependentTemplateSpecializationType>(T1); |
| 765 | const DependentTemplateSpecializationType *Spec2 = |
| 766 | cast<DependentTemplateSpecializationType>(T2); |
| 767 | if (!IsStructurallyEquivalent(Context, |
| 768 | Spec1->getQualifier(), |
| 769 | Spec2->getQualifier())) |
| 770 | return false; |
| 771 | if (!IsStructurallyEquivalent(Spec1->getIdentifier(), |
| 772 | Spec2->getIdentifier())) |
| 773 | return false; |
| 774 | if (Spec1->getNumArgs() != Spec2->getNumArgs()) |
| 775 | return false; |
| 776 | for (unsigned I = 0, N = Spec1->getNumArgs(); I != N; ++I) { |
| 777 | if (!IsStructurallyEquivalent(Context, |
| 778 | Spec1->getArg(I), Spec2->getArg(I))) |
| 779 | return false; |
| 780 | } |
| 781 | break; |
| 782 | } |
Douglas Gregor | d2fa766 | 2010-12-20 02:24:11 +0000 | [diff] [blame] | 783 | |
| 784 | case Type::PackExpansion: |
| 785 | if (!IsStructurallyEquivalent(Context, |
| 786 | cast<PackExpansionType>(T1)->getPattern(), |
| 787 | cast<PackExpansionType>(T2)->getPattern())) |
| 788 | return false; |
| 789 | break; |
| 790 | |
Douglas Gregor | 3996e24 | 2010-02-15 22:01:00 +0000 | [diff] [blame] | 791 | case Type::ObjCInterface: { |
| 792 | const ObjCInterfaceType *Iface1 = cast<ObjCInterfaceType>(T1); |
| 793 | const ObjCInterfaceType *Iface2 = cast<ObjCInterfaceType>(T2); |
| 794 | if (!IsStructurallyEquivalent(Context, |
| 795 | Iface1->getDecl(), Iface2->getDecl())) |
| 796 | return false; |
John McCall | 8b07ec2 | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 797 | break; |
| 798 | } |
| 799 | |
| 800 | case Type::ObjCObject: { |
| 801 | const ObjCObjectType *Obj1 = cast<ObjCObjectType>(T1); |
| 802 | const ObjCObjectType *Obj2 = cast<ObjCObjectType>(T2); |
| 803 | if (!IsStructurallyEquivalent(Context, |
| 804 | Obj1->getBaseType(), |
| 805 | Obj2->getBaseType())) |
Douglas Gregor | 3996e24 | 2010-02-15 22:01:00 +0000 | [diff] [blame] | 806 | return false; |
John McCall | 8b07ec2 | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 807 | if (Obj1->getNumProtocols() != Obj2->getNumProtocols()) |
| 808 | return false; |
| 809 | for (unsigned I = 0, N = Obj1->getNumProtocols(); I != N; ++I) { |
Douglas Gregor | 3996e24 | 2010-02-15 22:01:00 +0000 | [diff] [blame] | 810 | if (!IsStructurallyEquivalent(Context, |
John McCall | 8b07ec2 | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 811 | Obj1->getProtocol(I), |
| 812 | Obj2->getProtocol(I))) |
Douglas Gregor | 3996e24 | 2010-02-15 22:01:00 +0000 | [diff] [blame] | 813 | return false; |
| 814 | } |
| 815 | break; |
| 816 | } |
| 817 | |
| 818 | case Type::ObjCObjectPointer: { |
| 819 | const ObjCObjectPointerType *Ptr1 = cast<ObjCObjectPointerType>(T1); |
| 820 | const ObjCObjectPointerType *Ptr2 = cast<ObjCObjectPointerType>(T2); |
| 821 | if (!IsStructurallyEquivalent(Context, |
| 822 | Ptr1->getPointeeType(), |
| 823 | Ptr2->getPointeeType())) |
| 824 | return false; |
Douglas Gregor | 3996e24 | 2010-02-15 22:01:00 +0000 | [diff] [blame] | 825 | break; |
| 826 | } |
Eli Friedman | 0dfb889 | 2011-10-06 23:00:33 +0000 | [diff] [blame] | 827 | |
| 828 | case Type::Atomic: { |
| 829 | if (!IsStructurallyEquivalent(Context, |
| 830 | cast<AtomicType>(T1)->getValueType(), |
| 831 | cast<AtomicType>(T2)->getValueType())) |
| 832 | return false; |
| 833 | break; |
| 834 | } |
| 835 | |
Douglas Gregor | 3996e24 | 2010-02-15 22:01:00 +0000 | [diff] [blame] | 836 | } // end switch |
| 837 | |
| 838 | return true; |
| 839 | } |
| 840 | |
Douglas Gregor | 03d1ed3 | 2011-10-14 21:54:42 +0000 | [diff] [blame] | 841 | /// \brief Determine structural equivalence of two fields. |
| 842 | static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context, |
| 843 | FieldDecl *Field1, FieldDecl *Field2) { |
| 844 | RecordDecl *Owner2 = cast<RecordDecl>(Field2->getDeclContext()); |
Douglas Gregor | ceb32bf | 2012-10-26 16:45:11 +0000 | [diff] [blame] | 845 | |
| 846 | // For anonymous structs/unions, match up the anonymous struct/union type |
| 847 | // declarations directly, so that we don't go off searching for anonymous |
| 848 | // types |
| 849 | if (Field1->isAnonymousStructOrUnion() && |
| 850 | Field2->isAnonymousStructOrUnion()) { |
| 851 | RecordDecl *D1 = Field1->getType()->castAs<RecordType>()->getDecl(); |
| 852 | RecordDecl *D2 = Field2->getType()->castAs<RecordType>()->getDecl(); |
| 853 | return IsStructurallyEquivalent(Context, D1, D2); |
| 854 | } |
Sean Callanan | 969c5bd | 2013-04-26 22:49:25 +0000 | [diff] [blame] | 855 | |
| 856 | // Check for equivalent field names. |
| 857 | IdentifierInfo *Name1 = Field1->getIdentifier(); |
| 858 | IdentifierInfo *Name2 = Field2->getIdentifier(); |
| 859 | if (!::IsStructurallyEquivalent(Name1, Name2)) |
| 860 | return false; |
Douglas Gregor | ceb32bf | 2012-10-26 16:45:11 +0000 | [diff] [blame] | 861 | |
| 862 | if (!IsStructurallyEquivalent(Context, |
Douglas Gregor | 03d1ed3 | 2011-10-14 21:54:42 +0000 | [diff] [blame] | 863 | Field1->getType(), Field2->getType())) { |
Douglas Gregor | 069bbaf | 2012-10-26 15:34:11 +0000 | [diff] [blame] | 864 | if (Context.Complain) { |
| 865 | Context.Diag2(Owner2->getLocation(), diag::warn_odr_tag_type_inconsistent) |
| 866 | << Context.C2.getTypeDeclType(Owner2); |
| 867 | Context.Diag2(Field2->getLocation(), diag::note_odr_field) |
| 868 | << Field2->getDeclName() << Field2->getType(); |
| 869 | Context.Diag1(Field1->getLocation(), diag::note_odr_field) |
| 870 | << Field1->getDeclName() << Field1->getType(); |
| 871 | } |
Douglas Gregor | 03d1ed3 | 2011-10-14 21:54:42 +0000 | [diff] [blame] | 872 | return false; |
| 873 | } |
| 874 | |
| 875 | if (Field1->isBitField() != Field2->isBitField()) { |
Douglas Gregor | 069bbaf | 2012-10-26 15:34:11 +0000 | [diff] [blame] | 876 | if (Context.Complain) { |
| 877 | Context.Diag2(Owner2->getLocation(), diag::warn_odr_tag_type_inconsistent) |
| 878 | << Context.C2.getTypeDeclType(Owner2); |
| 879 | if (Field1->isBitField()) { |
| 880 | Context.Diag1(Field1->getLocation(), diag::note_odr_bit_field) |
| 881 | << Field1->getDeclName() << Field1->getType() |
| 882 | << Field1->getBitWidthValue(Context.C1); |
| 883 | Context.Diag2(Field2->getLocation(), diag::note_odr_not_bit_field) |
| 884 | << Field2->getDeclName(); |
| 885 | } else { |
| 886 | Context.Diag2(Field2->getLocation(), diag::note_odr_bit_field) |
| 887 | << Field2->getDeclName() << Field2->getType() |
| 888 | << Field2->getBitWidthValue(Context.C2); |
| 889 | Context.Diag1(Field1->getLocation(), diag::note_odr_not_bit_field) |
| 890 | << Field1->getDeclName(); |
| 891 | } |
Douglas Gregor | 03d1ed3 | 2011-10-14 21:54:42 +0000 | [diff] [blame] | 892 | } |
| 893 | return false; |
| 894 | } |
| 895 | |
| 896 | if (Field1->isBitField()) { |
| 897 | // Make sure that the bit-fields are the same length. |
| 898 | unsigned Bits1 = Field1->getBitWidthValue(Context.C1); |
| 899 | unsigned Bits2 = Field2->getBitWidthValue(Context.C2); |
| 900 | |
| 901 | if (Bits1 != Bits2) { |
Douglas Gregor | 069bbaf | 2012-10-26 15:34:11 +0000 | [diff] [blame] | 902 | if (Context.Complain) { |
| 903 | Context.Diag2(Owner2->getLocation(), diag::warn_odr_tag_type_inconsistent) |
| 904 | << Context.C2.getTypeDeclType(Owner2); |
| 905 | Context.Diag2(Field2->getLocation(), diag::note_odr_bit_field) |
| 906 | << Field2->getDeclName() << Field2->getType() << Bits2; |
| 907 | Context.Diag1(Field1->getLocation(), diag::note_odr_bit_field) |
| 908 | << Field1->getDeclName() << Field1->getType() << Bits1; |
| 909 | } |
Douglas Gregor | 03d1ed3 | 2011-10-14 21:54:42 +0000 | [diff] [blame] | 910 | return false; |
| 911 | } |
| 912 | } |
| 913 | |
| 914 | return true; |
| 915 | } |
| 916 | |
Douglas Gregor | ceb32bf | 2012-10-26 16:45:11 +0000 | [diff] [blame] | 917 | /// \brief Find the index of the given anonymous struct/union within its |
| 918 | /// context. |
| 919 | /// |
| 920 | /// \returns Returns the index of this anonymous struct/union in its context, |
| 921 | /// including the next assigned index (if none of them match). Returns an |
| 922 | /// empty option if the context is not a record, i.e.. if the anonymous |
| 923 | /// struct/union is at namespace or block scope. |
David Blaikie | 05785d1 | 2013-02-20 22:23:23 +0000 | [diff] [blame] | 924 | static Optional<unsigned> findAnonymousStructOrUnionIndex(RecordDecl *Anon) { |
Douglas Gregor | ceb32bf | 2012-10-26 16:45:11 +0000 | [diff] [blame] | 925 | ASTContext &Context = Anon->getASTContext(); |
| 926 | QualType AnonTy = Context.getRecordType(Anon); |
| 927 | |
| 928 | RecordDecl *Owner = dyn_cast<RecordDecl>(Anon->getDeclContext()); |
| 929 | if (!Owner) |
David Blaikie | 7a30dc5 | 2013-02-21 01:47:18 +0000 | [diff] [blame] | 930 | return None; |
Douglas Gregor | ceb32bf | 2012-10-26 16:45:11 +0000 | [diff] [blame] | 931 | |
| 932 | unsigned Index = 0; |
Aaron Ballman | 629afae | 2014-03-07 19:56:05 +0000 | [diff] [blame] | 933 | for (const auto *D : Owner->noload_decls()) { |
| 934 | const auto *F = dyn_cast<FieldDecl>(D); |
Douglas Gregor | ceb32bf | 2012-10-26 16:45:11 +0000 | [diff] [blame] | 935 | if (!F || !F->isAnonymousStructOrUnion()) |
| 936 | continue; |
| 937 | |
| 938 | if (Context.hasSameType(F->getType(), AnonTy)) |
| 939 | break; |
| 940 | |
| 941 | ++Index; |
| 942 | } |
| 943 | |
| 944 | return Index; |
| 945 | } |
| 946 | |
Douglas Gregor | 3996e24 | 2010-02-15 22:01:00 +0000 | [diff] [blame] | 947 | /// \brief Determine structural equivalence of two records. |
| 948 | static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context, |
| 949 | RecordDecl *D1, RecordDecl *D2) { |
| 950 | if (D1->isUnion() != D2->isUnion()) { |
Douglas Gregor | 069bbaf | 2012-10-26 15:34:11 +0000 | [diff] [blame] | 951 | if (Context.Complain) { |
| 952 | Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent) |
| 953 | << Context.C2.getTypeDeclType(D2); |
| 954 | Context.Diag1(D1->getLocation(), diag::note_odr_tag_kind_here) |
| 955 | << D1->getDeclName() << (unsigned)D1->getTagKind(); |
| 956 | } |
Douglas Gregor | 3996e24 | 2010-02-15 22:01:00 +0000 | [diff] [blame] | 957 | return false; |
| 958 | } |
Douglas Gregor | ceb32bf | 2012-10-26 16:45:11 +0000 | [diff] [blame] | 959 | |
| 960 | if (D1->isAnonymousStructOrUnion() && D2->isAnonymousStructOrUnion()) { |
| 961 | // If both anonymous structs/unions are in a record context, make sure |
| 962 | // they occur in the same location in the context records. |
David Blaikie | 05785d1 | 2013-02-20 22:23:23 +0000 | [diff] [blame] | 963 | if (Optional<unsigned> Index1 = findAnonymousStructOrUnionIndex(D1)) { |
| 964 | if (Optional<unsigned> Index2 = findAnonymousStructOrUnionIndex(D2)) { |
Douglas Gregor | ceb32bf | 2012-10-26 16:45:11 +0000 | [diff] [blame] | 965 | if (*Index1 != *Index2) |
| 966 | return false; |
| 967 | } |
| 968 | } |
| 969 | } |
| 970 | |
Douglas Gregor | e2e50d33 | 2010-12-01 01:36:18 +0000 | [diff] [blame] | 971 | // If both declarations are class template specializations, we know |
| 972 | // the ODR applies, so check the template and template arguments. |
| 973 | ClassTemplateSpecializationDecl *Spec1 |
| 974 | = dyn_cast<ClassTemplateSpecializationDecl>(D1); |
| 975 | ClassTemplateSpecializationDecl *Spec2 |
| 976 | = dyn_cast<ClassTemplateSpecializationDecl>(D2); |
| 977 | if (Spec1 && Spec2) { |
| 978 | // Check that the specialized templates are the same. |
| 979 | if (!IsStructurallyEquivalent(Context, Spec1->getSpecializedTemplate(), |
| 980 | Spec2->getSpecializedTemplate())) |
| 981 | return false; |
| 982 | |
| 983 | // Check that the template arguments are the same. |
| 984 | if (Spec1->getTemplateArgs().size() != Spec2->getTemplateArgs().size()) |
| 985 | return false; |
| 986 | |
| 987 | for (unsigned I = 0, N = Spec1->getTemplateArgs().size(); I != N; ++I) |
| 988 | if (!IsStructurallyEquivalent(Context, |
| 989 | Spec1->getTemplateArgs().get(I), |
| 990 | Spec2->getTemplateArgs().get(I))) |
| 991 | return false; |
| 992 | } |
| 993 | // If one is a class template specialization and the other is not, these |
Chris Lattner | 57540c5 | 2011-04-15 05:22:18 +0000 | [diff] [blame] | 994 | // structures are different. |
Douglas Gregor | e2e50d33 | 2010-12-01 01:36:18 +0000 | [diff] [blame] | 995 | else if (Spec1 || Spec2) |
| 996 | return false; |
| 997 | |
Douglas Gregor | b4964f7 | 2010-02-15 23:54:17 +0000 | [diff] [blame] | 998 | // Compare the definitions of these two records. If either or both are |
| 999 | // incomplete, we assume that they are equivalent. |
| 1000 | D1 = D1->getDefinition(); |
| 1001 | D2 = D2->getDefinition(); |
| 1002 | if (!D1 || !D2) |
| 1003 | return true; |
| 1004 | |
Douglas Gregor | 3996e24 | 2010-02-15 22:01:00 +0000 | [diff] [blame] | 1005 | if (CXXRecordDecl *D1CXX = dyn_cast<CXXRecordDecl>(D1)) { |
| 1006 | if (CXXRecordDecl *D2CXX = dyn_cast<CXXRecordDecl>(D2)) { |
| 1007 | if (D1CXX->getNumBases() != D2CXX->getNumBases()) { |
Douglas Gregor | 069bbaf | 2012-10-26 15:34:11 +0000 | [diff] [blame] | 1008 | if (Context.Complain) { |
| 1009 | Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent) |
| 1010 | << Context.C2.getTypeDeclType(D2); |
| 1011 | Context.Diag2(D2->getLocation(), diag::note_odr_number_of_bases) |
| 1012 | << D2CXX->getNumBases(); |
| 1013 | Context.Diag1(D1->getLocation(), diag::note_odr_number_of_bases) |
| 1014 | << D1CXX->getNumBases(); |
| 1015 | } |
Douglas Gregor | 3996e24 | 2010-02-15 22:01:00 +0000 | [diff] [blame] | 1016 | return false; |
| 1017 | } |
| 1018 | |
| 1019 | // Check the base classes. |
| 1020 | for (CXXRecordDecl::base_class_iterator Base1 = D1CXX->bases_begin(), |
| 1021 | BaseEnd1 = D1CXX->bases_end(), |
| 1022 | Base2 = D2CXX->bases_begin(); |
| 1023 | Base1 != BaseEnd1; |
| 1024 | ++Base1, ++Base2) { |
| 1025 | if (!IsStructurallyEquivalent(Context, |
| 1026 | Base1->getType(), Base2->getType())) { |
Douglas Gregor | 069bbaf | 2012-10-26 15:34:11 +0000 | [diff] [blame] | 1027 | if (Context.Complain) { |
| 1028 | Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent) |
| 1029 | << Context.C2.getTypeDeclType(D2); |
| 1030 | Context.Diag2(Base2->getLocStart(), diag::note_odr_base) |
| 1031 | << Base2->getType() |
| 1032 | << Base2->getSourceRange(); |
| 1033 | Context.Diag1(Base1->getLocStart(), diag::note_odr_base) |
| 1034 | << Base1->getType() |
| 1035 | << Base1->getSourceRange(); |
| 1036 | } |
Douglas Gregor | 3996e24 | 2010-02-15 22:01:00 +0000 | [diff] [blame] | 1037 | return false; |
| 1038 | } |
| 1039 | |
| 1040 | // Check virtual vs. non-virtual inheritance mismatch. |
| 1041 | if (Base1->isVirtual() != Base2->isVirtual()) { |
Douglas Gregor | 069bbaf | 2012-10-26 15:34:11 +0000 | [diff] [blame] | 1042 | if (Context.Complain) { |
| 1043 | Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent) |
| 1044 | << Context.C2.getTypeDeclType(D2); |
| 1045 | Context.Diag2(Base2->getLocStart(), |
| 1046 | diag::note_odr_virtual_base) |
| 1047 | << Base2->isVirtual() << Base2->getSourceRange(); |
| 1048 | Context.Diag1(Base1->getLocStart(), diag::note_odr_base) |
| 1049 | << Base1->isVirtual() |
| 1050 | << Base1->getSourceRange(); |
| 1051 | } |
Douglas Gregor | 3996e24 | 2010-02-15 22:01:00 +0000 | [diff] [blame] | 1052 | return false; |
| 1053 | } |
| 1054 | } |
| 1055 | } else if (D1CXX->getNumBases() > 0) { |
Douglas Gregor | 069bbaf | 2012-10-26 15:34:11 +0000 | [diff] [blame] | 1056 | if (Context.Complain) { |
| 1057 | Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent) |
| 1058 | << Context.C2.getTypeDeclType(D2); |
| 1059 | const CXXBaseSpecifier *Base1 = D1CXX->bases_begin(); |
| 1060 | Context.Diag1(Base1->getLocStart(), diag::note_odr_base) |
| 1061 | << Base1->getType() |
| 1062 | << Base1->getSourceRange(); |
| 1063 | Context.Diag2(D2->getLocation(), diag::note_odr_missing_base); |
| 1064 | } |
Douglas Gregor | 3996e24 | 2010-02-15 22:01:00 +0000 | [diff] [blame] | 1065 | return false; |
| 1066 | } |
| 1067 | } |
| 1068 | |
| 1069 | // Check the fields for consistency. |
Dmitri Gribenko | 898cff0 | 2012-05-19 17:17:26 +0000 | [diff] [blame] | 1070 | RecordDecl::field_iterator Field2 = D2->field_begin(), |
Douglas Gregor | 3996e24 | 2010-02-15 22:01:00 +0000 | [diff] [blame] | 1071 | Field2End = D2->field_end(); |
Dmitri Gribenko | 898cff0 | 2012-05-19 17:17:26 +0000 | [diff] [blame] | 1072 | for (RecordDecl::field_iterator Field1 = D1->field_begin(), |
Douglas Gregor | 3996e24 | 2010-02-15 22:01:00 +0000 | [diff] [blame] | 1073 | Field1End = D1->field_end(); |
| 1074 | Field1 != Field1End; |
| 1075 | ++Field1, ++Field2) { |
| 1076 | if (Field2 == Field2End) { |
Douglas Gregor | 069bbaf | 2012-10-26 15:34:11 +0000 | [diff] [blame] | 1077 | if (Context.Complain) { |
| 1078 | Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent) |
| 1079 | << Context.C2.getTypeDeclType(D2); |
| 1080 | Context.Diag1(Field1->getLocation(), diag::note_odr_field) |
| 1081 | << Field1->getDeclName() << Field1->getType(); |
| 1082 | Context.Diag2(D2->getLocation(), diag::note_odr_missing_field); |
| 1083 | } |
Douglas Gregor | 3996e24 | 2010-02-15 22:01:00 +0000 | [diff] [blame] | 1084 | return false; |
| 1085 | } |
| 1086 | |
David Blaikie | 40ed297 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 1087 | if (!IsStructurallyEquivalent(Context, *Field1, *Field2)) |
Douglas Gregor | 03d1ed3 | 2011-10-14 21:54:42 +0000 | [diff] [blame] | 1088 | return false; |
Douglas Gregor | 3996e24 | 2010-02-15 22:01:00 +0000 | [diff] [blame] | 1089 | } |
| 1090 | |
| 1091 | if (Field2 != Field2End) { |
Douglas Gregor | 069bbaf | 2012-10-26 15:34:11 +0000 | [diff] [blame] | 1092 | if (Context.Complain) { |
| 1093 | Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent) |
| 1094 | << Context.C2.getTypeDeclType(D2); |
| 1095 | Context.Diag2(Field2->getLocation(), diag::note_odr_field) |
| 1096 | << Field2->getDeclName() << Field2->getType(); |
| 1097 | Context.Diag1(D1->getLocation(), diag::note_odr_missing_field); |
| 1098 | } |
Douglas Gregor | 3996e24 | 2010-02-15 22:01:00 +0000 | [diff] [blame] | 1099 | return false; |
| 1100 | } |
| 1101 | |
| 1102 | return true; |
| 1103 | } |
| 1104 | |
| 1105 | /// \brief Determine structural equivalence of two enums. |
| 1106 | static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context, |
| 1107 | EnumDecl *D1, EnumDecl *D2) { |
| 1108 | EnumDecl::enumerator_iterator EC2 = D2->enumerator_begin(), |
| 1109 | EC2End = D2->enumerator_end(); |
| 1110 | for (EnumDecl::enumerator_iterator EC1 = D1->enumerator_begin(), |
| 1111 | EC1End = D1->enumerator_end(); |
| 1112 | EC1 != EC1End; ++EC1, ++EC2) { |
| 1113 | if (EC2 == EC2End) { |
Douglas Gregor | 069bbaf | 2012-10-26 15:34:11 +0000 | [diff] [blame] | 1114 | if (Context.Complain) { |
| 1115 | Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent) |
| 1116 | << Context.C2.getTypeDeclType(D2); |
| 1117 | Context.Diag1(EC1->getLocation(), diag::note_odr_enumerator) |
| 1118 | << EC1->getDeclName() |
| 1119 | << EC1->getInitVal().toString(10); |
| 1120 | Context.Diag2(D2->getLocation(), diag::note_odr_missing_enumerator); |
| 1121 | } |
Douglas Gregor | 3996e24 | 2010-02-15 22:01:00 +0000 | [diff] [blame] | 1122 | return false; |
| 1123 | } |
| 1124 | |
| 1125 | llvm::APSInt Val1 = EC1->getInitVal(); |
| 1126 | llvm::APSInt Val2 = EC2->getInitVal(); |
Eric Christopher | 6dcc376 | 2012-07-15 00:23:57 +0000 | [diff] [blame] | 1127 | if (!llvm::APSInt::isSameValue(Val1, Val2) || |
Douglas Gregor | 3996e24 | 2010-02-15 22:01:00 +0000 | [diff] [blame] | 1128 | !IsStructurallyEquivalent(EC1->getIdentifier(), EC2->getIdentifier())) { |
Douglas Gregor | 069bbaf | 2012-10-26 15:34:11 +0000 | [diff] [blame] | 1129 | if (Context.Complain) { |
| 1130 | Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent) |
| 1131 | << Context.C2.getTypeDeclType(D2); |
| 1132 | Context.Diag2(EC2->getLocation(), diag::note_odr_enumerator) |
| 1133 | << EC2->getDeclName() |
| 1134 | << EC2->getInitVal().toString(10); |
| 1135 | Context.Diag1(EC1->getLocation(), diag::note_odr_enumerator) |
| 1136 | << EC1->getDeclName() |
| 1137 | << EC1->getInitVal().toString(10); |
| 1138 | } |
Douglas Gregor | 3996e24 | 2010-02-15 22:01:00 +0000 | [diff] [blame] | 1139 | return false; |
| 1140 | } |
| 1141 | } |
| 1142 | |
| 1143 | if (EC2 != EC2End) { |
Douglas Gregor | 069bbaf | 2012-10-26 15:34:11 +0000 | [diff] [blame] | 1144 | if (Context.Complain) { |
| 1145 | Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent) |
| 1146 | << Context.C2.getTypeDeclType(D2); |
| 1147 | Context.Diag2(EC2->getLocation(), diag::note_odr_enumerator) |
| 1148 | << EC2->getDeclName() |
| 1149 | << EC2->getInitVal().toString(10); |
| 1150 | Context.Diag1(D1->getLocation(), diag::note_odr_missing_enumerator); |
| 1151 | } |
Douglas Gregor | 3996e24 | 2010-02-15 22:01:00 +0000 | [diff] [blame] | 1152 | return false; |
| 1153 | } |
| 1154 | |
| 1155 | return true; |
| 1156 | } |
Douglas Gregor | a082a49 | 2010-11-30 19:14:50 +0000 | [diff] [blame] | 1157 | |
| 1158 | static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context, |
| 1159 | TemplateParameterList *Params1, |
| 1160 | TemplateParameterList *Params2) { |
| 1161 | if (Params1->size() != Params2->size()) { |
Douglas Gregor | 069bbaf | 2012-10-26 15:34:11 +0000 | [diff] [blame] | 1162 | if (Context.Complain) { |
| 1163 | Context.Diag2(Params2->getTemplateLoc(), |
| 1164 | diag::err_odr_different_num_template_parameters) |
| 1165 | << Params1->size() << Params2->size(); |
| 1166 | Context.Diag1(Params1->getTemplateLoc(), |
| 1167 | diag::note_odr_template_parameter_list); |
| 1168 | } |
Douglas Gregor | a082a49 | 2010-11-30 19:14:50 +0000 | [diff] [blame] | 1169 | return false; |
| 1170 | } |
Douglas Gregor | 3996e24 | 2010-02-15 22:01:00 +0000 | [diff] [blame] | 1171 | |
Douglas Gregor | a082a49 | 2010-11-30 19:14:50 +0000 | [diff] [blame] | 1172 | for (unsigned I = 0, N = Params1->size(); I != N; ++I) { |
| 1173 | if (Params1->getParam(I)->getKind() != Params2->getParam(I)->getKind()) { |
Douglas Gregor | 069bbaf | 2012-10-26 15:34:11 +0000 | [diff] [blame] | 1174 | if (Context.Complain) { |
| 1175 | Context.Diag2(Params2->getParam(I)->getLocation(), |
| 1176 | diag::err_odr_different_template_parameter_kind); |
| 1177 | Context.Diag1(Params1->getParam(I)->getLocation(), |
| 1178 | diag::note_odr_template_parameter_here); |
| 1179 | } |
Douglas Gregor | a082a49 | 2010-11-30 19:14:50 +0000 | [diff] [blame] | 1180 | return false; |
| 1181 | } |
| 1182 | |
| 1183 | if (!Context.IsStructurallyEquivalent(Params1->getParam(I), |
| 1184 | Params2->getParam(I))) { |
| 1185 | |
| 1186 | return false; |
| 1187 | } |
| 1188 | } |
| 1189 | |
| 1190 | return true; |
| 1191 | } |
| 1192 | |
| 1193 | static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context, |
| 1194 | TemplateTypeParmDecl *D1, |
| 1195 | TemplateTypeParmDecl *D2) { |
| 1196 | if (D1->isParameterPack() != D2->isParameterPack()) { |
Douglas Gregor | 069bbaf | 2012-10-26 15:34:11 +0000 | [diff] [blame] | 1197 | if (Context.Complain) { |
| 1198 | Context.Diag2(D2->getLocation(), diag::err_odr_parameter_pack_non_pack) |
| 1199 | << D2->isParameterPack(); |
| 1200 | Context.Diag1(D1->getLocation(), diag::note_odr_parameter_pack_non_pack) |
| 1201 | << D1->isParameterPack(); |
| 1202 | } |
Douglas Gregor | a082a49 | 2010-11-30 19:14:50 +0000 | [diff] [blame] | 1203 | return false; |
| 1204 | } |
| 1205 | |
| 1206 | return true; |
| 1207 | } |
| 1208 | |
| 1209 | static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context, |
| 1210 | NonTypeTemplateParmDecl *D1, |
| 1211 | NonTypeTemplateParmDecl *D2) { |
Douglas Gregor | a082a49 | 2010-11-30 19:14:50 +0000 | [diff] [blame] | 1212 | if (D1->isParameterPack() != D2->isParameterPack()) { |
Douglas Gregor | 3c7380b | 2012-10-26 15:36:15 +0000 | [diff] [blame] | 1213 | if (Context.Complain) { |
Douglas Gregor | 069bbaf | 2012-10-26 15:34:11 +0000 | [diff] [blame] | 1214 | Context.Diag2(D2->getLocation(), diag::err_odr_parameter_pack_non_pack) |
| 1215 | << D2->isParameterPack(); |
| 1216 | Context.Diag1(D1->getLocation(), diag::note_odr_parameter_pack_non_pack) |
| 1217 | << D1->isParameterPack(); |
| 1218 | } |
Douglas Gregor | a082a49 | 2010-11-30 19:14:50 +0000 | [diff] [blame] | 1219 | return false; |
| 1220 | } |
Douglas Gregor | a082a49 | 2010-11-30 19:14:50 +0000 | [diff] [blame] | 1221 | |
| 1222 | // Check types. |
| 1223 | if (!Context.IsStructurallyEquivalent(D1->getType(), D2->getType())) { |
Douglas Gregor | 069bbaf | 2012-10-26 15:34:11 +0000 | [diff] [blame] | 1224 | if (Context.Complain) { |
| 1225 | Context.Diag2(D2->getLocation(), |
| 1226 | diag::err_odr_non_type_parameter_type_inconsistent) |
| 1227 | << D2->getType() << D1->getType(); |
| 1228 | Context.Diag1(D1->getLocation(), diag::note_odr_value_here) |
| 1229 | << D1->getType(); |
| 1230 | } |
Douglas Gregor | a082a49 | 2010-11-30 19:14:50 +0000 | [diff] [blame] | 1231 | return false; |
| 1232 | } |
| 1233 | |
| 1234 | return true; |
| 1235 | } |
| 1236 | |
| 1237 | static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context, |
| 1238 | TemplateTemplateParmDecl *D1, |
| 1239 | TemplateTemplateParmDecl *D2) { |
Douglas Gregor | a082a49 | 2010-11-30 19:14:50 +0000 | [diff] [blame] | 1240 | if (D1->isParameterPack() != D2->isParameterPack()) { |
Douglas Gregor | 069bbaf | 2012-10-26 15:34:11 +0000 | [diff] [blame] | 1241 | if (Context.Complain) { |
| 1242 | Context.Diag2(D2->getLocation(), diag::err_odr_parameter_pack_non_pack) |
| 1243 | << D2->isParameterPack(); |
| 1244 | Context.Diag1(D1->getLocation(), diag::note_odr_parameter_pack_non_pack) |
| 1245 | << D1->isParameterPack(); |
| 1246 | } |
Douglas Gregor | a082a49 | 2010-11-30 19:14:50 +0000 | [diff] [blame] | 1247 | return false; |
| 1248 | } |
Douglas Gregor | 3c7380b | 2012-10-26 15:36:15 +0000 | [diff] [blame] | 1249 | |
Douglas Gregor | a082a49 | 2010-11-30 19:14:50 +0000 | [diff] [blame] | 1250 | // Check template parameter lists. |
| 1251 | return IsStructurallyEquivalent(Context, D1->getTemplateParameters(), |
| 1252 | D2->getTemplateParameters()); |
| 1253 | } |
| 1254 | |
| 1255 | static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context, |
| 1256 | ClassTemplateDecl *D1, |
| 1257 | ClassTemplateDecl *D2) { |
| 1258 | // Check template parameters. |
| 1259 | if (!IsStructurallyEquivalent(Context, |
| 1260 | D1->getTemplateParameters(), |
| 1261 | D2->getTemplateParameters())) |
| 1262 | return false; |
| 1263 | |
| 1264 | // Check the templated declaration. |
| 1265 | return Context.IsStructurallyEquivalent(D1->getTemplatedDecl(), |
| 1266 | D2->getTemplatedDecl()); |
| 1267 | } |
| 1268 | |
Douglas Gregor | 3996e24 | 2010-02-15 22:01:00 +0000 | [diff] [blame] | 1269 | /// \brief Determine structural equivalence of two declarations. |
| 1270 | static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context, |
| 1271 | Decl *D1, Decl *D2) { |
| 1272 | // FIXME: Check for known structural equivalences via a callback of some sort. |
| 1273 | |
Douglas Gregor | b4964f7 | 2010-02-15 23:54:17 +0000 | [diff] [blame] | 1274 | // Check whether we already know that these two declarations are not |
| 1275 | // structurally equivalent. |
| 1276 | if (Context.NonEquivalentDecls.count(std::make_pair(D1->getCanonicalDecl(), |
| 1277 | D2->getCanonicalDecl()))) |
| 1278 | return false; |
| 1279 | |
Douglas Gregor | 3996e24 | 2010-02-15 22:01:00 +0000 | [diff] [blame] | 1280 | // Determine whether we've already produced a tentative equivalence for D1. |
| 1281 | Decl *&EquivToD1 = Context.TentativeEquivalences[D1->getCanonicalDecl()]; |
| 1282 | if (EquivToD1) |
| 1283 | return EquivToD1 == D2->getCanonicalDecl(); |
| 1284 | |
| 1285 | // Produce a tentative equivalence D1 <-> D2, which will be checked later. |
| 1286 | EquivToD1 = D2->getCanonicalDecl(); |
| 1287 | Context.DeclsToCheck.push_back(D1->getCanonicalDecl()); |
| 1288 | return true; |
| 1289 | } |
| 1290 | |
| 1291 | bool StructuralEquivalenceContext::IsStructurallyEquivalent(Decl *D1, |
| 1292 | Decl *D2) { |
| 1293 | if (!::IsStructurallyEquivalent(*this, D1, D2)) |
| 1294 | return false; |
| 1295 | |
| 1296 | return !Finish(); |
| 1297 | } |
| 1298 | |
| 1299 | bool StructuralEquivalenceContext::IsStructurallyEquivalent(QualType T1, |
| 1300 | QualType T2) { |
| 1301 | if (!::IsStructurallyEquivalent(*this, T1, T2)) |
| 1302 | return false; |
| 1303 | |
| 1304 | return !Finish(); |
| 1305 | } |
| 1306 | |
| 1307 | bool StructuralEquivalenceContext::Finish() { |
| 1308 | while (!DeclsToCheck.empty()) { |
| 1309 | // Check the next declaration. |
| 1310 | Decl *D1 = DeclsToCheck.front(); |
| 1311 | DeclsToCheck.pop_front(); |
| 1312 | |
| 1313 | Decl *D2 = TentativeEquivalences[D1]; |
| 1314 | assert(D2 && "Unrecorded tentative equivalence?"); |
| 1315 | |
Douglas Gregor | b4964f7 | 2010-02-15 23:54:17 +0000 | [diff] [blame] | 1316 | bool Equivalent = true; |
| 1317 | |
Douglas Gregor | 3996e24 | 2010-02-15 22:01:00 +0000 | [diff] [blame] | 1318 | // FIXME: Switch on all declaration kinds. For now, we're just going to |
| 1319 | // check the obvious ones. |
| 1320 | if (RecordDecl *Record1 = dyn_cast<RecordDecl>(D1)) { |
| 1321 | if (RecordDecl *Record2 = dyn_cast<RecordDecl>(D2)) { |
| 1322 | // Check for equivalent structure names. |
| 1323 | IdentifierInfo *Name1 = Record1->getIdentifier(); |
Richard Smith | dda56e4 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 1324 | if (!Name1 && Record1->getTypedefNameForAnonDecl()) |
| 1325 | Name1 = Record1->getTypedefNameForAnonDecl()->getIdentifier(); |
Douglas Gregor | 3996e24 | 2010-02-15 22:01:00 +0000 | [diff] [blame] | 1326 | IdentifierInfo *Name2 = Record2->getIdentifier(); |
Richard Smith | dda56e4 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 1327 | if (!Name2 && Record2->getTypedefNameForAnonDecl()) |
| 1328 | Name2 = Record2->getTypedefNameForAnonDecl()->getIdentifier(); |
Douglas Gregor | b4964f7 | 2010-02-15 23:54:17 +0000 | [diff] [blame] | 1329 | if (!::IsStructurallyEquivalent(Name1, Name2) || |
| 1330 | !::IsStructurallyEquivalent(*this, Record1, Record2)) |
| 1331 | Equivalent = false; |
Douglas Gregor | 3996e24 | 2010-02-15 22:01:00 +0000 | [diff] [blame] | 1332 | } else { |
| 1333 | // Record/non-record mismatch. |
Douglas Gregor | b4964f7 | 2010-02-15 23:54:17 +0000 | [diff] [blame] | 1334 | Equivalent = false; |
Douglas Gregor | 3996e24 | 2010-02-15 22:01:00 +0000 | [diff] [blame] | 1335 | } |
Douglas Gregor | b4964f7 | 2010-02-15 23:54:17 +0000 | [diff] [blame] | 1336 | } else if (EnumDecl *Enum1 = dyn_cast<EnumDecl>(D1)) { |
Douglas Gregor | 3996e24 | 2010-02-15 22:01:00 +0000 | [diff] [blame] | 1337 | if (EnumDecl *Enum2 = dyn_cast<EnumDecl>(D2)) { |
| 1338 | // Check for equivalent enum names. |
| 1339 | IdentifierInfo *Name1 = Enum1->getIdentifier(); |
Richard Smith | dda56e4 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 1340 | if (!Name1 && Enum1->getTypedefNameForAnonDecl()) |
| 1341 | Name1 = Enum1->getTypedefNameForAnonDecl()->getIdentifier(); |
Douglas Gregor | 3996e24 | 2010-02-15 22:01:00 +0000 | [diff] [blame] | 1342 | IdentifierInfo *Name2 = Enum2->getIdentifier(); |
Richard Smith | dda56e4 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 1343 | if (!Name2 && Enum2->getTypedefNameForAnonDecl()) |
| 1344 | Name2 = Enum2->getTypedefNameForAnonDecl()->getIdentifier(); |
Douglas Gregor | b4964f7 | 2010-02-15 23:54:17 +0000 | [diff] [blame] | 1345 | if (!::IsStructurallyEquivalent(Name1, Name2) || |
| 1346 | !::IsStructurallyEquivalent(*this, Enum1, Enum2)) |
| 1347 | Equivalent = false; |
Douglas Gregor | 3996e24 | 2010-02-15 22:01:00 +0000 | [diff] [blame] | 1348 | } else { |
| 1349 | // Enum/non-enum mismatch |
Douglas Gregor | b4964f7 | 2010-02-15 23:54:17 +0000 | [diff] [blame] | 1350 | Equivalent = false; |
Douglas Gregor | 3996e24 | 2010-02-15 22:01:00 +0000 | [diff] [blame] | 1351 | } |
Richard Smith | dda56e4 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 1352 | } else if (TypedefNameDecl *Typedef1 = dyn_cast<TypedefNameDecl>(D1)) { |
| 1353 | if (TypedefNameDecl *Typedef2 = dyn_cast<TypedefNameDecl>(D2)) { |
Douglas Gregor | 3996e24 | 2010-02-15 22:01:00 +0000 | [diff] [blame] | 1354 | if (!::IsStructurallyEquivalent(Typedef1->getIdentifier(), |
Douglas Gregor | b4964f7 | 2010-02-15 23:54:17 +0000 | [diff] [blame] | 1355 | Typedef2->getIdentifier()) || |
| 1356 | !::IsStructurallyEquivalent(*this, |
Douglas Gregor | 3996e24 | 2010-02-15 22:01:00 +0000 | [diff] [blame] | 1357 | Typedef1->getUnderlyingType(), |
| 1358 | Typedef2->getUnderlyingType())) |
Douglas Gregor | b4964f7 | 2010-02-15 23:54:17 +0000 | [diff] [blame] | 1359 | Equivalent = false; |
Douglas Gregor | 3996e24 | 2010-02-15 22:01:00 +0000 | [diff] [blame] | 1360 | } else { |
| 1361 | // Typedef/non-typedef mismatch. |
Douglas Gregor | b4964f7 | 2010-02-15 23:54:17 +0000 | [diff] [blame] | 1362 | Equivalent = false; |
Douglas Gregor | 3996e24 | 2010-02-15 22:01:00 +0000 | [diff] [blame] | 1363 | } |
Douglas Gregor | a082a49 | 2010-11-30 19:14:50 +0000 | [diff] [blame] | 1364 | } else if (ClassTemplateDecl *ClassTemplate1 |
| 1365 | = dyn_cast<ClassTemplateDecl>(D1)) { |
| 1366 | if (ClassTemplateDecl *ClassTemplate2 = dyn_cast<ClassTemplateDecl>(D2)) { |
| 1367 | if (!::IsStructurallyEquivalent(ClassTemplate1->getIdentifier(), |
| 1368 | ClassTemplate2->getIdentifier()) || |
| 1369 | !::IsStructurallyEquivalent(*this, ClassTemplate1, ClassTemplate2)) |
| 1370 | Equivalent = false; |
| 1371 | } else { |
| 1372 | // Class template/non-class-template mismatch. |
| 1373 | Equivalent = false; |
| 1374 | } |
| 1375 | } else if (TemplateTypeParmDecl *TTP1= dyn_cast<TemplateTypeParmDecl>(D1)) { |
| 1376 | if (TemplateTypeParmDecl *TTP2 = dyn_cast<TemplateTypeParmDecl>(D2)) { |
| 1377 | if (!::IsStructurallyEquivalent(*this, TTP1, TTP2)) |
| 1378 | Equivalent = false; |
| 1379 | } else { |
| 1380 | // Kind mismatch. |
| 1381 | Equivalent = false; |
| 1382 | } |
| 1383 | } else if (NonTypeTemplateParmDecl *NTTP1 |
| 1384 | = dyn_cast<NonTypeTemplateParmDecl>(D1)) { |
| 1385 | if (NonTypeTemplateParmDecl *NTTP2 |
| 1386 | = dyn_cast<NonTypeTemplateParmDecl>(D2)) { |
| 1387 | if (!::IsStructurallyEquivalent(*this, NTTP1, NTTP2)) |
| 1388 | Equivalent = false; |
| 1389 | } else { |
| 1390 | // Kind mismatch. |
| 1391 | Equivalent = false; |
| 1392 | } |
| 1393 | } else if (TemplateTemplateParmDecl *TTP1 |
| 1394 | = dyn_cast<TemplateTemplateParmDecl>(D1)) { |
| 1395 | if (TemplateTemplateParmDecl *TTP2 |
| 1396 | = dyn_cast<TemplateTemplateParmDecl>(D2)) { |
| 1397 | if (!::IsStructurallyEquivalent(*this, TTP1, TTP2)) |
| 1398 | Equivalent = false; |
| 1399 | } else { |
| 1400 | // Kind mismatch. |
| 1401 | Equivalent = false; |
| 1402 | } |
| 1403 | } |
| 1404 | |
Douglas Gregor | b4964f7 | 2010-02-15 23:54:17 +0000 | [diff] [blame] | 1405 | if (!Equivalent) { |
| 1406 | // Note that these two declarations are not equivalent (and we already |
| 1407 | // know about it). |
| 1408 | NonEquivalentDecls.insert(std::make_pair(D1->getCanonicalDecl(), |
| 1409 | D2->getCanonicalDecl())); |
| 1410 | return true; |
| 1411 | } |
Douglas Gregor | 3996e24 | 2010-02-15 22:01:00 +0000 | [diff] [blame] | 1412 | // FIXME: Check other declaration kinds! |
| 1413 | } |
| 1414 | |
| 1415 | return false; |
| 1416 | } |
| 1417 | |
| 1418 | //---------------------------------------------------------------------------- |
Douglas Gregor | 96e578d | 2010-02-05 17:54:41 +0000 | [diff] [blame] | 1419 | // Import Types |
| 1420 | //---------------------------------------------------------------------------- |
| 1421 | |
John McCall | 424cec9 | 2011-01-19 06:33:43 +0000 | [diff] [blame] | 1422 | QualType ASTNodeImporter::VisitType(const Type *T) { |
Douglas Gregor | e4c83e4 | 2010-02-09 22:48:33 +0000 | [diff] [blame] | 1423 | Importer.FromDiag(SourceLocation(), diag::err_unsupported_ast_node) |
| 1424 | << T->getTypeClassName(); |
| 1425 | return QualType(); |
| 1426 | } |
| 1427 | |
John McCall | 424cec9 | 2011-01-19 06:33:43 +0000 | [diff] [blame] | 1428 | QualType ASTNodeImporter::VisitBuiltinType(const BuiltinType *T) { |
Douglas Gregor | 96e578d | 2010-02-05 17:54:41 +0000 | [diff] [blame] | 1429 | switch (T->getKind()) { |
John McCall | e314e27 | 2011-10-18 21:02:43 +0000 | [diff] [blame] | 1430 | #define SHARED_SINGLETON_TYPE(Expansion) |
| 1431 | #define BUILTIN_TYPE(Id, SingletonId) \ |
| 1432 | case BuiltinType::Id: return Importer.getToContext().SingletonId; |
| 1433 | #include "clang/AST/BuiltinTypes.def" |
| 1434 | |
| 1435 | // FIXME: for Char16, Char32, and NullPtr, make sure that the "to" |
| 1436 | // context supports C++. |
| 1437 | |
| 1438 | // FIXME: for ObjCId, ObjCClass, and ObjCSel, make sure that the "to" |
| 1439 | // context supports ObjC. |
| 1440 | |
Douglas Gregor | 96e578d | 2010-02-05 17:54:41 +0000 | [diff] [blame] | 1441 | case BuiltinType::Char_U: |
| 1442 | // The context we're importing from has an unsigned 'char'. If we're |
| 1443 | // importing into a context with a signed 'char', translate to |
| 1444 | // 'unsigned char' instead. |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1445 | if (Importer.getToContext().getLangOpts().CharIsSigned) |
Douglas Gregor | 96e578d | 2010-02-05 17:54:41 +0000 | [diff] [blame] | 1446 | return Importer.getToContext().UnsignedCharTy; |
| 1447 | |
| 1448 | return Importer.getToContext().CharTy; |
| 1449 | |
Douglas Gregor | 96e578d | 2010-02-05 17:54:41 +0000 | [diff] [blame] | 1450 | case BuiltinType::Char_S: |
| 1451 | // The context we're importing from has an unsigned 'char'. If we're |
| 1452 | // importing into a context with a signed 'char', translate to |
| 1453 | // 'unsigned char' instead. |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1454 | if (!Importer.getToContext().getLangOpts().CharIsSigned) |
Douglas Gregor | 96e578d | 2010-02-05 17:54:41 +0000 | [diff] [blame] | 1455 | return Importer.getToContext().SignedCharTy; |
| 1456 | |
| 1457 | return Importer.getToContext().CharTy; |
| 1458 | |
Chris Lattner | ad3467e | 2010-12-25 23:25:43 +0000 | [diff] [blame] | 1459 | case BuiltinType::WChar_S: |
| 1460 | case BuiltinType::WChar_U: |
Douglas Gregor | 96e578d | 2010-02-05 17:54:41 +0000 | [diff] [blame] | 1461 | // FIXME: If not in C++, shall we translate to the C equivalent of |
| 1462 | // wchar_t? |
| 1463 | return Importer.getToContext().WCharTy; |
Douglas Gregor | 96e578d | 2010-02-05 17:54:41 +0000 | [diff] [blame] | 1464 | } |
David Blaikie | e4d798f | 2012-01-20 21:50:17 +0000 | [diff] [blame] | 1465 | |
| 1466 | llvm_unreachable("Invalid BuiltinType Kind!"); |
Douglas Gregor | 96e578d | 2010-02-05 17:54:41 +0000 | [diff] [blame] | 1467 | } |
| 1468 | |
John McCall | 424cec9 | 2011-01-19 06:33:43 +0000 | [diff] [blame] | 1469 | QualType ASTNodeImporter::VisitComplexType(const ComplexType *T) { |
Douglas Gregor | 96e578d | 2010-02-05 17:54:41 +0000 | [diff] [blame] | 1470 | QualType ToElementType = Importer.Import(T->getElementType()); |
| 1471 | if (ToElementType.isNull()) |
| 1472 | return QualType(); |
| 1473 | |
| 1474 | return Importer.getToContext().getComplexType(ToElementType); |
| 1475 | } |
| 1476 | |
John McCall | 424cec9 | 2011-01-19 06:33:43 +0000 | [diff] [blame] | 1477 | QualType ASTNodeImporter::VisitPointerType(const PointerType *T) { |
Douglas Gregor | 96e578d | 2010-02-05 17:54:41 +0000 | [diff] [blame] | 1478 | QualType ToPointeeType = Importer.Import(T->getPointeeType()); |
| 1479 | if (ToPointeeType.isNull()) |
| 1480 | return QualType(); |
| 1481 | |
| 1482 | return Importer.getToContext().getPointerType(ToPointeeType); |
| 1483 | } |
| 1484 | |
John McCall | 424cec9 | 2011-01-19 06:33:43 +0000 | [diff] [blame] | 1485 | QualType ASTNodeImporter::VisitBlockPointerType(const BlockPointerType *T) { |
Douglas Gregor | 96e578d | 2010-02-05 17:54:41 +0000 | [diff] [blame] | 1486 | // FIXME: Check for blocks support in "to" context. |
| 1487 | QualType ToPointeeType = Importer.Import(T->getPointeeType()); |
| 1488 | if (ToPointeeType.isNull()) |
| 1489 | return QualType(); |
| 1490 | |
| 1491 | return Importer.getToContext().getBlockPointerType(ToPointeeType); |
| 1492 | } |
| 1493 | |
John McCall | 424cec9 | 2011-01-19 06:33:43 +0000 | [diff] [blame] | 1494 | QualType |
| 1495 | ASTNodeImporter::VisitLValueReferenceType(const LValueReferenceType *T) { |
Douglas Gregor | 96e578d | 2010-02-05 17:54:41 +0000 | [diff] [blame] | 1496 | // FIXME: Check for C++ support in "to" context. |
| 1497 | QualType ToPointeeType = Importer.Import(T->getPointeeTypeAsWritten()); |
| 1498 | if (ToPointeeType.isNull()) |
| 1499 | return QualType(); |
| 1500 | |
| 1501 | return Importer.getToContext().getLValueReferenceType(ToPointeeType); |
| 1502 | } |
| 1503 | |
John McCall | 424cec9 | 2011-01-19 06:33:43 +0000 | [diff] [blame] | 1504 | QualType |
| 1505 | ASTNodeImporter::VisitRValueReferenceType(const RValueReferenceType *T) { |
Douglas Gregor | 96e578d | 2010-02-05 17:54:41 +0000 | [diff] [blame] | 1506 | // FIXME: Check for C++0x support in "to" context. |
| 1507 | QualType ToPointeeType = Importer.Import(T->getPointeeTypeAsWritten()); |
| 1508 | if (ToPointeeType.isNull()) |
| 1509 | return QualType(); |
| 1510 | |
| 1511 | return Importer.getToContext().getRValueReferenceType(ToPointeeType); |
| 1512 | } |
| 1513 | |
John McCall | 424cec9 | 2011-01-19 06:33:43 +0000 | [diff] [blame] | 1514 | QualType ASTNodeImporter::VisitMemberPointerType(const MemberPointerType *T) { |
Douglas Gregor | 96e578d | 2010-02-05 17:54:41 +0000 | [diff] [blame] | 1515 | // FIXME: Check for C++ support in "to" context. |
| 1516 | QualType ToPointeeType = Importer.Import(T->getPointeeType()); |
| 1517 | if (ToPointeeType.isNull()) |
| 1518 | return QualType(); |
| 1519 | |
| 1520 | QualType ClassType = Importer.Import(QualType(T->getClass(), 0)); |
| 1521 | return Importer.getToContext().getMemberPointerType(ToPointeeType, |
| 1522 | ClassType.getTypePtr()); |
| 1523 | } |
| 1524 | |
John McCall | 424cec9 | 2011-01-19 06:33:43 +0000 | [diff] [blame] | 1525 | QualType ASTNodeImporter::VisitConstantArrayType(const ConstantArrayType *T) { |
Douglas Gregor | 96e578d | 2010-02-05 17:54:41 +0000 | [diff] [blame] | 1526 | QualType ToElementType = Importer.Import(T->getElementType()); |
| 1527 | if (ToElementType.isNull()) |
| 1528 | return QualType(); |
| 1529 | |
| 1530 | return Importer.getToContext().getConstantArrayType(ToElementType, |
| 1531 | T->getSize(), |
| 1532 | T->getSizeModifier(), |
| 1533 | T->getIndexTypeCVRQualifiers()); |
| 1534 | } |
| 1535 | |
John McCall | 424cec9 | 2011-01-19 06:33:43 +0000 | [diff] [blame] | 1536 | QualType |
| 1537 | ASTNodeImporter::VisitIncompleteArrayType(const IncompleteArrayType *T) { |
Douglas Gregor | 96e578d | 2010-02-05 17:54:41 +0000 | [diff] [blame] | 1538 | QualType ToElementType = Importer.Import(T->getElementType()); |
| 1539 | if (ToElementType.isNull()) |
| 1540 | return QualType(); |
| 1541 | |
| 1542 | return Importer.getToContext().getIncompleteArrayType(ToElementType, |
| 1543 | T->getSizeModifier(), |
| 1544 | T->getIndexTypeCVRQualifiers()); |
| 1545 | } |
| 1546 | |
John McCall | 424cec9 | 2011-01-19 06:33:43 +0000 | [diff] [blame] | 1547 | QualType ASTNodeImporter::VisitVariableArrayType(const VariableArrayType *T) { |
Douglas Gregor | 96e578d | 2010-02-05 17:54:41 +0000 | [diff] [blame] | 1548 | QualType ToElementType = Importer.Import(T->getElementType()); |
| 1549 | if (ToElementType.isNull()) |
| 1550 | return QualType(); |
| 1551 | |
| 1552 | Expr *Size = Importer.Import(T->getSizeExpr()); |
| 1553 | if (!Size) |
| 1554 | return QualType(); |
| 1555 | |
| 1556 | SourceRange Brackets = Importer.Import(T->getBracketsRange()); |
| 1557 | return Importer.getToContext().getVariableArrayType(ToElementType, Size, |
| 1558 | T->getSizeModifier(), |
| 1559 | T->getIndexTypeCVRQualifiers(), |
| 1560 | Brackets); |
| 1561 | } |
| 1562 | |
John McCall | 424cec9 | 2011-01-19 06:33:43 +0000 | [diff] [blame] | 1563 | QualType ASTNodeImporter::VisitVectorType(const VectorType *T) { |
Douglas Gregor | 96e578d | 2010-02-05 17:54:41 +0000 | [diff] [blame] | 1564 | QualType ToElementType = Importer.Import(T->getElementType()); |
| 1565 | if (ToElementType.isNull()) |
| 1566 | return QualType(); |
| 1567 | |
| 1568 | return Importer.getToContext().getVectorType(ToElementType, |
| 1569 | T->getNumElements(), |
Bob Wilson | aeb5644 | 2010-11-10 21:56:12 +0000 | [diff] [blame] | 1570 | T->getVectorKind()); |
Douglas Gregor | 96e578d | 2010-02-05 17:54:41 +0000 | [diff] [blame] | 1571 | } |
| 1572 | |
John McCall | 424cec9 | 2011-01-19 06:33:43 +0000 | [diff] [blame] | 1573 | QualType ASTNodeImporter::VisitExtVectorType(const ExtVectorType *T) { |
Douglas Gregor | 96e578d | 2010-02-05 17:54:41 +0000 | [diff] [blame] | 1574 | QualType ToElementType = Importer.Import(T->getElementType()); |
| 1575 | if (ToElementType.isNull()) |
| 1576 | return QualType(); |
| 1577 | |
| 1578 | return Importer.getToContext().getExtVectorType(ToElementType, |
| 1579 | T->getNumElements()); |
| 1580 | } |
| 1581 | |
John McCall | 424cec9 | 2011-01-19 06:33:43 +0000 | [diff] [blame] | 1582 | QualType |
| 1583 | ASTNodeImporter::VisitFunctionNoProtoType(const FunctionNoProtoType *T) { |
Douglas Gregor | 96e578d | 2010-02-05 17:54:41 +0000 | [diff] [blame] | 1584 | // FIXME: What happens if we're importing a function without a prototype |
| 1585 | // into C++? Should we make it variadic? |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 1586 | QualType ToResultType = Importer.Import(T->getReturnType()); |
Douglas Gregor | 96e578d | 2010-02-05 17:54:41 +0000 | [diff] [blame] | 1587 | if (ToResultType.isNull()) |
| 1588 | return QualType(); |
Rafael Espindola | c50c27c | 2010-03-30 20:24:48 +0000 | [diff] [blame] | 1589 | |
Douglas Gregor | 96e578d | 2010-02-05 17:54:41 +0000 | [diff] [blame] | 1590 | return Importer.getToContext().getFunctionNoProtoType(ToResultType, |
Rafael Espindola | c50c27c | 2010-03-30 20:24:48 +0000 | [diff] [blame] | 1591 | T->getExtInfo()); |
Douglas Gregor | 96e578d | 2010-02-05 17:54:41 +0000 | [diff] [blame] | 1592 | } |
| 1593 | |
Argyrios Kyrtzidis | 2f45853 | 2012-09-25 19:26:39 +0000 | [diff] [blame] | 1594 | QualType ASTNodeImporter::VisitFunctionProtoType(const FunctionProtoType *T) { |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 1595 | QualType ToResultType = Importer.Import(T->getReturnType()); |
Douglas Gregor | 96e578d | 2010-02-05 17:54:41 +0000 | [diff] [blame] | 1596 | if (ToResultType.isNull()) |
| 1597 | return QualType(); |
| 1598 | |
| 1599 | // Import argument types |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 1600 | SmallVector<QualType, 4> ArgTypes; |
Aaron Ballman | 40bd0aa | 2014-03-17 15:23:01 +0000 | [diff] [blame] | 1601 | for (const auto &A : T->param_types()) { |
| 1602 | QualType ArgType = Importer.Import(A); |
Douglas Gregor | 96e578d | 2010-02-05 17:54:41 +0000 | [diff] [blame] | 1603 | if (ArgType.isNull()) |
| 1604 | return QualType(); |
| 1605 | ArgTypes.push_back(ArgType); |
| 1606 | } |
| 1607 | |
| 1608 | // Import exception types |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 1609 | SmallVector<QualType, 4> ExceptionTypes; |
Aaron Ballman | b088fbe | 2014-03-17 15:38:09 +0000 | [diff] [blame] | 1610 | for (const auto &E : T->exceptions()) { |
| 1611 | QualType ExceptionType = Importer.Import(E); |
Douglas Gregor | 96e578d | 2010-02-05 17:54:41 +0000 | [diff] [blame] | 1612 | if (ExceptionType.isNull()) |
| 1613 | return QualType(); |
| 1614 | ExceptionTypes.push_back(ExceptionType); |
| 1615 | } |
John McCall | db40c7f | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 1616 | |
Argyrios Kyrtzidis | b41791d | 2012-09-22 01:58:06 +0000 | [diff] [blame] | 1617 | FunctionProtoType::ExtProtoInfo FromEPI = T->getExtProtoInfo(); |
| 1618 | FunctionProtoType::ExtProtoInfo ToEPI; |
| 1619 | |
| 1620 | ToEPI.ExtInfo = FromEPI.ExtInfo; |
| 1621 | ToEPI.Variadic = FromEPI.Variadic; |
| 1622 | ToEPI.HasTrailingReturn = FromEPI.HasTrailingReturn; |
| 1623 | ToEPI.TypeQuals = FromEPI.TypeQuals; |
| 1624 | ToEPI.RefQualifier = FromEPI.RefQualifier; |
Richard Smith | 8acb428 | 2014-07-31 21:57:55 +0000 | [diff] [blame] | 1625 | ToEPI.ExceptionSpec.Type = FromEPI.ExceptionSpec.Type; |
| 1626 | ToEPI.ExceptionSpec.Exceptions = ExceptionTypes; |
| 1627 | ToEPI.ExceptionSpec.NoexceptExpr = |
| 1628 | Importer.Import(FromEPI.ExceptionSpec.NoexceptExpr); |
| 1629 | ToEPI.ExceptionSpec.SourceDecl = cast_or_null<FunctionDecl>( |
| 1630 | Importer.Import(FromEPI.ExceptionSpec.SourceDecl)); |
| 1631 | ToEPI.ExceptionSpec.SourceTemplate = cast_or_null<FunctionDecl>( |
| 1632 | Importer.Import(FromEPI.ExceptionSpec.SourceTemplate)); |
Argyrios Kyrtzidis | b41791d | 2012-09-22 01:58:06 +0000 | [diff] [blame] | 1633 | |
Jordan Rose | 5c38272 | 2013-03-08 21:51:21 +0000 | [diff] [blame] | 1634 | return Importer.getToContext().getFunctionType(ToResultType, ArgTypes, ToEPI); |
Argyrios Kyrtzidis | b41791d | 2012-09-22 01:58:06 +0000 | [diff] [blame] | 1635 | } |
| 1636 | |
Sean Callanan | da6df8a | 2011-08-11 16:56:07 +0000 | [diff] [blame] | 1637 | QualType ASTNodeImporter::VisitParenType(const ParenType *T) { |
| 1638 | QualType ToInnerType = Importer.Import(T->getInnerType()); |
| 1639 | if (ToInnerType.isNull()) |
| 1640 | return QualType(); |
| 1641 | |
| 1642 | return Importer.getToContext().getParenType(ToInnerType); |
| 1643 | } |
| 1644 | |
John McCall | 424cec9 | 2011-01-19 06:33:43 +0000 | [diff] [blame] | 1645 | QualType ASTNodeImporter::VisitTypedefType(const TypedefType *T) { |
Richard Smith | dda56e4 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 1646 | TypedefNameDecl *ToDecl |
| 1647 | = dyn_cast_or_null<TypedefNameDecl>(Importer.Import(T->getDecl())); |
Douglas Gregor | 96e578d | 2010-02-05 17:54:41 +0000 | [diff] [blame] | 1648 | if (!ToDecl) |
| 1649 | return QualType(); |
| 1650 | |
| 1651 | return Importer.getToContext().getTypeDeclType(ToDecl); |
| 1652 | } |
| 1653 | |
John McCall | 424cec9 | 2011-01-19 06:33:43 +0000 | [diff] [blame] | 1654 | QualType ASTNodeImporter::VisitTypeOfExprType(const TypeOfExprType *T) { |
Douglas Gregor | 96e578d | 2010-02-05 17:54:41 +0000 | [diff] [blame] | 1655 | Expr *ToExpr = Importer.Import(T->getUnderlyingExpr()); |
| 1656 | if (!ToExpr) |
| 1657 | return QualType(); |
| 1658 | |
| 1659 | return Importer.getToContext().getTypeOfExprType(ToExpr); |
| 1660 | } |
| 1661 | |
John McCall | 424cec9 | 2011-01-19 06:33:43 +0000 | [diff] [blame] | 1662 | QualType ASTNodeImporter::VisitTypeOfType(const TypeOfType *T) { |
Douglas Gregor | 96e578d | 2010-02-05 17:54:41 +0000 | [diff] [blame] | 1663 | QualType ToUnderlyingType = Importer.Import(T->getUnderlyingType()); |
| 1664 | if (ToUnderlyingType.isNull()) |
| 1665 | return QualType(); |
| 1666 | |
| 1667 | return Importer.getToContext().getTypeOfType(ToUnderlyingType); |
| 1668 | } |
| 1669 | |
John McCall | 424cec9 | 2011-01-19 06:33:43 +0000 | [diff] [blame] | 1670 | QualType ASTNodeImporter::VisitDecltypeType(const DecltypeType *T) { |
Richard Smith | 30482bc | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 1671 | // FIXME: Make sure that the "to" context supports C++0x! |
Douglas Gregor | 96e578d | 2010-02-05 17:54:41 +0000 | [diff] [blame] | 1672 | Expr *ToExpr = Importer.Import(T->getUnderlyingExpr()); |
| 1673 | if (!ToExpr) |
| 1674 | return QualType(); |
| 1675 | |
Douglas Gregor | 81495f3 | 2012-02-12 18:42:33 +0000 | [diff] [blame] | 1676 | QualType UnderlyingType = Importer.Import(T->getUnderlyingType()); |
| 1677 | if (UnderlyingType.isNull()) |
| 1678 | return QualType(); |
| 1679 | |
| 1680 | return Importer.getToContext().getDecltypeType(ToExpr, UnderlyingType); |
Douglas Gregor | 96e578d | 2010-02-05 17:54:41 +0000 | [diff] [blame] | 1681 | } |
| 1682 | |
Alexis Hunt | e852b10 | 2011-05-24 22:41:36 +0000 | [diff] [blame] | 1683 | QualType ASTNodeImporter::VisitUnaryTransformType(const UnaryTransformType *T) { |
| 1684 | QualType ToBaseType = Importer.Import(T->getBaseType()); |
| 1685 | QualType ToUnderlyingType = Importer.Import(T->getUnderlyingType()); |
| 1686 | if (ToBaseType.isNull() || ToUnderlyingType.isNull()) |
| 1687 | return QualType(); |
| 1688 | |
| 1689 | return Importer.getToContext().getUnaryTransformType(ToBaseType, |
| 1690 | ToUnderlyingType, |
| 1691 | T->getUTTKind()); |
| 1692 | } |
| 1693 | |
Richard Smith | 30482bc | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 1694 | QualType ASTNodeImporter::VisitAutoType(const AutoType *T) { |
Richard Smith | 74aeef5 | 2013-04-26 16:15:35 +0000 | [diff] [blame] | 1695 | // FIXME: Make sure that the "to" context supports C++11! |
Richard Smith | 30482bc | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 1696 | QualType FromDeduced = T->getDeducedType(); |
| 1697 | QualType ToDeduced; |
| 1698 | if (!FromDeduced.isNull()) { |
| 1699 | ToDeduced = Importer.Import(FromDeduced); |
| 1700 | if (ToDeduced.isNull()) |
| 1701 | return QualType(); |
| 1702 | } |
| 1703 | |
Faisal Vali | 2b391ab | 2013-09-26 19:54:12 +0000 | [diff] [blame] | 1704 | return Importer.getToContext().getAutoType(ToDeduced, T->isDecltypeAuto(), |
| 1705 | /*IsDependent*/false); |
Richard Smith | 30482bc | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 1706 | } |
| 1707 | |
John McCall | 424cec9 | 2011-01-19 06:33:43 +0000 | [diff] [blame] | 1708 | QualType ASTNodeImporter::VisitRecordType(const RecordType *T) { |
Douglas Gregor | 96e578d | 2010-02-05 17:54:41 +0000 | [diff] [blame] | 1709 | RecordDecl *ToDecl |
| 1710 | = dyn_cast_or_null<RecordDecl>(Importer.Import(T->getDecl())); |
| 1711 | if (!ToDecl) |
| 1712 | return QualType(); |
| 1713 | |
| 1714 | return Importer.getToContext().getTagDeclType(ToDecl); |
| 1715 | } |
| 1716 | |
John McCall | 424cec9 | 2011-01-19 06:33:43 +0000 | [diff] [blame] | 1717 | QualType ASTNodeImporter::VisitEnumType(const EnumType *T) { |
Douglas Gregor | 96e578d | 2010-02-05 17:54:41 +0000 | [diff] [blame] | 1718 | EnumDecl *ToDecl |
| 1719 | = dyn_cast_or_null<EnumDecl>(Importer.Import(T->getDecl())); |
| 1720 | if (!ToDecl) |
| 1721 | return QualType(); |
| 1722 | |
| 1723 | return Importer.getToContext().getTagDeclType(ToDecl); |
| 1724 | } |
| 1725 | |
Douglas Gregor | e2e50d33 | 2010-12-01 01:36:18 +0000 | [diff] [blame] | 1726 | QualType ASTNodeImporter::VisitTemplateSpecializationType( |
John McCall | 424cec9 | 2011-01-19 06:33:43 +0000 | [diff] [blame] | 1727 | const TemplateSpecializationType *T) { |
Douglas Gregor | e2e50d33 | 2010-12-01 01:36:18 +0000 | [diff] [blame] | 1728 | TemplateName ToTemplate = Importer.Import(T->getTemplateName()); |
| 1729 | if (ToTemplate.isNull()) |
| 1730 | return QualType(); |
| 1731 | |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 1732 | SmallVector<TemplateArgument, 2> ToTemplateArgs; |
Douglas Gregor | e2e50d33 | 2010-12-01 01:36:18 +0000 | [diff] [blame] | 1733 | if (ImportTemplateArguments(T->getArgs(), T->getNumArgs(), ToTemplateArgs)) |
| 1734 | return QualType(); |
| 1735 | |
| 1736 | QualType ToCanonType; |
| 1737 | if (!QualType(T, 0).isCanonical()) { |
| 1738 | QualType FromCanonType |
| 1739 | = Importer.getFromContext().getCanonicalType(QualType(T, 0)); |
| 1740 | ToCanonType =Importer.Import(FromCanonType); |
| 1741 | if (ToCanonType.isNull()) |
| 1742 | return QualType(); |
| 1743 | } |
| 1744 | return Importer.getToContext().getTemplateSpecializationType(ToTemplate, |
| 1745 | ToTemplateArgs.data(), |
| 1746 | ToTemplateArgs.size(), |
| 1747 | ToCanonType); |
| 1748 | } |
| 1749 | |
John McCall | 424cec9 | 2011-01-19 06:33:43 +0000 | [diff] [blame] | 1750 | QualType ASTNodeImporter::VisitElaboratedType(const ElaboratedType *T) { |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 1751 | NestedNameSpecifier *ToQualifier = nullptr; |
Abramo Bagnara | 6150c88 | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 1752 | // Note: the qualifier in an ElaboratedType is optional. |
| 1753 | if (T->getQualifier()) { |
| 1754 | ToQualifier = Importer.Import(T->getQualifier()); |
| 1755 | if (!ToQualifier) |
| 1756 | return QualType(); |
| 1757 | } |
Douglas Gregor | 96e578d | 2010-02-05 17:54:41 +0000 | [diff] [blame] | 1758 | |
| 1759 | QualType ToNamedType = Importer.Import(T->getNamedType()); |
| 1760 | if (ToNamedType.isNull()) |
| 1761 | return QualType(); |
| 1762 | |
Abramo Bagnara | 6150c88 | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 1763 | return Importer.getToContext().getElaboratedType(T->getKeyword(), |
| 1764 | ToQualifier, ToNamedType); |
Douglas Gregor | 96e578d | 2010-02-05 17:54:41 +0000 | [diff] [blame] | 1765 | } |
| 1766 | |
John McCall | 424cec9 | 2011-01-19 06:33:43 +0000 | [diff] [blame] | 1767 | QualType ASTNodeImporter::VisitObjCInterfaceType(const ObjCInterfaceType *T) { |
Douglas Gregor | 96e578d | 2010-02-05 17:54:41 +0000 | [diff] [blame] | 1768 | ObjCInterfaceDecl *Class |
| 1769 | = dyn_cast_or_null<ObjCInterfaceDecl>(Importer.Import(T->getDecl())); |
| 1770 | if (!Class) |
| 1771 | return QualType(); |
| 1772 | |
John McCall | 8b07ec2 | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 1773 | return Importer.getToContext().getObjCInterfaceType(Class); |
| 1774 | } |
| 1775 | |
John McCall | 424cec9 | 2011-01-19 06:33:43 +0000 | [diff] [blame] | 1776 | QualType ASTNodeImporter::VisitObjCObjectType(const ObjCObjectType *T) { |
John McCall | 8b07ec2 | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 1777 | QualType ToBaseType = Importer.Import(T->getBaseType()); |
| 1778 | if (ToBaseType.isNull()) |
| 1779 | return QualType(); |
| 1780 | |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 1781 | SmallVector<ObjCProtocolDecl *, 4> Protocols; |
Aaron Ballman | 1683f7b | 2014-03-17 15:55:30 +0000 | [diff] [blame] | 1782 | for (auto *P : T->quals()) { |
Douglas Gregor | 96e578d | 2010-02-05 17:54:41 +0000 | [diff] [blame] | 1783 | ObjCProtocolDecl *Protocol |
Aaron Ballman | 1683f7b | 2014-03-17 15:55:30 +0000 | [diff] [blame] | 1784 | = dyn_cast_or_null<ObjCProtocolDecl>(Importer.Import(P)); |
Douglas Gregor | 96e578d | 2010-02-05 17:54:41 +0000 | [diff] [blame] | 1785 | if (!Protocol) |
| 1786 | return QualType(); |
| 1787 | Protocols.push_back(Protocol); |
| 1788 | } |
| 1789 | |
John McCall | 8b07ec2 | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 1790 | return Importer.getToContext().getObjCObjectType(ToBaseType, |
| 1791 | Protocols.data(), |
| 1792 | Protocols.size()); |
Douglas Gregor | 96e578d | 2010-02-05 17:54:41 +0000 | [diff] [blame] | 1793 | } |
| 1794 | |
John McCall | 424cec9 | 2011-01-19 06:33:43 +0000 | [diff] [blame] | 1795 | QualType |
| 1796 | ASTNodeImporter::VisitObjCObjectPointerType(const ObjCObjectPointerType *T) { |
Douglas Gregor | 96e578d | 2010-02-05 17:54:41 +0000 | [diff] [blame] | 1797 | QualType ToPointeeType = Importer.Import(T->getPointeeType()); |
| 1798 | if (ToPointeeType.isNull()) |
| 1799 | return QualType(); |
| 1800 | |
John McCall | 8b07ec2 | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 1801 | return Importer.getToContext().getObjCObjectPointerType(ToPointeeType); |
Douglas Gregor | 96e578d | 2010-02-05 17:54:41 +0000 | [diff] [blame] | 1802 | } |
| 1803 | |
Douglas Gregor | 3aed6cd | 2010-02-08 21:09:39 +0000 | [diff] [blame] | 1804 | //---------------------------------------------------------------------------- |
| 1805 | // Import Declarations |
| 1806 | //---------------------------------------------------------------------------- |
Douglas Gregor | bb7930c | 2010-02-10 19:54:31 +0000 | [diff] [blame] | 1807 | bool ASTNodeImporter::ImportDeclParts(NamedDecl *D, DeclContext *&DC, |
| 1808 | DeclContext *&LexicalDC, |
| 1809 | DeclarationName &Name, |
| 1810 | SourceLocation &Loc) { |
| 1811 | // Import the context of this declaration. |
| 1812 | DC = Importer.ImportContext(D->getDeclContext()); |
| 1813 | if (!DC) |
| 1814 | return true; |
| 1815 | |
| 1816 | LexicalDC = DC; |
| 1817 | if (D->getDeclContext() != D->getLexicalDeclContext()) { |
| 1818 | LexicalDC = Importer.ImportContext(D->getLexicalDeclContext()); |
| 1819 | if (!LexicalDC) |
| 1820 | return true; |
| 1821 | } |
| 1822 | |
| 1823 | // Import the name of this declaration. |
| 1824 | Name = Importer.Import(D->getDeclName()); |
| 1825 | if (D->getDeclName() && !Name) |
| 1826 | return true; |
| 1827 | |
| 1828 | // Import the location of this declaration. |
| 1829 | Loc = Importer.Import(D->getLocation()); |
| 1830 | return false; |
| 1831 | } |
| 1832 | |
Douglas Gregor | d451ea9 | 2011-07-29 23:31:30 +0000 | [diff] [blame] | 1833 | void ASTNodeImporter::ImportDefinitionIfNeeded(Decl *FromD, Decl *ToD) { |
| 1834 | if (!FromD) |
| 1835 | return; |
| 1836 | |
| 1837 | if (!ToD) { |
| 1838 | ToD = Importer.Import(FromD); |
| 1839 | if (!ToD) |
| 1840 | return; |
| 1841 | } |
| 1842 | |
| 1843 | if (RecordDecl *FromRecord = dyn_cast<RecordDecl>(FromD)) { |
| 1844 | if (RecordDecl *ToRecord = cast_or_null<RecordDecl>(ToD)) { |
Sean Callanan | 19dfc93 | 2013-01-11 23:17:47 +0000 | [diff] [blame] | 1845 | if (FromRecord->getDefinition() && FromRecord->isCompleteDefinition() && !ToRecord->getDefinition()) { |
Douglas Gregor | d451ea9 | 2011-07-29 23:31:30 +0000 | [diff] [blame] | 1846 | ImportDefinition(FromRecord, ToRecord); |
| 1847 | } |
| 1848 | } |
| 1849 | return; |
| 1850 | } |
| 1851 | |
| 1852 | if (EnumDecl *FromEnum = dyn_cast<EnumDecl>(FromD)) { |
| 1853 | if (EnumDecl *ToEnum = cast_or_null<EnumDecl>(ToD)) { |
| 1854 | if (FromEnum->getDefinition() && !ToEnum->getDefinition()) { |
| 1855 | ImportDefinition(FromEnum, ToEnum); |
| 1856 | } |
| 1857 | } |
| 1858 | return; |
| 1859 | } |
| 1860 | } |
| 1861 | |
Abramo Bagnara | d6d2f18 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 1862 | void |
| 1863 | ASTNodeImporter::ImportDeclarationNameLoc(const DeclarationNameInfo &From, |
| 1864 | DeclarationNameInfo& To) { |
| 1865 | // NOTE: To.Name and To.Loc are already imported. |
| 1866 | // We only have to import To.LocInfo. |
| 1867 | switch (To.getName().getNameKind()) { |
| 1868 | case DeclarationName::Identifier: |
| 1869 | case DeclarationName::ObjCZeroArgSelector: |
| 1870 | case DeclarationName::ObjCOneArgSelector: |
| 1871 | case DeclarationName::ObjCMultiArgSelector: |
| 1872 | case DeclarationName::CXXUsingDirective: |
| 1873 | return; |
| 1874 | |
| 1875 | case DeclarationName::CXXOperatorName: { |
| 1876 | SourceRange Range = From.getCXXOperatorNameRange(); |
| 1877 | To.setCXXOperatorNameRange(Importer.Import(Range)); |
| 1878 | return; |
| 1879 | } |
| 1880 | case DeclarationName::CXXLiteralOperatorName: { |
| 1881 | SourceLocation Loc = From.getCXXLiteralOperatorNameLoc(); |
| 1882 | To.setCXXLiteralOperatorNameLoc(Importer.Import(Loc)); |
| 1883 | return; |
| 1884 | } |
| 1885 | case DeclarationName::CXXConstructorName: |
| 1886 | case DeclarationName::CXXDestructorName: |
| 1887 | case DeclarationName::CXXConversionFunctionName: { |
| 1888 | TypeSourceInfo *FromTInfo = From.getNamedTypeInfo(); |
| 1889 | To.setNamedTypeInfo(Importer.Import(FromTInfo)); |
| 1890 | return; |
| 1891 | } |
Abramo Bagnara | d6d2f18 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 1892 | } |
Douglas Gregor | 07216d1 | 2011-11-02 20:52:01 +0000 | [diff] [blame] | 1893 | llvm_unreachable("Unknown name kind."); |
Abramo Bagnara | d6d2f18 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 1894 | } |
| 1895 | |
Douglas Gregor | 2e15c84 | 2012-02-01 21:00:38 +0000 | [diff] [blame] | 1896 | void ASTNodeImporter::ImportDeclContext(DeclContext *FromDC, bool ForceImport) { |
Douglas Gregor | 0a79167 | 2011-01-18 03:11:38 +0000 | [diff] [blame] | 1897 | if (Importer.isMinimalImport() && !ForceImport) { |
Sean Callanan | 81d577c | 2011-07-22 23:46:03 +0000 | [diff] [blame] | 1898 | Importer.ImportContext(FromDC); |
Douglas Gregor | 0a79167 | 2011-01-18 03:11:38 +0000 | [diff] [blame] | 1899 | return; |
| 1900 | } |
| 1901 | |
Aaron Ballman | 629afae | 2014-03-07 19:56:05 +0000 | [diff] [blame] | 1902 | for (auto *From : FromDC->decls()) |
| 1903 | Importer.Import(From); |
Douglas Gregor | 968d633 | 2010-02-21 18:24:45 +0000 | [diff] [blame] | 1904 | } |
| 1905 | |
Douglas Gregor | d451ea9 | 2011-07-29 23:31:30 +0000 | [diff] [blame] | 1906 | bool ASTNodeImporter::ImportDefinition(RecordDecl *From, RecordDecl *To, |
Douglas Gregor | 95d8283 | 2012-01-24 18:36:04 +0000 | [diff] [blame] | 1907 | ImportDefinitionKind Kind) { |
| 1908 | if (To->getDefinition() || To->isBeingDefined()) { |
| 1909 | if (Kind == IDK_Everything) |
| 1910 | ImportDeclContext(From, /*ForceImport=*/true); |
| 1911 | |
Douglas Gregor | e2e50d33 | 2010-12-01 01:36:18 +0000 | [diff] [blame] | 1912 | return false; |
Douglas Gregor | 95d8283 | 2012-01-24 18:36:04 +0000 | [diff] [blame] | 1913 | } |
Douglas Gregor | e2e50d33 | 2010-12-01 01:36:18 +0000 | [diff] [blame] | 1914 | |
| 1915 | To->startDefinition(); |
| 1916 | |
| 1917 | // Add base classes. |
| 1918 | if (CXXRecordDecl *ToCXX = dyn_cast<CXXRecordDecl>(To)) { |
| 1919 | CXXRecordDecl *FromCXX = cast<CXXRecordDecl>(From); |
Douglas Gregor | 3c2404b | 2011-11-03 18:07:07 +0000 | [diff] [blame] | 1920 | |
| 1921 | struct CXXRecordDecl::DefinitionData &ToData = ToCXX->data(); |
| 1922 | struct CXXRecordDecl::DefinitionData &FromData = FromCXX->data(); |
| 1923 | ToData.UserDeclaredConstructor = FromData.UserDeclaredConstructor; |
Richard Smith | 328aae5 | 2012-11-30 05:11:39 +0000 | [diff] [blame] | 1924 | ToData.UserDeclaredSpecialMembers = FromData.UserDeclaredSpecialMembers; |
Douglas Gregor | 3c2404b | 2011-11-03 18:07:07 +0000 | [diff] [blame] | 1925 | ToData.Aggregate = FromData.Aggregate; |
| 1926 | ToData.PlainOldData = FromData.PlainOldData; |
| 1927 | ToData.Empty = FromData.Empty; |
| 1928 | ToData.Polymorphic = FromData.Polymorphic; |
| 1929 | ToData.Abstract = FromData.Abstract; |
| 1930 | ToData.IsStandardLayout = FromData.IsStandardLayout; |
| 1931 | ToData.HasNoNonEmptyBases = FromData.HasNoNonEmptyBases; |
| 1932 | ToData.HasPrivateFields = FromData.HasPrivateFields; |
| 1933 | ToData.HasProtectedFields = FromData.HasProtectedFields; |
| 1934 | ToData.HasPublicFields = FromData.HasPublicFields; |
| 1935 | ToData.HasMutableFields = FromData.HasMutableFields; |
Richard Smith | ab44d5b | 2013-12-10 08:25:00 +0000 | [diff] [blame] | 1936 | ToData.HasVariantMembers = FromData.HasVariantMembers; |
Richard Smith | 561fb15 | 2012-02-25 07:33:38 +0000 | [diff] [blame] | 1937 | ToData.HasOnlyCMembers = FromData.HasOnlyCMembers; |
Richard Smith | e2648ba | 2012-05-07 01:07:30 +0000 | [diff] [blame] | 1938 | ToData.HasInClassInitializer = FromData.HasInClassInitializer; |
Richard Smith | 593f993 | 2012-12-08 02:01:17 +0000 | [diff] [blame] | 1939 | ToData.HasUninitializedReferenceMember |
| 1940 | = FromData.HasUninitializedReferenceMember; |
Richard Smith | 6b02d46 | 2012-12-08 08:32:28 +0000 | [diff] [blame] | 1941 | ToData.NeedOverloadResolutionForMoveConstructor |
| 1942 | = FromData.NeedOverloadResolutionForMoveConstructor; |
| 1943 | ToData.NeedOverloadResolutionForMoveAssignment |
| 1944 | = FromData.NeedOverloadResolutionForMoveAssignment; |
| 1945 | ToData.NeedOverloadResolutionForDestructor |
| 1946 | = FromData.NeedOverloadResolutionForDestructor; |
| 1947 | ToData.DefaultedMoveConstructorIsDeleted |
| 1948 | = FromData.DefaultedMoveConstructorIsDeleted; |
| 1949 | ToData.DefaultedMoveAssignmentIsDeleted |
| 1950 | = FromData.DefaultedMoveAssignmentIsDeleted; |
| 1951 | ToData.DefaultedDestructorIsDeleted = FromData.DefaultedDestructorIsDeleted; |
Richard Smith | 328aae5 | 2012-11-30 05:11:39 +0000 | [diff] [blame] | 1952 | ToData.HasTrivialSpecialMembers = FromData.HasTrivialSpecialMembers; |
| 1953 | ToData.HasIrrelevantDestructor = FromData.HasIrrelevantDestructor; |
Douglas Gregor | 3c2404b | 2011-11-03 18:07:07 +0000 | [diff] [blame] | 1954 | ToData.HasConstexprNonCopyMoveConstructor |
| 1955 | = FromData.HasConstexprNonCopyMoveConstructor; |
Richard Smith | 561fb15 | 2012-02-25 07:33:38 +0000 | [diff] [blame] | 1956 | ToData.DefaultedDefaultConstructorIsConstexpr |
| 1957 | = FromData.DefaultedDefaultConstructorIsConstexpr; |
Richard Smith | 561fb15 | 2012-02-25 07:33:38 +0000 | [diff] [blame] | 1958 | ToData.HasConstexprDefaultConstructor |
| 1959 | = FromData.HasConstexprDefaultConstructor; |
Douglas Gregor | 3c2404b | 2011-11-03 18:07:07 +0000 | [diff] [blame] | 1960 | ToData.HasNonLiteralTypeFieldsOrBases |
| 1961 | = FromData.HasNonLiteralTypeFieldsOrBases; |
Richard Smith | 561fb15 | 2012-02-25 07:33:38 +0000 | [diff] [blame] | 1962 | // ComputedVisibleConversions not imported. |
Douglas Gregor | 3c2404b | 2011-11-03 18:07:07 +0000 | [diff] [blame] | 1963 | ToData.UserProvidedDefaultConstructor |
| 1964 | = FromData.UserProvidedDefaultConstructor; |
Richard Smith | 328aae5 | 2012-11-30 05:11:39 +0000 | [diff] [blame] | 1965 | ToData.DeclaredSpecialMembers = FromData.DeclaredSpecialMembers; |
Richard Smith | 1c33fe8 | 2012-11-28 06:23:12 +0000 | [diff] [blame] | 1966 | ToData.ImplicitCopyConstructorHasConstParam |
| 1967 | = FromData.ImplicitCopyConstructorHasConstParam; |
| 1968 | ToData.ImplicitCopyAssignmentHasConstParam |
| 1969 | = FromData.ImplicitCopyAssignmentHasConstParam; |
| 1970 | ToData.HasDeclaredCopyConstructorWithConstParam |
| 1971 | = FromData.HasDeclaredCopyConstructorWithConstParam; |
| 1972 | ToData.HasDeclaredCopyAssignmentWithConstParam |
| 1973 | = FromData.HasDeclaredCopyAssignmentWithConstParam; |
Richard Smith | 561fb15 | 2012-02-25 07:33:38 +0000 | [diff] [blame] | 1974 | ToData.IsLambda = FromData.IsLambda; |
| 1975 | |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 1976 | SmallVector<CXXBaseSpecifier *, 4> Bases; |
Aaron Ballman | 574705e | 2014-03-13 15:41:46 +0000 | [diff] [blame] | 1977 | for (const auto &Base1 : FromCXX->bases()) { |
| 1978 | QualType T = Importer.Import(Base1.getType()); |
Douglas Gregor | e2e50d33 | 2010-12-01 01:36:18 +0000 | [diff] [blame] | 1979 | if (T.isNull()) |
Douglas Gregor | 96303ea | 2010-12-02 19:33:37 +0000 | [diff] [blame] | 1980 | return true; |
Douglas Gregor | 752a595 | 2011-01-03 22:36:02 +0000 | [diff] [blame] | 1981 | |
| 1982 | SourceLocation EllipsisLoc; |
Aaron Ballman | 574705e | 2014-03-13 15:41:46 +0000 | [diff] [blame] | 1983 | if (Base1.isPackExpansion()) |
| 1984 | EllipsisLoc = Importer.Import(Base1.getEllipsisLoc()); |
Douglas Gregor | d451ea9 | 2011-07-29 23:31:30 +0000 | [diff] [blame] | 1985 | |
| 1986 | // Ensure that we have a definition for the base. |
Aaron Ballman | 574705e | 2014-03-13 15:41:46 +0000 | [diff] [blame] | 1987 | ImportDefinitionIfNeeded(Base1.getType()->getAsCXXRecordDecl()); |
Douglas Gregor | d451ea9 | 2011-07-29 23:31:30 +0000 | [diff] [blame] | 1988 | |
Douglas Gregor | e2e50d33 | 2010-12-01 01:36:18 +0000 | [diff] [blame] | 1989 | Bases.push_back( |
| 1990 | new (Importer.getToContext()) |
Aaron Ballman | 574705e | 2014-03-13 15:41:46 +0000 | [diff] [blame] | 1991 | CXXBaseSpecifier(Importer.Import(Base1.getSourceRange()), |
| 1992 | Base1.isVirtual(), |
| 1993 | Base1.isBaseOfClass(), |
| 1994 | Base1.getAccessSpecifierAsWritten(), |
| 1995 | Importer.Import(Base1.getTypeSourceInfo()), |
Douglas Gregor | 752a595 | 2011-01-03 22:36:02 +0000 | [diff] [blame] | 1996 | EllipsisLoc)); |
Douglas Gregor | e2e50d33 | 2010-12-01 01:36:18 +0000 | [diff] [blame] | 1997 | } |
| 1998 | if (!Bases.empty()) |
| 1999 | ToCXX->setBases(Bases.data(), Bases.size()); |
| 2000 | } |
| 2001 | |
Douglas Gregor | 2e15c84 | 2012-02-01 21:00:38 +0000 | [diff] [blame] | 2002 | if (shouldForceImportDeclContext(Kind)) |
Douglas Gregor | 95d8283 | 2012-01-24 18:36:04 +0000 | [diff] [blame] | 2003 | ImportDeclContext(From, /*ForceImport=*/true); |
| 2004 | |
Douglas Gregor | e2e50d33 | 2010-12-01 01:36:18 +0000 | [diff] [blame] | 2005 | To->completeDefinition(); |
Douglas Gregor | 96303ea | 2010-12-02 19:33:37 +0000 | [diff] [blame] | 2006 | return false; |
Douglas Gregor | e2e50d33 | 2010-12-01 01:36:18 +0000 | [diff] [blame] | 2007 | } |
| 2008 | |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 2009 | bool ASTNodeImporter::ImportDefinition(VarDecl *From, VarDecl *To, |
| 2010 | ImportDefinitionKind Kind) { |
| 2011 | if (To->getDefinition()) |
| 2012 | return false; |
| 2013 | |
| 2014 | // FIXME: Can we really import any initializer? Alternatively, we could force |
| 2015 | // ourselves to import every declaration of a variable and then only use |
| 2016 | // getInit() here. |
| 2017 | To->setInit(Importer.Import(const_cast<Expr *>(From->getAnyInitializer()))); |
| 2018 | |
| 2019 | // FIXME: Other bits to merge? |
| 2020 | |
| 2021 | return false; |
| 2022 | } |
| 2023 | |
Douglas Gregor | d451ea9 | 2011-07-29 23:31:30 +0000 | [diff] [blame] | 2024 | bool ASTNodeImporter::ImportDefinition(EnumDecl *From, EnumDecl *To, |
Douglas Gregor | 2e15c84 | 2012-02-01 21:00:38 +0000 | [diff] [blame] | 2025 | ImportDefinitionKind Kind) { |
| 2026 | if (To->getDefinition() || To->isBeingDefined()) { |
| 2027 | if (Kind == IDK_Everything) |
| 2028 | ImportDeclContext(From, /*ForceImport=*/true); |
Douglas Gregor | d451ea9 | 2011-07-29 23:31:30 +0000 | [diff] [blame] | 2029 | return false; |
Douglas Gregor | 2e15c84 | 2012-02-01 21:00:38 +0000 | [diff] [blame] | 2030 | } |
Douglas Gregor | d451ea9 | 2011-07-29 23:31:30 +0000 | [diff] [blame] | 2031 | |
| 2032 | To->startDefinition(); |
| 2033 | |
| 2034 | QualType T = Importer.Import(Importer.getFromContext().getTypeDeclType(From)); |
| 2035 | if (T.isNull()) |
| 2036 | return true; |
| 2037 | |
| 2038 | QualType ToPromotionType = Importer.Import(From->getPromotionType()); |
| 2039 | if (ToPromotionType.isNull()) |
| 2040 | return true; |
Douglas Gregor | 2e15c84 | 2012-02-01 21:00:38 +0000 | [diff] [blame] | 2041 | |
| 2042 | if (shouldForceImportDeclContext(Kind)) |
| 2043 | ImportDeclContext(From, /*ForceImport=*/true); |
Douglas Gregor | d451ea9 | 2011-07-29 23:31:30 +0000 | [diff] [blame] | 2044 | |
| 2045 | // FIXME: we might need to merge the number of positive or negative bits |
| 2046 | // if the enumerator lists don't match. |
| 2047 | To->completeDefinition(T, ToPromotionType, |
| 2048 | From->getNumPositiveBits(), |
| 2049 | From->getNumNegativeBits()); |
| 2050 | return false; |
| 2051 | } |
| 2052 | |
Douglas Gregor | a082a49 | 2010-11-30 19:14:50 +0000 | [diff] [blame] | 2053 | TemplateParameterList *ASTNodeImporter::ImportTemplateParameterList( |
| 2054 | TemplateParameterList *Params) { |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2055 | SmallVector<NamedDecl *, 4> ToParams; |
Douglas Gregor | a082a49 | 2010-11-30 19:14:50 +0000 | [diff] [blame] | 2056 | ToParams.reserve(Params->size()); |
| 2057 | for (TemplateParameterList::iterator P = Params->begin(), |
| 2058 | PEnd = Params->end(); |
| 2059 | P != PEnd; ++P) { |
| 2060 | Decl *To = Importer.Import(*P); |
| 2061 | if (!To) |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 2062 | return nullptr; |
| 2063 | |
Douglas Gregor | a082a49 | 2010-11-30 19:14:50 +0000 | [diff] [blame] | 2064 | ToParams.push_back(cast<NamedDecl>(To)); |
| 2065 | } |
| 2066 | |
| 2067 | return TemplateParameterList::Create(Importer.getToContext(), |
| 2068 | Importer.Import(Params->getTemplateLoc()), |
| 2069 | Importer.Import(Params->getLAngleLoc()), |
| 2070 | ToParams.data(), ToParams.size(), |
| 2071 | Importer.Import(Params->getRAngleLoc())); |
| 2072 | } |
| 2073 | |
Douglas Gregor | e2e50d33 | 2010-12-01 01:36:18 +0000 | [diff] [blame] | 2074 | TemplateArgument |
| 2075 | ASTNodeImporter::ImportTemplateArgument(const TemplateArgument &From) { |
| 2076 | switch (From.getKind()) { |
| 2077 | case TemplateArgument::Null: |
| 2078 | return TemplateArgument(); |
| 2079 | |
| 2080 | case TemplateArgument::Type: { |
| 2081 | QualType ToType = Importer.Import(From.getAsType()); |
| 2082 | if (ToType.isNull()) |
| 2083 | return TemplateArgument(); |
| 2084 | return TemplateArgument(ToType); |
| 2085 | } |
| 2086 | |
| 2087 | case TemplateArgument::Integral: { |
| 2088 | QualType ToType = Importer.Import(From.getIntegralType()); |
| 2089 | if (ToType.isNull()) |
| 2090 | return TemplateArgument(); |
Benjamin Kramer | 6003ad5 | 2012-06-07 15:09:51 +0000 | [diff] [blame] | 2091 | return TemplateArgument(From, ToType); |
Douglas Gregor | e2e50d33 | 2010-12-01 01:36:18 +0000 | [diff] [blame] | 2092 | } |
| 2093 | |
Eli Friedman | b826a00 | 2012-09-26 02:36:12 +0000 | [diff] [blame] | 2094 | case TemplateArgument::Declaration: { |
David Blaikie | 3c7dd6b | 2014-10-22 19:54:16 +0000 | [diff] [blame^] | 2095 | ValueDecl *To = cast_or_null<ValueDecl>(Importer.Import(From.getAsDecl())); |
| 2096 | QualType ToType = Importer.Import(From.getParamTypeForDecl()); |
| 2097 | if (!To || ToType.isNull()) |
| 2098 | return TemplateArgument(); |
| 2099 | return TemplateArgument(To, ToType); |
Eli Friedman | b826a00 | 2012-09-26 02:36:12 +0000 | [diff] [blame] | 2100 | } |
| 2101 | |
| 2102 | case TemplateArgument::NullPtr: { |
| 2103 | QualType ToType = Importer.Import(From.getNullPtrType()); |
| 2104 | if (ToType.isNull()) |
| 2105 | return TemplateArgument(); |
| 2106 | return TemplateArgument(ToType, /*isNullPtr*/true); |
| 2107 | } |
| 2108 | |
Douglas Gregor | e2e50d33 | 2010-12-01 01:36:18 +0000 | [diff] [blame] | 2109 | case TemplateArgument::Template: { |
| 2110 | TemplateName ToTemplate = Importer.Import(From.getAsTemplate()); |
| 2111 | if (ToTemplate.isNull()) |
| 2112 | return TemplateArgument(); |
| 2113 | |
| 2114 | return TemplateArgument(ToTemplate); |
| 2115 | } |
Douglas Gregor | e4ff4b5 | 2011-01-05 18:58:31 +0000 | [diff] [blame] | 2116 | |
| 2117 | case TemplateArgument::TemplateExpansion: { |
| 2118 | TemplateName ToTemplate |
| 2119 | = Importer.Import(From.getAsTemplateOrTemplatePattern()); |
| 2120 | if (ToTemplate.isNull()) |
| 2121 | return TemplateArgument(); |
| 2122 | |
Douglas Gregor | e1d60df | 2011-01-14 23:41:42 +0000 | [diff] [blame] | 2123 | return TemplateArgument(ToTemplate, From.getNumTemplateExpansions()); |
Douglas Gregor | e4ff4b5 | 2011-01-05 18:58:31 +0000 | [diff] [blame] | 2124 | } |
| 2125 | |
Douglas Gregor | e2e50d33 | 2010-12-01 01:36:18 +0000 | [diff] [blame] | 2126 | case TemplateArgument::Expression: |
| 2127 | if (Expr *ToExpr = Importer.Import(From.getAsExpr())) |
| 2128 | return TemplateArgument(ToExpr); |
| 2129 | return TemplateArgument(); |
| 2130 | |
| 2131 | case TemplateArgument::Pack: { |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2132 | SmallVector<TemplateArgument, 2> ToPack; |
Douglas Gregor | e2e50d33 | 2010-12-01 01:36:18 +0000 | [diff] [blame] | 2133 | ToPack.reserve(From.pack_size()); |
| 2134 | if (ImportTemplateArguments(From.pack_begin(), From.pack_size(), ToPack)) |
| 2135 | return TemplateArgument(); |
| 2136 | |
| 2137 | TemplateArgument *ToArgs |
| 2138 | = new (Importer.getToContext()) TemplateArgument[ToPack.size()]; |
| 2139 | std::copy(ToPack.begin(), ToPack.end(), ToArgs); |
| 2140 | return TemplateArgument(ToArgs, ToPack.size()); |
| 2141 | } |
| 2142 | } |
| 2143 | |
| 2144 | llvm_unreachable("Invalid template argument kind"); |
Douglas Gregor | e2e50d33 | 2010-12-01 01:36:18 +0000 | [diff] [blame] | 2145 | } |
| 2146 | |
| 2147 | bool ASTNodeImporter::ImportTemplateArguments(const TemplateArgument *FromArgs, |
| 2148 | unsigned NumFromArgs, |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2149 | SmallVectorImpl<TemplateArgument> &ToArgs) { |
Douglas Gregor | e2e50d33 | 2010-12-01 01:36:18 +0000 | [diff] [blame] | 2150 | for (unsigned I = 0; I != NumFromArgs; ++I) { |
| 2151 | TemplateArgument To = ImportTemplateArgument(FromArgs[I]); |
| 2152 | if (To.isNull() && !FromArgs[I].isNull()) |
| 2153 | return true; |
| 2154 | |
| 2155 | ToArgs.push_back(To); |
| 2156 | } |
| 2157 | |
| 2158 | return false; |
| 2159 | } |
| 2160 | |
Douglas Gregor | 5c73e91 | 2010-02-11 00:48:18 +0000 | [diff] [blame] | 2161 | bool ASTNodeImporter::IsStructuralMatch(RecordDecl *FromRecord, |
Douglas Gregor | dd6006f | 2012-07-17 21:16:27 +0000 | [diff] [blame] | 2162 | RecordDecl *ToRecord, bool Complain) { |
Sean Callanan | c665c9e | 2013-10-09 21:45:11 +0000 | [diff] [blame] | 2163 | // Eliminate a potential failure point where we attempt to re-import |
| 2164 | // something we're trying to import while completing ToRecord. |
| 2165 | Decl *ToOrigin = Importer.GetOriginalDecl(ToRecord); |
| 2166 | if (ToOrigin) { |
| 2167 | RecordDecl *ToOriginRecord = dyn_cast<RecordDecl>(ToOrigin); |
| 2168 | if (ToOriginRecord) |
| 2169 | ToRecord = ToOriginRecord; |
| 2170 | } |
| 2171 | |
Benjamin Kramer | 26d19c5 | 2010-02-18 13:02:13 +0000 | [diff] [blame] | 2172 | StructuralEquivalenceContext Ctx(Importer.getFromContext(), |
Sean Callanan | c665c9e | 2013-10-09 21:45:11 +0000 | [diff] [blame] | 2173 | ToRecord->getASTContext(), |
Douglas Gregor | dd6006f | 2012-07-17 21:16:27 +0000 | [diff] [blame] | 2174 | Importer.getNonEquivalentDecls(), |
| 2175 | false, Complain); |
Benjamin Kramer | 26d19c5 | 2010-02-18 13:02:13 +0000 | [diff] [blame] | 2176 | return Ctx.IsStructurallyEquivalent(FromRecord, ToRecord); |
Douglas Gregor | 5c73e91 | 2010-02-11 00:48:18 +0000 | [diff] [blame] | 2177 | } |
| 2178 | |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 2179 | bool ASTNodeImporter::IsStructuralMatch(VarDecl *FromVar, VarDecl *ToVar, |
| 2180 | bool Complain) { |
| 2181 | StructuralEquivalenceContext Ctx( |
| 2182 | Importer.getFromContext(), Importer.getToContext(), |
| 2183 | Importer.getNonEquivalentDecls(), false, Complain); |
| 2184 | return Ctx.IsStructurallyEquivalent(FromVar, ToVar); |
| 2185 | } |
| 2186 | |
Douglas Gregor | 98c1018 | 2010-02-12 22:17:39 +0000 | [diff] [blame] | 2187 | bool ASTNodeImporter::IsStructuralMatch(EnumDecl *FromEnum, EnumDecl *ToEnum) { |
Benjamin Kramer | 26d19c5 | 2010-02-18 13:02:13 +0000 | [diff] [blame] | 2188 | StructuralEquivalenceContext Ctx(Importer.getFromContext(), |
Douglas Gregor | 3996e24 | 2010-02-15 22:01:00 +0000 | [diff] [blame] | 2189 | Importer.getToContext(), |
Douglas Gregor | b4964f7 | 2010-02-15 23:54:17 +0000 | [diff] [blame] | 2190 | Importer.getNonEquivalentDecls()); |
Benjamin Kramer | 26d19c5 | 2010-02-18 13:02:13 +0000 | [diff] [blame] | 2191 | return Ctx.IsStructurallyEquivalent(FromEnum, ToEnum); |
Douglas Gregor | 98c1018 | 2010-02-12 22:17:39 +0000 | [diff] [blame] | 2192 | } |
| 2193 | |
Douglas Gregor | 9115508 | 2012-11-14 22:29:20 +0000 | [diff] [blame] | 2194 | bool ASTNodeImporter::IsStructuralMatch(EnumConstantDecl *FromEC, |
| 2195 | EnumConstantDecl *ToEC) |
| 2196 | { |
| 2197 | const llvm::APSInt &FromVal = FromEC->getInitVal(); |
| 2198 | const llvm::APSInt &ToVal = ToEC->getInitVal(); |
| 2199 | |
| 2200 | return FromVal.isSigned() == ToVal.isSigned() && |
| 2201 | FromVal.getBitWidth() == ToVal.getBitWidth() && |
| 2202 | FromVal == ToVal; |
| 2203 | } |
| 2204 | |
| 2205 | bool ASTNodeImporter::IsStructuralMatch(ClassTemplateDecl *From, |
Douglas Gregor | a082a49 | 2010-11-30 19:14:50 +0000 | [diff] [blame] | 2206 | ClassTemplateDecl *To) { |
| 2207 | StructuralEquivalenceContext Ctx(Importer.getFromContext(), |
| 2208 | Importer.getToContext(), |
| 2209 | Importer.getNonEquivalentDecls()); |
| 2210 | return Ctx.IsStructurallyEquivalent(From, To); |
| 2211 | } |
| 2212 | |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 2213 | bool ASTNodeImporter::IsStructuralMatch(VarTemplateDecl *From, |
| 2214 | VarTemplateDecl *To) { |
| 2215 | StructuralEquivalenceContext Ctx(Importer.getFromContext(), |
| 2216 | Importer.getToContext(), |
| 2217 | Importer.getNonEquivalentDecls()); |
| 2218 | return Ctx.IsStructurallyEquivalent(From, To); |
| 2219 | } |
| 2220 | |
Douglas Gregor | e4c83e4 | 2010-02-09 22:48:33 +0000 | [diff] [blame] | 2221 | Decl *ASTNodeImporter::VisitDecl(Decl *D) { |
Douglas Gregor | 811663e | 2010-02-10 00:15:17 +0000 | [diff] [blame] | 2222 | Importer.FromDiag(D->getLocation(), diag::err_unsupported_ast_node) |
Douglas Gregor | e4c83e4 | 2010-02-09 22:48:33 +0000 | [diff] [blame] | 2223 | << D->getDeclKindName(); |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 2224 | return nullptr; |
Douglas Gregor | e4c83e4 | 2010-02-09 22:48:33 +0000 | [diff] [blame] | 2225 | } |
| 2226 | |
Sean Callanan | 6519827 | 2011-11-17 23:20:56 +0000 | [diff] [blame] | 2227 | Decl *ASTNodeImporter::VisitTranslationUnitDecl(TranslationUnitDecl *D) { |
| 2228 | TranslationUnitDecl *ToD = |
| 2229 | Importer.getToContext().getTranslationUnitDecl(); |
| 2230 | |
| 2231 | Importer.Imported(D, ToD); |
| 2232 | |
| 2233 | return ToD; |
| 2234 | } |
| 2235 | |
Douglas Gregor | f18a2c7 | 2010-02-21 18:26:36 +0000 | [diff] [blame] | 2236 | Decl *ASTNodeImporter::VisitNamespaceDecl(NamespaceDecl *D) { |
| 2237 | // Import the major distinguishing characteristics of this namespace. |
| 2238 | DeclContext *DC, *LexicalDC; |
| 2239 | DeclarationName Name; |
| 2240 | SourceLocation Loc; |
| 2241 | if (ImportDeclParts(D, DC, LexicalDC, Name, Loc)) |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 2242 | return nullptr; |
| 2243 | |
| 2244 | NamespaceDecl *MergeWithNamespace = nullptr; |
Douglas Gregor | f18a2c7 | 2010-02-21 18:26:36 +0000 | [diff] [blame] | 2245 | if (!Name) { |
| 2246 | // This is an anonymous namespace. Adopt an existing anonymous |
| 2247 | // namespace if we can. |
| 2248 | // FIXME: Not testable. |
| 2249 | if (TranslationUnitDecl *TU = dyn_cast<TranslationUnitDecl>(DC)) |
| 2250 | MergeWithNamespace = TU->getAnonymousNamespace(); |
| 2251 | else |
| 2252 | MergeWithNamespace = cast<NamespaceDecl>(DC)->getAnonymousNamespace(); |
| 2253 | } else { |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2254 | SmallVector<NamedDecl *, 4> ConflictingDecls; |
Dmitri Gribenko | f857950 | 2013-01-12 19:30:44 +0000 | [diff] [blame] | 2255 | SmallVector<NamedDecl *, 2> FoundDecls; |
Douglas Gregor | 9e0a5b3 | 2011-10-15 00:10:27 +0000 | [diff] [blame] | 2256 | DC->localUncachedLookup(Name, FoundDecls); |
| 2257 | for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) { |
| 2258 | if (!FoundDecls[I]->isInIdentifierNamespace(Decl::IDNS_Namespace)) |
Douglas Gregor | f18a2c7 | 2010-02-21 18:26:36 +0000 | [diff] [blame] | 2259 | continue; |
| 2260 | |
Douglas Gregor | 9e0a5b3 | 2011-10-15 00:10:27 +0000 | [diff] [blame] | 2261 | if (NamespaceDecl *FoundNS = dyn_cast<NamespaceDecl>(FoundDecls[I])) { |
Douglas Gregor | f18a2c7 | 2010-02-21 18:26:36 +0000 | [diff] [blame] | 2262 | MergeWithNamespace = FoundNS; |
| 2263 | ConflictingDecls.clear(); |
| 2264 | break; |
| 2265 | } |
| 2266 | |
Douglas Gregor | 9e0a5b3 | 2011-10-15 00:10:27 +0000 | [diff] [blame] | 2267 | ConflictingDecls.push_back(FoundDecls[I]); |
Douglas Gregor | f18a2c7 | 2010-02-21 18:26:36 +0000 | [diff] [blame] | 2268 | } |
| 2269 | |
| 2270 | if (!ConflictingDecls.empty()) { |
John McCall | e87beb2 | 2010-04-23 18:46:30 +0000 | [diff] [blame] | 2271 | Name = Importer.HandleNameConflict(Name, DC, Decl::IDNS_Namespace, |
Douglas Gregor | f18a2c7 | 2010-02-21 18:26:36 +0000 | [diff] [blame] | 2272 | ConflictingDecls.data(), |
| 2273 | ConflictingDecls.size()); |
| 2274 | } |
| 2275 | } |
| 2276 | |
| 2277 | // Create the "to" namespace, if needed. |
| 2278 | NamespaceDecl *ToNamespace = MergeWithNamespace; |
| 2279 | if (!ToNamespace) { |
Abramo Bagnara | b5545be | 2011-03-08 12:38:20 +0000 | [diff] [blame] | 2280 | ToNamespace = NamespaceDecl::Create(Importer.getToContext(), DC, |
Douglas Gregor | e57e752 | 2012-01-07 09:11:48 +0000 | [diff] [blame] | 2281 | D->isInline(), |
Abramo Bagnara | b5545be | 2011-03-08 12:38:20 +0000 | [diff] [blame] | 2282 | Importer.Import(D->getLocStart()), |
Douglas Gregor | e57e752 | 2012-01-07 09:11:48 +0000 | [diff] [blame] | 2283 | Loc, Name.getAsIdentifierInfo(), |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 2284 | /*PrevDecl=*/nullptr); |
Douglas Gregor | f18a2c7 | 2010-02-21 18:26:36 +0000 | [diff] [blame] | 2285 | ToNamespace->setLexicalDeclContext(LexicalDC); |
Sean Callanan | 95e74be | 2011-10-21 02:57:43 +0000 | [diff] [blame] | 2286 | LexicalDC->addDeclInternal(ToNamespace); |
Douglas Gregor | f18a2c7 | 2010-02-21 18:26:36 +0000 | [diff] [blame] | 2287 | |
| 2288 | // If this is an anonymous namespace, register it as the anonymous |
| 2289 | // namespace within its context. |
| 2290 | if (!Name) { |
| 2291 | if (TranslationUnitDecl *TU = dyn_cast<TranslationUnitDecl>(DC)) |
| 2292 | TU->setAnonymousNamespace(ToNamespace); |
| 2293 | else |
| 2294 | cast<NamespaceDecl>(DC)->setAnonymousNamespace(ToNamespace); |
| 2295 | } |
| 2296 | } |
| 2297 | Importer.Imported(D, ToNamespace); |
| 2298 | |
| 2299 | ImportDeclContext(D); |
| 2300 | |
| 2301 | return ToNamespace; |
| 2302 | } |
| 2303 | |
Richard Smith | dda56e4 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 2304 | Decl *ASTNodeImporter::VisitTypedefNameDecl(TypedefNameDecl *D, bool IsAlias) { |
Douglas Gregor | 5fa74c3 | 2010-02-10 21:10:29 +0000 | [diff] [blame] | 2305 | // Import the major distinguishing characteristics of this typedef. |
| 2306 | DeclContext *DC, *LexicalDC; |
| 2307 | DeclarationName Name; |
| 2308 | SourceLocation Loc; |
| 2309 | if (ImportDeclParts(D, DC, LexicalDC, Name, Loc)) |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 2310 | return nullptr; |
| 2311 | |
Douglas Gregor | 5fa74c3 | 2010-02-10 21:10:29 +0000 | [diff] [blame] | 2312 | // If this typedef is not in block scope, determine whether we've |
| 2313 | // seen a typedef with the same name (that we can merge with) or any |
| 2314 | // other entity by that name (which name lookup could conflict with). |
| 2315 | if (!DC->isFunctionOrMethod()) { |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2316 | SmallVector<NamedDecl *, 4> ConflictingDecls; |
Douglas Gregor | 5fa74c3 | 2010-02-10 21:10:29 +0000 | [diff] [blame] | 2317 | unsigned IDNS = Decl::IDNS_Ordinary; |
Dmitri Gribenko | f857950 | 2013-01-12 19:30:44 +0000 | [diff] [blame] | 2318 | SmallVector<NamedDecl *, 2> FoundDecls; |
Douglas Gregor | 9e0a5b3 | 2011-10-15 00:10:27 +0000 | [diff] [blame] | 2319 | DC->localUncachedLookup(Name, FoundDecls); |
| 2320 | for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) { |
| 2321 | if (!FoundDecls[I]->isInIdentifierNamespace(IDNS)) |
Douglas Gregor | 5fa74c3 | 2010-02-10 21:10:29 +0000 | [diff] [blame] | 2322 | continue; |
Richard Smith | dda56e4 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 2323 | if (TypedefNameDecl *FoundTypedef = |
Douglas Gregor | 9e0a5b3 | 2011-10-15 00:10:27 +0000 | [diff] [blame] | 2324 | dyn_cast<TypedefNameDecl>(FoundDecls[I])) { |
Douglas Gregor | b4964f7 | 2010-02-15 23:54:17 +0000 | [diff] [blame] | 2325 | if (Importer.IsStructurallyEquivalent(D->getUnderlyingType(), |
| 2326 | FoundTypedef->getUnderlyingType())) |
Douglas Gregor | 8cdbe64 | 2010-02-12 23:44:20 +0000 | [diff] [blame] | 2327 | return Importer.Imported(D, FoundTypedef); |
Douglas Gregor | 5fa74c3 | 2010-02-10 21:10:29 +0000 | [diff] [blame] | 2328 | } |
| 2329 | |
Douglas Gregor | 9e0a5b3 | 2011-10-15 00:10:27 +0000 | [diff] [blame] | 2330 | ConflictingDecls.push_back(FoundDecls[I]); |
Douglas Gregor | 5fa74c3 | 2010-02-10 21:10:29 +0000 | [diff] [blame] | 2331 | } |
| 2332 | |
| 2333 | if (!ConflictingDecls.empty()) { |
| 2334 | Name = Importer.HandleNameConflict(Name, DC, IDNS, |
| 2335 | ConflictingDecls.data(), |
| 2336 | ConflictingDecls.size()); |
| 2337 | if (!Name) |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 2338 | return nullptr; |
Douglas Gregor | 5fa74c3 | 2010-02-10 21:10:29 +0000 | [diff] [blame] | 2339 | } |
| 2340 | } |
| 2341 | |
Douglas Gregor | b4964f7 | 2010-02-15 23:54:17 +0000 | [diff] [blame] | 2342 | // Import the underlying type of this typedef; |
| 2343 | QualType T = Importer.Import(D->getUnderlyingType()); |
| 2344 | if (T.isNull()) |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 2345 | return nullptr; |
| 2346 | |
Douglas Gregor | 5fa74c3 | 2010-02-10 21:10:29 +0000 | [diff] [blame] | 2347 | // Create the new typedef node. |
| 2348 | TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo()); |
Abramo Bagnara | b3185b0 | 2011-03-06 15:48:19 +0000 | [diff] [blame] | 2349 | SourceLocation StartL = Importer.Import(D->getLocStart()); |
Richard Smith | dda56e4 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 2350 | TypedefNameDecl *ToTypedef; |
| 2351 | if (IsAlias) |
Douglas Gregor | 03d1ed3 | 2011-10-14 21:54:42 +0000 | [diff] [blame] | 2352 | ToTypedef = TypeAliasDecl::Create(Importer.getToContext(), DC, |
| 2353 | StartL, Loc, |
| 2354 | Name.getAsIdentifierInfo(), |
| 2355 | TInfo); |
| 2356 | else |
Richard Smith | dda56e4 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 2357 | ToTypedef = TypedefDecl::Create(Importer.getToContext(), DC, |
| 2358 | StartL, Loc, |
| 2359 | Name.getAsIdentifierInfo(), |
| 2360 | TInfo); |
Douglas Gregor | 03d1ed3 | 2011-10-14 21:54:42 +0000 | [diff] [blame] | 2361 | |
Douglas Gregor | dd48317 | 2010-02-22 17:42:47 +0000 | [diff] [blame] | 2362 | ToTypedef->setAccess(D->getAccess()); |
Douglas Gregor | 5fa74c3 | 2010-02-10 21:10:29 +0000 | [diff] [blame] | 2363 | ToTypedef->setLexicalDeclContext(LexicalDC); |
Douglas Gregor | 8cdbe64 | 2010-02-12 23:44:20 +0000 | [diff] [blame] | 2364 | Importer.Imported(D, ToTypedef); |
Sean Callanan | 95e74be | 2011-10-21 02:57:43 +0000 | [diff] [blame] | 2365 | LexicalDC->addDeclInternal(ToTypedef); |
Douglas Gregor | b4964f7 | 2010-02-15 23:54:17 +0000 | [diff] [blame] | 2366 | |
Douglas Gregor | 5fa74c3 | 2010-02-10 21:10:29 +0000 | [diff] [blame] | 2367 | return ToTypedef; |
| 2368 | } |
| 2369 | |
Richard Smith | dda56e4 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 2370 | Decl *ASTNodeImporter::VisitTypedefDecl(TypedefDecl *D) { |
| 2371 | return VisitTypedefNameDecl(D, /*IsAlias=*/false); |
| 2372 | } |
| 2373 | |
| 2374 | Decl *ASTNodeImporter::VisitTypeAliasDecl(TypeAliasDecl *D) { |
| 2375 | return VisitTypedefNameDecl(D, /*IsAlias=*/true); |
| 2376 | } |
| 2377 | |
Douglas Gregor | 98c1018 | 2010-02-12 22:17:39 +0000 | [diff] [blame] | 2378 | Decl *ASTNodeImporter::VisitEnumDecl(EnumDecl *D) { |
| 2379 | // Import the major distinguishing characteristics of this enum. |
| 2380 | DeclContext *DC, *LexicalDC; |
| 2381 | DeclarationName Name; |
| 2382 | SourceLocation Loc; |
| 2383 | if (ImportDeclParts(D, DC, LexicalDC, Name, Loc)) |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 2384 | return nullptr; |
| 2385 | |
Douglas Gregor | 98c1018 | 2010-02-12 22:17:39 +0000 | [diff] [blame] | 2386 | // Figure out what enum name we're looking for. |
| 2387 | unsigned IDNS = Decl::IDNS_Tag; |
| 2388 | DeclarationName SearchName = Name; |
Richard Smith | dda56e4 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 2389 | if (!SearchName && D->getTypedefNameForAnonDecl()) { |
| 2390 | SearchName = Importer.Import(D->getTypedefNameForAnonDecl()->getDeclName()); |
Douglas Gregor | 98c1018 | 2010-02-12 22:17:39 +0000 | [diff] [blame] | 2391 | IDNS = Decl::IDNS_Ordinary; |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 2392 | } else if (Importer.getToContext().getLangOpts().CPlusPlus) |
Douglas Gregor | 98c1018 | 2010-02-12 22:17:39 +0000 | [diff] [blame] | 2393 | IDNS |= Decl::IDNS_Ordinary; |
| 2394 | |
| 2395 | // We may already have an enum of the same name; try to find and match it. |
| 2396 | if (!DC->isFunctionOrMethod() && SearchName) { |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2397 | SmallVector<NamedDecl *, 4> ConflictingDecls; |
Dmitri Gribenko | f857950 | 2013-01-12 19:30:44 +0000 | [diff] [blame] | 2398 | SmallVector<NamedDecl *, 2> FoundDecls; |
Douglas Gregor | 9e0a5b3 | 2011-10-15 00:10:27 +0000 | [diff] [blame] | 2399 | DC->localUncachedLookup(SearchName, FoundDecls); |
| 2400 | for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) { |
| 2401 | if (!FoundDecls[I]->isInIdentifierNamespace(IDNS)) |
Douglas Gregor | 98c1018 | 2010-02-12 22:17:39 +0000 | [diff] [blame] | 2402 | continue; |
| 2403 | |
Douglas Gregor | 9e0a5b3 | 2011-10-15 00:10:27 +0000 | [diff] [blame] | 2404 | Decl *Found = FoundDecls[I]; |
Richard Smith | dda56e4 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 2405 | if (TypedefNameDecl *Typedef = dyn_cast<TypedefNameDecl>(Found)) { |
Douglas Gregor | 98c1018 | 2010-02-12 22:17:39 +0000 | [diff] [blame] | 2406 | if (const TagType *Tag = Typedef->getUnderlyingType()->getAs<TagType>()) |
| 2407 | Found = Tag->getDecl(); |
| 2408 | } |
| 2409 | |
| 2410 | if (EnumDecl *FoundEnum = dyn_cast<EnumDecl>(Found)) { |
Douglas Gregor | 8cdbe64 | 2010-02-12 23:44:20 +0000 | [diff] [blame] | 2411 | if (IsStructuralMatch(D, FoundEnum)) |
| 2412 | return Importer.Imported(D, FoundEnum); |
Douglas Gregor | 98c1018 | 2010-02-12 22:17:39 +0000 | [diff] [blame] | 2413 | } |
| 2414 | |
Douglas Gregor | 9e0a5b3 | 2011-10-15 00:10:27 +0000 | [diff] [blame] | 2415 | ConflictingDecls.push_back(FoundDecls[I]); |
Douglas Gregor | 98c1018 | 2010-02-12 22:17:39 +0000 | [diff] [blame] | 2416 | } |
| 2417 | |
| 2418 | if (!ConflictingDecls.empty()) { |
| 2419 | Name = Importer.HandleNameConflict(Name, DC, IDNS, |
| 2420 | ConflictingDecls.data(), |
| 2421 | ConflictingDecls.size()); |
| 2422 | } |
| 2423 | } |
| 2424 | |
| 2425 | // Create the enum declaration. |
Abramo Bagnara | 29c2d46 | 2011-03-09 14:09:51 +0000 | [diff] [blame] | 2426 | EnumDecl *D2 = EnumDecl::Create(Importer.getToContext(), DC, |
| 2427 | Importer.Import(D->getLocStart()), |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 2428 | Loc, Name.getAsIdentifierInfo(), nullptr, |
Abramo Bagnara | 0e05e24 | 2010-12-03 18:54:17 +0000 | [diff] [blame] | 2429 | D->isScoped(), D->isScopedUsingClassTag(), |
| 2430 | D->isFixed()); |
John McCall | 3e11ebe | 2010-03-15 10:12:16 +0000 | [diff] [blame] | 2431 | // Import the qualifier, if any. |
Douglas Gregor | 1445480 | 2011-02-25 02:25:35 +0000 | [diff] [blame] | 2432 | D2->setQualifierInfo(Importer.Import(D->getQualifierLoc())); |
Douglas Gregor | dd48317 | 2010-02-22 17:42:47 +0000 | [diff] [blame] | 2433 | D2->setAccess(D->getAccess()); |
Douglas Gregor | 3996e24 | 2010-02-15 22:01:00 +0000 | [diff] [blame] | 2434 | D2->setLexicalDeclContext(LexicalDC); |
| 2435 | Importer.Imported(D, D2); |
Sean Callanan | 95e74be | 2011-10-21 02:57:43 +0000 | [diff] [blame] | 2436 | LexicalDC->addDeclInternal(D2); |
Douglas Gregor | 98c1018 | 2010-02-12 22:17:39 +0000 | [diff] [blame] | 2437 | |
| 2438 | // Import the integer type. |
| 2439 | QualType ToIntegerType = Importer.Import(D->getIntegerType()); |
| 2440 | if (ToIntegerType.isNull()) |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 2441 | return nullptr; |
Douglas Gregor | 3996e24 | 2010-02-15 22:01:00 +0000 | [diff] [blame] | 2442 | D2->setIntegerType(ToIntegerType); |
Douglas Gregor | 98c1018 | 2010-02-12 22:17:39 +0000 | [diff] [blame] | 2443 | |
| 2444 | // Import the definition |
John McCall | f937c02 | 2011-10-07 06:10:15 +0000 | [diff] [blame] | 2445 | if (D->isCompleteDefinition() && ImportDefinition(D, D2)) |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 2446 | return nullptr; |
Douglas Gregor | 98c1018 | 2010-02-12 22:17:39 +0000 | [diff] [blame] | 2447 | |
Douglas Gregor | 3996e24 | 2010-02-15 22:01:00 +0000 | [diff] [blame] | 2448 | return D2; |
Douglas Gregor | 98c1018 | 2010-02-12 22:17:39 +0000 | [diff] [blame] | 2449 | } |
| 2450 | |
Douglas Gregor | 5c73e91 | 2010-02-11 00:48:18 +0000 | [diff] [blame] | 2451 | Decl *ASTNodeImporter::VisitRecordDecl(RecordDecl *D) { |
| 2452 | // If this record has a definition in the translation unit we're coming from, |
| 2453 | // but this particular declaration is not that definition, import the |
| 2454 | // definition and map to that. |
Douglas Gregor | 0a5a221 | 2010-02-11 01:04:33 +0000 | [diff] [blame] | 2455 | TagDecl *Definition = D->getDefinition(); |
Douglas Gregor | 5c73e91 | 2010-02-11 00:48:18 +0000 | [diff] [blame] | 2456 | if (Definition && Definition != D) { |
| 2457 | Decl *ImportedDef = Importer.Import(Definition); |
Douglas Gregor | 8cdbe64 | 2010-02-12 23:44:20 +0000 | [diff] [blame] | 2458 | if (!ImportedDef) |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 2459 | return nullptr; |
| 2460 | |
Douglas Gregor | 8cdbe64 | 2010-02-12 23:44:20 +0000 | [diff] [blame] | 2461 | return Importer.Imported(D, ImportedDef); |
Douglas Gregor | 5c73e91 | 2010-02-11 00:48:18 +0000 | [diff] [blame] | 2462 | } |
| 2463 | |
| 2464 | // Import the major distinguishing characteristics of this record. |
| 2465 | DeclContext *DC, *LexicalDC; |
| 2466 | DeclarationName Name; |
| 2467 | SourceLocation Loc; |
| 2468 | if (ImportDeclParts(D, DC, LexicalDC, Name, Loc)) |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 2469 | return nullptr; |
| 2470 | |
Douglas Gregor | 5c73e91 | 2010-02-11 00:48:18 +0000 | [diff] [blame] | 2471 | // Figure out what structure name we're looking for. |
| 2472 | unsigned IDNS = Decl::IDNS_Tag; |
| 2473 | DeclarationName SearchName = Name; |
Richard Smith | dda56e4 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 2474 | if (!SearchName && D->getTypedefNameForAnonDecl()) { |
| 2475 | SearchName = Importer.Import(D->getTypedefNameForAnonDecl()->getDeclName()); |
Douglas Gregor | 5c73e91 | 2010-02-11 00:48:18 +0000 | [diff] [blame] | 2476 | IDNS = Decl::IDNS_Ordinary; |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 2477 | } else if (Importer.getToContext().getLangOpts().CPlusPlus) |
Douglas Gregor | 5c73e91 | 2010-02-11 00:48:18 +0000 | [diff] [blame] | 2478 | IDNS |= Decl::IDNS_Ordinary; |
| 2479 | |
| 2480 | // We may already have a record of the same name; try to find and match it. |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 2481 | RecordDecl *AdoptDecl = nullptr; |
Douglas Gregor | dd6006f | 2012-07-17 21:16:27 +0000 | [diff] [blame] | 2482 | if (!DC->isFunctionOrMethod()) { |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2483 | SmallVector<NamedDecl *, 4> ConflictingDecls; |
Dmitri Gribenko | f857950 | 2013-01-12 19:30:44 +0000 | [diff] [blame] | 2484 | SmallVector<NamedDecl *, 2> FoundDecls; |
Douglas Gregor | 9e0a5b3 | 2011-10-15 00:10:27 +0000 | [diff] [blame] | 2485 | DC->localUncachedLookup(SearchName, FoundDecls); |
| 2486 | for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) { |
| 2487 | if (!FoundDecls[I]->isInIdentifierNamespace(IDNS)) |
Douglas Gregor | 5c73e91 | 2010-02-11 00:48:18 +0000 | [diff] [blame] | 2488 | continue; |
| 2489 | |
Douglas Gregor | 9e0a5b3 | 2011-10-15 00:10:27 +0000 | [diff] [blame] | 2490 | Decl *Found = FoundDecls[I]; |
Richard Smith | dda56e4 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 2491 | if (TypedefNameDecl *Typedef = dyn_cast<TypedefNameDecl>(Found)) { |
Douglas Gregor | 5c73e91 | 2010-02-11 00:48:18 +0000 | [diff] [blame] | 2492 | if (const TagType *Tag = Typedef->getUnderlyingType()->getAs<TagType>()) |
| 2493 | Found = Tag->getDecl(); |
| 2494 | } |
| 2495 | |
| 2496 | if (RecordDecl *FoundRecord = dyn_cast<RecordDecl>(Found)) { |
Douglas Gregor | ceb32bf | 2012-10-26 16:45:11 +0000 | [diff] [blame] | 2497 | if (D->isAnonymousStructOrUnion() && |
| 2498 | FoundRecord->isAnonymousStructOrUnion()) { |
| 2499 | // If both anonymous structs/unions are in a record context, make sure |
| 2500 | // they occur in the same location in the context records. |
David Blaikie | 05785d1 | 2013-02-20 22:23:23 +0000 | [diff] [blame] | 2501 | if (Optional<unsigned> Index1 |
Douglas Gregor | ceb32bf | 2012-10-26 16:45:11 +0000 | [diff] [blame] | 2502 | = findAnonymousStructOrUnionIndex(D)) { |
David Blaikie | 05785d1 | 2013-02-20 22:23:23 +0000 | [diff] [blame] | 2503 | if (Optional<unsigned> Index2 = |
| 2504 | findAnonymousStructOrUnionIndex(FoundRecord)) { |
Douglas Gregor | ceb32bf | 2012-10-26 16:45:11 +0000 | [diff] [blame] | 2505 | if (*Index1 != *Index2) |
| 2506 | continue; |
| 2507 | } |
| 2508 | } |
| 2509 | } |
| 2510 | |
Douglas Gregor | 2579105 | 2010-02-12 00:09:27 +0000 | [diff] [blame] | 2511 | if (RecordDecl *FoundDef = FoundRecord->getDefinition()) { |
Douglas Gregor | dd6006f | 2012-07-17 21:16:27 +0000 | [diff] [blame] | 2512 | if ((SearchName && !D->isCompleteDefinition()) |
| 2513 | || (D->isCompleteDefinition() && |
| 2514 | D->isAnonymousStructOrUnion() |
| 2515 | == FoundDef->isAnonymousStructOrUnion() && |
| 2516 | IsStructuralMatch(D, FoundDef))) { |
Douglas Gregor | 2579105 | 2010-02-12 00:09:27 +0000 | [diff] [blame] | 2517 | // The record types structurally match, or the "from" translation |
| 2518 | // unit only had a forward declaration anyway; call it the same |
| 2519 | // function. |
| 2520 | // FIXME: For C++, we should also merge methods here. |
Douglas Gregor | 8cdbe64 | 2010-02-12 23:44:20 +0000 | [diff] [blame] | 2521 | return Importer.Imported(D, FoundDef); |
Douglas Gregor | 2579105 | 2010-02-12 00:09:27 +0000 | [diff] [blame] | 2522 | } |
Douglas Gregor | dd6006f | 2012-07-17 21:16:27 +0000 | [diff] [blame] | 2523 | } else if (!D->isCompleteDefinition()) { |
Douglas Gregor | 2579105 | 2010-02-12 00:09:27 +0000 | [diff] [blame] | 2524 | // We have a forward declaration of this type, so adopt that forward |
| 2525 | // declaration rather than building a new one. |
Sean Callanan | c94711c | 2014-03-04 18:11:50 +0000 | [diff] [blame] | 2526 | |
| 2527 | // If one or both can be completed from external storage then try one |
| 2528 | // last time to complete and compare them before doing this. |
| 2529 | |
| 2530 | if (FoundRecord->hasExternalLexicalStorage() && |
| 2531 | !FoundRecord->isCompleteDefinition()) |
| 2532 | FoundRecord->getASTContext().getExternalSource()->CompleteType(FoundRecord); |
| 2533 | if (D->hasExternalLexicalStorage()) |
| 2534 | D->getASTContext().getExternalSource()->CompleteType(D); |
| 2535 | |
| 2536 | if (FoundRecord->isCompleteDefinition() && |
| 2537 | D->isCompleteDefinition() && |
| 2538 | !IsStructuralMatch(D, FoundRecord)) |
| 2539 | continue; |
| 2540 | |
Douglas Gregor | 2579105 | 2010-02-12 00:09:27 +0000 | [diff] [blame] | 2541 | AdoptDecl = FoundRecord; |
| 2542 | continue; |
Douglas Gregor | dd6006f | 2012-07-17 21:16:27 +0000 | [diff] [blame] | 2543 | } else if (!SearchName) { |
| 2544 | continue; |
| 2545 | } |
Douglas Gregor | 5c73e91 | 2010-02-11 00:48:18 +0000 | [diff] [blame] | 2546 | } |
| 2547 | |
Douglas Gregor | 9e0a5b3 | 2011-10-15 00:10:27 +0000 | [diff] [blame] | 2548 | ConflictingDecls.push_back(FoundDecls[I]); |
Douglas Gregor | 5c73e91 | 2010-02-11 00:48:18 +0000 | [diff] [blame] | 2549 | } |
| 2550 | |
Douglas Gregor | dd6006f | 2012-07-17 21:16:27 +0000 | [diff] [blame] | 2551 | if (!ConflictingDecls.empty() && SearchName) { |
Douglas Gregor | 5c73e91 | 2010-02-11 00:48:18 +0000 | [diff] [blame] | 2552 | Name = Importer.HandleNameConflict(Name, DC, IDNS, |
| 2553 | ConflictingDecls.data(), |
| 2554 | ConflictingDecls.size()); |
| 2555 | } |
| 2556 | } |
| 2557 | |
| 2558 | // Create the record declaration. |
Douglas Gregor | 3996e24 | 2010-02-15 22:01:00 +0000 | [diff] [blame] | 2559 | RecordDecl *D2 = AdoptDecl; |
Abramo Bagnara | 29c2d46 | 2011-03-09 14:09:51 +0000 | [diff] [blame] | 2560 | SourceLocation StartLoc = Importer.Import(D->getLocStart()); |
Douglas Gregor | 3996e24 | 2010-02-15 22:01:00 +0000 | [diff] [blame] | 2561 | if (!D2) { |
John McCall | 1c70e99 | 2010-06-03 19:28:45 +0000 | [diff] [blame] | 2562 | if (isa<CXXRecordDecl>(D)) { |
Douglas Gregor | 3996e24 | 2010-02-15 22:01:00 +0000 | [diff] [blame] | 2563 | CXXRecordDecl *D2CXX = CXXRecordDecl::Create(Importer.getToContext(), |
Douglas Gregor | 2579105 | 2010-02-12 00:09:27 +0000 | [diff] [blame] | 2564 | D->getTagKind(), |
Abramo Bagnara | 29c2d46 | 2011-03-09 14:09:51 +0000 | [diff] [blame] | 2565 | DC, StartLoc, Loc, |
| 2566 | Name.getAsIdentifierInfo()); |
Douglas Gregor | 3996e24 | 2010-02-15 22:01:00 +0000 | [diff] [blame] | 2567 | D2 = D2CXX; |
Douglas Gregor | dd48317 | 2010-02-22 17:42:47 +0000 | [diff] [blame] | 2568 | D2->setAccess(D->getAccess()); |
Douglas Gregor | 2579105 | 2010-02-12 00:09:27 +0000 | [diff] [blame] | 2569 | } else { |
Douglas Gregor | 3996e24 | 2010-02-15 22:01:00 +0000 | [diff] [blame] | 2570 | D2 = RecordDecl::Create(Importer.getToContext(), D->getTagKind(), |
Abramo Bagnara | 29c2d46 | 2011-03-09 14:09:51 +0000 | [diff] [blame] | 2571 | DC, StartLoc, Loc, Name.getAsIdentifierInfo()); |
Douglas Gregor | 5c73e91 | 2010-02-11 00:48:18 +0000 | [diff] [blame] | 2572 | } |
Douglas Gregor | 1445480 | 2011-02-25 02:25:35 +0000 | [diff] [blame] | 2573 | |
| 2574 | D2->setQualifierInfo(Importer.Import(D->getQualifierLoc())); |
Douglas Gregor | 3996e24 | 2010-02-15 22:01:00 +0000 | [diff] [blame] | 2575 | D2->setLexicalDeclContext(LexicalDC); |
Sean Callanan | 95e74be | 2011-10-21 02:57:43 +0000 | [diff] [blame] | 2576 | LexicalDC->addDeclInternal(D2); |
Douglas Gregor | dd6006f | 2012-07-17 21:16:27 +0000 | [diff] [blame] | 2577 | if (D->isAnonymousStructOrUnion()) |
| 2578 | D2->setAnonymousStructOrUnion(true); |
Douglas Gregor | 5c73e91 | 2010-02-11 00:48:18 +0000 | [diff] [blame] | 2579 | } |
Douglas Gregor | 8cdbe64 | 2010-02-12 23:44:20 +0000 | [diff] [blame] | 2580 | |
Douglas Gregor | 3996e24 | 2010-02-15 22:01:00 +0000 | [diff] [blame] | 2581 | Importer.Imported(D, D2); |
Douglas Gregor | 2579105 | 2010-02-12 00:09:27 +0000 | [diff] [blame] | 2582 | |
Douglas Gregor | 95d8283 | 2012-01-24 18:36:04 +0000 | [diff] [blame] | 2583 | if (D->isCompleteDefinition() && ImportDefinition(D, D2, IDK_Default)) |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 2584 | return nullptr; |
| 2585 | |
Douglas Gregor | 3996e24 | 2010-02-15 22:01:00 +0000 | [diff] [blame] | 2586 | return D2; |
Douglas Gregor | 5c73e91 | 2010-02-11 00:48:18 +0000 | [diff] [blame] | 2587 | } |
| 2588 | |
Douglas Gregor | 98c1018 | 2010-02-12 22:17:39 +0000 | [diff] [blame] | 2589 | Decl *ASTNodeImporter::VisitEnumConstantDecl(EnumConstantDecl *D) { |
| 2590 | // Import the major distinguishing characteristics of this enumerator. |
| 2591 | DeclContext *DC, *LexicalDC; |
| 2592 | DeclarationName Name; |
| 2593 | SourceLocation Loc; |
Douglas Gregor | b4964f7 | 2010-02-15 23:54:17 +0000 | [diff] [blame] | 2594 | if (ImportDeclParts(D, DC, LexicalDC, Name, Loc)) |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 2595 | return nullptr; |
Douglas Gregor | b4964f7 | 2010-02-15 23:54:17 +0000 | [diff] [blame] | 2596 | |
| 2597 | QualType T = Importer.Import(D->getType()); |
| 2598 | if (T.isNull()) |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 2599 | return nullptr; |
Douglas Gregor | b4964f7 | 2010-02-15 23:54:17 +0000 | [diff] [blame] | 2600 | |
Douglas Gregor | 98c1018 | 2010-02-12 22:17:39 +0000 | [diff] [blame] | 2601 | // Determine whether there are any other declarations with the same name and |
| 2602 | // in the same context. |
| 2603 | if (!LexicalDC->isFunctionOrMethod()) { |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2604 | SmallVector<NamedDecl *, 4> ConflictingDecls; |
Douglas Gregor | 98c1018 | 2010-02-12 22:17:39 +0000 | [diff] [blame] | 2605 | unsigned IDNS = Decl::IDNS_Ordinary; |
Dmitri Gribenko | f857950 | 2013-01-12 19:30:44 +0000 | [diff] [blame] | 2606 | SmallVector<NamedDecl *, 2> FoundDecls; |
Douglas Gregor | 9e0a5b3 | 2011-10-15 00:10:27 +0000 | [diff] [blame] | 2607 | DC->localUncachedLookup(Name, FoundDecls); |
| 2608 | for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) { |
| 2609 | if (!FoundDecls[I]->isInIdentifierNamespace(IDNS)) |
Douglas Gregor | 98c1018 | 2010-02-12 22:17:39 +0000 | [diff] [blame] | 2610 | continue; |
Douglas Gregor | 9115508 | 2012-11-14 22:29:20 +0000 | [diff] [blame] | 2611 | |
| 2612 | if (EnumConstantDecl *FoundEnumConstant |
| 2613 | = dyn_cast<EnumConstantDecl>(FoundDecls[I])) { |
| 2614 | if (IsStructuralMatch(D, FoundEnumConstant)) |
| 2615 | return Importer.Imported(D, FoundEnumConstant); |
| 2616 | } |
| 2617 | |
Douglas Gregor | 9e0a5b3 | 2011-10-15 00:10:27 +0000 | [diff] [blame] | 2618 | ConflictingDecls.push_back(FoundDecls[I]); |
Douglas Gregor | 98c1018 | 2010-02-12 22:17:39 +0000 | [diff] [blame] | 2619 | } |
| 2620 | |
| 2621 | if (!ConflictingDecls.empty()) { |
| 2622 | Name = Importer.HandleNameConflict(Name, DC, IDNS, |
| 2623 | ConflictingDecls.data(), |
| 2624 | ConflictingDecls.size()); |
| 2625 | if (!Name) |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 2626 | return nullptr; |
Douglas Gregor | 98c1018 | 2010-02-12 22:17:39 +0000 | [diff] [blame] | 2627 | } |
| 2628 | } |
| 2629 | |
| 2630 | Expr *Init = Importer.Import(D->getInitExpr()); |
| 2631 | if (D->getInitExpr() && !Init) |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 2632 | return nullptr; |
| 2633 | |
Douglas Gregor | 98c1018 | 2010-02-12 22:17:39 +0000 | [diff] [blame] | 2634 | EnumConstantDecl *ToEnumerator |
| 2635 | = EnumConstantDecl::Create(Importer.getToContext(), cast<EnumDecl>(DC), Loc, |
| 2636 | Name.getAsIdentifierInfo(), T, |
| 2637 | Init, D->getInitVal()); |
Douglas Gregor | dd48317 | 2010-02-22 17:42:47 +0000 | [diff] [blame] | 2638 | ToEnumerator->setAccess(D->getAccess()); |
Douglas Gregor | 98c1018 | 2010-02-12 22:17:39 +0000 | [diff] [blame] | 2639 | ToEnumerator->setLexicalDeclContext(LexicalDC); |
Douglas Gregor | 8cdbe64 | 2010-02-12 23:44:20 +0000 | [diff] [blame] | 2640 | Importer.Imported(D, ToEnumerator); |
Sean Callanan | 95e74be | 2011-10-21 02:57:43 +0000 | [diff] [blame] | 2641 | LexicalDC->addDeclInternal(ToEnumerator); |
Douglas Gregor | 98c1018 | 2010-02-12 22:17:39 +0000 | [diff] [blame] | 2642 | return ToEnumerator; |
| 2643 | } |
Douglas Gregor | 5c73e91 | 2010-02-11 00:48:18 +0000 | [diff] [blame] | 2644 | |
Douglas Gregor | bb7930c | 2010-02-10 19:54:31 +0000 | [diff] [blame] | 2645 | Decl *ASTNodeImporter::VisitFunctionDecl(FunctionDecl *D) { |
| 2646 | // Import the major distinguishing characteristics of this function. |
| 2647 | DeclContext *DC, *LexicalDC; |
| 2648 | DeclarationName Name; |
Douglas Gregor | bb7930c | 2010-02-10 19:54:31 +0000 | [diff] [blame] | 2649 | SourceLocation Loc; |
Douglas Gregor | b4964f7 | 2010-02-15 23:54:17 +0000 | [diff] [blame] | 2650 | if (ImportDeclParts(D, DC, LexicalDC, Name, Loc)) |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 2651 | return nullptr; |
Abramo Bagnara | d6d2f18 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 2652 | |
Douglas Gregor | bb7930c | 2010-02-10 19:54:31 +0000 | [diff] [blame] | 2653 | // Try to find a function in our own ("to") context with the same name, same |
| 2654 | // type, and in the same context as the function we're importing. |
| 2655 | if (!LexicalDC->isFunctionOrMethod()) { |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2656 | SmallVector<NamedDecl *, 4> ConflictingDecls; |
Douglas Gregor | bb7930c | 2010-02-10 19:54:31 +0000 | [diff] [blame] | 2657 | unsigned IDNS = Decl::IDNS_Ordinary; |
Dmitri Gribenko | f857950 | 2013-01-12 19:30:44 +0000 | [diff] [blame] | 2658 | SmallVector<NamedDecl *, 2> FoundDecls; |
Douglas Gregor | 9e0a5b3 | 2011-10-15 00:10:27 +0000 | [diff] [blame] | 2659 | DC->localUncachedLookup(Name, FoundDecls); |
| 2660 | for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) { |
| 2661 | if (!FoundDecls[I]->isInIdentifierNamespace(IDNS)) |
Douglas Gregor | bb7930c | 2010-02-10 19:54:31 +0000 | [diff] [blame] | 2662 | continue; |
Douglas Gregor | 3aed6cd | 2010-02-08 21:09:39 +0000 | [diff] [blame] | 2663 | |
Douglas Gregor | 9e0a5b3 | 2011-10-15 00:10:27 +0000 | [diff] [blame] | 2664 | if (FunctionDecl *FoundFunction = dyn_cast<FunctionDecl>(FoundDecls[I])) { |
Rafael Espindola | 3ae0005 | 2013-05-13 00:12:11 +0000 | [diff] [blame] | 2665 | if (FoundFunction->hasExternalFormalLinkage() && |
| 2666 | D->hasExternalFormalLinkage()) { |
Douglas Gregor | b4964f7 | 2010-02-15 23:54:17 +0000 | [diff] [blame] | 2667 | if (Importer.IsStructurallyEquivalent(D->getType(), |
| 2668 | FoundFunction->getType())) { |
Douglas Gregor | bb7930c | 2010-02-10 19:54:31 +0000 | [diff] [blame] | 2669 | // FIXME: Actually try to merge the body and other attributes. |
Douglas Gregor | 8cdbe64 | 2010-02-12 23:44:20 +0000 | [diff] [blame] | 2670 | return Importer.Imported(D, FoundFunction); |
Douglas Gregor | bb7930c | 2010-02-10 19:54:31 +0000 | [diff] [blame] | 2671 | } |
| 2672 | |
| 2673 | // FIXME: Check for overloading more carefully, e.g., by boosting |
| 2674 | // Sema::IsOverload out to the AST library. |
| 2675 | |
| 2676 | // Function overloading is okay in C++. |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 2677 | if (Importer.getToContext().getLangOpts().CPlusPlus) |
Douglas Gregor | bb7930c | 2010-02-10 19:54:31 +0000 | [diff] [blame] | 2678 | continue; |
| 2679 | |
| 2680 | // Complain about inconsistent function types. |
| 2681 | Importer.ToDiag(Loc, diag::err_odr_function_type_inconsistent) |
Douglas Gregor | b4964f7 | 2010-02-15 23:54:17 +0000 | [diff] [blame] | 2682 | << Name << D->getType() << FoundFunction->getType(); |
Douglas Gregor | bb7930c | 2010-02-10 19:54:31 +0000 | [diff] [blame] | 2683 | Importer.ToDiag(FoundFunction->getLocation(), |
| 2684 | diag::note_odr_value_here) |
| 2685 | << FoundFunction->getType(); |
| 2686 | } |
| 2687 | } |
| 2688 | |
Douglas Gregor | 9e0a5b3 | 2011-10-15 00:10:27 +0000 | [diff] [blame] | 2689 | ConflictingDecls.push_back(FoundDecls[I]); |
Douglas Gregor | bb7930c | 2010-02-10 19:54:31 +0000 | [diff] [blame] | 2690 | } |
| 2691 | |
| 2692 | if (!ConflictingDecls.empty()) { |
| 2693 | Name = Importer.HandleNameConflict(Name, DC, IDNS, |
| 2694 | ConflictingDecls.data(), |
| 2695 | ConflictingDecls.size()); |
| 2696 | if (!Name) |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 2697 | return nullptr; |
Douglas Gregor | bb7930c | 2010-02-10 19:54:31 +0000 | [diff] [blame] | 2698 | } |
Douglas Gregor | 62d311f | 2010-02-09 19:21:46 +0000 | [diff] [blame] | 2699 | } |
Douglas Gregor | b4964f7 | 2010-02-15 23:54:17 +0000 | [diff] [blame] | 2700 | |
Abramo Bagnara | d6d2f18 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 2701 | DeclarationNameInfo NameInfo(Name, Loc); |
| 2702 | // Import additional name location/type info. |
| 2703 | ImportDeclarationNameLoc(D->getNameInfo(), NameInfo); |
| 2704 | |
Argyrios Kyrtzidis | 2f45853 | 2012-09-25 19:26:39 +0000 | [diff] [blame] | 2705 | QualType FromTy = D->getType(); |
| 2706 | bool usedDifferentExceptionSpec = false; |
| 2707 | |
| 2708 | if (const FunctionProtoType * |
| 2709 | FromFPT = D->getType()->getAs<FunctionProtoType>()) { |
| 2710 | FunctionProtoType::ExtProtoInfo FromEPI = FromFPT->getExtProtoInfo(); |
| 2711 | // FunctionProtoType::ExtProtoInfo's ExceptionSpecDecl can point to the |
| 2712 | // FunctionDecl that we are importing the FunctionProtoType for. |
| 2713 | // To avoid an infinite recursion when importing, create the FunctionDecl |
| 2714 | // with a simplified function type and update it afterwards. |
Richard Smith | 8acb428 | 2014-07-31 21:57:55 +0000 | [diff] [blame] | 2715 | if (FromEPI.ExceptionSpec.SourceDecl || |
| 2716 | FromEPI.ExceptionSpec.SourceTemplate || |
| 2717 | FromEPI.ExceptionSpec.NoexceptExpr) { |
Argyrios Kyrtzidis | 2f45853 | 2012-09-25 19:26:39 +0000 | [diff] [blame] | 2718 | FunctionProtoType::ExtProtoInfo DefaultEPI; |
| 2719 | FromTy = Importer.getFromContext().getFunctionType( |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 2720 | FromFPT->getReturnType(), FromFPT->getParamTypes(), DefaultEPI); |
Argyrios Kyrtzidis | 2f45853 | 2012-09-25 19:26:39 +0000 | [diff] [blame] | 2721 | usedDifferentExceptionSpec = true; |
| 2722 | } |
| 2723 | } |
| 2724 | |
Douglas Gregor | b4964f7 | 2010-02-15 23:54:17 +0000 | [diff] [blame] | 2725 | // Import the type. |
Argyrios Kyrtzidis | 2f45853 | 2012-09-25 19:26:39 +0000 | [diff] [blame] | 2726 | QualType T = Importer.Import(FromTy); |
Douglas Gregor | b4964f7 | 2010-02-15 23:54:17 +0000 | [diff] [blame] | 2727 | if (T.isNull()) |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 2728 | return nullptr; |
| 2729 | |
Douglas Gregor | bb7930c | 2010-02-10 19:54:31 +0000 | [diff] [blame] | 2730 | // Import the function parameters. |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2731 | SmallVector<ParmVarDecl *, 8> Parameters; |
Aaron Ballman | f6bf62e | 2014-03-07 15:12:56 +0000 | [diff] [blame] | 2732 | for (auto P : D->params()) { |
| 2733 | ParmVarDecl *ToP = cast_or_null<ParmVarDecl>(Importer.Import(P)); |
Douglas Gregor | bb7930c | 2010-02-10 19:54:31 +0000 | [diff] [blame] | 2734 | if (!ToP) |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 2735 | return nullptr; |
| 2736 | |
Douglas Gregor | bb7930c | 2010-02-10 19:54:31 +0000 | [diff] [blame] | 2737 | Parameters.push_back(ToP); |
| 2738 | } |
| 2739 | |
| 2740 | // Create the imported function. |
| 2741 | TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo()); |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 2742 | FunctionDecl *ToFunction = nullptr; |
Douglas Gregor | 00eace1 | 2010-02-21 18:29:16 +0000 | [diff] [blame] | 2743 | if (CXXConstructorDecl *FromConstructor = dyn_cast<CXXConstructorDecl>(D)) { |
| 2744 | ToFunction = CXXConstructorDecl::Create(Importer.getToContext(), |
| 2745 | cast<CXXRecordDecl>(DC), |
Abramo Bagnara | dff1930 | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 2746 | D->getInnerLocStart(), |
Abramo Bagnara | d6d2f18 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 2747 | NameInfo, T, TInfo, |
Douglas Gregor | 00eace1 | 2010-02-21 18:29:16 +0000 | [diff] [blame] | 2748 | FromConstructor->isExplicit(), |
| 2749 | D->isInlineSpecified(), |
Richard Smith | a77a0a6 | 2011-08-15 21:04:07 +0000 | [diff] [blame] | 2750 | D->isImplicit(), |
| 2751 | D->isConstexpr()); |
Douglas Gregor | 00eace1 | 2010-02-21 18:29:16 +0000 | [diff] [blame] | 2752 | } else if (isa<CXXDestructorDecl>(D)) { |
| 2753 | ToFunction = CXXDestructorDecl::Create(Importer.getToContext(), |
| 2754 | cast<CXXRecordDecl>(DC), |
Abramo Bagnara | dff1930 | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 2755 | D->getInnerLocStart(), |
Craig Silverstein | af8808d | 2010-10-21 00:44:50 +0000 | [diff] [blame] | 2756 | NameInfo, T, TInfo, |
Douglas Gregor | 00eace1 | 2010-02-21 18:29:16 +0000 | [diff] [blame] | 2757 | D->isInlineSpecified(), |
| 2758 | D->isImplicit()); |
| 2759 | } else if (CXXConversionDecl *FromConversion |
| 2760 | = dyn_cast<CXXConversionDecl>(D)) { |
| 2761 | ToFunction = CXXConversionDecl::Create(Importer.getToContext(), |
| 2762 | cast<CXXRecordDecl>(DC), |
Abramo Bagnara | dff1930 | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 2763 | D->getInnerLocStart(), |
Abramo Bagnara | d6d2f18 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 2764 | NameInfo, T, TInfo, |
Douglas Gregor | 00eace1 | 2010-02-21 18:29:16 +0000 | [diff] [blame] | 2765 | D->isInlineSpecified(), |
Douglas Gregor | f2f0806 | 2011-03-08 17:10:18 +0000 | [diff] [blame] | 2766 | FromConversion->isExplicit(), |
Richard Smith | a77a0a6 | 2011-08-15 21:04:07 +0000 | [diff] [blame] | 2767 | D->isConstexpr(), |
Douglas Gregor | f2f0806 | 2011-03-08 17:10:18 +0000 | [diff] [blame] | 2768 | Importer.Import(D->getLocEnd())); |
Douglas Gregor | a50ad13 | 2010-11-29 16:04:58 +0000 | [diff] [blame] | 2769 | } else if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(D)) { |
| 2770 | ToFunction = CXXMethodDecl::Create(Importer.getToContext(), |
| 2771 | cast<CXXRecordDecl>(DC), |
Abramo Bagnara | dff1930 | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 2772 | D->getInnerLocStart(), |
Douglas Gregor | a50ad13 | 2010-11-29 16:04:58 +0000 | [diff] [blame] | 2773 | NameInfo, T, TInfo, |
Rafael Espindola | 6ae7e50 | 2013-04-03 19:27:57 +0000 | [diff] [blame] | 2774 | Method->getStorageClass(), |
Douglas Gregor | f2f0806 | 2011-03-08 17:10:18 +0000 | [diff] [blame] | 2775 | Method->isInlineSpecified(), |
Richard Smith | a77a0a6 | 2011-08-15 21:04:07 +0000 | [diff] [blame] | 2776 | D->isConstexpr(), |
Douglas Gregor | f2f0806 | 2011-03-08 17:10:18 +0000 | [diff] [blame] | 2777 | Importer.Import(D->getLocEnd())); |
Douglas Gregor | 00eace1 | 2010-02-21 18:29:16 +0000 | [diff] [blame] | 2778 | } else { |
Abramo Bagnara | d6d2f18 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 2779 | ToFunction = FunctionDecl::Create(Importer.getToContext(), DC, |
Abramo Bagnara | dff1930 | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 2780 | D->getInnerLocStart(), |
Abramo Bagnara | d6d2f18 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 2781 | NameInfo, T, TInfo, D->getStorageClass(), |
Douglas Gregor | 00eace1 | 2010-02-21 18:29:16 +0000 | [diff] [blame] | 2782 | D->isInlineSpecified(), |
Richard Smith | a77a0a6 | 2011-08-15 21:04:07 +0000 | [diff] [blame] | 2783 | D->hasWrittenPrototype(), |
| 2784 | D->isConstexpr()); |
Douglas Gregor | 00eace1 | 2010-02-21 18:29:16 +0000 | [diff] [blame] | 2785 | } |
John McCall | 3e11ebe | 2010-03-15 10:12:16 +0000 | [diff] [blame] | 2786 | |
| 2787 | // Import the qualifier, if any. |
Douglas Gregor | 1445480 | 2011-02-25 02:25:35 +0000 | [diff] [blame] | 2788 | ToFunction->setQualifierInfo(Importer.Import(D->getQualifierLoc())); |
Douglas Gregor | dd48317 | 2010-02-22 17:42:47 +0000 | [diff] [blame] | 2789 | ToFunction->setAccess(D->getAccess()); |
Douglas Gregor | 43f5479 | 2010-02-17 02:12:47 +0000 | [diff] [blame] | 2790 | ToFunction->setLexicalDeclContext(LexicalDC); |
John McCall | 08432c8 | 2011-01-27 02:37:01 +0000 | [diff] [blame] | 2791 | ToFunction->setVirtualAsWritten(D->isVirtualAsWritten()); |
| 2792 | ToFunction->setTrivial(D->isTrivial()); |
| 2793 | ToFunction->setPure(D->isPure()); |
Douglas Gregor | 43f5479 | 2010-02-17 02:12:47 +0000 | [diff] [blame] | 2794 | Importer.Imported(D, ToFunction); |
Douglas Gregor | 62d311f | 2010-02-09 19:21:46 +0000 | [diff] [blame] | 2795 | |
Douglas Gregor | bb7930c | 2010-02-10 19:54:31 +0000 | [diff] [blame] | 2796 | // Set the parameters. |
| 2797 | for (unsigned I = 0, N = Parameters.size(); I != N; ++I) { |
Douglas Gregor | 43f5479 | 2010-02-17 02:12:47 +0000 | [diff] [blame] | 2798 | Parameters[I]->setOwningFunction(ToFunction); |
Sean Callanan | 95e74be | 2011-10-21 02:57:43 +0000 | [diff] [blame] | 2799 | ToFunction->addDeclInternal(Parameters[I]); |
Douglas Gregor | bb7930c | 2010-02-10 19:54:31 +0000 | [diff] [blame] | 2800 | } |
David Blaikie | 9c70e04 | 2011-09-21 18:16:56 +0000 | [diff] [blame] | 2801 | ToFunction->setParams(Parameters); |
Douglas Gregor | bb7930c | 2010-02-10 19:54:31 +0000 | [diff] [blame] | 2802 | |
Argyrios Kyrtzidis | 2f45853 | 2012-09-25 19:26:39 +0000 | [diff] [blame] | 2803 | if (usedDifferentExceptionSpec) { |
| 2804 | // Update FunctionProtoType::ExtProtoInfo. |
| 2805 | QualType T = Importer.Import(D->getType()); |
| 2806 | if (T.isNull()) |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 2807 | return nullptr; |
Argyrios Kyrtzidis | 2f45853 | 2012-09-25 19:26:39 +0000 | [diff] [blame] | 2808 | ToFunction->setType(T); |
Argyrios Kyrtzidis | b41791d | 2012-09-22 01:58:06 +0000 | [diff] [blame] | 2809 | } |
| 2810 | |
Douglas Gregor | bb7930c | 2010-02-10 19:54:31 +0000 | [diff] [blame] | 2811 | // FIXME: Other bits to merge? |
Douglas Gregor | 0eaa2bf | 2010-10-01 23:55:07 +0000 | [diff] [blame] | 2812 | |
| 2813 | // Add this function to the lexical context. |
Sean Callanan | 95e74be | 2011-10-21 02:57:43 +0000 | [diff] [blame] | 2814 | LexicalDC->addDeclInternal(ToFunction); |
Douglas Gregor | 0eaa2bf | 2010-10-01 23:55:07 +0000 | [diff] [blame] | 2815 | |
Douglas Gregor | 43f5479 | 2010-02-17 02:12:47 +0000 | [diff] [blame] | 2816 | return ToFunction; |
Douglas Gregor | bb7930c | 2010-02-10 19:54:31 +0000 | [diff] [blame] | 2817 | } |
| 2818 | |
Douglas Gregor | 00eace1 | 2010-02-21 18:29:16 +0000 | [diff] [blame] | 2819 | Decl *ASTNodeImporter::VisitCXXMethodDecl(CXXMethodDecl *D) { |
| 2820 | return VisitFunctionDecl(D); |
| 2821 | } |
| 2822 | |
| 2823 | Decl *ASTNodeImporter::VisitCXXConstructorDecl(CXXConstructorDecl *D) { |
| 2824 | return VisitCXXMethodDecl(D); |
| 2825 | } |
| 2826 | |
| 2827 | Decl *ASTNodeImporter::VisitCXXDestructorDecl(CXXDestructorDecl *D) { |
| 2828 | return VisitCXXMethodDecl(D); |
| 2829 | } |
| 2830 | |
| 2831 | Decl *ASTNodeImporter::VisitCXXConversionDecl(CXXConversionDecl *D) { |
| 2832 | return VisitCXXMethodDecl(D); |
| 2833 | } |
| 2834 | |
Douglas Gregor | ceb32bf | 2012-10-26 16:45:11 +0000 | [diff] [blame] | 2835 | static unsigned getFieldIndex(Decl *F) { |
| 2836 | RecordDecl *Owner = dyn_cast<RecordDecl>(F->getDeclContext()); |
| 2837 | if (!Owner) |
| 2838 | return 0; |
| 2839 | |
| 2840 | unsigned Index = 1; |
Aaron Ballman | 629afae | 2014-03-07 19:56:05 +0000 | [diff] [blame] | 2841 | for (const auto *D : Owner->noload_decls()) { |
| 2842 | if (D == F) |
Douglas Gregor | ceb32bf | 2012-10-26 16:45:11 +0000 | [diff] [blame] | 2843 | return Index; |
| 2844 | |
| 2845 | if (isa<FieldDecl>(*D) || isa<IndirectFieldDecl>(*D)) |
| 2846 | ++Index; |
| 2847 | } |
| 2848 | |
| 2849 | return Index; |
| 2850 | } |
| 2851 | |
Douglas Gregor | 5c73e91 | 2010-02-11 00:48:18 +0000 | [diff] [blame] | 2852 | Decl *ASTNodeImporter::VisitFieldDecl(FieldDecl *D) { |
| 2853 | // Import the major distinguishing characteristics of a variable. |
| 2854 | DeclContext *DC, *LexicalDC; |
| 2855 | DeclarationName Name; |
Douglas Gregor | 5c73e91 | 2010-02-11 00:48:18 +0000 | [diff] [blame] | 2856 | SourceLocation Loc; |
Douglas Gregor | b4964f7 | 2010-02-15 23:54:17 +0000 | [diff] [blame] | 2857 | if (ImportDeclParts(D, DC, LexicalDC, Name, Loc)) |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 2858 | return nullptr; |
| 2859 | |
Douglas Gregor | 03d1ed3 | 2011-10-14 21:54:42 +0000 | [diff] [blame] | 2860 | // Determine whether we've already imported this field. |
Dmitri Gribenko | f857950 | 2013-01-12 19:30:44 +0000 | [diff] [blame] | 2861 | SmallVector<NamedDecl *, 2> FoundDecls; |
Douglas Gregor | 9e0a5b3 | 2011-10-15 00:10:27 +0000 | [diff] [blame] | 2862 | DC->localUncachedLookup(Name, FoundDecls); |
| 2863 | for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) { |
| 2864 | if (FieldDecl *FoundField = dyn_cast<FieldDecl>(FoundDecls[I])) { |
Douglas Gregor | ceb32bf | 2012-10-26 16:45:11 +0000 | [diff] [blame] | 2865 | // For anonymous fields, match up by index. |
| 2866 | if (!Name && getFieldIndex(D) != getFieldIndex(FoundField)) |
| 2867 | continue; |
| 2868 | |
Douglas Gregor | 03d1ed3 | 2011-10-14 21:54:42 +0000 | [diff] [blame] | 2869 | if (Importer.IsStructurallyEquivalent(D->getType(), |
| 2870 | FoundField->getType())) { |
| 2871 | Importer.Imported(D, FoundField); |
| 2872 | return FoundField; |
| 2873 | } |
| 2874 | |
| 2875 | Importer.ToDiag(Loc, diag::err_odr_field_type_inconsistent) |
| 2876 | << Name << D->getType() << FoundField->getType(); |
| 2877 | Importer.ToDiag(FoundField->getLocation(), diag::note_odr_value_here) |
| 2878 | << FoundField->getType(); |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 2879 | return nullptr; |
Douglas Gregor | 03d1ed3 | 2011-10-14 21:54:42 +0000 | [diff] [blame] | 2880 | } |
| 2881 | } |
| 2882 | |
Douglas Gregor | b4964f7 | 2010-02-15 23:54:17 +0000 | [diff] [blame] | 2883 | // Import the type. |
| 2884 | QualType T = Importer.Import(D->getType()); |
| 2885 | if (T.isNull()) |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 2886 | return nullptr; |
| 2887 | |
Douglas Gregor | 5c73e91 | 2010-02-11 00:48:18 +0000 | [diff] [blame] | 2888 | TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo()); |
| 2889 | Expr *BitWidth = Importer.Import(D->getBitWidth()); |
| 2890 | if (!BitWidth && D->getBitWidth()) |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 2891 | return nullptr; |
| 2892 | |
Abramo Bagnara | dff1930 | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 2893 | FieldDecl *ToField = FieldDecl::Create(Importer.getToContext(), DC, |
| 2894 | Importer.Import(D->getInnerLocStart()), |
Douglas Gregor | 5c73e91 | 2010-02-11 00:48:18 +0000 | [diff] [blame] | 2895 | Loc, Name.getAsIdentifierInfo(), |
Richard Smith | 938f40b | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 2896 | T, TInfo, BitWidth, D->isMutable(), |
Richard Smith | 2b01318 | 2012-06-10 03:12:00 +0000 | [diff] [blame] | 2897 | D->getInClassInitStyle()); |
Douglas Gregor | dd48317 | 2010-02-22 17:42:47 +0000 | [diff] [blame] | 2898 | ToField->setAccess(D->getAccess()); |
Douglas Gregor | 5c73e91 | 2010-02-11 00:48:18 +0000 | [diff] [blame] | 2899 | ToField->setLexicalDeclContext(LexicalDC); |
Richard Smith | 938f40b | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 2900 | if (ToField->hasInClassInitializer()) |
| 2901 | ToField->setInClassInitializer(D->getInClassInitializer()); |
Douglas Gregor | ceb32bf | 2012-10-26 16:45:11 +0000 | [diff] [blame] | 2902 | ToField->setImplicit(D->isImplicit()); |
Douglas Gregor | 8cdbe64 | 2010-02-12 23:44:20 +0000 | [diff] [blame] | 2903 | Importer.Imported(D, ToField); |
Sean Callanan | 95e74be | 2011-10-21 02:57:43 +0000 | [diff] [blame] | 2904 | LexicalDC->addDeclInternal(ToField); |
Douglas Gregor | 5c73e91 | 2010-02-11 00:48:18 +0000 | [diff] [blame] | 2905 | return ToField; |
| 2906 | } |
| 2907 | |
Francois Pichet | 783dd6e | 2010-11-21 06:08:52 +0000 | [diff] [blame] | 2908 | Decl *ASTNodeImporter::VisitIndirectFieldDecl(IndirectFieldDecl *D) { |
| 2909 | // Import the major distinguishing characteristics of a variable. |
| 2910 | DeclContext *DC, *LexicalDC; |
| 2911 | DeclarationName Name; |
| 2912 | SourceLocation Loc; |
| 2913 | if (ImportDeclParts(D, DC, LexicalDC, Name, Loc)) |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 2914 | return nullptr; |
Francois Pichet | 783dd6e | 2010-11-21 06:08:52 +0000 | [diff] [blame] | 2915 | |
Douglas Gregor | 03d1ed3 | 2011-10-14 21:54:42 +0000 | [diff] [blame] | 2916 | // Determine whether we've already imported this field. |
Dmitri Gribenko | f857950 | 2013-01-12 19:30:44 +0000 | [diff] [blame] | 2917 | SmallVector<NamedDecl *, 2> FoundDecls; |
Douglas Gregor | 9e0a5b3 | 2011-10-15 00:10:27 +0000 | [diff] [blame] | 2918 | DC->localUncachedLookup(Name, FoundDecls); |
| 2919 | for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) { |
Douglas Gregor | 03d1ed3 | 2011-10-14 21:54:42 +0000 | [diff] [blame] | 2920 | if (IndirectFieldDecl *FoundField |
Douglas Gregor | 9e0a5b3 | 2011-10-15 00:10:27 +0000 | [diff] [blame] | 2921 | = dyn_cast<IndirectFieldDecl>(FoundDecls[I])) { |
Douglas Gregor | ceb32bf | 2012-10-26 16:45:11 +0000 | [diff] [blame] | 2922 | // For anonymous indirect fields, match up by index. |
| 2923 | if (!Name && getFieldIndex(D) != getFieldIndex(FoundField)) |
| 2924 | continue; |
| 2925 | |
Douglas Gregor | 03d1ed3 | 2011-10-14 21:54:42 +0000 | [diff] [blame] | 2926 | if (Importer.IsStructurallyEquivalent(D->getType(), |
Douglas Gregor | dd6006f | 2012-07-17 21:16:27 +0000 | [diff] [blame] | 2927 | FoundField->getType(), |
David Blaikie | 7d17010 | 2013-05-15 07:37:26 +0000 | [diff] [blame] | 2928 | !Name.isEmpty())) { |
Douglas Gregor | 03d1ed3 | 2011-10-14 21:54:42 +0000 | [diff] [blame] | 2929 | Importer.Imported(D, FoundField); |
| 2930 | return FoundField; |
| 2931 | } |
Douglas Gregor | dd6006f | 2012-07-17 21:16:27 +0000 | [diff] [blame] | 2932 | |
| 2933 | // If there are more anonymous fields to check, continue. |
| 2934 | if (!Name && I < N-1) |
| 2935 | continue; |
| 2936 | |
Douglas Gregor | 03d1ed3 | 2011-10-14 21:54:42 +0000 | [diff] [blame] | 2937 | Importer.ToDiag(Loc, diag::err_odr_field_type_inconsistent) |
| 2938 | << Name << D->getType() << FoundField->getType(); |
| 2939 | Importer.ToDiag(FoundField->getLocation(), diag::note_odr_value_here) |
| 2940 | << FoundField->getType(); |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 2941 | return nullptr; |
Douglas Gregor | 03d1ed3 | 2011-10-14 21:54:42 +0000 | [diff] [blame] | 2942 | } |
| 2943 | } |
| 2944 | |
Francois Pichet | 783dd6e | 2010-11-21 06:08:52 +0000 | [diff] [blame] | 2945 | // Import the type. |
| 2946 | QualType T = Importer.Import(D->getType()); |
| 2947 | if (T.isNull()) |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 2948 | return nullptr; |
Francois Pichet | 783dd6e | 2010-11-21 06:08:52 +0000 | [diff] [blame] | 2949 | |
| 2950 | NamedDecl **NamedChain = |
| 2951 | new (Importer.getToContext())NamedDecl*[D->getChainingSize()]; |
| 2952 | |
| 2953 | unsigned i = 0; |
Aaron Ballman | 29c9460 | 2014-03-07 18:36:15 +0000 | [diff] [blame] | 2954 | for (auto *PI : D->chain()) { |
Aaron Ballman | 1391608 | 2014-03-07 18:11:58 +0000 | [diff] [blame] | 2955 | Decl *D = Importer.Import(PI); |
Francois Pichet | 783dd6e | 2010-11-21 06:08:52 +0000 | [diff] [blame] | 2956 | if (!D) |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 2957 | return nullptr; |
Francois Pichet | 783dd6e | 2010-11-21 06:08:52 +0000 | [diff] [blame] | 2958 | NamedChain[i++] = cast<NamedDecl>(D); |
| 2959 | } |
| 2960 | |
| 2961 | IndirectFieldDecl *ToIndirectField = IndirectFieldDecl::Create( |
Aaron Ballman | 260995b | 2014-10-15 16:58:18 +0000 | [diff] [blame] | 2962 | Importer.getToContext(), DC, Loc, Name.getAsIdentifierInfo(), T, |
| 2963 | NamedChain, D->getChainingSize()); |
| 2964 | |
| 2965 | for (const auto *Attr : D->attrs()) |
| 2966 | ToIndirectField->addAttr(Attr->clone(Importer.getToContext())); |
| 2967 | |
Francois Pichet | 783dd6e | 2010-11-21 06:08:52 +0000 | [diff] [blame] | 2968 | ToIndirectField->setAccess(D->getAccess()); |
| 2969 | ToIndirectField->setLexicalDeclContext(LexicalDC); |
| 2970 | Importer.Imported(D, ToIndirectField); |
Sean Callanan | 95e74be | 2011-10-21 02:57:43 +0000 | [diff] [blame] | 2971 | LexicalDC->addDeclInternal(ToIndirectField); |
Francois Pichet | 783dd6e | 2010-11-21 06:08:52 +0000 | [diff] [blame] | 2972 | return ToIndirectField; |
| 2973 | } |
| 2974 | |
Douglas Gregor | 7244b0b | 2010-02-17 00:34:30 +0000 | [diff] [blame] | 2975 | Decl *ASTNodeImporter::VisitObjCIvarDecl(ObjCIvarDecl *D) { |
| 2976 | // Import the major distinguishing characteristics of an ivar. |
| 2977 | DeclContext *DC, *LexicalDC; |
| 2978 | DeclarationName Name; |
| 2979 | SourceLocation Loc; |
| 2980 | if (ImportDeclParts(D, DC, LexicalDC, Name, Loc)) |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 2981 | return nullptr; |
| 2982 | |
Douglas Gregor | 7244b0b | 2010-02-17 00:34:30 +0000 | [diff] [blame] | 2983 | // Determine whether we've already imported this ivar |
Dmitri Gribenko | f857950 | 2013-01-12 19:30:44 +0000 | [diff] [blame] | 2984 | SmallVector<NamedDecl *, 2> FoundDecls; |
Douglas Gregor | 9e0a5b3 | 2011-10-15 00:10:27 +0000 | [diff] [blame] | 2985 | DC->localUncachedLookup(Name, FoundDecls); |
| 2986 | for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) { |
| 2987 | if (ObjCIvarDecl *FoundIvar = dyn_cast<ObjCIvarDecl>(FoundDecls[I])) { |
Douglas Gregor | 7244b0b | 2010-02-17 00:34:30 +0000 | [diff] [blame] | 2988 | if (Importer.IsStructurallyEquivalent(D->getType(), |
| 2989 | FoundIvar->getType())) { |
| 2990 | Importer.Imported(D, FoundIvar); |
| 2991 | return FoundIvar; |
| 2992 | } |
| 2993 | |
| 2994 | Importer.ToDiag(Loc, diag::err_odr_ivar_type_inconsistent) |
| 2995 | << Name << D->getType() << FoundIvar->getType(); |
| 2996 | Importer.ToDiag(FoundIvar->getLocation(), diag::note_odr_value_here) |
| 2997 | << FoundIvar->getType(); |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 2998 | return nullptr; |
Douglas Gregor | 7244b0b | 2010-02-17 00:34:30 +0000 | [diff] [blame] | 2999 | } |
| 3000 | } |
| 3001 | |
| 3002 | // Import the type. |
| 3003 | QualType T = Importer.Import(D->getType()); |
| 3004 | if (T.isNull()) |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 3005 | return nullptr; |
| 3006 | |
Douglas Gregor | 7244b0b | 2010-02-17 00:34:30 +0000 | [diff] [blame] | 3007 | TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo()); |
| 3008 | Expr *BitWidth = Importer.Import(D->getBitWidth()); |
| 3009 | if (!BitWidth && D->getBitWidth()) |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 3010 | return nullptr; |
| 3011 | |
Daniel Dunbar | fe3ead7 | 2010-04-02 20:10:03 +0000 | [diff] [blame] | 3012 | ObjCIvarDecl *ToIvar = ObjCIvarDecl::Create(Importer.getToContext(), |
| 3013 | cast<ObjCContainerDecl>(DC), |
Abramo Bagnara | dff1930 | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 3014 | Importer.Import(D->getInnerLocStart()), |
Douglas Gregor | 7244b0b | 2010-02-17 00:34:30 +0000 | [diff] [blame] | 3015 | Loc, Name.getAsIdentifierInfo(), |
| 3016 | T, TInfo, D->getAccessControl(), |
Argyrios Kyrtzidis | 2080d90 | 2014-01-03 18:32:18 +0000 | [diff] [blame] | 3017 | BitWidth, D->getSynthesize()); |
Douglas Gregor | 7244b0b | 2010-02-17 00:34:30 +0000 | [diff] [blame] | 3018 | ToIvar->setLexicalDeclContext(LexicalDC); |
| 3019 | Importer.Imported(D, ToIvar); |
Sean Callanan | 95e74be | 2011-10-21 02:57:43 +0000 | [diff] [blame] | 3020 | LexicalDC->addDeclInternal(ToIvar); |
Douglas Gregor | 7244b0b | 2010-02-17 00:34:30 +0000 | [diff] [blame] | 3021 | return ToIvar; |
| 3022 | |
| 3023 | } |
| 3024 | |
Douglas Gregor | bb7930c | 2010-02-10 19:54:31 +0000 | [diff] [blame] | 3025 | Decl *ASTNodeImporter::VisitVarDecl(VarDecl *D) { |
| 3026 | // Import the major distinguishing characteristics of a variable. |
| 3027 | DeclContext *DC, *LexicalDC; |
| 3028 | DeclarationName Name; |
Douglas Gregor | bb7930c | 2010-02-10 19:54:31 +0000 | [diff] [blame] | 3029 | SourceLocation Loc; |
Douglas Gregor | b4964f7 | 2010-02-15 23:54:17 +0000 | [diff] [blame] | 3030 | if (ImportDeclParts(D, DC, LexicalDC, Name, Loc)) |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 3031 | return nullptr; |
| 3032 | |
Douglas Gregor | 3aed6cd | 2010-02-08 21:09:39 +0000 | [diff] [blame] | 3033 | // Try to find a variable in our own ("to") context with the same name and |
| 3034 | // in the same context as the variable we're importing. |
Douglas Gregor | 62d311f | 2010-02-09 19:21:46 +0000 | [diff] [blame] | 3035 | if (D->isFileVarDecl()) { |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 3036 | VarDecl *MergeWithVar = nullptr; |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 3037 | SmallVector<NamedDecl *, 4> ConflictingDecls; |
Douglas Gregor | 3aed6cd | 2010-02-08 21:09:39 +0000 | [diff] [blame] | 3038 | unsigned IDNS = Decl::IDNS_Ordinary; |
Dmitri Gribenko | f857950 | 2013-01-12 19:30:44 +0000 | [diff] [blame] | 3039 | SmallVector<NamedDecl *, 2> FoundDecls; |
Douglas Gregor | 9e0a5b3 | 2011-10-15 00:10:27 +0000 | [diff] [blame] | 3040 | DC->localUncachedLookup(Name, FoundDecls); |
| 3041 | for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) { |
| 3042 | if (!FoundDecls[I]->isInIdentifierNamespace(IDNS)) |
Douglas Gregor | 3aed6cd | 2010-02-08 21:09:39 +0000 | [diff] [blame] | 3043 | continue; |
| 3044 | |
Douglas Gregor | 9e0a5b3 | 2011-10-15 00:10:27 +0000 | [diff] [blame] | 3045 | if (VarDecl *FoundVar = dyn_cast<VarDecl>(FoundDecls[I])) { |
Douglas Gregor | 3aed6cd | 2010-02-08 21:09:39 +0000 | [diff] [blame] | 3046 | // We have found a variable that we may need to merge with. Check it. |
Rafael Espindola | 3ae0005 | 2013-05-13 00:12:11 +0000 | [diff] [blame] | 3047 | if (FoundVar->hasExternalFormalLinkage() && |
| 3048 | D->hasExternalFormalLinkage()) { |
Douglas Gregor | b4964f7 | 2010-02-15 23:54:17 +0000 | [diff] [blame] | 3049 | if (Importer.IsStructurallyEquivalent(D->getType(), |
| 3050 | FoundVar->getType())) { |
Douglas Gregor | 3aed6cd | 2010-02-08 21:09:39 +0000 | [diff] [blame] | 3051 | MergeWithVar = FoundVar; |
| 3052 | break; |
| 3053 | } |
| 3054 | |
Douglas Gregor | 56521c5 | 2010-02-12 17:23:39 +0000 | [diff] [blame] | 3055 | const ArrayType *FoundArray |
| 3056 | = Importer.getToContext().getAsArrayType(FoundVar->getType()); |
| 3057 | const ArrayType *TArray |
Douglas Gregor | b4964f7 | 2010-02-15 23:54:17 +0000 | [diff] [blame] | 3058 | = Importer.getToContext().getAsArrayType(D->getType()); |
Douglas Gregor | 56521c5 | 2010-02-12 17:23:39 +0000 | [diff] [blame] | 3059 | if (FoundArray && TArray) { |
| 3060 | if (isa<IncompleteArrayType>(FoundArray) && |
| 3061 | isa<ConstantArrayType>(TArray)) { |
Douglas Gregor | b4964f7 | 2010-02-15 23:54:17 +0000 | [diff] [blame] | 3062 | // Import the type. |
| 3063 | QualType T = Importer.Import(D->getType()); |
| 3064 | if (T.isNull()) |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 3065 | return nullptr; |
| 3066 | |
Douglas Gregor | 56521c5 | 2010-02-12 17:23:39 +0000 | [diff] [blame] | 3067 | FoundVar->setType(T); |
| 3068 | MergeWithVar = FoundVar; |
| 3069 | break; |
| 3070 | } else if (isa<IncompleteArrayType>(TArray) && |
| 3071 | isa<ConstantArrayType>(FoundArray)) { |
| 3072 | MergeWithVar = FoundVar; |
| 3073 | break; |
Douglas Gregor | 2fbe558 | 2010-02-10 17:16:49 +0000 | [diff] [blame] | 3074 | } |
| 3075 | } |
| 3076 | |
Douglas Gregor | 3aed6cd | 2010-02-08 21:09:39 +0000 | [diff] [blame] | 3077 | Importer.ToDiag(Loc, diag::err_odr_variable_type_inconsistent) |
Douglas Gregor | b4964f7 | 2010-02-15 23:54:17 +0000 | [diff] [blame] | 3078 | << Name << D->getType() << FoundVar->getType(); |
Douglas Gregor | 3aed6cd | 2010-02-08 21:09:39 +0000 | [diff] [blame] | 3079 | Importer.ToDiag(FoundVar->getLocation(), diag::note_odr_value_here) |
| 3080 | << FoundVar->getType(); |
| 3081 | } |
| 3082 | } |
| 3083 | |
Douglas Gregor | 9e0a5b3 | 2011-10-15 00:10:27 +0000 | [diff] [blame] | 3084 | ConflictingDecls.push_back(FoundDecls[I]); |
Douglas Gregor | 3aed6cd | 2010-02-08 21:09:39 +0000 | [diff] [blame] | 3085 | } |
| 3086 | |
| 3087 | if (MergeWithVar) { |
| 3088 | // An equivalent variable with external linkage has been found. Link |
| 3089 | // the two declarations, then merge them. |
Douglas Gregor | 8cdbe64 | 2010-02-12 23:44:20 +0000 | [diff] [blame] | 3090 | Importer.Imported(D, MergeWithVar); |
Douglas Gregor | 3aed6cd | 2010-02-08 21:09:39 +0000 | [diff] [blame] | 3091 | |
| 3092 | if (VarDecl *DDef = D->getDefinition()) { |
| 3093 | if (VarDecl *ExistingDef = MergeWithVar->getDefinition()) { |
| 3094 | Importer.ToDiag(ExistingDef->getLocation(), |
| 3095 | diag::err_odr_variable_multiple_def) |
| 3096 | << Name; |
| 3097 | Importer.FromDiag(DDef->getLocation(), diag::note_odr_defined_here); |
| 3098 | } else { |
| 3099 | Expr *Init = Importer.Import(DDef->getInit()); |
Douglas Gregor | d505812 | 2010-02-11 01:19:42 +0000 | [diff] [blame] | 3100 | MergeWithVar->setInit(Init); |
Richard Smith | d0b4dd6 | 2011-12-19 06:19:21 +0000 | [diff] [blame] | 3101 | if (DDef->isInitKnownICE()) { |
| 3102 | EvaluatedStmt *Eval = MergeWithVar->ensureEvaluatedStmt(); |
| 3103 | Eval->CheckedICE = true; |
| 3104 | Eval->IsICE = DDef->isInitICE(); |
| 3105 | } |
Douglas Gregor | 3aed6cd | 2010-02-08 21:09:39 +0000 | [diff] [blame] | 3106 | } |
| 3107 | } |
| 3108 | |
| 3109 | return MergeWithVar; |
| 3110 | } |
| 3111 | |
| 3112 | if (!ConflictingDecls.empty()) { |
| 3113 | Name = Importer.HandleNameConflict(Name, DC, IDNS, |
| 3114 | ConflictingDecls.data(), |
| 3115 | ConflictingDecls.size()); |
| 3116 | if (!Name) |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 3117 | return nullptr; |
Douglas Gregor | 3aed6cd | 2010-02-08 21:09:39 +0000 | [diff] [blame] | 3118 | } |
| 3119 | } |
Douglas Gregor | fa7a0e5 | 2010-02-10 17:47:19 +0000 | [diff] [blame] | 3120 | |
Douglas Gregor | b4964f7 | 2010-02-15 23:54:17 +0000 | [diff] [blame] | 3121 | // Import the type. |
| 3122 | QualType T = Importer.Import(D->getType()); |
| 3123 | if (T.isNull()) |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 3124 | return nullptr; |
| 3125 | |
Douglas Gregor | 3aed6cd | 2010-02-08 21:09:39 +0000 | [diff] [blame] | 3126 | // Create the imported variable. |
Douglas Gregor | fa7a0e5 | 2010-02-10 17:47:19 +0000 | [diff] [blame] | 3127 | TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo()); |
Abramo Bagnara | dff1930 | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 3128 | VarDecl *ToVar = VarDecl::Create(Importer.getToContext(), DC, |
| 3129 | Importer.Import(D->getInnerLocStart()), |
| 3130 | Loc, Name.getAsIdentifierInfo(), |
| 3131 | T, TInfo, |
Rafael Espindola | 6ae7e50 | 2013-04-03 19:27:57 +0000 | [diff] [blame] | 3132 | D->getStorageClass()); |
Douglas Gregor | 1445480 | 2011-02-25 02:25:35 +0000 | [diff] [blame] | 3133 | ToVar->setQualifierInfo(Importer.Import(D->getQualifierLoc())); |
Douglas Gregor | dd48317 | 2010-02-22 17:42:47 +0000 | [diff] [blame] | 3134 | ToVar->setAccess(D->getAccess()); |
Douglas Gregor | 62d311f | 2010-02-09 19:21:46 +0000 | [diff] [blame] | 3135 | ToVar->setLexicalDeclContext(LexicalDC); |
Douglas Gregor | 8cdbe64 | 2010-02-12 23:44:20 +0000 | [diff] [blame] | 3136 | Importer.Imported(D, ToVar); |
Sean Callanan | 95e74be | 2011-10-21 02:57:43 +0000 | [diff] [blame] | 3137 | LexicalDC->addDeclInternal(ToVar); |
Douglas Gregor | 62d311f | 2010-02-09 19:21:46 +0000 | [diff] [blame] | 3138 | |
Douglas Gregor | 3aed6cd | 2010-02-08 21:09:39 +0000 | [diff] [blame] | 3139 | // Merge the initializer. |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 3140 | if (ImportDefinition(D, ToVar)) |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 3141 | return nullptr; |
Douglas Gregor | 3aed6cd | 2010-02-08 21:09:39 +0000 | [diff] [blame] | 3142 | |
Douglas Gregor | 3aed6cd | 2010-02-08 21:09:39 +0000 | [diff] [blame] | 3143 | return ToVar; |
| 3144 | } |
| 3145 | |
Douglas Gregor | 8b228d7 | 2010-02-17 21:22:52 +0000 | [diff] [blame] | 3146 | Decl *ASTNodeImporter::VisitImplicitParamDecl(ImplicitParamDecl *D) { |
| 3147 | // Parameters are created in the translation unit's context, then moved |
| 3148 | // into the function declaration's context afterward. |
| 3149 | DeclContext *DC = Importer.getToContext().getTranslationUnitDecl(); |
| 3150 | |
| 3151 | // Import the name of this declaration. |
| 3152 | DeclarationName Name = Importer.Import(D->getDeclName()); |
| 3153 | if (D->getDeclName() && !Name) |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 3154 | return nullptr; |
| 3155 | |
Douglas Gregor | 8b228d7 | 2010-02-17 21:22:52 +0000 | [diff] [blame] | 3156 | // Import the location of this declaration. |
| 3157 | SourceLocation Loc = Importer.Import(D->getLocation()); |
| 3158 | |
| 3159 | // Import the parameter's type. |
| 3160 | QualType T = Importer.Import(D->getType()); |
| 3161 | if (T.isNull()) |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 3162 | return nullptr; |
| 3163 | |
Douglas Gregor | 8b228d7 | 2010-02-17 21:22:52 +0000 | [diff] [blame] | 3164 | // Create the imported parameter. |
| 3165 | ImplicitParamDecl *ToParm |
| 3166 | = ImplicitParamDecl::Create(Importer.getToContext(), DC, |
| 3167 | Loc, Name.getAsIdentifierInfo(), |
| 3168 | T); |
| 3169 | return Importer.Imported(D, ToParm); |
| 3170 | } |
| 3171 | |
Douglas Gregor | bb7930c | 2010-02-10 19:54:31 +0000 | [diff] [blame] | 3172 | Decl *ASTNodeImporter::VisitParmVarDecl(ParmVarDecl *D) { |
| 3173 | // Parameters are created in the translation unit's context, then moved |
| 3174 | // into the function declaration's context afterward. |
| 3175 | DeclContext *DC = Importer.getToContext().getTranslationUnitDecl(); |
| 3176 | |
Douglas Gregor | fa7a0e5 | 2010-02-10 17:47:19 +0000 | [diff] [blame] | 3177 | // Import the name of this declaration. |
| 3178 | DeclarationName Name = Importer.Import(D->getDeclName()); |
| 3179 | if (D->getDeclName() && !Name) |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 3180 | return nullptr; |
| 3181 | |
Douglas Gregor | bb7930c | 2010-02-10 19:54:31 +0000 | [diff] [blame] | 3182 | // Import the location of this declaration. |
| 3183 | SourceLocation Loc = Importer.Import(D->getLocation()); |
| 3184 | |
| 3185 | // Import the parameter's type. |
| 3186 | QualType T = Importer.Import(D->getType()); |
| 3187 | if (T.isNull()) |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 3188 | return nullptr; |
| 3189 | |
Douglas Gregor | bb7930c | 2010-02-10 19:54:31 +0000 | [diff] [blame] | 3190 | // Create the imported parameter. |
| 3191 | TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo()); |
| 3192 | ParmVarDecl *ToParm = ParmVarDecl::Create(Importer.getToContext(), DC, |
Abramo Bagnara | dff1930 | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 3193 | Importer.Import(D->getInnerLocStart()), |
Douglas Gregor | bb7930c | 2010-02-10 19:54:31 +0000 | [diff] [blame] | 3194 | Loc, Name.getAsIdentifierInfo(), |
| 3195 | T, TInfo, D->getStorageClass(), |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 3196 | /*FIXME: Default argument*/nullptr); |
John McCall | f3cd665 | 2010-03-12 18:31:32 +0000 | [diff] [blame] | 3197 | ToParm->setHasInheritedDefaultArg(D->hasInheritedDefaultArg()); |
Douglas Gregor | 8cdbe64 | 2010-02-12 23:44:20 +0000 | [diff] [blame] | 3198 | return Importer.Imported(D, ToParm); |
Douglas Gregor | bb7930c | 2010-02-10 19:54:31 +0000 | [diff] [blame] | 3199 | } |
| 3200 | |
Douglas Gregor | 43f5479 | 2010-02-17 02:12:47 +0000 | [diff] [blame] | 3201 | Decl *ASTNodeImporter::VisitObjCMethodDecl(ObjCMethodDecl *D) { |
| 3202 | // Import the major distinguishing characteristics of a method. |
| 3203 | DeclContext *DC, *LexicalDC; |
| 3204 | DeclarationName Name; |
| 3205 | SourceLocation Loc; |
| 3206 | if (ImportDeclParts(D, DC, LexicalDC, Name, Loc)) |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 3207 | return nullptr; |
| 3208 | |
Dmitri Gribenko | f857950 | 2013-01-12 19:30:44 +0000 | [diff] [blame] | 3209 | SmallVector<NamedDecl *, 2> FoundDecls; |
Douglas Gregor | 9e0a5b3 | 2011-10-15 00:10:27 +0000 | [diff] [blame] | 3210 | DC->localUncachedLookup(Name, FoundDecls); |
| 3211 | for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) { |
| 3212 | if (ObjCMethodDecl *FoundMethod = dyn_cast<ObjCMethodDecl>(FoundDecls[I])) { |
Douglas Gregor | 43f5479 | 2010-02-17 02:12:47 +0000 | [diff] [blame] | 3213 | if (FoundMethod->isInstanceMethod() != D->isInstanceMethod()) |
| 3214 | continue; |
| 3215 | |
| 3216 | // Check return types. |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 3217 | if (!Importer.IsStructurallyEquivalent(D->getReturnType(), |
| 3218 | FoundMethod->getReturnType())) { |
Douglas Gregor | 43f5479 | 2010-02-17 02:12:47 +0000 | [diff] [blame] | 3219 | Importer.ToDiag(Loc, diag::err_odr_objc_method_result_type_inconsistent) |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 3220 | << D->isInstanceMethod() << Name << D->getReturnType() |
| 3221 | << FoundMethod->getReturnType(); |
Douglas Gregor | 43f5479 | 2010-02-17 02:12:47 +0000 | [diff] [blame] | 3222 | Importer.ToDiag(FoundMethod->getLocation(), |
| 3223 | diag::note_odr_objc_method_here) |
| 3224 | << D->isInstanceMethod() << Name; |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 3225 | return nullptr; |
Douglas Gregor | 43f5479 | 2010-02-17 02:12:47 +0000 | [diff] [blame] | 3226 | } |
| 3227 | |
| 3228 | // Check the number of parameters. |
| 3229 | if (D->param_size() != FoundMethod->param_size()) { |
| 3230 | Importer.ToDiag(Loc, diag::err_odr_objc_method_num_params_inconsistent) |
| 3231 | << D->isInstanceMethod() << Name |
| 3232 | << D->param_size() << FoundMethod->param_size(); |
| 3233 | Importer.ToDiag(FoundMethod->getLocation(), |
| 3234 | diag::note_odr_objc_method_here) |
| 3235 | << D->isInstanceMethod() << Name; |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 3236 | return nullptr; |
Douglas Gregor | 43f5479 | 2010-02-17 02:12:47 +0000 | [diff] [blame] | 3237 | } |
| 3238 | |
| 3239 | // Check parameter types. |
| 3240 | for (ObjCMethodDecl::param_iterator P = D->param_begin(), |
| 3241 | PEnd = D->param_end(), FoundP = FoundMethod->param_begin(); |
| 3242 | P != PEnd; ++P, ++FoundP) { |
| 3243 | if (!Importer.IsStructurallyEquivalent((*P)->getType(), |
| 3244 | (*FoundP)->getType())) { |
| 3245 | Importer.FromDiag((*P)->getLocation(), |
| 3246 | diag::err_odr_objc_method_param_type_inconsistent) |
| 3247 | << D->isInstanceMethod() << Name |
| 3248 | << (*P)->getType() << (*FoundP)->getType(); |
| 3249 | Importer.ToDiag((*FoundP)->getLocation(), diag::note_odr_value_here) |
| 3250 | << (*FoundP)->getType(); |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 3251 | return nullptr; |
Douglas Gregor | 43f5479 | 2010-02-17 02:12:47 +0000 | [diff] [blame] | 3252 | } |
| 3253 | } |
| 3254 | |
| 3255 | // Check variadic/non-variadic. |
| 3256 | // Check the number of parameters. |
| 3257 | if (D->isVariadic() != FoundMethod->isVariadic()) { |
| 3258 | Importer.ToDiag(Loc, diag::err_odr_objc_method_variadic_inconsistent) |
| 3259 | << D->isInstanceMethod() << Name; |
| 3260 | Importer.ToDiag(FoundMethod->getLocation(), |
| 3261 | diag::note_odr_objc_method_here) |
| 3262 | << D->isInstanceMethod() << Name; |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 3263 | return nullptr; |
Douglas Gregor | 43f5479 | 2010-02-17 02:12:47 +0000 | [diff] [blame] | 3264 | } |
| 3265 | |
| 3266 | // FIXME: Any other bits we need to merge? |
| 3267 | return Importer.Imported(D, FoundMethod); |
| 3268 | } |
| 3269 | } |
| 3270 | |
| 3271 | // Import the result type. |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 3272 | QualType ResultTy = Importer.Import(D->getReturnType()); |
Douglas Gregor | 43f5479 | 2010-02-17 02:12:47 +0000 | [diff] [blame] | 3273 | if (ResultTy.isNull()) |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 3274 | return nullptr; |
Douglas Gregor | 43f5479 | 2010-02-17 02:12:47 +0000 | [diff] [blame] | 3275 | |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 3276 | TypeSourceInfo *ReturnTInfo = Importer.Import(D->getReturnTypeSourceInfo()); |
Douglas Gregor | 12852d9 | 2010-03-08 14:59:44 +0000 | [diff] [blame] | 3277 | |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 3278 | ObjCMethodDecl *ToMethod = ObjCMethodDecl::Create( |
| 3279 | Importer.getToContext(), Loc, Importer.Import(D->getLocEnd()), |
| 3280 | Name.getObjCSelector(), ResultTy, ReturnTInfo, DC, D->isInstanceMethod(), |
| 3281 | D->isVariadic(), D->isPropertyAccessor(), D->isImplicit(), D->isDefined(), |
| 3282 | D->getImplementationControl(), D->hasRelatedResultType()); |
Douglas Gregor | 43f5479 | 2010-02-17 02:12:47 +0000 | [diff] [blame] | 3283 | |
| 3284 | // FIXME: When we decide to merge method definitions, we'll need to |
| 3285 | // deal with implicit parameters. |
| 3286 | |
| 3287 | // Import the parameters |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 3288 | SmallVector<ParmVarDecl *, 5> ToParams; |
Aaron Ballman | 43b68be | 2014-03-07 17:50:17 +0000 | [diff] [blame] | 3289 | for (auto *FromP : D->params()) { |
| 3290 | ParmVarDecl *ToP = cast_or_null<ParmVarDecl>(Importer.Import(FromP)); |
Douglas Gregor | 43f5479 | 2010-02-17 02:12:47 +0000 | [diff] [blame] | 3291 | if (!ToP) |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 3292 | return nullptr; |
| 3293 | |
Douglas Gregor | 43f5479 | 2010-02-17 02:12:47 +0000 | [diff] [blame] | 3294 | ToParams.push_back(ToP); |
| 3295 | } |
| 3296 | |
| 3297 | // Set the parameters. |
| 3298 | for (unsigned I = 0, N = ToParams.size(); I != N; ++I) { |
| 3299 | ToParams[I]->setOwningFunction(ToMethod); |
Sean Callanan | 95e74be | 2011-10-21 02:57:43 +0000 | [diff] [blame] | 3300 | ToMethod->addDeclInternal(ToParams[I]); |
Douglas Gregor | 43f5479 | 2010-02-17 02:12:47 +0000 | [diff] [blame] | 3301 | } |
Argyrios Kyrtzidis | b8c3aaf | 2011-10-03 06:37:04 +0000 | [diff] [blame] | 3302 | SmallVector<SourceLocation, 12> SelLocs; |
| 3303 | D->getSelectorLocs(SelLocs); |
| 3304 | ToMethod->setMethodParams(Importer.getToContext(), ToParams, SelLocs); |
Douglas Gregor | 43f5479 | 2010-02-17 02:12:47 +0000 | [diff] [blame] | 3305 | |
| 3306 | ToMethod->setLexicalDeclContext(LexicalDC); |
| 3307 | Importer.Imported(D, ToMethod); |
Sean Callanan | 95e74be | 2011-10-21 02:57:43 +0000 | [diff] [blame] | 3308 | LexicalDC->addDeclInternal(ToMethod); |
Douglas Gregor | 43f5479 | 2010-02-17 02:12:47 +0000 | [diff] [blame] | 3309 | return ToMethod; |
| 3310 | } |
| 3311 | |
Douglas Gregor | 84c51c3 | 2010-02-18 01:47:50 +0000 | [diff] [blame] | 3312 | Decl *ASTNodeImporter::VisitObjCCategoryDecl(ObjCCategoryDecl *D) { |
| 3313 | // Import the major distinguishing characteristics of a category. |
| 3314 | DeclContext *DC, *LexicalDC; |
| 3315 | DeclarationName Name; |
| 3316 | SourceLocation Loc; |
| 3317 | if (ImportDeclParts(D, DC, LexicalDC, Name, Loc)) |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 3318 | return nullptr; |
| 3319 | |
Douglas Gregor | 84c51c3 | 2010-02-18 01:47:50 +0000 | [diff] [blame] | 3320 | ObjCInterfaceDecl *ToInterface |
| 3321 | = cast_or_null<ObjCInterfaceDecl>(Importer.Import(D->getClassInterface())); |
| 3322 | if (!ToInterface) |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 3323 | return nullptr; |
| 3324 | |
Douglas Gregor | 84c51c3 | 2010-02-18 01:47:50 +0000 | [diff] [blame] | 3325 | // Determine if we've already encountered this category. |
| 3326 | ObjCCategoryDecl *MergeWithCategory |
| 3327 | = ToInterface->FindCategoryDeclaration(Name.getAsIdentifierInfo()); |
| 3328 | ObjCCategoryDecl *ToCategory = MergeWithCategory; |
| 3329 | if (!ToCategory) { |
| 3330 | ToCategory = ObjCCategoryDecl::Create(Importer.getToContext(), DC, |
Argyrios Kyrtzidis | 52f53fb | 2011-10-04 04:48:02 +0000 | [diff] [blame] | 3331 | Importer.Import(D->getAtStartLoc()), |
Douglas Gregor | 84c51c3 | 2010-02-18 01:47:50 +0000 | [diff] [blame] | 3332 | Loc, |
| 3333 | Importer.Import(D->getCategoryNameLoc()), |
Argyrios Kyrtzidis | 3a5094b | 2011-08-30 19:43:26 +0000 | [diff] [blame] | 3334 | Name.getAsIdentifierInfo(), |
Fariborz Jahanian | a7765fe | 2012-02-20 20:09:20 +0000 | [diff] [blame] | 3335 | ToInterface, |
| 3336 | Importer.Import(D->getIvarLBraceLoc()), |
| 3337 | Importer.Import(D->getIvarRBraceLoc())); |
Douglas Gregor | 84c51c3 | 2010-02-18 01:47:50 +0000 | [diff] [blame] | 3338 | ToCategory->setLexicalDeclContext(LexicalDC); |
Sean Callanan | 95e74be | 2011-10-21 02:57:43 +0000 | [diff] [blame] | 3339 | LexicalDC->addDeclInternal(ToCategory); |
Douglas Gregor | 84c51c3 | 2010-02-18 01:47:50 +0000 | [diff] [blame] | 3340 | Importer.Imported(D, ToCategory); |
| 3341 | |
Douglas Gregor | 84c51c3 | 2010-02-18 01:47:50 +0000 | [diff] [blame] | 3342 | // Import protocols |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 3343 | SmallVector<ObjCProtocolDecl *, 4> Protocols; |
| 3344 | SmallVector<SourceLocation, 4> ProtocolLocs; |
Douglas Gregor | 84c51c3 | 2010-02-18 01:47:50 +0000 | [diff] [blame] | 3345 | ObjCCategoryDecl::protocol_loc_iterator FromProtoLoc |
| 3346 | = D->protocol_loc_begin(); |
| 3347 | for (ObjCCategoryDecl::protocol_iterator FromProto = D->protocol_begin(), |
| 3348 | FromProtoEnd = D->protocol_end(); |
| 3349 | FromProto != FromProtoEnd; |
| 3350 | ++FromProto, ++FromProtoLoc) { |
| 3351 | ObjCProtocolDecl *ToProto |
| 3352 | = cast_or_null<ObjCProtocolDecl>(Importer.Import(*FromProto)); |
| 3353 | if (!ToProto) |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 3354 | return nullptr; |
Douglas Gregor | 84c51c3 | 2010-02-18 01:47:50 +0000 | [diff] [blame] | 3355 | Protocols.push_back(ToProto); |
| 3356 | ProtocolLocs.push_back(Importer.Import(*FromProtoLoc)); |
| 3357 | } |
| 3358 | |
| 3359 | // FIXME: If we're merging, make sure that the protocol list is the same. |
| 3360 | ToCategory->setProtocolList(Protocols.data(), Protocols.size(), |
| 3361 | ProtocolLocs.data(), Importer.getToContext()); |
| 3362 | |
| 3363 | } else { |
| 3364 | Importer.Imported(D, ToCategory); |
| 3365 | } |
| 3366 | |
| 3367 | // Import all of the members of this category. |
Douglas Gregor | 968d633 | 2010-02-21 18:24:45 +0000 | [diff] [blame] | 3368 | ImportDeclContext(D); |
Douglas Gregor | 84c51c3 | 2010-02-18 01:47:50 +0000 | [diff] [blame] | 3369 | |
| 3370 | // If we have an implementation, import it as well. |
| 3371 | if (D->getImplementation()) { |
| 3372 | ObjCCategoryImplDecl *Impl |
Douglas Gregor | 35fd7bc | 2010-12-08 16:41:55 +0000 | [diff] [blame] | 3373 | = cast_or_null<ObjCCategoryImplDecl>( |
| 3374 | Importer.Import(D->getImplementation())); |
Douglas Gregor | 84c51c3 | 2010-02-18 01:47:50 +0000 | [diff] [blame] | 3375 | if (!Impl) |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 3376 | return nullptr; |
| 3377 | |
Douglas Gregor | 84c51c3 | 2010-02-18 01:47:50 +0000 | [diff] [blame] | 3378 | ToCategory->setImplementation(Impl); |
| 3379 | } |
| 3380 | |
| 3381 | return ToCategory; |
| 3382 | } |
| 3383 | |
Douglas Gregor | 2aa5377 | 2012-01-24 17:42:07 +0000 | [diff] [blame] | 3384 | bool ASTNodeImporter::ImportDefinition(ObjCProtocolDecl *From, |
| 3385 | ObjCProtocolDecl *To, |
Douglas Gregor | 2e15c84 | 2012-02-01 21:00:38 +0000 | [diff] [blame] | 3386 | ImportDefinitionKind Kind) { |
Douglas Gregor | 2aa5377 | 2012-01-24 17:42:07 +0000 | [diff] [blame] | 3387 | if (To->getDefinition()) { |
Douglas Gregor | 2e15c84 | 2012-02-01 21:00:38 +0000 | [diff] [blame] | 3388 | if (shouldForceImportDeclContext(Kind)) |
| 3389 | ImportDeclContext(From); |
Douglas Gregor | 2aa5377 | 2012-01-24 17:42:07 +0000 | [diff] [blame] | 3390 | return false; |
| 3391 | } |
| 3392 | |
| 3393 | // Start the protocol definition |
| 3394 | To->startDefinition(); |
| 3395 | |
| 3396 | // Import protocols |
| 3397 | SmallVector<ObjCProtocolDecl *, 4> Protocols; |
| 3398 | SmallVector<SourceLocation, 4> ProtocolLocs; |
| 3399 | ObjCProtocolDecl::protocol_loc_iterator |
| 3400 | FromProtoLoc = From->protocol_loc_begin(); |
| 3401 | for (ObjCProtocolDecl::protocol_iterator FromProto = From->protocol_begin(), |
| 3402 | FromProtoEnd = From->protocol_end(); |
| 3403 | FromProto != FromProtoEnd; |
| 3404 | ++FromProto, ++FromProtoLoc) { |
| 3405 | ObjCProtocolDecl *ToProto |
| 3406 | = cast_or_null<ObjCProtocolDecl>(Importer.Import(*FromProto)); |
| 3407 | if (!ToProto) |
| 3408 | return true; |
| 3409 | Protocols.push_back(ToProto); |
| 3410 | ProtocolLocs.push_back(Importer.Import(*FromProtoLoc)); |
| 3411 | } |
| 3412 | |
| 3413 | // FIXME: If we're merging, make sure that the protocol list is the same. |
| 3414 | To->setProtocolList(Protocols.data(), Protocols.size(), |
| 3415 | ProtocolLocs.data(), Importer.getToContext()); |
| 3416 | |
Douglas Gregor | 2e15c84 | 2012-02-01 21:00:38 +0000 | [diff] [blame] | 3417 | if (shouldForceImportDeclContext(Kind)) { |
| 3418 | // Import all of the members of this protocol. |
| 3419 | ImportDeclContext(From, /*ForceImport=*/true); |
| 3420 | } |
Douglas Gregor | 2aa5377 | 2012-01-24 17:42:07 +0000 | [diff] [blame] | 3421 | return false; |
| 3422 | } |
| 3423 | |
Douglas Gregor | 98d156a | 2010-02-17 16:12:00 +0000 | [diff] [blame] | 3424 | Decl *ASTNodeImporter::VisitObjCProtocolDecl(ObjCProtocolDecl *D) { |
Douglas Gregor | 2aa5377 | 2012-01-24 17:42:07 +0000 | [diff] [blame] | 3425 | // If this protocol has a definition in the translation unit we're coming |
| 3426 | // from, but this particular declaration is not that definition, import the |
| 3427 | // definition and map to that. |
| 3428 | ObjCProtocolDecl *Definition = D->getDefinition(); |
| 3429 | if (Definition && Definition != D) { |
| 3430 | Decl *ImportedDef = Importer.Import(Definition); |
| 3431 | if (!ImportedDef) |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 3432 | return nullptr; |
| 3433 | |
Douglas Gregor | 2aa5377 | 2012-01-24 17:42:07 +0000 | [diff] [blame] | 3434 | return Importer.Imported(D, ImportedDef); |
| 3435 | } |
| 3436 | |
Douglas Gregor | 84c51c3 | 2010-02-18 01:47:50 +0000 | [diff] [blame] | 3437 | // Import the major distinguishing characteristics of a protocol. |
Douglas Gregor | 98d156a | 2010-02-17 16:12:00 +0000 | [diff] [blame] | 3438 | DeclContext *DC, *LexicalDC; |
| 3439 | DeclarationName Name; |
| 3440 | SourceLocation Loc; |
| 3441 | if (ImportDeclParts(D, DC, LexicalDC, Name, Loc)) |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 3442 | return nullptr; |
Douglas Gregor | 98d156a | 2010-02-17 16:12:00 +0000 | [diff] [blame] | 3443 | |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 3444 | ObjCProtocolDecl *MergeWithProtocol = nullptr; |
Dmitri Gribenko | f857950 | 2013-01-12 19:30:44 +0000 | [diff] [blame] | 3445 | SmallVector<NamedDecl *, 2> FoundDecls; |
Douglas Gregor | 9e0a5b3 | 2011-10-15 00:10:27 +0000 | [diff] [blame] | 3446 | DC->localUncachedLookup(Name, FoundDecls); |
| 3447 | for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) { |
| 3448 | if (!FoundDecls[I]->isInIdentifierNamespace(Decl::IDNS_ObjCProtocol)) |
Douglas Gregor | 98d156a | 2010-02-17 16:12:00 +0000 | [diff] [blame] | 3449 | continue; |
| 3450 | |
Douglas Gregor | 9e0a5b3 | 2011-10-15 00:10:27 +0000 | [diff] [blame] | 3451 | if ((MergeWithProtocol = dyn_cast<ObjCProtocolDecl>(FoundDecls[I]))) |
Douglas Gregor | 98d156a | 2010-02-17 16:12:00 +0000 | [diff] [blame] | 3452 | break; |
| 3453 | } |
| 3454 | |
| 3455 | ObjCProtocolDecl *ToProto = MergeWithProtocol; |
Douglas Gregor | 2aa5377 | 2012-01-24 17:42:07 +0000 | [diff] [blame] | 3456 | if (!ToProto) { |
| 3457 | ToProto = ObjCProtocolDecl::Create(Importer.getToContext(), DC, |
| 3458 | Name.getAsIdentifierInfo(), Loc, |
| 3459 | Importer.Import(D->getAtStartLoc()), |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 3460 | /*PrevDecl=*/nullptr); |
Douglas Gregor | 2aa5377 | 2012-01-24 17:42:07 +0000 | [diff] [blame] | 3461 | ToProto->setLexicalDeclContext(LexicalDC); |
| 3462 | LexicalDC->addDeclInternal(ToProto); |
Douglas Gregor | 98d156a | 2010-02-17 16:12:00 +0000 | [diff] [blame] | 3463 | } |
Douglas Gregor | 2aa5377 | 2012-01-24 17:42:07 +0000 | [diff] [blame] | 3464 | |
| 3465 | Importer.Imported(D, ToProto); |
Douglas Gregor | 98d156a | 2010-02-17 16:12:00 +0000 | [diff] [blame] | 3466 | |
Douglas Gregor | 2aa5377 | 2012-01-24 17:42:07 +0000 | [diff] [blame] | 3467 | if (D->isThisDeclarationADefinition() && ImportDefinition(D, ToProto)) |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 3468 | return nullptr; |
| 3469 | |
Douglas Gregor | 98d156a | 2010-02-17 16:12:00 +0000 | [diff] [blame] | 3470 | return ToProto; |
| 3471 | } |
| 3472 | |
Douglas Gregor | 2aa5377 | 2012-01-24 17:42:07 +0000 | [diff] [blame] | 3473 | bool ASTNodeImporter::ImportDefinition(ObjCInterfaceDecl *From, |
| 3474 | ObjCInterfaceDecl *To, |
Douglas Gregor | 2e15c84 | 2012-02-01 21:00:38 +0000 | [diff] [blame] | 3475 | ImportDefinitionKind Kind) { |
Douglas Gregor | 2aa5377 | 2012-01-24 17:42:07 +0000 | [diff] [blame] | 3476 | if (To->getDefinition()) { |
| 3477 | // Check consistency of superclass. |
| 3478 | ObjCInterfaceDecl *FromSuper = From->getSuperClass(); |
| 3479 | if (FromSuper) { |
| 3480 | FromSuper = cast_or_null<ObjCInterfaceDecl>(Importer.Import(FromSuper)); |
| 3481 | if (!FromSuper) |
| 3482 | return true; |
| 3483 | } |
| 3484 | |
| 3485 | ObjCInterfaceDecl *ToSuper = To->getSuperClass(); |
| 3486 | if ((bool)FromSuper != (bool)ToSuper || |
| 3487 | (FromSuper && !declaresSameEntity(FromSuper, ToSuper))) { |
| 3488 | Importer.ToDiag(To->getLocation(), |
| 3489 | diag::err_odr_objc_superclass_inconsistent) |
| 3490 | << To->getDeclName(); |
| 3491 | if (ToSuper) |
| 3492 | Importer.ToDiag(To->getSuperClassLoc(), diag::note_odr_objc_superclass) |
| 3493 | << To->getSuperClass()->getDeclName(); |
| 3494 | else |
| 3495 | Importer.ToDiag(To->getLocation(), |
| 3496 | diag::note_odr_objc_missing_superclass); |
| 3497 | if (From->getSuperClass()) |
| 3498 | Importer.FromDiag(From->getSuperClassLoc(), |
| 3499 | diag::note_odr_objc_superclass) |
| 3500 | << From->getSuperClass()->getDeclName(); |
| 3501 | else |
| 3502 | Importer.FromDiag(From->getLocation(), |
| 3503 | diag::note_odr_objc_missing_superclass); |
| 3504 | } |
| 3505 | |
Douglas Gregor | 2e15c84 | 2012-02-01 21:00:38 +0000 | [diff] [blame] | 3506 | if (shouldForceImportDeclContext(Kind)) |
| 3507 | ImportDeclContext(From); |
Douglas Gregor | 2aa5377 | 2012-01-24 17:42:07 +0000 | [diff] [blame] | 3508 | return false; |
| 3509 | } |
| 3510 | |
| 3511 | // Start the definition. |
| 3512 | To->startDefinition(); |
| 3513 | |
| 3514 | // If this class has a superclass, import it. |
| 3515 | if (From->getSuperClass()) { |
| 3516 | ObjCInterfaceDecl *Super = cast_or_null<ObjCInterfaceDecl>( |
| 3517 | Importer.Import(From->getSuperClass())); |
| 3518 | if (!Super) |
| 3519 | return true; |
| 3520 | |
| 3521 | To->setSuperClass(Super); |
| 3522 | To->setSuperClassLoc(Importer.Import(From->getSuperClassLoc())); |
| 3523 | } |
| 3524 | |
| 3525 | // Import protocols |
| 3526 | SmallVector<ObjCProtocolDecl *, 4> Protocols; |
| 3527 | SmallVector<SourceLocation, 4> ProtocolLocs; |
| 3528 | ObjCInterfaceDecl::protocol_loc_iterator |
| 3529 | FromProtoLoc = From->protocol_loc_begin(); |
| 3530 | |
| 3531 | for (ObjCInterfaceDecl::protocol_iterator FromProto = From->protocol_begin(), |
| 3532 | FromProtoEnd = From->protocol_end(); |
| 3533 | FromProto != FromProtoEnd; |
| 3534 | ++FromProto, ++FromProtoLoc) { |
| 3535 | ObjCProtocolDecl *ToProto |
| 3536 | = cast_or_null<ObjCProtocolDecl>(Importer.Import(*FromProto)); |
| 3537 | if (!ToProto) |
| 3538 | return true; |
| 3539 | Protocols.push_back(ToProto); |
| 3540 | ProtocolLocs.push_back(Importer.Import(*FromProtoLoc)); |
| 3541 | } |
| 3542 | |
| 3543 | // FIXME: If we're merging, make sure that the protocol list is the same. |
| 3544 | To->setProtocolList(Protocols.data(), Protocols.size(), |
| 3545 | ProtocolLocs.data(), Importer.getToContext()); |
| 3546 | |
| 3547 | // Import categories. When the categories themselves are imported, they'll |
| 3548 | // hook themselves into this interface. |
Aaron Ballman | 15063e1 | 2014-03-13 21:35:02 +0000 | [diff] [blame] | 3549 | for (auto *Cat : From->known_categories()) |
| 3550 | Importer.Import(Cat); |
Douglas Gregor | 048fbfa | 2013-01-16 23:00:23 +0000 | [diff] [blame] | 3551 | |
Douglas Gregor | 2aa5377 | 2012-01-24 17:42:07 +0000 | [diff] [blame] | 3552 | // If we have an @implementation, import it as well. |
| 3553 | if (From->getImplementation()) { |
| 3554 | ObjCImplementationDecl *Impl = cast_or_null<ObjCImplementationDecl>( |
| 3555 | Importer.Import(From->getImplementation())); |
| 3556 | if (!Impl) |
| 3557 | return true; |
| 3558 | |
| 3559 | To->setImplementation(Impl); |
| 3560 | } |
| 3561 | |
Douglas Gregor | 2e15c84 | 2012-02-01 21:00:38 +0000 | [diff] [blame] | 3562 | if (shouldForceImportDeclContext(Kind)) { |
| 3563 | // Import all of the members of this class. |
| 3564 | ImportDeclContext(From, /*ForceImport=*/true); |
| 3565 | } |
Douglas Gregor | 2aa5377 | 2012-01-24 17:42:07 +0000 | [diff] [blame] | 3566 | return false; |
| 3567 | } |
| 3568 | |
Douglas Gregor | 4563532 | 2010-02-16 01:20:57 +0000 | [diff] [blame] | 3569 | Decl *ASTNodeImporter::VisitObjCInterfaceDecl(ObjCInterfaceDecl *D) { |
Douglas Gregor | 2aa5377 | 2012-01-24 17:42:07 +0000 | [diff] [blame] | 3570 | // If this class has a definition in the translation unit we're coming from, |
| 3571 | // but this particular declaration is not that definition, import the |
| 3572 | // definition and map to that. |
| 3573 | ObjCInterfaceDecl *Definition = D->getDefinition(); |
| 3574 | if (Definition && Definition != D) { |
| 3575 | Decl *ImportedDef = Importer.Import(Definition); |
| 3576 | if (!ImportedDef) |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 3577 | return nullptr; |
| 3578 | |
Douglas Gregor | 2aa5377 | 2012-01-24 17:42:07 +0000 | [diff] [blame] | 3579 | return Importer.Imported(D, ImportedDef); |
| 3580 | } |
| 3581 | |
Douglas Gregor | 4563532 | 2010-02-16 01:20:57 +0000 | [diff] [blame] | 3582 | // Import the major distinguishing characteristics of an @interface. |
| 3583 | DeclContext *DC, *LexicalDC; |
| 3584 | DeclarationName Name; |
| 3585 | SourceLocation Loc; |
| 3586 | if (ImportDeclParts(D, DC, LexicalDC, Name, Loc)) |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 3587 | return nullptr; |
Douglas Gregor | 4563532 | 2010-02-16 01:20:57 +0000 | [diff] [blame] | 3588 | |
Douglas Gregor | 2aa5377 | 2012-01-24 17:42:07 +0000 | [diff] [blame] | 3589 | // Look for an existing interface with the same name. |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 3590 | ObjCInterfaceDecl *MergeWithIface = nullptr; |
Dmitri Gribenko | f857950 | 2013-01-12 19:30:44 +0000 | [diff] [blame] | 3591 | SmallVector<NamedDecl *, 2> FoundDecls; |
Douglas Gregor | 9e0a5b3 | 2011-10-15 00:10:27 +0000 | [diff] [blame] | 3592 | DC->localUncachedLookup(Name, FoundDecls); |
| 3593 | for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) { |
| 3594 | if (!FoundDecls[I]->isInIdentifierNamespace(Decl::IDNS_Ordinary)) |
Douglas Gregor | 4563532 | 2010-02-16 01:20:57 +0000 | [diff] [blame] | 3595 | continue; |
| 3596 | |
Douglas Gregor | 9e0a5b3 | 2011-10-15 00:10:27 +0000 | [diff] [blame] | 3597 | if ((MergeWithIface = dyn_cast<ObjCInterfaceDecl>(FoundDecls[I]))) |
Douglas Gregor | 4563532 | 2010-02-16 01:20:57 +0000 | [diff] [blame] | 3598 | break; |
| 3599 | } |
| 3600 | |
Douglas Gregor | 2aa5377 | 2012-01-24 17:42:07 +0000 | [diff] [blame] | 3601 | // Create an interface declaration, if one does not already exist. |
Douglas Gregor | 4563532 | 2010-02-16 01:20:57 +0000 | [diff] [blame] | 3602 | ObjCInterfaceDecl *ToIface = MergeWithIface; |
Douglas Gregor | 2aa5377 | 2012-01-24 17:42:07 +0000 | [diff] [blame] | 3603 | if (!ToIface) { |
| 3604 | ToIface = ObjCInterfaceDecl::Create(Importer.getToContext(), DC, |
| 3605 | Importer.Import(D->getAtStartLoc()), |
| 3606 | Name.getAsIdentifierInfo(), |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 3607 | /*PrevDecl=*/nullptr, Loc, |
Douglas Gregor | 2aa5377 | 2012-01-24 17:42:07 +0000 | [diff] [blame] | 3608 | D->isImplicitInterfaceDecl()); |
| 3609 | ToIface->setLexicalDeclContext(LexicalDC); |
| 3610 | LexicalDC->addDeclInternal(ToIface); |
Douglas Gregor | 4563532 | 2010-02-16 01:20:57 +0000 | [diff] [blame] | 3611 | } |
Douglas Gregor | 2aa5377 | 2012-01-24 17:42:07 +0000 | [diff] [blame] | 3612 | Importer.Imported(D, ToIface); |
Douglas Gregor | 4563532 | 2010-02-16 01:20:57 +0000 | [diff] [blame] | 3613 | |
Douglas Gregor | 2aa5377 | 2012-01-24 17:42:07 +0000 | [diff] [blame] | 3614 | if (D->isThisDeclarationADefinition() && ImportDefinition(D, ToIface)) |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 3615 | return nullptr; |
| 3616 | |
Douglas Gregor | 98d156a | 2010-02-17 16:12:00 +0000 | [diff] [blame] | 3617 | return ToIface; |
Douglas Gregor | 4563532 | 2010-02-16 01:20:57 +0000 | [diff] [blame] | 3618 | } |
| 3619 | |
Douglas Gregor | 4da9d68 | 2010-12-07 15:32:12 +0000 | [diff] [blame] | 3620 | Decl *ASTNodeImporter::VisitObjCCategoryImplDecl(ObjCCategoryImplDecl *D) { |
| 3621 | ObjCCategoryDecl *Category = cast_or_null<ObjCCategoryDecl>( |
| 3622 | Importer.Import(D->getCategoryDecl())); |
| 3623 | if (!Category) |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 3624 | return nullptr; |
| 3625 | |
Douglas Gregor | 4da9d68 | 2010-12-07 15:32:12 +0000 | [diff] [blame] | 3626 | ObjCCategoryImplDecl *ToImpl = Category->getImplementation(); |
| 3627 | if (!ToImpl) { |
| 3628 | DeclContext *DC = Importer.ImportContext(D->getDeclContext()); |
| 3629 | if (!DC) |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 3630 | return nullptr; |
| 3631 | |
Argyrios Kyrtzidis | 4996f5f | 2011-12-09 00:31:40 +0000 | [diff] [blame] | 3632 | SourceLocation CategoryNameLoc = Importer.Import(D->getCategoryNameLoc()); |
Douglas Gregor | 4da9d68 | 2010-12-07 15:32:12 +0000 | [diff] [blame] | 3633 | ToImpl = ObjCCategoryImplDecl::Create(Importer.getToContext(), DC, |
Douglas Gregor | 4da9d68 | 2010-12-07 15:32:12 +0000 | [diff] [blame] | 3634 | Importer.Import(D->getIdentifier()), |
Argyrios Kyrtzidis | 52f53fb | 2011-10-04 04:48:02 +0000 | [diff] [blame] | 3635 | Category->getClassInterface(), |
| 3636 | Importer.Import(D->getLocation()), |
Argyrios Kyrtzidis | 4996f5f | 2011-12-09 00:31:40 +0000 | [diff] [blame] | 3637 | Importer.Import(D->getAtStartLoc()), |
| 3638 | CategoryNameLoc); |
Douglas Gregor | 4da9d68 | 2010-12-07 15:32:12 +0000 | [diff] [blame] | 3639 | |
| 3640 | DeclContext *LexicalDC = DC; |
| 3641 | if (D->getDeclContext() != D->getLexicalDeclContext()) { |
| 3642 | LexicalDC = Importer.ImportContext(D->getLexicalDeclContext()); |
| 3643 | if (!LexicalDC) |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 3644 | return nullptr; |
| 3645 | |
Douglas Gregor | 4da9d68 | 2010-12-07 15:32:12 +0000 | [diff] [blame] | 3646 | ToImpl->setLexicalDeclContext(LexicalDC); |
| 3647 | } |
| 3648 | |
Sean Callanan | 95e74be | 2011-10-21 02:57:43 +0000 | [diff] [blame] | 3649 | LexicalDC->addDeclInternal(ToImpl); |
Douglas Gregor | 4da9d68 | 2010-12-07 15:32:12 +0000 | [diff] [blame] | 3650 | Category->setImplementation(ToImpl); |
| 3651 | } |
| 3652 | |
| 3653 | Importer.Imported(D, ToImpl); |
Douglas Gregor | 35fd7bc | 2010-12-08 16:41:55 +0000 | [diff] [blame] | 3654 | ImportDeclContext(D); |
Douglas Gregor | 4da9d68 | 2010-12-07 15:32:12 +0000 | [diff] [blame] | 3655 | return ToImpl; |
| 3656 | } |
| 3657 | |
Douglas Gregor | da8025c | 2010-12-07 01:26:03 +0000 | [diff] [blame] | 3658 | Decl *ASTNodeImporter::VisitObjCImplementationDecl(ObjCImplementationDecl *D) { |
| 3659 | // Find the corresponding interface. |
| 3660 | ObjCInterfaceDecl *Iface = cast_or_null<ObjCInterfaceDecl>( |
| 3661 | Importer.Import(D->getClassInterface())); |
| 3662 | if (!Iface) |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 3663 | return nullptr; |
Douglas Gregor | da8025c | 2010-12-07 01:26:03 +0000 | [diff] [blame] | 3664 | |
| 3665 | // Import the superclass, if any. |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 3666 | ObjCInterfaceDecl *Super = nullptr; |
Douglas Gregor | da8025c | 2010-12-07 01:26:03 +0000 | [diff] [blame] | 3667 | if (D->getSuperClass()) { |
| 3668 | Super = cast_or_null<ObjCInterfaceDecl>( |
| 3669 | Importer.Import(D->getSuperClass())); |
| 3670 | if (!Super) |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 3671 | return nullptr; |
Douglas Gregor | da8025c | 2010-12-07 01:26:03 +0000 | [diff] [blame] | 3672 | } |
| 3673 | |
| 3674 | ObjCImplementationDecl *Impl = Iface->getImplementation(); |
| 3675 | if (!Impl) { |
| 3676 | // We haven't imported an implementation yet. Create a new @implementation |
| 3677 | // now. |
| 3678 | Impl = ObjCImplementationDecl::Create(Importer.getToContext(), |
| 3679 | Importer.ImportContext(D->getDeclContext()), |
Argyrios Kyrtzidis | 52f53fb | 2011-10-04 04:48:02 +0000 | [diff] [blame] | 3680 | Iface, Super, |
Douglas Gregor | da8025c | 2010-12-07 01:26:03 +0000 | [diff] [blame] | 3681 | Importer.Import(D->getLocation()), |
Fariborz Jahanian | a7765fe | 2012-02-20 20:09:20 +0000 | [diff] [blame] | 3682 | Importer.Import(D->getAtStartLoc()), |
Argyrios Kyrtzidis | 5d2ce84 | 2013-05-03 22:31:26 +0000 | [diff] [blame] | 3683 | Importer.Import(D->getSuperClassLoc()), |
Fariborz Jahanian | a7765fe | 2012-02-20 20:09:20 +0000 | [diff] [blame] | 3684 | Importer.Import(D->getIvarLBraceLoc()), |
| 3685 | Importer.Import(D->getIvarRBraceLoc())); |
Douglas Gregor | da8025c | 2010-12-07 01:26:03 +0000 | [diff] [blame] | 3686 | |
| 3687 | if (D->getDeclContext() != D->getLexicalDeclContext()) { |
| 3688 | DeclContext *LexicalDC |
| 3689 | = Importer.ImportContext(D->getLexicalDeclContext()); |
| 3690 | if (!LexicalDC) |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 3691 | return nullptr; |
Douglas Gregor | da8025c | 2010-12-07 01:26:03 +0000 | [diff] [blame] | 3692 | Impl->setLexicalDeclContext(LexicalDC); |
| 3693 | } |
| 3694 | |
| 3695 | // Associate the implementation with the class it implements. |
| 3696 | Iface->setImplementation(Impl); |
| 3697 | Importer.Imported(D, Iface->getImplementation()); |
| 3698 | } else { |
| 3699 | Importer.Imported(D, Iface->getImplementation()); |
| 3700 | |
| 3701 | // Verify that the existing @implementation has the same superclass. |
| 3702 | if ((Super && !Impl->getSuperClass()) || |
| 3703 | (!Super && Impl->getSuperClass()) || |
Craig Topper | dcfc60f | 2014-05-07 06:57:44 +0000 | [diff] [blame] | 3704 | (Super && Impl->getSuperClass() && |
| 3705 | !declaresSameEntity(Super->getCanonicalDecl(), |
| 3706 | Impl->getSuperClass()))) { |
| 3707 | Importer.ToDiag(Impl->getLocation(), |
| 3708 | diag::err_odr_objc_superclass_inconsistent) |
| 3709 | << Iface->getDeclName(); |
| 3710 | // FIXME: It would be nice to have the location of the superclass |
| 3711 | // below. |
| 3712 | if (Impl->getSuperClass()) |
| 3713 | Importer.ToDiag(Impl->getLocation(), |
| 3714 | diag::note_odr_objc_superclass) |
| 3715 | << Impl->getSuperClass()->getDeclName(); |
| 3716 | else |
| 3717 | Importer.ToDiag(Impl->getLocation(), |
| 3718 | diag::note_odr_objc_missing_superclass); |
| 3719 | if (D->getSuperClass()) |
| 3720 | Importer.FromDiag(D->getLocation(), |
Douglas Gregor | da8025c | 2010-12-07 01:26:03 +0000 | [diff] [blame] | 3721 | diag::note_odr_objc_superclass) |
Craig Topper | dcfc60f | 2014-05-07 06:57:44 +0000 | [diff] [blame] | 3722 | << D->getSuperClass()->getDeclName(); |
| 3723 | else |
| 3724 | Importer.FromDiag(D->getLocation(), |
Douglas Gregor | da8025c | 2010-12-07 01:26:03 +0000 | [diff] [blame] | 3725 | diag::note_odr_objc_missing_superclass); |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 3726 | return nullptr; |
Douglas Gregor | da8025c | 2010-12-07 01:26:03 +0000 | [diff] [blame] | 3727 | } |
| 3728 | } |
| 3729 | |
| 3730 | // Import all of the members of this @implementation. |
| 3731 | ImportDeclContext(D); |
| 3732 | |
| 3733 | return Impl; |
| 3734 | } |
| 3735 | |
Douglas Gregor | a11c458 | 2010-02-17 18:02:10 +0000 | [diff] [blame] | 3736 | Decl *ASTNodeImporter::VisitObjCPropertyDecl(ObjCPropertyDecl *D) { |
| 3737 | // Import the major distinguishing characteristics of an @property. |
| 3738 | DeclContext *DC, *LexicalDC; |
| 3739 | DeclarationName Name; |
| 3740 | SourceLocation Loc; |
| 3741 | if (ImportDeclParts(D, DC, LexicalDC, Name, Loc)) |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 3742 | return nullptr; |
Douglas Gregor | a11c458 | 2010-02-17 18:02:10 +0000 | [diff] [blame] | 3743 | |
| 3744 | // Check whether we have already imported this property. |
Dmitri Gribenko | f857950 | 2013-01-12 19:30:44 +0000 | [diff] [blame] | 3745 | SmallVector<NamedDecl *, 2> FoundDecls; |
Douglas Gregor | 9e0a5b3 | 2011-10-15 00:10:27 +0000 | [diff] [blame] | 3746 | DC->localUncachedLookup(Name, FoundDecls); |
| 3747 | for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) { |
Douglas Gregor | a11c458 | 2010-02-17 18:02:10 +0000 | [diff] [blame] | 3748 | if (ObjCPropertyDecl *FoundProp |
Douglas Gregor | 9e0a5b3 | 2011-10-15 00:10:27 +0000 | [diff] [blame] | 3749 | = dyn_cast<ObjCPropertyDecl>(FoundDecls[I])) { |
Douglas Gregor | a11c458 | 2010-02-17 18:02:10 +0000 | [diff] [blame] | 3750 | // Check property types. |
| 3751 | if (!Importer.IsStructurallyEquivalent(D->getType(), |
| 3752 | FoundProp->getType())) { |
| 3753 | Importer.ToDiag(Loc, diag::err_odr_objc_property_type_inconsistent) |
| 3754 | << Name << D->getType() << FoundProp->getType(); |
| 3755 | Importer.ToDiag(FoundProp->getLocation(), diag::note_odr_value_here) |
| 3756 | << FoundProp->getType(); |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 3757 | return nullptr; |
Douglas Gregor | a11c458 | 2010-02-17 18:02:10 +0000 | [diff] [blame] | 3758 | } |
| 3759 | |
| 3760 | // FIXME: Check property attributes, getters, setters, etc.? |
| 3761 | |
| 3762 | // Consider these properties to be equivalent. |
| 3763 | Importer.Imported(D, FoundProp); |
| 3764 | return FoundProp; |
| 3765 | } |
| 3766 | } |
| 3767 | |
| 3768 | // Import the type. |
John McCall | 339bb66 | 2010-06-04 20:50:08 +0000 | [diff] [blame] | 3769 | TypeSourceInfo *T = Importer.Import(D->getTypeSourceInfo()); |
| 3770 | if (!T) |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 3771 | return nullptr; |
Douglas Gregor | a11c458 | 2010-02-17 18:02:10 +0000 | [diff] [blame] | 3772 | |
| 3773 | // Create the new property. |
| 3774 | ObjCPropertyDecl *ToProperty |
| 3775 | = ObjCPropertyDecl::Create(Importer.getToContext(), DC, Loc, |
| 3776 | Name.getAsIdentifierInfo(), |
| 3777 | Importer.Import(D->getAtLoc()), |
Fariborz Jahanian | 86c2f5c | 2012-02-29 22:18:55 +0000 | [diff] [blame] | 3778 | Importer.Import(D->getLParenLoc()), |
Douglas Gregor | a11c458 | 2010-02-17 18:02:10 +0000 | [diff] [blame] | 3779 | T, |
| 3780 | D->getPropertyImplementation()); |
| 3781 | Importer.Imported(D, ToProperty); |
| 3782 | ToProperty->setLexicalDeclContext(LexicalDC); |
Sean Callanan | 95e74be | 2011-10-21 02:57:43 +0000 | [diff] [blame] | 3783 | LexicalDC->addDeclInternal(ToProperty); |
Douglas Gregor | a11c458 | 2010-02-17 18:02:10 +0000 | [diff] [blame] | 3784 | |
| 3785 | ToProperty->setPropertyAttributes(D->getPropertyAttributes()); |
Fariborz Jahanian | 3bf0ded | 2010-06-22 23:20:40 +0000 | [diff] [blame] | 3786 | ToProperty->setPropertyAttributesAsWritten( |
| 3787 | D->getPropertyAttributesAsWritten()); |
Douglas Gregor | a11c458 | 2010-02-17 18:02:10 +0000 | [diff] [blame] | 3788 | ToProperty->setGetterName(Importer.Import(D->getGetterName())); |
| 3789 | ToProperty->setSetterName(Importer.Import(D->getSetterName())); |
| 3790 | ToProperty->setGetterMethodDecl( |
| 3791 | cast_or_null<ObjCMethodDecl>(Importer.Import(D->getGetterMethodDecl()))); |
| 3792 | ToProperty->setSetterMethodDecl( |
| 3793 | cast_or_null<ObjCMethodDecl>(Importer.Import(D->getSetterMethodDecl()))); |
| 3794 | ToProperty->setPropertyIvarDecl( |
| 3795 | cast_or_null<ObjCIvarDecl>(Importer.Import(D->getPropertyIvarDecl()))); |
| 3796 | return ToProperty; |
| 3797 | } |
| 3798 | |
Douglas Gregor | 14a49e2 | 2010-12-07 18:32:03 +0000 | [diff] [blame] | 3799 | Decl *ASTNodeImporter::VisitObjCPropertyImplDecl(ObjCPropertyImplDecl *D) { |
| 3800 | ObjCPropertyDecl *Property = cast_or_null<ObjCPropertyDecl>( |
| 3801 | Importer.Import(D->getPropertyDecl())); |
| 3802 | if (!Property) |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 3803 | return nullptr; |
Douglas Gregor | 14a49e2 | 2010-12-07 18:32:03 +0000 | [diff] [blame] | 3804 | |
| 3805 | DeclContext *DC = Importer.ImportContext(D->getDeclContext()); |
| 3806 | if (!DC) |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 3807 | return nullptr; |
| 3808 | |
Douglas Gregor | 14a49e2 | 2010-12-07 18:32:03 +0000 | [diff] [blame] | 3809 | // Import the lexical declaration context. |
| 3810 | DeclContext *LexicalDC = DC; |
| 3811 | if (D->getDeclContext() != D->getLexicalDeclContext()) { |
| 3812 | LexicalDC = Importer.ImportContext(D->getLexicalDeclContext()); |
| 3813 | if (!LexicalDC) |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 3814 | return nullptr; |
Douglas Gregor | 14a49e2 | 2010-12-07 18:32:03 +0000 | [diff] [blame] | 3815 | } |
| 3816 | |
| 3817 | ObjCImplDecl *InImpl = dyn_cast<ObjCImplDecl>(LexicalDC); |
| 3818 | if (!InImpl) |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 3819 | return nullptr; |
Douglas Gregor | 14a49e2 | 2010-12-07 18:32:03 +0000 | [diff] [blame] | 3820 | |
| 3821 | // Import the ivar (for an @synthesize). |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 3822 | ObjCIvarDecl *Ivar = nullptr; |
Douglas Gregor | 14a49e2 | 2010-12-07 18:32:03 +0000 | [diff] [blame] | 3823 | if (D->getPropertyIvarDecl()) { |
| 3824 | Ivar = cast_or_null<ObjCIvarDecl>( |
| 3825 | Importer.Import(D->getPropertyIvarDecl())); |
| 3826 | if (!Ivar) |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 3827 | return nullptr; |
Douglas Gregor | 14a49e2 | 2010-12-07 18:32:03 +0000 | [diff] [blame] | 3828 | } |
| 3829 | |
| 3830 | ObjCPropertyImplDecl *ToImpl |
| 3831 | = InImpl->FindPropertyImplDecl(Property->getIdentifier()); |
| 3832 | if (!ToImpl) { |
| 3833 | ToImpl = ObjCPropertyImplDecl::Create(Importer.getToContext(), DC, |
| 3834 | Importer.Import(D->getLocStart()), |
| 3835 | Importer.Import(D->getLocation()), |
| 3836 | Property, |
| 3837 | D->getPropertyImplementation(), |
| 3838 | Ivar, |
| 3839 | Importer.Import(D->getPropertyIvarDeclLoc())); |
| 3840 | ToImpl->setLexicalDeclContext(LexicalDC); |
| 3841 | Importer.Imported(D, ToImpl); |
Sean Callanan | 95e74be | 2011-10-21 02:57:43 +0000 | [diff] [blame] | 3842 | LexicalDC->addDeclInternal(ToImpl); |
Douglas Gregor | 14a49e2 | 2010-12-07 18:32:03 +0000 | [diff] [blame] | 3843 | } else { |
| 3844 | // Check that we have the same kind of property implementation (@synthesize |
| 3845 | // vs. @dynamic). |
| 3846 | if (D->getPropertyImplementation() != ToImpl->getPropertyImplementation()) { |
| 3847 | Importer.ToDiag(ToImpl->getLocation(), |
| 3848 | diag::err_odr_objc_property_impl_kind_inconsistent) |
| 3849 | << Property->getDeclName() |
| 3850 | << (ToImpl->getPropertyImplementation() |
| 3851 | == ObjCPropertyImplDecl::Dynamic); |
| 3852 | Importer.FromDiag(D->getLocation(), |
| 3853 | diag::note_odr_objc_property_impl_kind) |
| 3854 | << D->getPropertyDecl()->getDeclName() |
| 3855 | << (D->getPropertyImplementation() == ObjCPropertyImplDecl::Dynamic); |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 3856 | return nullptr; |
Douglas Gregor | 14a49e2 | 2010-12-07 18:32:03 +0000 | [diff] [blame] | 3857 | } |
| 3858 | |
| 3859 | // For @synthesize, check that we have the same |
| 3860 | if (D->getPropertyImplementation() == ObjCPropertyImplDecl::Synthesize && |
| 3861 | Ivar != ToImpl->getPropertyIvarDecl()) { |
| 3862 | Importer.ToDiag(ToImpl->getPropertyIvarDeclLoc(), |
| 3863 | diag::err_odr_objc_synthesize_ivar_inconsistent) |
| 3864 | << Property->getDeclName() |
| 3865 | << ToImpl->getPropertyIvarDecl()->getDeclName() |
| 3866 | << Ivar->getDeclName(); |
| 3867 | Importer.FromDiag(D->getPropertyIvarDeclLoc(), |
| 3868 | diag::note_odr_objc_synthesize_ivar_here) |
| 3869 | << D->getPropertyIvarDecl()->getDeclName(); |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 3870 | return nullptr; |
Douglas Gregor | 14a49e2 | 2010-12-07 18:32:03 +0000 | [diff] [blame] | 3871 | } |
| 3872 | |
| 3873 | // Merge the existing implementation with the new implementation. |
| 3874 | Importer.Imported(D, ToImpl); |
| 3875 | } |
| 3876 | |
| 3877 | return ToImpl; |
| 3878 | } |
| 3879 | |
Douglas Gregor | a082a49 | 2010-11-30 19:14:50 +0000 | [diff] [blame] | 3880 | Decl *ASTNodeImporter::VisitTemplateTypeParmDecl(TemplateTypeParmDecl *D) { |
| 3881 | // For template arguments, we adopt the translation unit as our declaration |
| 3882 | // context. This context will be fixed when the actual template declaration |
| 3883 | // is created. |
| 3884 | |
| 3885 | // FIXME: Import default argument. |
| 3886 | return TemplateTypeParmDecl::Create(Importer.getToContext(), |
| 3887 | Importer.getToContext().getTranslationUnitDecl(), |
Abramo Bagnara | b3185b0 | 2011-03-06 15:48:19 +0000 | [diff] [blame] | 3888 | Importer.Import(D->getLocStart()), |
Douglas Gregor | a082a49 | 2010-11-30 19:14:50 +0000 | [diff] [blame] | 3889 | Importer.Import(D->getLocation()), |
| 3890 | D->getDepth(), |
| 3891 | D->getIndex(), |
| 3892 | Importer.Import(D->getIdentifier()), |
| 3893 | D->wasDeclaredWithTypename(), |
| 3894 | D->isParameterPack()); |
| 3895 | } |
| 3896 | |
| 3897 | Decl * |
| 3898 | ASTNodeImporter::VisitNonTypeTemplateParmDecl(NonTypeTemplateParmDecl *D) { |
| 3899 | // Import the name of this declaration. |
| 3900 | DeclarationName Name = Importer.Import(D->getDeclName()); |
| 3901 | if (D->getDeclName() && !Name) |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 3902 | return nullptr; |
| 3903 | |
Douglas Gregor | a082a49 | 2010-11-30 19:14:50 +0000 | [diff] [blame] | 3904 | // Import the location of this declaration. |
| 3905 | SourceLocation Loc = Importer.Import(D->getLocation()); |
| 3906 | |
| 3907 | // Import the type of this declaration. |
| 3908 | QualType T = Importer.Import(D->getType()); |
| 3909 | if (T.isNull()) |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 3910 | return nullptr; |
| 3911 | |
Douglas Gregor | a082a49 | 2010-11-30 19:14:50 +0000 | [diff] [blame] | 3912 | // Import type-source information. |
| 3913 | TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo()); |
| 3914 | if (D->getTypeSourceInfo() && !TInfo) |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 3915 | return nullptr; |
| 3916 | |
Douglas Gregor | a082a49 | 2010-11-30 19:14:50 +0000 | [diff] [blame] | 3917 | // FIXME: Import default argument. |
| 3918 | |
| 3919 | return NonTypeTemplateParmDecl::Create(Importer.getToContext(), |
| 3920 | Importer.getToContext().getTranslationUnitDecl(), |
Abramo Bagnara | dff1930 | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 3921 | Importer.Import(D->getInnerLocStart()), |
Douglas Gregor | a082a49 | 2010-11-30 19:14:50 +0000 | [diff] [blame] | 3922 | Loc, D->getDepth(), D->getPosition(), |
| 3923 | Name.getAsIdentifierInfo(), |
Douglas Gregor | da3cc0d | 2010-12-23 23:51:58 +0000 | [diff] [blame] | 3924 | T, D->isParameterPack(), TInfo); |
Douglas Gregor | a082a49 | 2010-11-30 19:14:50 +0000 | [diff] [blame] | 3925 | } |
| 3926 | |
| 3927 | Decl * |
| 3928 | ASTNodeImporter::VisitTemplateTemplateParmDecl(TemplateTemplateParmDecl *D) { |
| 3929 | // Import the name of this declaration. |
| 3930 | DeclarationName Name = Importer.Import(D->getDeclName()); |
| 3931 | if (D->getDeclName() && !Name) |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 3932 | return nullptr; |
| 3933 | |
Douglas Gregor | a082a49 | 2010-11-30 19:14:50 +0000 | [diff] [blame] | 3934 | // Import the location of this declaration. |
| 3935 | SourceLocation Loc = Importer.Import(D->getLocation()); |
| 3936 | |
| 3937 | // Import template parameters. |
| 3938 | TemplateParameterList *TemplateParams |
| 3939 | = ImportTemplateParameterList(D->getTemplateParameters()); |
| 3940 | if (!TemplateParams) |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 3941 | return nullptr; |
| 3942 | |
Douglas Gregor | a082a49 | 2010-11-30 19:14:50 +0000 | [diff] [blame] | 3943 | // FIXME: Import default argument. |
| 3944 | |
| 3945 | return TemplateTemplateParmDecl::Create(Importer.getToContext(), |
| 3946 | Importer.getToContext().getTranslationUnitDecl(), |
| 3947 | Loc, D->getDepth(), D->getPosition(), |
Douglas Gregor | f550077 | 2011-01-05 15:48:55 +0000 | [diff] [blame] | 3948 | D->isParameterPack(), |
Douglas Gregor | a082a49 | 2010-11-30 19:14:50 +0000 | [diff] [blame] | 3949 | Name.getAsIdentifierInfo(), |
| 3950 | TemplateParams); |
| 3951 | } |
| 3952 | |
| 3953 | Decl *ASTNodeImporter::VisitClassTemplateDecl(ClassTemplateDecl *D) { |
| 3954 | // If this record has a definition in the translation unit we're coming from, |
| 3955 | // but this particular declaration is not that definition, import the |
| 3956 | // definition and map to that. |
| 3957 | CXXRecordDecl *Definition |
| 3958 | = cast_or_null<CXXRecordDecl>(D->getTemplatedDecl()->getDefinition()); |
| 3959 | if (Definition && Definition != D->getTemplatedDecl()) { |
| 3960 | Decl *ImportedDef |
| 3961 | = Importer.Import(Definition->getDescribedClassTemplate()); |
| 3962 | if (!ImportedDef) |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 3963 | return nullptr; |
| 3964 | |
Douglas Gregor | a082a49 | 2010-11-30 19:14:50 +0000 | [diff] [blame] | 3965 | return Importer.Imported(D, ImportedDef); |
| 3966 | } |
| 3967 | |
| 3968 | // Import the major distinguishing characteristics of this class template. |
| 3969 | DeclContext *DC, *LexicalDC; |
| 3970 | DeclarationName Name; |
| 3971 | SourceLocation Loc; |
| 3972 | if (ImportDeclParts(D, DC, LexicalDC, Name, Loc)) |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 3973 | return nullptr; |
| 3974 | |
Douglas Gregor | a082a49 | 2010-11-30 19:14:50 +0000 | [diff] [blame] | 3975 | // We may already have a template of the same name; try to find and match it. |
| 3976 | if (!DC->isFunctionOrMethod()) { |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 3977 | SmallVector<NamedDecl *, 4> ConflictingDecls; |
Dmitri Gribenko | f857950 | 2013-01-12 19:30:44 +0000 | [diff] [blame] | 3978 | SmallVector<NamedDecl *, 2> FoundDecls; |
Douglas Gregor | 9e0a5b3 | 2011-10-15 00:10:27 +0000 | [diff] [blame] | 3979 | DC->localUncachedLookup(Name, FoundDecls); |
| 3980 | for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) { |
| 3981 | if (!FoundDecls[I]->isInIdentifierNamespace(Decl::IDNS_Ordinary)) |
Douglas Gregor | a082a49 | 2010-11-30 19:14:50 +0000 | [diff] [blame] | 3982 | continue; |
| 3983 | |
Douglas Gregor | 9e0a5b3 | 2011-10-15 00:10:27 +0000 | [diff] [blame] | 3984 | Decl *Found = FoundDecls[I]; |
Douglas Gregor | a082a49 | 2010-11-30 19:14:50 +0000 | [diff] [blame] | 3985 | if (ClassTemplateDecl *FoundTemplate |
| 3986 | = dyn_cast<ClassTemplateDecl>(Found)) { |
| 3987 | if (IsStructuralMatch(D, FoundTemplate)) { |
| 3988 | // The class templates structurally match; call it the same template. |
| 3989 | // FIXME: We may be filling in a forward declaration here. Handle |
| 3990 | // this case! |
| 3991 | Importer.Imported(D->getTemplatedDecl(), |
| 3992 | FoundTemplate->getTemplatedDecl()); |
| 3993 | return Importer.Imported(D, FoundTemplate); |
| 3994 | } |
| 3995 | } |
| 3996 | |
Douglas Gregor | 9e0a5b3 | 2011-10-15 00:10:27 +0000 | [diff] [blame] | 3997 | ConflictingDecls.push_back(FoundDecls[I]); |
Douglas Gregor | a082a49 | 2010-11-30 19:14:50 +0000 | [diff] [blame] | 3998 | } |
| 3999 | |
| 4000 | if (!ConflictingDecls.empty()) { |
| 4001 | Name = Importer.HandleNameConflict(Name, DC, Decl::IDNS_Ordinary, |
| 4002 | ConflictingDecls.data(), |
| 4003 | ConflictingDecls.size()); |
| 4004 | } |
| 4005 | |
| 4006 | if (!Name) |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 4007 | return nullptr; |
Douglas Gregor | a082a49 | 2010-11-30 19:14:50 +0000 | [diff] [blame] | 4008 | } |
| 4009 | |
| 4010 | CXXRecordDecl *DTemplated = D->getTemplatedDecl(); |
| 4011 | |
| 4012 | // Create the declaration that is being templated. |
Abramo Bagnara | 29c2d46 | 2011-03-09 14:09:51 +0000 | [diff] [blame] | 4013 | SourceLocation StartLoc = Importer.Import(DTemplated->getLocStart()); |
| 4014 | SourceLocation IdLoc = Importer.Import(DTemplated->getLocation()); |
Douglas Gregor | a082a49 | 2010-11-30 19:14:50 +0000 | [diff] [blame] | 4015 | CXXRecordDecl *D2Templated = CXXRecordDecl::Create(Importer.getToContext(), |
| 4016 | DTemplated->getTagKind(), |
Abramo Bagnara | 29c2d46 | 2011-03-09 14:09:51 +0000 | [diff] [blame] | 4017 | DC, StartLoc, IdLoc, |
| 4018 | Name.getAsIdentifierInfo()); |
Douglas Gregor | a082a49 | 2010-11-30 19:14:50 +0000 | [diff] [blame] | 4019 | D2Templated->setAccess(DTemplated->getAccess()); |
Douglas Gregor | 1445480 | 2011-02-25 02:25:35 +0000 | [diff] [blame] | 4020 | D2Templated->setQualifierInfo(Importer.Import(DTemplated->getQualifierLoc())); |
Douglas Gregor | a082a49 | 2010-11-30 19:14:50 +0000 | [diff] [blame] | 4021 | D2Templated->setLexicalDeclContext(LexicalDC); |
| 4022 | |
| 4023 | // Create the class template declaration itself. |
| 4024 | TemplateParameterList *TemplateParams |
| 4025 | = ImportTemplateParameterList(D->getTemplateParameters()); |
| 4026 | if (!TemplateParams) |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 4027 | return nullptr; |
| 4028 | |
Douglas Gregor | a082a49 | 2010-11-30 19:14:50 +0000 | [diff] [blame] | 4029 | ClassTemplateDecl *D2 = ClassTemplateDecl::Create(Importer.getToContext(), DC, |
| 4030 | Loc, Name, TemplateParams, |
| 4031 | D2Templated, |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 4032 | /*PrevDecl=*/nullptr); |
Douglas Gregor | a082a49 | 2010-11-30 19:14:50 +0000 | [diff] [blame] | 4033 | D2Templated->setDescribedClassTemplate(D2); |
| 4034 | |
| 4035 | D2->setAccess(D->getAccess()); |
| 4036 | D2->setLexicalDeclContext(LexicalDC); |
Sean Callanan | 95e74be | 2011-10-21 02:57:43 +0000 | [diff] [blame] | 4037 | LexicalDC->addDeclInternal(D2); |
Douglas Gregor | a082a49 | 2010-11-30 19:14:50 +0000 | [diff] [blame] | 4038 | |
| 4039 | // Note the relationship between the class templates. |
| 4040 | Importer.Imported(D, D2); |
| 4041 | Importer.Imported(DTemplated, D2Templated); |
| 4042 | |
John McCall | f937c02 | 2011-10-07 06:10:15 +0000 | [diff] [blame] | 4043 | if (DTemplated->isCompleteDefinition() && |
| 4044 | !D2Templated->isCompleteDefinition()) { |
Douglas Gregor | a082a49 | 2010-11-30 19:14:50 +0000 | [diff] [blame] | 4045 | // FIXME: Import definition! |
| 4046 | } |
| 4047 | |
| 4048 | return D2; |
| 4049 | } |
| 4050 | |
Douglas Gregor | e2e50d33 | 2010-12-01 01:36:18 +0000 | [diff] [blame] | 4051 | Decl *ASTNodeImporter::VisitClassTemplateSpecializationDecl( |
| 4052 | ClassTemplateSpecializationDecl *D) { |
| 4053 | // If this record has a definition in the translation unit we're coming from, |
| 4054 | // but this particular declaration is not that definition, import the |
| 4055 | // definition and map to that. |
| 4056 | TagDecl *Definition = D->getDefinition(); |
| 4057 | if (Definition && Definition != D) { |
| 4058 | Decl *ImportedDef = Importer.Import(Definition); |
| 4059 | if (!ImportedDef) |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 4060 | return nullptr; |
| 4061 | |
Douglas Gregor | e2e50d33 | 2010-12-01 01:36:18 +0000 | [diff] [blame] | 4062 | return Importer.Imported(D, ImportedDef); |
| 4063 | } |
| 4064 | |
| 4065 | ClassTemplateDecl *ClassTemplate |
| 4066 | = cast_or_null<ClassTemplateDecl>(Importer.Import( |
| 4067 | D->getSpecializedTemplate())); |
| 4068 | if (!ClassTemplate) |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 4069 | return nullptr; |
| 4070 | |
Douglas Gregor | e2e50d33 | 2010-12-01 01:36:18 +0000 | [diff] [blame] | 4071 | // Import the context of this declaration. |
| 4072 | DeclContext *DC = ClassTemplate->getDeclContext(); |
| 4073 | if (!DC) |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 4074 | return nullptr; |
| 4075 | |
Douglas Gregor | e2e50d33 | 2010-12-01 01:36:18 +0000 | [diff] [blame] | 4076 | DeclContext *LexicalDC = DC; |
| 4077 | if (D->getDeclContext() != D->getLexicalDeclContext()) { |
| 4078 | LexicalDC = Importer.ImportContext(D->getLexicalDeclContext()); |
| 4079 | if (!LexicalDC) |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 4080 | return nullptr; |
Douglas Gregor | e2e50d33 | 2010-12-01 01:36:18 +0000 | [diff] [blame] | 4081 | } |
| 4082 | |
| 4083 | // Import the location of this declaration. |
Abramo Bagnara | 29c2d46 | 2011-03-09 14:09:51 +0000 | [diff] [blame] | 4084 | SourceLocation StartLoc = Importer.Import(D->getLocStart()); |
| 4085 | SourceLocation IdLoc = Importer.Import(D->getLocation()); |
Douglas Gregor | e2e50d33 | 2010-12-01 01:36:18 +0000 | [diff] [blame] | 4086 | |
| 4087 | // Import template arguments. |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 4088 | SmallVector<TemplateArgument, 2> TemplateArgs; |
Douglas Gregor | e2e50d33 | 2010-12-01 01:36:18 +0000 | [diff] [blame] | 4089 | if (ImportTemplateArguments(D->getTemplateArgs().data(), |
| 4090 | D->getTemplateArgs().size(), |
| 4091 | TemplateArgs)) |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 4092 | return nullptr; |
| 4093 | |
Douglas Gregor | e2e50d33 | 2010-12-01 01:36:18 +0000 | [diff] [blame] | 4094 | // Try to find an existing specialization with these template arguments. |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 4095 | void *InsertPos = nullptr; |
Douglas Gregor | e2e50d33 | 2010-12-01 01:36:18 +0000 | [diff] [blame] | 4096 | ClassTemplateSpecializationDecl *D2 |
Craig Topper | 7e0daca | 2014-06-26 04:58:53 +0000 | [diff] [blame] | 4097 | = ClassTemplate->findSpecialization(TemplateArgs, InsertPos); |
Douglas Gregor | e2e50d33 | 2010-12-01 01:36:18 +0000 | [diff] [blame] | 4098 | if (D2) { |
| 4099 | // We already have a class template specialization with these template |
| 4100 | // arguments. |
| 4101 | |
| 4102 | // FIXME: Check for specialization vs. instantiation errors. |
| 4103 | |
| 4104 | if (RecordDecl *FoundDef = D2->getDefinition()) { |
John McCall | f937c02 | 2011-10-07 06:10:15 +0000 | [diff] [blame] | 4105 | if (!D->isCompleteDefinition() || IsStructuralMatch(D, FoundDef)) { |
Douglas Gregor | e2e50d33 | 2010-12-01 01:36:18 +0000 | [diff] [blame] | 4106 | // The record types structurally match, or the "from" translation |
| 4107 | // unit only had a forward declaration anyway; call it the same |
| 4108 | // function. |
| 4109 | return Importer.Imported(D, FoundDef); |
| 4110 | } |
| 4111 | } |
| 4112 | } else { |
| 4113 | // Create a new specialization. |
| 4114 | D2 = ClassTemplateSpecializationDecl::Create(Importer.getToContext(), |
| 4115 | D->getTagKind(), DC, |
Abramo Bagnara | 29c2d46 | 2011-03-09 14:09:51 +0000 | [diff] [blame] | 4116 | StartLoc, IdLoc, |
| 4117 | ClassTemplate, |
Douglas Gregor | e2e50d33 | 2010-12-01 01:36:18 +0000 | [diff] [blame] | 4118 | TemplateArgs.data(), |
| 4119 | TemplateArgs.size(), |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 4120 | /*PrevDecl=*/nullptr); |
Douglas Gregor | e2e50d33 | 2010-12-01 01:36:18 +0000 | [diff] [blame] | 4121 | D2->setSpecializationKind(D->getSpecializationKind()); |
| 4122 | |
| 4123 | // Add this specialization to the class template. |
| 4124 | ClassTemplate->AddSpecialization(D2, InsertPos); |
| 4125 | |
| 4126 | // Import the qualifier, if any. |
Douglas Gregor | 1445480 | 2011-02-25 02:25:35 +0000 | [diff] [blame] | 4127 | D2->setQualifierInfo(Importer.Import(D->getQualifierLoc())); |
Douglas Gregor | e2e50d33 | 2010-12-01 01:36:18 +0000 | [diff] [blame] | 4128 | |
| 4129 | // Add the specialization to this context. |
| 4130 | D2->setLexicalDeclContext(LexicalDC); |
Sean Callanan | 95e74be | 2011-10-21 02:57:43 +0000 | [diff] [blame] | 4131 | LexicalDC->addDeclInternal(D2); |
Douglas Gregor | e2e50d33 | 2010-12-01 01:36:18 +0000 | [diff] [blame] | 4132 | } |
| 4133 | Importer.Imported(D, D2); |
| 4134 | |
John McCall | f937c02 | 2011-10-07 06:10:15 +0000 | [diff] [blame] | 4135 | if (D->isCompleteDefinition() && ImportDefinition(D, D2)) |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 4136 | return nullptr; |
| 4137 | |
Douglas Gregor | e2e50d33 | 2010-12-01 01:36:18 +0000 | [diff] [blame] | 4138 | return D2; |
| 4139 | } |
| 4140 | |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 4141 | Decl *ASTNodeImporter::VisitVarTemplateDecl(VarTemplateDecl *D) { |
| 4142 | // If this variable has a definition in the translation unit we're coming |
| 4143 | // from, |
| 4144 | // but this particular declaration is not that definition, import the |
| 4145 | // definition and map to that. |
| 4146 | VarDecl *Definition = |
| 4147 | cast_or_null<VarDecl>(D->getTemplatedDecl()->getDefinition()); |
| 4148 | if (Definition && Definition != D->getTemplatedDecl()) { |
| 4149 | Decl *ImportedDef = Importer.Import(Definition->getDescribedVarTemplate()); |
| 4150 | if (!ImportedDef) |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 4151 | return nullptr; |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 4152 | |
| 4153 | return Importer.Imported(D, ImportedDef); |
| 4154 | } |
| 4155 | |
| 4156 | // Import the major distinguishing characteristics of this variable template. |
| 4157 | DeclContext *DC, *LexicalDC; |
| 4158 | DeclarationName Name; |
| 4159 | SourceLocation Loc; |
| 4160 | if (ImportDeclParts(D, DC, LexicalDC, Name, Loc)) |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 4161 | return nullptr; |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 4162 | |
| 4163 | // We may already have a template of the same name; try to find and match it. |
| 4164 | assert(!DC->isFunctionOrMethod() && |
| 4165 | "Variable templates cannot be declared at function scope"); |
| 4166 | SmallVector<NamedDecl *, 4> ConflictingDecls; |
| 4167 | SmallVector<NamedDecl *, 2> FoundDecls; |
| 4168 | DC->localUncachedLookup(Name, FoundDecls); |
| 4169 | for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) { |
| 4170 | if (!FoundDecls[I]->isInIdentifierNamespace(Decl::IDNS_Ordinary)) |
| 4171 | continue; |
| 4172 | |
| 4173 | Decl *Found = FoundDecls[I]; |
| 4174 | if (VarTemplateDecl *FoundTemplate = dyn_cast<VarTemplateDecl>(Found)) { |
| 4175 | if (IsStructuralMatch(D, FoundTemplate)) { |
| 4176 | // The variable templates structurally match; call it the same template. |
| 4177 | Importer.Imported(D->getTemplatedDecl(), |
| 4178 | FoundTemplate->getTemplatedDecl()); |
| 4179 | return Importer.Imported(D, FoundTemplate); |
| 4180 | } |
| 4181 | } |
| 4182 | |
| 4183 | ConflictingDecls.push_back(FoundDecls[I]); |
| 4184 | } |
| 4185 | |
| 4186 | if (!ConflictingDecls.empty()) { |
| 4187 | Name = Importer.HandleNameConflict(Name, DC, Decl::IDNS_Ordinary, |
| 4188 | ConflictingDecls.data(), |
| 4189 | ConflictingDecls.size()); |
| 4190 | } |
| 4191 | |
| 4192 | if (!Name) |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 4193 | return nullptr; |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 4194 | |
| 4195 | VarDecl *DTemplated = D->getTemplatedDecl(); |
| 4196 | |
| 4197 | // Import the type. |
| 4198 | QualType T = Importer.Import(DTemplated->getType()); |
| 4199 | if (T.isNull()) |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 4200 | return nullptr; |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 4201 | |
| 4202 | // Create the declaration that is being templated. |
| 4203 | SourceLocation StartLoc = Importer.Import(DTemplated->getLocStart()); |
| 4204 | SourceLocation IdLoc = Importer.Import(DTemplated->getLocation()); |
| 4205 | TypeSourceInfo *TInfo = Importer.Import(DTemplated->getTypeSourceInfo()); |
| 4206 | VarDecl *D2Templated = VarDecl::Create(Importer.getToContext(), DC, StartLoc, |
| 4207 | IdLoc, Name.getAsIdentifierInfo(), T, |
| 4208 | TInfo, DTemplated->getStorageClass()); |
| 4209 | D2Templated->setAccess(DTemplated->getAccess()); |
| 4210 | D2Templated->setQualifierInfo(Importer.Import(DTemplated->getQualifierLoc())); |
| 4211 | D2Templated->setLexicalDeclContext(LexicalDC); |
| 4212 | |
| 4213 | // Importer.Imported(DTemplated, D2Templated); |
| 4214 | // LexicalDC->addDeclInternal(D2Templated); |
| 4215 | |
| 4216 | // Merge the initializer. |
| 4217 | if (ImportDefinition(DTemplated, D2Templated)) |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 4218 | return nullptr; |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 4219 | |
| 4220 | // Create the variable template declaration itself. |
| 4221 | TemplateParameterList *TemplateParams = |
| 4222 | ImportTemplateParameterList(D->getTemplateParameters()); |
| 4223 | if (!TemplateParams) |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 4224 | return nullptr; |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 4225 | |
| 4226 | VarTemplateDecl *D2 = VarTemplateDecl::Create( |
Richard Smith | beef345 | 2014-01-16 23:39:20 +0000 | [diff] [blame] | 4227 | Importer.getToContext(), DC, Loc, Name, TemplateParams, D2Templated); |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 4228 | D2Templated->setDescribedVarTemplate(D2); |
| 4229 | |
| 4230 | D2->setAccess(D->getAccess()); |
| 4231 | D2->setLexicalDeclContext(LexicalDC); |
| 4232 | LexicalDC->addDeclInternal(D2); |
| 4233 | |
| 4234 | // Note the relationship between the variable templates. |
| 4235 | Importer.Imported(D, D2); |
| 4236 | Importer.Imported(DTemplated, D2Templated); |
| 4237 | |
| 4238 | if (DTemplated->isThisDeclarationADefinition() && |
| 4239 | !D2Templated->isThisDeclarationADefinition()) { |
| 4240 | // FIXME: Import definition! |
| 4241 | } |
| 4242 | |
| 4243 | return D2; |
| 4244 | } |
| 4245 | |
| 4246 | Decl *ASTNodeImporter::VisitVarTemplateSpecializationDecl( |
| 4247 | VarTemplateSpecializationDecl *D) { |
| 4248 | // If this record has a definition in the translation unit we're coming from, |
| 4249 | // but this particular declaration is not that definition, import the |
| 4250 | // definition and map to that. |
| 4251 | VarDecl *Definition = D->getDefinition(); |
| 4252 | if (Definition && Definition != D) { |
| 4253 | Decl *ImportedDef = Importer.Import(Definition); |
| 4254 | if (!ImportedDef) |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 4255 | return nullptr; |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 4256 | |
| 4257 | return Importer.Imported(D, ImportedDef); |
| 4258 | } |
| 4259 | |
| 4260 | VarTemplateDecl *VarTemplate = cast_or_null<VarTemplateDecl>( |
| 4261 | Importer.Import(D->getSpecializedTemplate())); |
| 4262 | if (!VarTemplate) |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 4263 | return nullptr; |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 4264 | |
| 4265 | // Import the context of this declaration. |
| 4266 | DeclContext *DC = VarTemplate->getDeclContext(); |
| 4267 | if (!DC) |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 4268 | return nullptr; |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 4269 | |
| 4270 | DeclContext *LexicalDC = DC; |
| 4271 | if (D->getDeclContext() != D->getLexicalDeclContext()) { |
| 4272 | LexicalDC = Importer.ImportContext(D->getLexicalDeclContext()); |
| 4273 | if (!LexicalDC) |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 4274 | return nullptr; |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 4275 | } |
| 4276 | |
| 4277 | // Import the location of this declaration. |
| 4278 | SourceLocation StartLoc = Importer.Import(D->getLocStart()); |
| 4279 | SourceLocation IdLoc = Importer.Import(D->getLocation()); |
| 4280 | |
| 4281 | // Import template arguments. |
| 4282 | SmallVector<TemplateArgument, 2> TemplateArgs; |
| 4283 | if (ImportTemplateArguments(D->getTemplateArgs().data(), |
| 4284 | D->getTemplateArgs().size(), TemplateArgs)) |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 4285 | return nullptr; |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 4286 | |
| 4287 | // Try to find an existing specialization with these template arguments. |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 4288 | void *InsertPos = nullptr; |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 4289 | VarTemplateSpecializationDecl *D2 = VarTemplate->findSpecialization( |
Craig Topper | 7e0daca | 2014-06-26 04:58:53 +0000 | [diff] [blame] | 4290 | TemplateArgs, InsertPos); |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 4291 | if (D2) { |
| 4292 | // We already have a variable template specialization with these template |
| 4293 | // arguments. |
| 4294 | |
| 4295 | // FIXME: Check for specialization vs. instantiation errors. |
| 4296 | |
| 4297 | if (VarDecl *FoundDef = D2->getDefinition()) { |
| 4298 | if (!D->isThisDeclarationADefinition() || |
| 4299 | IsStructuralMatch(D, FoundDef)) { |
| 4300 | // The record types structurally match, or the "from" translation |
| 4301 | // unit only had a forward declaration anyway; call it the same |
| 4302 | // variable. |
| 4303 | return Importer.Imported(D, FoundDef); |
| 4304 | } |
| 4305 | } |
| 4306 | } else { |
| 4307 | |
| 4308 | // Import the type. |
| 4309 | QualType T = Importer.Import(D->getType()); |
| 4310 | if (T.isNull()) |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 4311 | return nullptr; |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 4312 | TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo()); |
| 4313 | |
| 4314 | // Create a new specialization. |
| 4315 | D2 = VarTemplateSpecializationDecl::Create( |
| 4316 | Importer.getToContext(), DC, StartLoc, IdLoc, VarTemplate, T, TInfo, |
| 4317 | D->getStorageClass(), TemplateArgs.data(), TemplateArgs.size()); |
| 4318 | D2->setSpecializationKind(D->getSpecializationKind()); |
| 4319 | D2->setTemplateArgsInfo(D->getTemplateArgsInfo()); |
| 4320 | |
| 4321 | // Add this specialization to the class template. |
| 4322 | VarTemplate->AddSpecialization(D2, InsertPos); |
| 4323 | |
| 4324 | // Import the qualifier, if any. |
| 4325 | D2->setQualifierInfo(Importer.Import(D->getQualifierLoc())); |
| 4326 | |
| 4327 | // Add the specialization to this context. |
| 4328 | D2->setLexicalDeclContext(LexicalDC); |
| 4329 | LexicalDC->addDeclInternal(D2); |
| 4330 | } |
| 4331 | Importer.Imported(D, D2); |
| 4332 | |
| 4333 | if (D->isThisDeclarationADefinition() && ImportDefinition(D, D2)) |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 4334 | return nullptr; |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 4335 | |
| 4336 | return D2; |
| 4337 | } |
| 4338 | |
Douglas Gregor | 7eeb597 | 2010-02-11 19:21:55 +0000 | [diff] [blame] | 4339 | //---------------------------------------------------------------------------- |
| 4340 | // Import Statements |
| 4341 | //---------------------------------------------------------------------------- |
| 4342 | |
| 4343 | Stmt *ASTNodeImporter::VisitStmt(Stmt *S) { |
| 4344 | Importer.FromDiag(S->getLocStart(), diag::err_unsupported_ast_node) |
| 4345 | << S->getStmtClassName(); |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 4346 | return nullptr; |
Douglas Gregor | 7eeb597 | 2010-02-11 19:21:55 +0000 | [diff] [blame] | 4347 | } |
| 4348 | |
| 4349 | //---------------------------------------------------------------------------- |
| 4350 | // Import Expressions |
| 4351 | //---------------------------------------------------------------------------- |
| 4352 | Expr *ASTNodeImporter::VisitExpr(Expr *E) { |
| 4353 | Importer.FromDiag(E->getLocStart(), diag::err_unsupported_ast_node) |
| 4354 | << E->getStmtClassName(); |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 4355 | return nullptr; |
Douglas Gregor | 7eeb597 | 2010-02-11 19:21:55 +0000 | [diff] [blame] | 4356 | } |
| 4357 | |
Douglas Gregor | 52f820e | 2010-02-19 01:17:02 +0000 | [diff] [blame] | 4358 | Expr *ASTNodeImporter::VisitDeclRefExpr(DeclRefExpr *E) { |
Douglas Gregor | 52f820e | 2010-02-19 01:17:02 +0000 | [diff] [blame] | 4359 | ValueDecl *ToD = cast_or_null<ValueDecl>(Importer.Import(E->getDecl())); |
| 4360 | if (!ToD) |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 4361 | return nullptr; |
Chandler Carruth | 8d26bb0 | 2011-05-01 23:48:14 +0000 | [diff] [blame] | 4362 | |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 4363 | NamedDecl *FoundD = nullptr; |
Chandler Carruth | 8d26bb0 | 2011-05-01 23:48:14 +0000 | [diff] [blame] | 4364 | if (E->getDecl() != E->getFoundDecl()) { |
| 4365 | FoundD = cast_or_null<NamedDecl>(Importer.Import(E->getFoundDecl())); |
| 4366 | if (!FoundD) |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 4367 | return nullptr; |
Chandler Carruth | 8d26bb0 | 2011-05-01 23:48:14 +0000 | [diff] [blame] | 4368 | } |
Douglas Gregor | 52f820e | 2010-02-19 01:17:02 +0000 | [diff] [blame] | 4369 | |
| 4370 | QualType T = Importer.Import(E->getType()); |
| 4371 | if (T.isNull()) |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 4372 | return nullptr; |
Abramo Bagnara | 635ed24e | 2011-10-05 07:56:41 +0000 | [diff] [blame] | 4373 | |
| 4374 | DeclRefExpr *DRE = DeclRefExpr::Create(Importer.getToContext(), |
| 4375 | Importer.Import(E->getQualifierLoc()), |
Abramo Bagnara | 7945c98 | 2012-01-27 09:46:47 +0000 | [diff] [blame] | 4376 | Importer.Import(E->getTemplateKeywordLoc()), |
Abramo Bagnara | 635ed24e | 2011-10-05 07:56:41 +0000 | [diff] [blame] | 4377 | ToD, |
John McCall | 113bee0 | 2012-03-10 09:33:50 +0000 | [diff] [blame] | 4378 | E->refersToEnclosingLocal(), |
Abramo Bagnara | 635ed24e | 2011-10-05 07:56:41 +0000 | [diff] [blame] | 4379 | Importer.Import(E->getLocation()), |
| 4380 | T, E->getValueKind(), |
| 4381 | FoundD, |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 4382 | /*FIXME:TemplateArgs=*/nullptr); |
Abramo Bagnara | 635ed24e | 2011-10-05 07:56:41 +0000 | [diff] [blame] | 4383 | if (E->hadMultipleCandidates()) |
| 4384 | DRE->setHadMultipleCandidates(true); |
| 4385 | return DRE; |
Douglas Gregor | 52f820e | 2010-02-19 01:17:02 +0000 | [diff] [blame] | 4386 | } |
| 4387 | |
Douglas Gregor | 7eeb597 | 2010-02-11 19:21:55 +0000 | [diff] [blame] | 4388 | Expr *ASTNodeImporter::VisitIntegerLiteral(IntegerLiteral *E) { |
| 4389 | QualType T = Importer.Import(E->getType()); |
| 4390 | if (T.isNull()) |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 4391 | return nullptr; |
Douglas Gregor | 7eeb597 | 2010-02-11 19:21:55 +0000 | [diff] [blame] | 4392 | |
Argyrios Kyrtzidis | 43b2057 | 2010-08-28 09:06:06 +0000 | [diff] [blame] | 4393 | return IntegerLiteral::Create(Importer.getToContext(), |
| 4394 | E->getValue(), T, |
| 4395 | Importer.Import(E->getLocation())); |
Douglas Gregor | 7eeb597 | 2010-02-11 19:21:55 +0000 | [diff] [blame] | 4396 | } |
| 4397 | |
Douglas Gregor | 623421d | 2010-02-18 02:21:22 +0000 | [diff] [blame] | 4398 | Expr *ASTNodeImporter::VisitCharacterLiteral(CharacterLiteral *E) { |
| 4399 | QualType T = Importer.Import(E->getType()); |
| 4400 | if (T.isNull()) |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 4401 | return nullptr; |
| 4402 | |
Douglas Gregor | fb65e59 | 2011-07-27 05:40:30 +0000 | [diff] [blame] | 4403 | return new (Importer.getToContext()) CharacterLiteral(E->getValue(), |
| 4404 | E->getKind(), T, |
Douglas Gregor | 623421d | 2010-02-18 02:21:22 +0000 | [diff] [blame] | 4405 | Importer.Import(E->getLocation())); |
| 4406 | } |
| 4407 | |
Douglas Gregor | c74247e | 2010-02-19 01:07:06 +0000 | [diff] [blame] | 4408 | Expr *ASTNodeImporter::VisitParenExpr(ParenExpr *E) { |
| 4409 | Expr *SubExpr = Importer.Import(E->getSubExpr()); |
| 4410 | if (!SubExpr) |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 4411 | return nullptr; |
| 4412 | |
Douglas Gregor | c74247e | 2010-02-19 01:07:06 +0000 | [diff] [blame] | 4413 | return new (Importer.getToContext()) |
| 4414 | ParenExpr(Importer.Import(E->getLParen()), |
| 4415 | Importer.Import(E->getRParen()), |
| 4416 | SubExpr); |
| 4417 | } |
| 4418 | |
| 4419 | Expr *ASTNodeImporter::VisitUnaryOperator(UnaryOperator *E) { |
| 4420 | QualType T = Importer.Import(E->getType()); |
| 4421 | if (T.isNull()) |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 4422 | return nullptr; |
Douglas Gregor | c74247e | 2010-02-19 01:07:06 +0000 | [diff] [blame] | 4423 | |
| 4424 | Expr *SubExpr = Importer.Import(E->getSubExpr()); |
| 4425 | if (!SubExpr) |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 4426 | return nullptr; |
| 4427 | |
Douglas Gregor | c74247e | 2010-02-19 01:07:06 +0000 | [diff] [blame] | 4428 | return new (Importer.getToContext()) UnaryOperator(SubExpr, E->getOpcode(), |
John McCall | 7decc9e | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 4429 | T, E->getValueKind(), |
| 4430 | E->getObjectKind(), |
Douglas Gregor | c74247e | 2010-02-19 01:07:06 +0000 | [diff] [blame] | 4431 | Importer.Import(E->getOperatorLoc())); |
| 4432 | } |
| 4433 | |
Peter Collingbourne | e190dee | 2011-03-11 19:24:49 +0000 | [diff] [blame] | 4434 | Expr *ASTNodeImporter::VisitUnaryExprOrTypeTraitExpr( |
| 4435 | UnaryExprOrTypeTraitExpr *E) { |
Douglas Gregor | d8552cd | 2010-02-19 01:24:23 +0000 | [diff] [blame] | 4436 | QualType ResultType = Importer.Import(E->getType()); |
| 4437 | |
| 4438 | if (E->isArgumentType()) { |
| 4439 | TypeSourceInfo *TInfo = Importer.Import(E->getArgumentTypeInfo()); |
| 4440 | if (!TInfo) |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 4441 | return nullptr; |
| 4442 | |
Peter Collingbourne | e190dee | 2011-03-11 19:24:49 +0000 | [diff] [blame] | 4443 | return new (Importer.getToContext()) UnaryExprOrTypeTraitExpr(E->getKind(), |
| 4444 | TInfo, ResultType, |
Douglas Gregor | d8552cd | 2010-02-19 01:24:23 +0000 | [diff] [blame] | 4445 | Importer.Import(E->getOperatorLoc()), |
| 4446 | Importer.Import(E->getRParenLoc())); |
| 4447 | } |
| 4448 | |
| 4449 | Expr *SubExpr = Importer.Import(E->getArgumentExpr()); |
| 4450 | if (!SubExpr) |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 4451 | return nullptr; |
| 4452 | |
Peter Collingbourne | e190dee | 2011-03-11 19:24:49 +0000 | [diff] [blame] | 4453 | return new (Importer.getToContext()) UnaryExprOrTypeTraitExpr(E->getKind(), |
| 4454 | SubExpr, ResultType, |
Douglas Gregor | d8552cd | 2010-02-19 01:24:23 +0000 | [diff] [blame] | 4455 | Importer.Import(E->getOperatorLoc()), |
| 4456 | Importer.Import(E->getRParenLoc())); |
| 4457 | } |
| 4458 | |
Douglas Gregor | c74247e | 2010-02-19 01:07:06 +0000 | [diff] [blame] | 4459 | Expr *ASTNodeImporter::VisitBinaryOperator(BinaryOperator *E) { |
| 4460 | QualType T = Importer.Import(E->getType()); |
| 4461 | if (T.isNull()) |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 4462 | return nullptr; |
Douglas Gregor | c74247e | 2010-02-19 01:07:06 +0000 | [diff] [blame] | 4463 | |
| 4464 | Expr *LHS = Importer.Import(E->getLHS()); |
| 4465 | if (!LHS) |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 4466 | return nullptr; |
| 4467 | |
Douglas Gregor | c74247e | 2010-02-19 01:07:06 +0000 | [diff] [blame] | 4468 | Expr *RHS = Importer.Import(E->getRHS()); |
| 4469 | if (!RHS) |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 4470 | return nullptr; |
| 4471 | |
Douglas Gregor | c74247e | 2010-02-19 01:07:06 +0000 | [diff] [blame] | 4472 | return new (Importer.getToContext()) BinaryOperator(LHS, RHS, E->getOpcode(), |
John McCall | 7decc9e | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 4473 | T, E->getValueKind(), |
| 4474 | E->getObjectKind(), |
Lang Hames | 5de91cc | 2012-10-02 04:45:10 +0000 | [diff] [blame] | 4475 | Importer.Import(E->getOperatorLoc()), |
| 4476 | E->isFPContractable()); |
Douglas Gregor | c74247e | 2010-02-19 01:07:06 +0000 | [diff] [blame] | 4477 | } |
| 4478 | |
| 4479 | Expr *ASTNodeImporter::VisitCompoundAssignOperator(CompoundAssignOperator *E) { |
| 4480 | QualType T = Importer.Import(E->getType()); |
| 4481 | if (T.isNull()) |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 4482 | return nullptr; |
| 4483 | |
Douglas Gregor | c74247e | 2010-02-19 01:07:06 +0000 | [diff] [blame] | 4484 | QualType CompLHSType = Importer.Import(E->getComputationLHSType()); |
| 4485 | if (CompLHSType.isNull()) |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 4486 | return nullptr; |
| 4487 | |
Douglas Gregor | c74247e | 2010-02-19 01:07:06 +0000 | [diff] [blame] | 4488 | QualType CompResultType = Importer.Import(E->getComputationResultType()); |
| 4489 | if (CompResultType.isNull()) |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 4490 | return nullptr; |
| 4491 | |
Douglas Gregor | c74247e | 2010-02-19 01:07:06 +0000 | [diff] [blame] | 4492 | Expr *LHS = Importer.Import(E->getLHS()); |
| 4493 | if (!LHS) |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 4494 | return nullptr; |
| 4495 | |
Douglas Gregor | c74247e | 2010-02-19 01:07:06 +0000 | [diff] [blame] | 4496 | Expr *RHS = Importer.Import(E->getRHS()); |
| 4497 | if (!RHS) |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 4498 | return nullptr; |
| 4499 | |
Douglas Gregor | c74247e | 2010-02-19 01:07:06 +0000 | [diff] [blame] | 4500 | return new (Importer.getToContext()) |
| 4501 | CompoundAssignOperator(LHS, RHS, E->getOpcode(), |
John McCall | 7decc9e | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 4502 | T, E->getValueKind(), |
| 4503 | E->getObjectKind(), |
| 4504 | CompLHSType, CompResultType, |
Lang Hames | 5de91cc | 2012-10-02 04:45:10 +0000 | [diff] [blame] | 4505 | Importer.Import(E->getOperatorLoc()), |
| 4506 | E->isFPContractable()); |
Douglas Gregor | c74247e | 2010-02-19 01:07:06 +0000 | [diff] [blame] | 4507 | } |
| 4508 | |
Benjamin Kramer | 8aef596 | 2011-03-26 12:38:21 +0000 | [diff] [blame] | 4509 | static bool ImportCastPath(CastExpr *E, CXXCastPath &Path) { |
John McCall | cf14216 | 2010-08-07 06:22:56 +0000 | [diff] [blame] | 4510 | if (E->path_empty()) return false; |
| 4511 | |
| 4512 | // TODO: import cast paths |
| 4513 | return true; |
| 4514 | } |
| 4515 | |
Douglas Gregor | 98c1018 | 2010-02-12 22:17:39 +0000 | [diff] [blame] | 4516 | Expr *ASTNodeImporter::VisitImplicitCastExpr(ImplicitCastExpr *E) { |
| 4517 | QualType T = Importer.Import(E->getType()); |
| 4518 | if (T.isNull()) |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 4519 | return nullptr; |
Douglas Gregor | 98c1018 | 2010-02-12 22:17:39 +0000 | [diff] [blame] | 4520 | |
| 4521 | Expr *SubExpr = Importer.Import(E->getSubExpr()); |
| 4522 | if (!SubExpr) |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 4523 | return nullptr; |
John McCall | cf14216 | 2010-08-07 06:22:56 +0000 | [diff] [blame] | 4524 | |
| 4525 | CXXCastPath BasePath; |
| 4526 | if (ImportCastPath(E, BasePath)) |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 4527 | return nullptr; |
John McCall | cf14216 | 2010-08-07 06:22:56 +0000 | [diff] [blame] | 4528 | |
| 4529 | return ImplicitCastExpr::Create(Importer.getToContext(), T, E->getCastKind(), |
John McCall | 2536c6d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 4530 | SubExpr, &BasePath, E->getValueKind()); |
Douglas Gregor | 98c1018 | 2010-02-12 22:17:39 +0000 | [diff] [blame] | 4531 | } |
| 4532 | |
Douglas Gregor | 5481d32 | 2010-02-19 01:32:14 +0000 | [diff] [blame] | 4533 | Expr *ASTNodeImporter::VisitCStyleCastExpr(CStyleCastExpr *E) { |
| 4534 | QualType T = Importer.Import(E->getType()); |
| 4535 | if (T.isNull()) |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 4536 | return nullptr; |
| 4537 | |
Douglas Gregor | 5481d32 | 2010-02-19 01:32:14 +0000 | [diff] [blame] | 4538 | Expr *SubExpr = Importer.Import(E->getSubExpr()); |
| 4539 | if (!SubExpr) |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 4540 | return nullptr; |
Douglas Gregor | 5481d32 | 2010-02-19 01:32:14 +0000 | [diff] [blame] | 4541 | |
| 4542 | TypeSourceInfo *TInfo = Importer.Import(E->getTypeInfoAsWritten()); |
| 4543 | if (!TInfo && E->getTypeInfoAsWritten()) |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 4544 | return nullptr; |
| 4545 | |
John McCall | cf14216 | 2010-08-07 06:22:56 +0000 | [diff] [blame] | 4546 | CXXCastPath BasePath; |
| 4547 | if (ImportCastPath(E, BasePath)) |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 4548 | return nullptr; |
John McCall | cf14216 | 2010-08-07 06:22:56 +0000 | [diff] [blame] | 4549 | |
John McCall | 7decc9e | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 4550 | return CStyleCastExpr::Create(Importer.getToContext(), T, |
| 4551 | E->getValueKind(), E->getCastKind(), |
John McCall | cf14216 | 2010-08-07 06:22:56 +0000 | [diff] [blame] | 4552 | SubExpr, &BasePath, TInfo, |
| 4553 | Importer.Import(E->getLParenLoc()), |
| 4554 | Importer.Import(E->getRParenLoc())); |
Douglas Gregor | 5481d32 | 2010-02-19 01:32:14 +0000 | [diff] [blame] | 4555 | } |
| 4556 | |
Argyrios Kyrtzidis | d004064 | 2010-11-18 20:06:41 +0000 | [diff] [blame] | 4557 | ASTImporter::ASTImporter(ASTContext &ToContext, FileManager &ToFileManager, |
Douglas Gregor | 0a79167 | 2011-01-18 03:11:38 +0000 | [diff] [blame] | 4558 | ASTContext &FromContext, FileManager &FromFileManager, |
| 4559 | bool MinimalImport) |
Douglas Gregor | 96e578d | 2010-02-05 17:54:41 +0000 | [diff] [blame] | 4560 | : ToContext(ToContext), FromContext(FromContext), |
Douglas Gregor | 0a79167 | 2011-01-18 03:11:38 +0000 | [diff] [blame] | 4561 | ToFileManager(ToFileManager), FromFileManager(FromFileManager), |
Richard Smith | 5bb4cdf | 2012-12-20 02:22:15 +0000 | [diff] [blame] | 4562 | Minimal(MinimalImport), LastDiagFromFrom(false) |
Douglas Gregor | 0a79167 | 2011-01-18 03:11:38 +0000 | [diff] [blame] | 4563 | { |
Douglas Gregor | 62d311f | 2010-02-09 19:21:46 +0000 | [diff] [blame] | 4564 | ImportedDecls[FromContext.getTranslationUnitDecl()] |
| 4565 | = ToContext.getTranslationUnitDecl(); |
| 4566 | } |
| 4567 | |
| 4568 | ASTImporter::~ASTImporter() { } |
Douglas Gregor | 96e578d | 2010-02-05 17:54:41 +0000 | [diff] [blame] | 4569 | |
| 4570 | QualType ASTImporter::Import(QualType FromT) { |
| 4571 | if (FromT.isNull()) |
| 4572 | return QualType(); |
John McCall | 424cec9 | 2011-01-19 06:33:43 +0000 | [diff] [blame] | 4573 | |
| 4574 | const Type *fromTy = FromT.getTypePtr(); |
Douglas Gregor | 96e578d | 2010-02-05 17:54:41 +0000 | [diff] [blame] | 4575 | |
Douglas Gregor | f65bbb3 | 2010-02-08 15:18:58 +0000 | [diff] [blame] | 4576 | // Check whether we've already imported this type. |
John McCall | 424cec9 | 2011-01-19 06:33:43 +0000 | [diff] [blame] | 4577 | llvm::DenseMap<const Type *, const Type *>::iterator Pos |
| 4578 | = ImportedTypes.find(fromTy); |
Douglas Gregor | f65bbb3 | 2010-02-08 15:18:58 +0000 | [diff] [blame] | 4579 | if (Pos != ImportedTypes.end()) |
John McCall | 424cec9 | 2011-01-19 06:33:43 +0000 | [diff] [blame] | 4580 | return ToContext.getQualifiedType(Pos->second, FromT.getLocalQualifiers()); |
Douglas Gregor | 96e578d | 2010-02-05 17:54:41 +0000 | [diff] [blame] | 4581 | |
Douglas Gregor | f65bbb3 | 2010-02-08 15:18:58 +0000 | [diff] [blame] | 4582 | // Import the type |
Douglas Gregor | 96e578d | 2010-02-05 17:54:41 +0000 | [diff] [blame] | 4583 | ASTNodeImporter Importer(*this); |
John McCall | 424cec9 | 2011-01-19 06:33:43 +0000 | [diff] [blame] | 4584 | QualType ToT = Importer.Visit(fromTy); |
Douglas Gregor | 96e578d | 2010-02-05 17:54:41 +0000 | [diff] [blame] | 4585 | if (ToT.isNull()) |
| 4586 | return ToT; |
| 4587 | |
Douglas Gregor | f65bbb3 | 2010-02-08 15:18:58 +0000 | [diff] [blame] | 4588 | // Record the imported type. |
John McCall | 424cec9 | 2011-01-19 06:33:43 +0000 | [diff] [blame] | 4589 | ImportedTypes[fromTy] = ToT.getTypePtr(); |
Douglas Gregor | f65bbb3 | 2010-02-08 15:18:58 +0000 | [diff] [blame] | 4590 | |
John McCall | 424cec9 | 2011-01-19 06:33:43 +0000 | [diff] [blame] | 4591 | return ToContext.getQualifiedType(ToT, FromT.getLocalQualifiers()); |
Douglas Gregor | 96e578d | 2010-02-05 17:54:41 +0000 | [diff] [blame] | 4592 | } |
| 4593 | |
Douglas Gregor | 62d311f | 2010-02-09 19:21:46 +0000 | [diff] [blame] | 4594 | TypeSourceInfo *ASTImporter::Import(TypeSourceInfo *FromTSI) { |
Douglas Gregor | fa7a0e5 | 2010-02-10 17:47:19 +0000 | [diff] [blame] | 4595 | if (!FromTSI) |
| 4596 | return FromTSI; |
| 4597 | |
| 4598 | // FIXME: For now we just create a "trivial" type source info based |
Nick Lewycky | 19b9f95 | 2010-07-26 16:56:01 +0000 | [diff] [blame] | 4599 | // on the type and a single location. Implement a real version of this. |
Douglas Gregor | fa7a0e5 | 2010-02-10 17:47:19 +0000 | [diff] [blame] | 4600 | QualType T = Import(FromTSI->getType()); |
| 4601 | if (T.isNull()) |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 4602 | return nullptr; |
Douglas Gregor | fa7a0e5 | 2010-02-10 17:47:19 +0000 | [diff] [blame] | 4603 | |
| 4604 | return ToContext.getTrivialTypeSourceInfo(T, |
Daniel Dunbar | 62ee641 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 4605 | FromTSI->getTypeLoc().getLocStart()); |
Douglas Gregor | 62d311f | 2010-02-09 19:21:46 +0000 | [diff] [blame] | 4606 | } |
| 4607 | |
| 4608 | Decl *ASTImporter::Import(Decl *FromD) { |
| 4609 | if (!FromD) |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 4610 | return nullptr; |
Douglas Gregor | 62d311f | 2010-02-09 19:21:46 +0000 | [diff] [blame] | 4611 | |
Douglas Gregor | d451ea9 | 2011-07-29 23:31:30 +0000 | [diff] [blame] | 4612 | ASTNodeImporter Importer(*this); |
| 4613 | |
Douglas Gregor | 62d311f | 2010-02-09 19:21:46 +0000 | [diff] [blame] | 4614 | // Check whether we've already imported this declaration. |
| 4615 | llvm::DenseMap<Decl *, Decl *>::iterator Pos = ImportedDecls.find(FromD); |
Douglas Gregor | d451ea9 | 2011-07-29 23:31:30 +0000 | [diff] [blame] | 4616 | if (Pos != ImportedDecls.end()) { |
| 4617 | Decl *ToD = Pos->second; |
| 4618 | Importer.ImportDefinitionIfNeeded(FromD, ToD); |
| 4619 | return ToD; |
| 4620 | } |
Douglas Gregor | 62d311f | 2010-02-09 19:21:46 +0000 | [diff] [blame] | 4621 | |
| 4622 | // Import the type |
Douglas Gregor | 62d311f | 2010-02-09 19:21:46 +0000 | [diff] [blame] | 4623 | Decl *ToD = Importer.Visit(FromD); |
| 4624 | if (!ToD) |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 4625 | return nullptr; |
| 4626 | |
Douglas Gregor | 62d311f | 2010-02-09 19:21:46 +0000 | [diff] [blame] | 4627 | // Record the imported declaration. |
| 4628 | ImportedDecls[FromD] = ToD; |
Douglas Gregor | b4964f7 | 2010-02-15 23:54:17 +0000 | [diff] [blame] | 4629 | |
| 4630 | if (TagDecl *FromTag = dyn_cast<TagDecl>(FromD)) { |
| 4631 | // Keep track of anonymous tags that have an associated typedef. |
Richard Smith | dda56e4 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 4632 | if (FromTag->getTypedefNameForAnonDecl()) |
Douglas Gregor | b4964f7 | 2010-02-15 23:54:17 +0000 | [diff] [blame] | 4633 | AnonTagsWithPendingTypedefs.push_back(FromTag); |
Richard Smith | dda56e4 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 4634 | } else if (TypedefNameDecl *FromTypedef = dyn_cast<TypedefNameDecl>(FromD)) { |
Douglas Gregor | b4964f7 | 2010-02-15 23:54:17 +0000 | [diff] [blame] | 4635 | // When we've finished transforming a typedef, see whether it was the |
| 4636 | // typedef for an anonymous tag. |
Craig Topper | 2341c0d | 2013-07-04 03:08:24 +0000 | [diff] [blame] | 4637 | for (SmallVectorImpl<TagDecl *>::iterator |
Douglas Gregor | b4964f7 | 2010-02-15 23:54:17 +0000 | [diff] [blame] | 4638 | FromTag = AnonTagsWithPendingTypedefs.begin(), |
| 4639 | FromTagEnd = AnonTagsWithPendingTypedefs.end(); |
| 4640 | FromTag != FromTagEnd; ++FromTag) { |
Richard Smith | dda56e4 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 4641 | if ((*FromTag)->getTypedefNameForAnonDecl() == FromTypedef) { |
Douglas Gregor | b4964f7 | 2010-02-15 23:54:17 +0000 | [diff] [blame] | 4642 | if (TagDecl *ToTag = cast_or_null<TagDecl>(Import(*FromTag))) { |
| 4643 | // We found the typedef for an anonymous tag; link them. |
Richard Smith | dda56e4 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 4644 | ToTag->setTypedefNameForAnonDecl(cast<TypedefNameDecl>(ToD)); |
Douglas Gregor | b4964f7 | 2010-02-15 23:54:17 +0000 | [diff] [blame] | 4645 | AnonTagsWithPendingTypedefs.erase(FromTag); |
| 4646 | break; |
| 4647 | } |
| 4648 | } |
| 4649 | } |
| 4650 | } |
| 4651 | |
Douglas Gregor | 62d311f | 2010-02-09 19:21:46 +0000 | [diff] [blame] | 4652 | return ToD; |
| 4653 | } |
| 4654 | |
| 4655 | DeclContext *ASTImporter::ImportContext(DeclContext *FromDC) { |
| 4656 | if (!FromDC) |
| 4657 | return FromDC; |
| 4658 | |
Douglas Gregor | 95d8283 | 2012-01-24 18:36:04 +0000 | [diff] [blame] | 4659 | DeclContext *ToDC = cast_or_null<DeclContext>(Import(cast<Decl>(FromDC))); |
Douglas Gregor | 2e15c84 | 2012-02-01 21:00:38 +0000 | [diff] [blame] | 4660 | if (!ToDC) |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 4661 | return nullptr; |
| 4662 | |
Douglas Gregor | 2e15c84 | 2012-02-01 21:00:38 +0000 | [diff] [blame] | 4663 | // When we're using a record/enum/Objective-C class/protocol as a context, we |
| 4664 | // need it to have a definition. |
| 4665 | if (RecordDecl *ToRecord = dyn_cast<RecordDecl>(ToDC)) { |
Douglas Gregor | 63db971 | 2012-01-25 01:13:20 +0000 | [diff] [blame] | 4666 | RecordDecl *FromRecord = cast<RecordDecl>(FromDC); |
Douglas Gregor | 2e15c84 | 2012-02-01 21:00:38 +0000 | [diff] [blame] | 4667 | if (ToRecord->isCompleteDefinition()) { |
| 4668 | // Do nothing. |
| 4669 | } else if (FromRecord->isCompleteDefinition()) { |
| 4670 | ASTNodeImporter(*this).ImportDefinition(FromRecord, ToRecord, |
| 4671 | ASTNodeImporter::IDK_Basic); |
| 4672 | } else { |
| 4673 | CompleteDecl(ToRecord); |
| 4674 | } |
| 4675 | } else if (EnumDecl *ToEnum = dyn_cast<EnumDecl>(ToDC)) { |
| 4676 | EnumDecl *FromEnum = cast<EnumDecl>(FromDC); |
| 4677 | if (ToEnum->isCompleteDefinition()) { |
| 4678 | // Do nothing. |
| 4679 | } else if (FromEnum->isCompleteDefinition()) { |
| 4680 | ASTNodeImporter(*this).ImportDefinition(FromEnum, ToEnum, |
| 4681 | ASTNodeImporter::IDK_Basic); |
| 4682 | } else { |
| 4683 | CompleteDecl(ToEnum); |
| 4684 | } |
| 4685 | } else if (ObjCInterfaceDecl *ToClass = dyn_cast<ObjCInterfaceDecl>(ToDC)) { |
| 4686 | ObjCInterfaceDecl *FromClass = cast<ObjCInterfaceDecl>(FromDC); |
| 4687 | if (ToClass->getDefinition()) { |
| 4688 | // Do nothing. |
| 4689 | } else if (ObjCInterfaceDecl *FromDef = FromClass->getDefinition()) { |
| 4690 | ASTNodeImporter(*this).ImportDefinition(FromDef, ToClass, |
| 4691 | ASTNodeImporter::IDK_Basic); |
| 4692 | } else { |
| 4693 | CompleteDecl(ToClass); |
| 4694 | } |
| 4695 | } else if (ObjCProtocolDecl *ToProto = dyn_cast<ObjCProtocolDecl>(ToDC)) { |
| 4696 | ObjCProtocolDecl *FromProto = cast<ObjCProtocolDecl>(FromDC); |
| 4697 | if (ToProto->getDefinition()) { |
| 4698 | // Do nothing. |
| 4699 | } else if (ObjCProtocolDecl *FromDef = FromProto->getDefinition()) { |
| 4700 | ASTNodeImporter(*this).ImportDefinition(FromDef, ToProto, |
| 4701 | ASTNodeImporter::IDK_Basic); |
| 4702 | } else { |
| 4703 | CompleteDecl(ToProto); |
| 4704 | } |
Douglas Gregor | 95d8283 | 2012-01-24 18:36:04 +0000 | [diff] [blame] | 4705 | } |
| 4706 | |
| 4707 | return ToDC; |
Douglas Gregor | 62d311f | 2010-02-09 19:21:46 +0000 | [diff] [blame] | 4708 | } |
| 4709 | |
| 4710 | Expr *ASTImporter::Import(Expr *FromE) { |
| 4711 | if (!FromE) |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 4712 | return nullptr; |
Douglas Gregor | 62d311f | 2010-02-09 19:21:46 +0000 | [diff] [blame] | 4713 | |
| 4714 | return cast_or_null<Expr>(Import(cast<Stmt>(FromE))); |
| 4715 | } |
| 4716 | |
| 4717 | Stmt *ASTImporter::Import(Stmt *FromS) { |
| 4718 | if (!FromS) |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 4719 | return nullptr; |
Douglas Gregor | 62d311f | 2010-02-09 19:21:46 +0000 | [diff] [blame] | 4720 | |
Douglas Gregor | 7eeb597 | 2010-02-11 19:21:55 +0000 | [diff] [blame] | 4721 | // Check whether we've already imported this declaration. |
| 4722 | llvm::DenseMap<Stmt *, Stmt *>::iterator Pos = ImportedStmts.find(FromS); |
| 4723 | if (Pos != ImportedStmts.end()) |
| 4724 | return Pos->second; |
| 4725 | |
| 4726 | // Import the type |
| 4727 | ASTNodeImporter Importer(*this); |
| 4728 | Stmt *ToS = Importer.Visit(FromS); |
| 4729 | if (!ToS) |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 4730 | return nullptr; |
| 4731 | |
Douglas Gregor | 7eeb597 | 2010-02-11 19:21:55 +0000 | [diff] [blame] | 4732 | // Record the imported declaration. |
| 4733 | ImportedStmts[FromS] = ToS; |
| 4734 | return ToS; |
Douglas Gregor | 62d311f | 2010-02-09 19:21:46 +0000 | [diff] [blame] | 4735 | } |
| 4736 | |
| 4737 | NestedNameSpecifier *ASTImporter::Import(NestedNameSpecifier *FromNNS) { |
| 4738 | if (!FromNNS) |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 4739 | return nullptr; |
Douglas Gregor | 62d311f | 2010-02-09 19:21:46 +0000 | [diff] [blame] | 4740 | |
Douglas Gregor | 90ebf25 | 2011-04-27 16:48:40 +0000 | [diff] [blame] | 4741 | NestedNameSpecifier *prefix = Import(FromNNS->getPrefix()); |
| 4742 | |
| 4743 | switch (FromNNS->getKind()) { |
| 4744 | case NestedNameSpecifier::Identifier: |
| 4745 | if (IdentifierInfo *II = Import(FromNNS->getAsIdentifier())) { |
| 4746 | return NestedNameSpecifier::Create(ToContext, prefix, II); |
| 4747 | } |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 4748 | return nullptr; |
Douglas Gregor | 90ebf25 | 2011-04-27 16:48:40 +0000 | [diff] [blame] | 4749 | |
| 4750 | case NestedNameSpecifier::Namespace: |
| 4751 | if (NamespaceDecl *NS = |
| 4752 | cast<NamespaceDecl>(Import(FromNNS->getAsNamespace()))) { |
| 4753 | return NestedNameSpecifier::Create(ToContext, prefix, NS); |
| 4754 | } |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 4755 | return nullptr; |
Douglas Gregor | 90ebf25 | 2011-04-27 16:48:40 +0000 | [diff] [blame] | 4756 | |
| 4757 | case NestedNameSpecifier::NamespaceAlias: |
| 4758 | if (NamespaceAliasDecl *NSAD = |
| 4759 | cast<NamespaceAliasDecl>(Import(FromNNS->getAsNamespaceAlias()))) { |
| 4760 | return NestedNameSpecifier::Create(ToContext, prefix, NSAD); |
| 4761 | } |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 4762 | return nullptr; |
Douglas Gregor | 90ebf25 | 2011-04-27 16:48:40 +0000 | [diff] [blame] | 4763 | |
| 4764 | case NestedNameSpecifier::Global: |
| 4765 | return NestedNameSpecifier::GlobalSpecifier(ToContext); |
| 4766 | |
Nikola Smiljanic | 6786024 | 2014-09-26 00:28:20 +0000 | [diff] [blame] | 4767 | case NestedNameSpecifier::Super: |
| 4768 | if (CXXRecordDecl *RD = |
| 4769 | cast<CXXRecordDecl>(Import(FromNNS->getAsRecordDecl()))) { |
| 4770 | return NestedNameSpecifier::SuperSpecifier(ToContext, RD); |
| 4771 | } |
| 4772 | return nullptr; |
| 4773 | |
Douglas Gregor | 90ebf25 | 2011-04-27 16:48:40 +0000 | [diff] [blame] | 4774 | case NestedNameSpecifier::TypeSpec: |
| 4775 | case NestedNameSpecifier::TypeSpecWithTemplate: { |
| 4776 | QualType T = Import(QualType(FromNNS->getAsType(), 0u)); |
| 4777 | if (!T.isNull()) { |
| 4778 | bool bTemplate = FromNNS->getKind() == |
| 4779 | NestedNameSpecifier::TypeSpecWithTemplate; |
| 4780 | return NestedNameSpecifier::Create(ToContext, prefix, |
| 4781 | bTemplate, T.getTypePtr()); |
| 4782 | } |
| 4783 | } |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 4784 | return nullptr; |
Douglas Gregor | 90ebf25 | 2011-04-27 16:48:40 +0000 | [diff] [blame] | 4785 | } |
| 4786 | |
| 4787 | llvm_unreachable("Invalid nested name specifier kind"); |
Douglas Gregor | 62d311f | 2010-02-09 19:21:46 +0000 | [diff] [blame] | 4788 | } |
| 4789 | |
Douglas Gregor | 1445480 | 2011-02-25 02:25:35 +0000 | [diff] [blame] | 4790 | NestedNameSpecifierLoc ASTImporter::Import(NestedNameSpecifierLoc FromNNS) { |
| 4791 | // FIXME: Implement! |
| 4792 | return NestedNameSpecifierLoc(); |
| 4793 | } |
| 4794 | |
Douglas Gregor | e2e50d33 | 2010-12-01 01:36:18 +0000 | [diff] [blame] | 4795 | TemplateName ASTImporter::Import(TemplateName From) { |
| 4796 | switch (From.getKind()) { |
| 4797 | case TemplateName::Template: |
| 4798 | if (TemplateDecl *ToTemplate |
| 4799 | = cast_or_null<TemplateDecl>(Import(From.getAsTemplateDecl()))) |
| 4800 | return TemplateName(ToTemplate); |
| 4801 | |
| 4802 | return TemplateName(); |
| 4803 | |
| 4804 | case TemplateName::OverloadedTemplate: { |
| 4805 | OverloadedTemplateStorage *FromStorage = From.getAsOverloadedTemplate(); |
| 4806 | UnresolvedSet<2> ToTemplates; |
| 4807 | for (OverloadedTemplateStorage::iterator I = FromStorage->begin(), |
| 4808 | E = FromStorage->end(); |
| 4809 | I != E; ++I) { |
| 4810 | if (NamedDecl *To = cast_or_null<NamedDecl>(Import(*I))) |
| 4811 | ToTemplates.addDecl(To); |
| 4812 | else |
| 4813 | return TemplateName(); |
| 4814 | } |
| 4815 | return ToContext.getOverloadedTemplateName(ToTemplates.begin(), |
| 4816 | ToTemplates.end()); |
| 4817 | } |
| 4818 | |
| 4819 | case TemplateName::QualifiedTemplate: { |
| 4820 | QualifiedTemplateName *QTN = From.getAsQualifiedTemplateName(); |
| 4821 | NestedNameSpecifier *Qualifier = Import(QTN->getQualifier()); |
| 4822 | if (!Qualifier) |
| 4823 | return TemplateName(); |
| 4824 | |
| 4825 | if (TemplateDecl *ToTemplate |
| 4826 | = cast_or_null<TemplateDecl>(Import(From.getAsTemplateDecl()))) |
| 4827 | return ToContext.getQualifiedTemplateName(Qualifier, |
| 4828 | QTN->hasTemplateKeyword(), |
| 4829 | ToTemplate); |
| 4830 | |
| 4831 | return TemplateName(); |
| 4832 | } |
| 4833 | |
| 4834 | case TemplateName::DependentTemplate: { |
| 4835 | DependentTemplateName *DTN = From.getAsDependentTemplateName(); |
| 4836 | NestedNameSpecifier *Qualifier = Import(DTN->getQualifier()); |
| 4837 | if (!Qualifier) |
| 4838 | return TemplateName(); |
| 4839 | |
| 4840 | if (DTN->isIdentifier()) { |
| 4841 | return ToContext.getDependentTemplateName(Qualifier, |
| 4842 | Import(DTN->getIdentifier())); |
| 4843 | } |
| 4844 | |
| 4845 | return ToContext.getDependentTemplateName(Qualifier, DTN->getOperator()); |
| 4846 | } |
John McCall | d9dfe3a | 2011-06-30 08:33:18 +0000 | [diff] [blame] | 4847 | |
| 4848 | case TemplateName::SubstTemplateTemplateParm: { |
| 4849 | SubstTemplateTemplateParmStorage *subst |
| 4850 | = From.getAsSubstTemplateTemplateParm(); |
| 4851 | TemplateTemplateParmDecl *param |
| 4852 | = cast_or_null<TemplateTemplateParmDecl>(Import(subst->getParameter())); |
| 4853 | if (!param) |
| 4854 | return TemplateName(); |
| 4855 | |
| 4856 | TemplateName replacement = Import(subst->getReplacement()); |
| 4857 | if (replacement.isNull()) return TemplateName(); |
| 4858 | |
| 4859 | return ToContext.getSubstTemplateTemplateParm(param, replacement); |
| 4860 | } |
Douglas Gregor | 5590be0 | 2011-01-15 06:45:20 +0000 | [diff] [blame] | 4861 | |
| 4862 | case TemplateName::SubstTemplateTemplateParmPack: { |
| 4863 | SubstTemplateTemplateParmPackStorage *SubstPack |
| 4864 | = From.getAsSubstTemplateTemplateParmPack(); |
| 4865 | TemplateTemplateParmDecl *Param |
| 4866 | = cast_or_null<TemplateTemplateParmDecl>( |
| 4867 | Import(SubstPack->getParameterPack())); |
| 4868 | if (!Param) |
| 4869 | return TemplateName(); |
| 4870 | |
| 4871 | ASTNodeImporter Importer(*this); |
| 4872 | TemplateArgument ArgPack |
| 4873 | = Importer.ImportTemplateArgument(SubstPack->getArgumentPack()); |
| 4874 | if (ArgPack.isNull()) |
| 4875 | return TemplateName(); |
| 4876 | |
| 4877 | return ToContext.getSubstTemplateTemplateParmPack(Param, ArgPack); |
| 4878 | } |
Douglas Gregor | e2e50d33 | 2010-12-01 01:36:18 +0000 | [diff] [blame] | 4879 | } |
| 4880 | |
| 4881 | llvm_unreachable("Invalid template name kind"); |
Douglas Gregor | e2e50d33 | 2010-12-01 01:36:18 +0000 | [diff] [blame] | 4882 | } |
| 4883 | |
Douglas Gregor | 62d311f | 2010-02-09 19:21:46 +0000 | [diff] [blame] | 4884 | SourceLocation ASTImporter::Import(SourceLocation FromLoc) { |
| 4885 | if (FromLoc.isInvalid()) |
| 4886 | return SourceLocation(); |
| 4887 | |
Douglas Gregor | 811663e | 2010-02-10 00:15:17 +0000 | [diff] [blame] | 4888 | SourceManager &FromSM = FromContext.getSourceManager(); |
| 4889 | |
| 4890 | // For now, map everything down to its spelling location, so that we |
Chandler Carruth | 2536641 | 2011-07-15 00:04:35 +0000 | [diff] [blame] | 4891 | // don't have to import macro expansions. |
| 4892 | // FIXME: Import macro expansions! |
Douglas Gregor | 811663e | 2010-02-10 00:15:17 +0000 | [diff] [blame] | 4893 | FromLoc = FromSM.getSpellingLoc(FromLoc); |
| 4894 | std::pair<FileID, unsigned> Decomposed = FromSM.getDecomposedLoc(FromLoc); |
| 4895 | SourceManager &ToSM = ToContext.getSourceManager(); |
| 4896 | return ToSM.getLocForStartOfFile(Import(Decomposed.first)) |
Argyrios Kyrtzidis | e6e67de | 2011-09-19 20:40:19 +0000 | [diff] [blame] | 4897 | .getLocWithOffset(Decomposed.second); |
Douglas Gregor | 62d311f | 2010-02-09 19:21:46 +0000 | [diff] [blame] | 4898 | } |
| 4899 | |
| 4900 | SourceRange ASTImporter::Import(SourceRange FromRange) { |
| 4901 | return SourceRange(Import(FromRange.getBegin()), Import(FromRange.getEnd())); |
| 4902 | } |
| 4903 | |
Douglas Gregor | 811663e | 2010-02-10 00:15:17 +0000 | [diff] [blame] | 4904 | FileID ASTImporter::Import(FileID FromID) { |
Sebastian Redl | 99219f1 | 2010-09-30 01:03:06 +0000 | [diff] [blame] | 4905 | llvm::DenseMap<FileID, FileID>::iterator Pos |
| 4906 | = ImportedFileIDs.find(FromID); |
Douglas Gregor | 811663e | 2010-02-10 00:15:17 +0000 | [diff] [blame] | 4907 | if (Pos != ImportedFileIDs.end()) |
| 4908 | return Pos->second; |
| 4909 | |
| 4910 | SourceManager &FromSM = FromContext.getSourceManager(); |
| 4911 | SourceManager &ToSM = ToContext.getSourceManager(); |
| 4912 | const SrcMgr::SLocEntry &FromSLoc = FromSM.getSLocEntry(FromID); |
Chandler Carruth | 2536641 | 2011-07-15 00:04:35 +0000 | [diff] [blame] | 4913 | assert(FromSLoc.isFile() && "Cannot handle macro expansions yet"); |
Douglas Gregor | 811663e | 2010-02-10 00:15:17 +0000 | [diff] [blame] | 4914 | |
| 4915 | // Include location of this file. |
| 4916 | SourceLocation ToIncludeLoc = Import(FromSLoc.getFile().getIncludeLoc()); |
| 4917 | |
| 4918 | // Map the FileID for to the "to" source manager. |
| 4919 | FileID ToID; |
| 4920 | const SrcMgr::ContentCache *Cache = FromSLoc.getFile().getContentCache(); |
Argyrios Kyrtzidis | 11e6f0a | 2011-03-05 01:03:53 +0000 | [diff] [blame] | 4921 | if (Cache->OrigEntry) { |
Douglas Gregor | 811663e | 2010-02-10 00:15:17 +0000 | [diff] [blame] | 4922 | // FIXME: We probably want to use getVirtualFile(), so we don't hit the |
| 4923 | // disk again |
| 4924 | // FIXME: We definitely want to re-use the existing MemoryBuffer, rather |
| 4925 | // than mmap the files several times. |
Argyrios Kyrtzidis | 11e6f0a | 2011-03-05 01:03:53 +0000 | [diff] [blame] | 4926 | const FileEntry *Entry = ToFileManager.getFile(Cache->OrigEntry->getName()); |
Douglas Gregor | 811663e | 2010-02-10 00:15:17 +0000 | [diff] [blame] | 4927 | ToID = ToSM.createFileID(Entry, ToIncludeLoc, |
| 4928 | FromSLoc.getFile().getFileCharacteristic()); |
| 4929 | } else { |
| 4930 | // FIXME: We want to re-use the existing MemoryBuffer! |
Argyrios Kyrtzidis | d004064 | 2010-11-18 20:06:41 +0000 | [diff] [blame] | 4931 | const llvm::MemoryBuffer * |
| 4932 | FromBuf = Cache->getBuffer(FromContext.getDiagnostics(), FromSM); |
Rafael Espindola | d87f8d7 | 2014-08-27 20:03:29 +0000 | [diff] [blame] | 4933 | std::unique_ptr<llvm::MemoryBuffer> ToBuf |
Chris Lattner | 58c7934 | 2010-04-05 22:42:27 +0000 | [diff] [blame] | 4934 | = llvm::MemoryBuffer::getMemBufferCopy(FromBuf->getBuffer(), |
Douglas Gregor | 811663e | 2010-02-10 00:15:17 +0000 | [diff] [blame] | 4935 | FromBuf->getBufferIdentifier()); |
David Blaikie | 50a5f97 | 2014-08-29 07:59:55 +0000 | [diff] [blame] | 4936 | ToID = ToSM.createFileID(std::move(ToBuf), |
Rafael Espindola | d87f8d7 | 2014-08-27 20:03:29 +0000 | [diff] [blame] | 4937 | FromSLoc.getFile().getFileCharacteristic()); |
Douglas Gregor | 811663e | 2010-02-10 00:15:17 +0000 | [diff] [blame] | 4938 | } |
| 4939 | |
| 4940 | |
Sebastian Redl | 99219f1 | 2010-09-30 01:03:06 +0000 | [diff] [blame] | 4941 | ImportedFileIDs[FromID] = ToID; |
Douglas Gregor | 811663e | 2010-02-10 00:15:17 +0000 | [diff] [blame] | 4942 | return ToID; |
| 4943 | } |
| 4944 | |
Douglas Gregor | 0a79167 | 2011-01-18 03:11:38 +0000 | [diff] [blame] | 4945 | void ASTImporter::ImportDefinition(Decl *From) { |
| 4946 | Decl *To = Import(From); |
| 4947 | if (!To) |
| 4948 | return; |
| 4949 | |
| 4950 | if (DeclContext *FromDC = cast<DeclContext>(From)) { |
| 4951 | ASTNodeImporter Importer(*this); |
Sean Callanan | 53a6bff | 2011-07-19 22:38:25 +0000 | [diff] [blame] | 4952 | |
| 4953 | if (RecordDecl *ToRecord = dyn_cast<RecordDecl>(To)) { |
| 4954 | if (!ToRecord->getDefinition()) { |
| 4955 | Importer.ImportDefinition(cast<RecordDecl>(FromDC), ToRecord, |
Douglas Gregor | 95d8283 | 2012-01-24 18:36:04 +0000 | [diff] [blame] | 4956 | ASTNodeImporter::IDK_Everything); |
Sean Callanan | 53a6bff | 2011-07-19 22:38:25 +0000 | [diff] [blame] | 4957 | return; |
| 4958 | } |
| 4959 | } |
Douglas Gregor | d451ea9 | 2011-07-29 23:31:30 +0000 | [diff] [blame] | 4960 | |
| 4961 | if (EnumDecl *ToEnum = dyn_cast<EnumDecl>(To)) { |
| 4962 | if (!ToEnum->getDefinition()) { |
| 4963 | Importer.ImportDefinition(cast<EnumDecl>(FromDC), ToEnum, |
Douglas Gregor | 2e15c84 | 2012-02-01 21:00:38 +0000 | [diff] [blame] | 4964 | ASTNodeImporter::IDK_Everything); |
Douglas Gregor | d451ea9 | 2011-07-29 23:31:30 +0000 | [diff] [blame] | 4965 | return; |
| 4966 | } |
| 4967 | } |
Douglas Gregor | 2aa5377 | 2012-01-24 17:42:07 +0000 | [diff] [blame] | 4968 | |
| 4969 | if (ObjCInterfaceDecl *ToIFace = dyn_cast<ObjCInterfaceDecl>(To)) { |
| 4970 | if (!ToIFace->getDefinition()) { |
| 4971 | Importer.ImportDefinition(cast<ObjCInterfaceDecl>(FromDC), ToIFace, |
Douglas Gregor | 2e15c84 | 2012-02-01 21:00:38 +0000 | [diff] [blame] | 4972 | ASTNodeImporter::IDK_Everything); |
Douglas Gregor | 2aa5377 | 2012-01-24 17:42:07 +0000 | [diff] [blame] | 4973 | return; |
| 4974 | } |
| 4975 | } |
Douglas Gregor | d451ea9 | 2011-07-29 23:31:30 +0000 | [diff] [blame] | 4976 | |
Douglas Gregor | 2aa5377 | 2012-01-24 17:42:07 +0000 | [diff] [blame] | 4977 | if (ObjCProtocolDecl *ToProto = dyn_cast<ObjCProtocolDecl>(To)) { |
| 4978 | if (!ToProto->getDefinition()) { |
| 4979 | Importer.ImportDefinition(cast<ObjCProtocolDecl>(FromDC), ToProto, |
Douglas Gregor | 2e15c84 | 2012-02-01 21:00:38 +0000 | [diff] [blame] | 4980 | ASTNodeImporter::IDK_Everything); |
Douglas Gregor | 2aa5377 | 2012-01-24 17:42:07 +0000 | [diff] [blame] | 4981 | return; |
| 4982 | } |
| 4983 | } |
| 4984 | |
Douglas Gregor | 0a79167 | 2011-01-18 03:11:38 +0000 | [diff] [blame] | 4985 | Importer.ImportDeclContext(FromDC, true); |
| 4986 | } |
| 4987 | } |
| 4988 | |
Douglas Gregor | 96e578d | 2010-02-05 17:54:41 +0000 | [diff] [blame] | 4989 | DeclarationName ASTImporter::Import(DeclarationName FromName) { |
| 4990 | if (!FromName) |
| 4991 | return DeclarationName(); |
| 4992 | |
| 4993 | switch (FromName.getNameKind()) { |
| 4994 | case DeclarationName::Identifier: |
| 4995 | return Import(FromName.getAsIdentifierInfo()); |
| 4996 | |
| 4997 | case DeclarationName::ObjCZeroArgSelector: |
| 4998 | case DeclarationName::ObjCOneArgSelector: |
| 4999 | case DeclarationName::ObjCMultiArgSelector: |
| 5000 | return Import(FromName.getObjCSelector()); |
| 5001 | |
| 5002 | case DeclarationName::CXXConstructorName: { |
| 5003 | QualType T = Import(FromName.getCXXNameType()); |
| 5004 | if (T.isNull()) |
| 5005 | return DeclarationName(); |
| 5006 | |
| 5007 | return ToContext.DeclarationNames.getCXXConstructorName( |
| 5008 | ToContext.getCanonicalType(T)); |
| 5009 | } |
| 5010 | |
| 5011 | case DeclarationName::CXXDestructorName: { |
| 5012 | QualType T = Import(FromName.getCXXNameType()); |
| 5013 | if (T.isNull()) |
| 5014 | return DeclarationName(); |
| 5015 | |
| 5016 | return ToContext.DeclarationNames.getCXXDestructorName( |
| 5017 | ToContext.getCanonicalType(T)); |
| 5018 | } |
| 5019 | |
| 5020 | case DeclarationName::CXXConversionFunctionName: { |
| 5021 | QualType T = Import(FromName.getCXXNameType()); |
| 5022 | if (T.isNull()) |
| 5023 | return DeclarationName(); |
| 5024 | |
| 5025 | return ToContext.DeclarationNames.getCXXConversionFunctionName( |
| 5026 | ToContext.getCanonicalType(T)); |
| 5027 | } |
| 5028 | |
| 5029 | case DeclarationName::CXXOperatorName: |
| 5030 | return ToContext.DeclarationNames.getCXXOperatorName( |
| 5031 | FromName.getCXXOverloadedOperator()); |
| 5032 | |
| 5033 | case DeclarationName::CXXLiteralOperatorName: |
| 5034 | return ToContext.DeclarationNames.getCXXLiteralOperatorName( |
| 5035 | Import(FromName.getCXXLiteralIdentifier())); |
| 5036 | |
| 5037 | case DeclarationName::CXXUsingDirective: |
| 5038 | // FIXME: STATICS! |
| 5039 | return DeclarationName::getUsingDirectiveName(); |
| 5040 | } |
| 5041 | |
David Blaikie | e4d798f | 2012-01-20 21:50:17 +0000 | [diff] [blame] | 5042 | llvm_unreachable("Invalid DeclarationName Kind!"); |
Douglas Gregor | 96e578d | 2010-02-05 17:54:41 +0000 | [diff] [blame] | 5043 | } |
| 5044 | |
Douglas Gregor | e2e50d33 | 2010-12-01 01:36:18 +0000 | [diff] [blame] | 5045 | IdentifierInfo *ASTImporter::Import(const IdentifierInfo *FromId) { |
Douglas Gregor | 96e578d | 2010-02-05 17:54:41 +0000 | [diff] [blame] | 5046 | if (!FromId) |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 5047 | return nullptr; |
Douglas Gregor | 96e578d | 2010-02-05 17:54:41 +0000 | [diff] [blame] | 5048 | |
| 5049 | return &ToContext.Idents.get(FromId->getName()); |
| 5050 | } |
Douglas Gregor | 3aed6cd | 2010-02-08 21:09:39 +0000 | [diff] [blame] | 5051 | |
Douglas Gregor | 43f5479 | 2010-02-17 02:12:47 +0000 | [diff] [blame] | 5052 | Selector ASTImporter::Import(Selector FromSel) { |
| 5053 | if (FromSel.isNull()) |
| 5054 | return Selector(); |
| 5055 | |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 5056 | SmallVector<IdentifierInfo *, 4> Idents; |
Douglas Gregor | 43f5479 | 2010-02-17 02:12:47 +0000 | [diff] [blame] | 5057 | Idents.push_back(Import(FromSel.getIdentifierInfoForSlot(0))); |
| 5058 | for (unsigned I = 1, N = FromSel.getNumArgs(); I < N; ++I) |
| 5059 | Idents.push_back(Import(FromSel.getIdentifierInfoForSlot(I))); |
| 5060 | return ToContext.Selectors.getSelector(FromSel.getNumArgs(), Idents.data()); |
| 5061 | } |
| 5062 | |
Douglas Gregor | 3aed6cd | 2010-02-08 21:09:39 +0000 | [diff] [blame] | 5063 | DeclarationName ASTImporter::HandleNameConflict(DeclarationName Name, |
| 5064 | DeclContext *DC, |
| 5065 | unsigned IDNS, |
| 5066 | NamedDecl **Decls, |
| 5067 | unsigned NumDecls) { |
| 5068 | return Name; |
| 5069 | } |
| 5070 | |
| 5071 | DiagnosticBuilder ASTImporter::ToDiag(SourceLocation Loc, unsigned DiagID) { |
Richard Smith | 5bb4cdf | 2012-12-20 02:22:15 +0000 | [diff] [blame] | 5072 | if (LastDiagFromFrom) |
| 5073 | ToContext.getDiagnostics().notePriorDiagnosticFrom( |
| 5074 | FromContext.getDiagnostics()); |
| 5075 | LastDiagFromFrom = false; |
Argyrios Kyrtzidis | d004064 | 2010-11-18 20:06:41 +0000 | [diff] [blame] | 5076 | return ToContext.getDiagnostics().Report(Loc, DiagID); |
Douglas Gregor | 3aed6cd | 2010-02-08 21:09:39 +0000 | [diff] [blame] | 5077 | } |
| 5078 | |
| 5079 | DiagnosticBuilder ASTImporter::FromDiag(SourceLocation Loc, unsigned DiagID) { |
Richard Smith | 5bb4cdf | 2012-12-20 02:22:15 +0000 | [diff] [blame] | 5080 | if (!LastDiagFromFrom) |
| 5081 | FromContext.getDiagnostics().notePriorDiagnosticFrom( |
| 5082 | ToContext.getDiagnostics()); |
| 5083 | LastDiagFromFrom = true; |
Argyrios Kyrtzidis | d004064 | 2010-11-18 20:06:41 +0000 | [diff] [blame] | 5084 | return FromContext.getDiagnostics().Report(Loc, DiagID); |
Douglas Gregor | 3aed6cd | 2010-02-08 21:09:39 +0000 | [diff] [blame] | 5085 | } |
Douglas Gregor | 8cdbe64 | 2010-02-12 23:44:20 +0000 | [diff] [blame] | 5086 | |
Douglas Gregor | 2e15c84 | 2012-02-01 21:00:38 +0000 | [diff] [blame] | 5087 | void ASTImporter::CompleteDecl (Decl *D) { |
| 5088 | if (ObjCInterfaceDecl *ID = dyn_cast<ObjCInterfaceDecl>(D)) { |
| 5089 | if (!ID->getDefinition()) |
| 5090 | ID->startDefinition(); |
| 5091 | } |
| 5092 | else if (ObjCProtocolDecl *PD = dyn_cast<ObjCProtocolDecl>(D)) { |
| 5093 | if (!PD->getDefinition()) |
| 5094 | PD->startDefinition(); |
| 5095 | } |
| 5096 | else if (TagDecl *TD = dyn_cast<TagDecl>(D)) { |
| 5097 | if (!TD->getDefinition() && !TD->isBeingDefined()) { |
| 5098 | TD->startDefinition(); |
| 5099 | TD->setCompleteDefinition(true); |
| 5100 | } |
| 5101 | } |
| 5102 | else { |
| 5103 | assert (0 && "CompleteDecl called on a Decl that can't be completed"); |
| 5104 | } |
| 5105 | } |
| 5106 | |
Douglas Gregor | 8cdbe64 | 2010-02-12 23:44:20 +0000 | [diff] [blame] | 5107 | Decl *ASTImporter::Imported(Decl *From, Decl *To) { |
| 5108 | ImportedDecls[From] = To; |
| 5109 | return To; |
Daniel Dunbar | 9ced542 | 2010-02-13 20:24:39 +0000 | [diff] [blame] | 5110 | } |
Douglas Gregor | b4964f7 | 2010-02-15 23:54:17 +0000 | [diff] [blame] | 5111 | |
Douglas Gregor | dd6006f | 2012-07-17 21:16:27 +0000 | [diff] [blame] | 5112 | bool ASTImporter::IsStructurallyEquivalent(QualType From, QualType To, |
| 5113 | bool Complain) { |
John McCall | 424cec9 | 2011-01-19 06:33:43 +0000 | [diff] [blame] | 5114 | llvm::DenseMap<const Type *, const Type *>::iterator Pos |
Douglas Gregor | b4964f7 | 2010-02-15 23:54:17 +0000 | [diff] [blame] | 5115 | = ImportedTypes.find(From.getTypePtr()); |
| 5116 | if (Pos != ImportedTypes.end() && ToContext.hasSameType(Import(From), To)) |
| 5117 | return true; |
| 5118 | |
Douglas Gregor | dd6006f | 2012-07-17 21:16:27 +0000 | [diff] [blame] | 5119 | StructuralEquivalenceContext Ctx(FromContext, ToContext, NonEquivalentDecls, |
| 5120 | false, Complain); |
Benjamin Kramer | 26d19c5 | 2010-02-18 13:02:13 +0000 | [diff] [blame] | 5121 | return Ctx.IsStructurallyEquivalent(From, To); |
Douglas Gregor | b4964f7 | 2010-02-15 23:54:17 +0000 | [diff] [blame] | 5122 | } |