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